1
22
23 package com.liferay.portlet.tags.service.impl;
24
25 import com.liferay.portal.PortalException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.model.ResourceConstants;
28 import com.liferay.portal.model.User;
29 import com.liferay.portal.service.ServiceContext;
30 import com.liferay.portlet.tags.DuplicateVocabularyException;
31 import com.liferay.portlet.tags.model.TagsVocabulary;
32 import com.liferay.portlet.tags.service.base.TagsVocabularyLocalServiceBaseImpl;
33
34 import java.util.Date;
35 import java.util.List;
36
37
44 public class TagsVocabularyLocalServiceImpl
45 extends TagsVocabularyLocalServiceBaseImpl {
46
47 public TagsVocabulary addVocabulary(
48 long userId, String name, boolean folksonomy,
49 ServiceContext serviceContext)
50 throws PortalException, SystemException {
51
52
54 User user = userPersistence.findByPrimaryKey(userId);
55 long groupId = serviceContext.getScopeGroupId();
56 name = name.trim();
57 Date now = new Date();
58
59 if (hasVocabulary(groupId, name)) {
60 throw new DuplicateVocabularyException(
61 "A vocabulary with the name " + name + " already exists");
62 }
63
64 long vocabularyId = counterLocalService.increment();
65
66 TagsVocabulary vocabulary = tagsVocabularyPersistence.create(
67 vocabularyId);
68
69 vocabulary.setGroupId(groupId);
70 vocabulary.setCompanyId(user.getCompanyId());
71 vocabulary.setUserId(user.getUserId());
72 vocabulary.setUserName(user.getFullName());
73 vocabulary.setCreateDate(now);
74 vocabulary.setModifiedDate(now);
75 vocabulary.setName(name);
76 vocabulary.setFolksonomy(folksonomy);
77
78 tagsVocabularyPersistence.update(vocabulary, false);
79
80
82 if (serviceContext.getAddCommunityPermissions() ||
83 serviceContext.getAddGuestPermissions()) {
84
85 addVocabularyResources(
86 vocabulary, serviceContext.getAddCommunityPermissions(),
87 serviceContext.getAddGuestPermissions());
88 }
89 else {
90 addVocabularyResources(
91 vocabulary, serviceContext.getCommunityPermissions(),
92 serviceContext.getGuestPermissions());
93 }
94
95 return vocabulary;
96
97 }
98
99 public void addVocabularyResources(
100 TagsVocabulary vocabulary, boolean addCommunityPermissions,
101 boolean addGuestPermissions)
102 throws PortalException, SystemException {
103
104 resourceLocalService.addResources(
105 vocabulary.getCompanyId(), vocabulary.getGroupId(),
106 vocabulary.getUserId(), TagsVocabulary.class.getName(),
107 vocabulary.getVocabularyId(), false, addCommunityPermissions,
108 addGuestPermissions);
109 }
110
111 public void addVocabularyResources(
112 TagsVocabulary vocabulary, String[] communityPermissions,
113 String[] guestPermissions)
114 throws PortalException, SystemException {
115
116 resourceLocalService.addModelResources(
117 vocabulary.getCompanyId(), vocabulary.getGroupId(),
118 vocabulary.getUserId(), TagsVocabulary.class.getName(),
119 vocabulary.getVocabularyId(), communityPermissions,
120 guestPermissions);
121 }
122
123 public void deleteVocabulary(long vocabularyId)
124 throws PortalException, SystemException {
125
126 TagsVocabulary vocabulary = tagsVocabularyPersistence.findByPrimaryKey(
127 vocabularyId);
128
129 deleteVocabulary(vocabulary);
130 }
131
132 public void deleteVocabulary(TagsVocabulary vocabulary)
133 throws PortalException, SystemException {
134
135
137 tagsEntryLocalService.deleteVocabularyEntries(
138 vocabulary.getVocabularyId());
139
140
142 resourceLocalService.deleteResource(
143 vocabulary.getCompanyId(), TagsVocabulary.class.getName(),
144 ResourceConstants.SCOPE_INDIVIDUAL, vocabulary.getVocabularyId());
145
146 tagsVocabularyPersistence.remove(vocabulary);
147 }
148
149 public List<TagsVocabulary> getCompanyVocabularies(
150 long companyId, boolean folksonomy)
151 throws SystemException {
152
153 return tagsVocabularyPersistence.findByC_F(companyId, folksonomy);
154 }
155
156 public List<TagsVocabulary> getGroupVocabularies(
157 long groupId, boolean folksonomy)
158 throws SystemException {
159
160 return tagsVocabularyPersistence.findByG_F(groupId, folksonomy);
161 }
162
163 public TagsVocabulary getGroupVocabulary(long groupId, String name)
164 throws PortalException, SystemException {
165
166 return tagsVocabularyPersistence.findByG_N(groupId, name);
167 }
168
169 public TagsVocabulary getVocabulary(long vocabularyId)
170 throws PortalException, SystemException {
171
172 return tagsVocabularyPersistence.findByPrimaryKey(vocabularyId);
173 }
174
175 public TagsVocabulary updateVocabulary(
176 long vocabularyId, String name, boolean folksonomy)
177 throws PortalException, SystemException {
178
179 name = name.trim();
180
181 TagsVocabulary vocabulary = tagsVocabularyPersistence.findByPrimaryKey(
182 vocabularyId);
183
184 if (!vocabulary.getName().equals(name) &&
185 hasVocabulary(vocabulary.getGroupId(), name)) {
186
187 throw new DuplicateVocabularyException(name);
188 }
189
190 vocabulary.setModifiedDate(new Date());
191 vocabulary.setName(name);
192 vocabulary.setFolksonomy(folksonomy);
193
194 tagsVocabularyPersistence.update(vocabulary, false);
195
196 return vocabulary;
197 }
198
199 protected boolean hasVocabulary(long groupId, String name)
200 throws SystemException {
201
202 if (tagsVocabularyPersistence.countByG_N(groupId, name) == 0) {
203 return false;
204 }
205 else {
206 return true;
207 }
208 }
209
210 }