1
14
15 package com.liferay.portlet.asset.service.impl;
16
17 import com.liferay.portal.kernel.exception.PortalException;
18 import com.liferay.portal.kernel.exception.SystemException;
19 import com.liferay.portal.kernel.util.ListUtil;
20 import com.liferay.portal.kernel.util.LocaleUtil;
21 import com.liferay.portal.kernel.util.StringPool;
22 import com.liferay.portal.model.Group;
23 import com.liferay.portal.model.ResourceConstants;
24 import com.liferay.portal.model.User;
25 import com.liferay.portal.service.ServiceContext;
26 import com.liferay.portal.util.PropsValues;
27 import com.liferay.portlet.asset.DuplicateVocabularyException;
28 import com.liferay.portlet.asset.model.AssetVocabulary;
29 import com.liferay.portlet.asset.service.base.AssetVocabularyLocalServiceBaseImpl;
30
31 import java.util.ArrayList;
32 import java.util.Date;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Locale;
36 import java.util.Map;
37
38
46 public class AssetVocabularyLocalServiceImpl
47 extends AssetVocabularyLocalServiceBaseImpl {
48
49 public AssetVocabulary addVocabulary(
50 String uuid, long userId, Map<Locale, String> titleMap,
51 Map<Locale, String> descriptionMap, String settings,
52 ServiceContext serviceContext)
53 throws PortalException, SystemException {
54
55
57 User user = userPersistence.findByPrimaryKey(userId);
58 long groupId = serviceContext.getScopeGroupId();
59 String name = titleMap.get(LocaleUtil.getDefault());
60
61 Date now = new Date();
62
63 if (hasVocabulary(groupId, name)) {
64 throw new DuplicateVocabularyException(
65 "A category vocabulary with the name " + name +
66 " already exists");
67 }
68
69 long vocabularyId = counterLocalService.increment();
70
71 AssetVocabulary vocabulary = assetVocabularyPersistence.create(
72 vocabularyId);
73
74 vocabulary.setUuid(uuid);
75 vocabulary.setGroupId(groupId);
76 vocabulary.setCompanyId(user.getCompanyId());
77 vocabulary.setUserId(user.getUserId());
78 vocabulary.setUserName(user.getFullName());
79 vocabulary.setCreateDate(now);
80 vocabulary.setModifiedDate(now);
81 vocabulary.setName(name);
82 vocabulary.setTitleMap(titleMap);
83 vocabulary.setDescriptionMap(descriptionMap);
84 vocabulary.setSettings(settings);
85
86 assetVocabularyPersistence.update(vocabulary, false);
87
88
90 if (serviceContext.getAddCommunityPermissions() ||
91 serviceContext.getAddGuestPermissions()) {
92
93 addVocabularyResources(
94 vocabulary, serviceContext.getAddCommunityPermissions(),
95 serviceContext.getAddGuestPermissions());
96 }
97 else {
98 addVocabularyResources(
99 vocabulary, serviceContext.getCommunityPermissions(),
100 serviceContext.getGuestPermissions());
101 }
102
103 return vocabulary;
104 }
105
106 public void addVocabularyResources(
107 AssetVocabulary vocabulary, boolean addCommunityPermissions,
108 boolean addGuestPermissions)
109 throws PortalException, SystemException {
110
111 resourceLocalService.addResources(
112 vocabulary.getCompanyId(), vocabulary.getGroupId(),
113 vocabulary.getUserId(), AssetVocabulary.class.getName(),
114 vocabulary.getVocabularyId(), false, addCommunityPermissions,
115 addGuestPermissions);
116 }
117
118 public void addVocabularyResources(
119 AssetVocabulary vocabulary, String[] communityPermissions,
120 String[] guestPermissions)
121 throws PortalException, SystemException {
122
123 resourceLocalService.addModelResources(
124 vocabulary.getCompanyId(), vocabulary.getGroupId(),
125 vocabulary.getUserId(), AssetVocabulary.class.getName(),
126 vocabulary.getVocabularyId(), communityPermissions,
127 guestPermissions);
128 }
129
130 public void deleteVocabulary(AssetVocabulary vocabulary)
131 throws PortalException, SystemException {
132
133
135 assetVocabularyPersistence.remove(vocabulary);
136
137
139 resourceLocalService.deleteResource(
140 vocabulary.getCompanyId(), AssetVocabulary.class.getName(),
141 ResourceConstants.SCOPE_INDIVIDUAL, vocabulary.getVocabularyId());
142
143
145 assetCategoryLocalService.deleteVocabularyCategories(
146 vocabulary.getVocabularyId());
147 }
148
149 public void deleteVocabulary(long vocabularyId)
150 throws PortalException, SystemException {
151
152 AssetVocabulary vocabulary =
153 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
154
155 deleteVocabulary(vocabulary);
156 }
157
158 public List<AssetVocabulary> getCompanyVocabularies(long companyId)
159 throws SystemException {
160
161 return assetVocabularyPersistence.findByCompanyId(companyId);
162 }
163
164 public List<AssetVocabulary> getGroupsVocabularies(long[] groupIds)
165 throws PortalException, SystemException {
166
167 List<AssetVocabulary> vocabularies = new ArrayList<AssetVocabulary>();
168
169 for (long groupId : groupIds) {
170 vocabularies.addAll(getGroupVocabularies(groupId));
171 }
172
173 return vocabularies;
174 }
175
176 public List<AssetVocabulary> getGroupVocabularies(long groupId)
177 throws PortalException, SystemException {
178
179 List<AssetVocabulary> vocabularies =
180 assetVocabularyPersistence.findByGroupId(groupId);
181
182 if (vocabularies.isEmpty()) {
183 Group group = groupLocalService.getGroup(groupId);
184
185 long defaultUserId = userLocalService.getDefaultUserId(
186 group.getCompanyId());
187
188 ServiceContext serviceContext = new ServiceContext();
189
190 serviceContext.setScopeGroupId(groupId);
191
192 Map<Locale, String> titleMap = new HashMap<Locale, String>();
193
194 titleMap.put(
195 LocaleUtil.getDefault(), PropsValues.ASSET_VOCABULARY_DEFAULT);
196
197 AssetVocabulary vocabulary =
198 assetVocabularyLocalService.addVocabulary(
199 null, defaultUserId, titleMap, null, StringPool.BLANK,
200 serviceContext);
201
202 vocabularies = ListUtil.copy(vocabularies);
203
204 vocabularies.add(vocabulary);
205 }
206
207 return vocabularies;
208 }
209
210 public AssetVocabulary getGroupVocabulary(long groupId, String name)
211 throws PortalException, SystemException {
212
213 return assetVocabularyPersistence.findByG_N(groupId, name);
214 }
215
216 public AssetVocabulary getVocabulary(long vocabularyId)
217 throws PortalException, SystemException {
218
219 return assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
220 }
221
222 public AssetVocabulary updateVocabulary(
223 long vocabularyId, Map<Locale, String> titleMap,
224 Map<Locale, String> descriptionMap, String settings,
225 ServiceContext serviceContext)
226 throws PortalException, SystemException {
227
228 String name = titleMap.get(LocaleUtil.getDefault());
229
230 AssetVocabulary vocabulary =
231 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
232
233 if (!vocabulary.getName().equals(name) &&
234 hasVocabulary(vocabulary.getGroupId(), name)) {
235
236 throw new DuplicateVocabularyException(name);
237 }
238
239 vocabulary.setModifiedDate(new Date());
240 vocabulary.setName(name);
241 vocabulary.setTitleMap(titleMap);
242 vocabulary.setDescriptionMap(descriptionMap);
243 vocabulary.setSettings(settings);
244
245 assetVocabularyPersistence.update(vocabulary, false);
246
247 return vocabulary;
248 }
249
250 protected boolean hasVocabulary(long groupId, String name)
251 throws SystemException {
252
253 if (assetVocabularyPersistence.countByG_N(groupId, name) == 0) {
254 return false;
255 }
256 else {
257 return true;
258 }
259 }
260
261 }