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.json.JSONArray;
20 import com.liferay.portal.kernel.util.GetterUtil;
21 import com.liferay.portal.kernel.util.ListUtil;
22 import com.liferay.portal.kernel.util.LocaleUtil;
23 import com.liferay.portal.kernel.util.OrderByComparator;
24 import com.liferay.portal.kernel.util.StringBundler;
25 import com.liferay.portal.kernel.util.StringPool;
26 import com.liferay.portal.kernel.util.StringUtil;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portal.model.ResourceConstants;
29 import com.liferay.portal.model.User;
30 import com.liferay.portal.service.ServiceContext;
31 import com.liferay.portal.util.PortalUtil;
32 import com.liferay.portlet.asset.DuplicateCategoryException;
33 import com.liferay.portlet.asset.model.AssetCategory;
34 import com.liferay.portlet.asset.model.AssetCategoryConstants;
35 import com.liferay.portlet.asset.model.AssetCategoryProperty;
36 import com.liferay.portlet.asset.model.AssetEntry;
37 import com.liferay.portlet.asset.service.base.AssetCategoryLocalServiceBaseImpl;
38 import com.liferay.util.Autocomplete;
39
40 import java.util.Date;
41 import java.util.List;
42 import java.util.Locale;
43 import java.util.Map;
44
45
54 public class AssetCategoryLocalServiceImpl
55 extends AssetCategoryLocalServiceBaseImpl {
56
57 public AssetCategory addCategory(
58 String uuid, long userId, long parentCategoryId,
59 Map<Locale, String> titleMap, long vocabularyId,
60 String[] categoryProperties, ServiceContext serviceContext)
61 throws PortalException, SystemException {
62
63
65 User user = userPersistence.findByPrimaryKey(userId);
66 long groupId = serviceContext.getScopeGroupId();
67 String name = titleMap.get(LocaleUtil.getDefault());
68
69 if (categoryProperties == null) {
70 categoryProperties = new String[0];
71 }
72
73 Date now = new Date();
74
75 validate(0, parentCategoryId, name, vocabularyId);
76
77 if (parentCategoryId > 0) {
78 assetCategoryPersistence.findByPrimaryKey(parentCategoryId);
79 }
80
81 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
82
83 long categoryId = counterLocalService.increment();
84
85 AssetCategory category = assetCategoryPersistence.create(categoryId);
86
87 category.setUuid(uuid);
88 category.setGroupId(groupId);
89 category.setCompanyId(user.getCompanyId());
90 category.setUserId(user.getUserId());
91 category.setUserName(user.getFullName());
92 category.setCreateDate(now);
93 category.setModifiedDate(now);
94 category.setParentCategoryId(parentCategoryId);
95 category.setName(name);
96 category.setTitleMap(titleMap);
97 category.setVocabularyId(vocabularyId);
98
99 assetCategoryPersistence.update(category, false);
100
101
103 if (serviceContext.getAddCommunityPermissions() ||
104 serviceContext.getAddGuestPermissions()) {
105
106 addCategoryResources(
107 category, serviceContext.getAddCommunityPermissions(),
108 serviceContext.getAddGuestPermissions());
109 }
110 else {
111 addCategoryResources(
112 category, serviceContext.getCommunityPermissions(),
113 serviceContext.getGuestPermissions());
114 }
115
116
118 for (int i = 0; i < categoryProperties.length; i++) {
119 String[] categoryProperty = StringUtil.split(
120 categoryProperties[i], StringPool.COLON);
121
122 String key = StringPool.BLANK;
123 String value = StringPool.BLANK;
124
125 if (categoryProperty.length > 1) {
126 key = GetterUtil.getString(categoryProperty[0]);
127 value = GetterUtil.getString(categoryProperty[1]);
128 }
129
130 if (Validator.isNotNull(key)) {
131 assetCategoryPropertyLocalService.addCategoryProperty(
132 userId, categoryId, key, value);
133 }
134 }
135
136 return category;
137 }
138
139 public void addCategoryResources(
140 AssetCategory category, boolean addCommunityPermissions,
141 boolean addGuestPermissions)
142 throws PortalException, SystemException {
143
144 resourceLocalService.addResources(
145 category.getCompanyId(), category.getGroupId(),
146 category.getUserId(), AssetCategory.class.getName(),
147 category.getCategoryId(), false, addCommunityPermissions,
148 addGuestPermissions);
149 }
150
151 public void addCategoryResources(
152 AssetCategory category, String[] communityPermissions,
153 String[] guestPermissions)
154 throws PortalException, SystemException {
155
156 resourceLocalService.addModelResources(
157 category.getCompanyId(), category.getGroupId(),
158 category.getUserId(), AssetCategory.class.getName(),
159 category.getCategoryId(), communityPermissions, guestPermissions);
160 }
161
162 public void deleteCategory(AssetCategory category)
163 throws PortalException, SystemException {
164
165
167 assetCategoryPersistence.remove(category);
168
169
171 resourceLocalService.deleteResource(
172 category.getCompanyId(), AssetCategory.class.getName(),
173 ResourceConstants.SCOPE_INDIVIDUAL, category.getCategoryId());
174
175
177 List<AssetCategory> categories =
178 assetCategoryPersistence.findByParentCategoryId(
179 category.getCategoryId());
180
181 for (AssetCategory curCategory : categories) {
182 deleteCategory(curCategory);
183 }
184
185
187 assetCategoryPropertyLocalService.deleteCategoryProperties(
188 category.getCategoryId());
189 }
190
191 public void deleteCategory(long categoryId)
192 throws PortalException, SystemException {
193
194 AssetCategory category = assetCategoryPersistence.findByPrimaryKey(
195 categoryId);
196
197 deleteCategory(category);
198 }
199
200 public void deleteVocabularyCategories(long vocabularyId)
201 throws PortalException, SystemException {
202
203 List<AssetCategory> categories =
204 assetCategoryPersistence.findByVocabularyId(vocabularyId);
205
206 for (AssetCategory category : categories) {
207 deleteCategory(category);
208 }
209 }
210
211 public List<AssetCategory> getCategories() throws SystemException {
212 return assetCategoryPersistence.findAll();
213 }
214
215 public List<AssetCategory> getCategories(long classNameId, long classPK)
216 throws SystemException {
217
218 return assetCategoryFinder.findByC_C(classNameId, classPK);
219 }
220
221 public List<AssetCategory> getCategories(String className, long classPK)
222 throws SystemException {
223
224 long classNameId = PortalUtil.getClassNameId(className);
225
226 return getCategories(classNameId, classPK);
227 }
228
229 public AssetCategory getCategory(long categoryId)
230 throws PortalException, SystemException {
231
232 return assetCategoryPersistence.findByPrimaryKey(categoryId);
233 }
234
235 public long[] getCategoryIds(String className, long classPK)
236 throws SystemException {
237
238 return getCategoryIds(getCategories(className, classPK));
239 }
240
241 public List<AssetCategory> getChildCategories(
242 long parentCategoryId, int start, int end, OrderByComparator obc)
243 throws SystemException {
244
245 return assetCategoryPersistence.findByParentCategoryId(
246 parentCategoryId, start, end, obc);
247 }
248
249 public int getChildCategoriesCount(long parentCategoryId)
250 throws SystemException {
251
252 return assetCategoryPersistence.countByParentCategoryId(
253 parentCategoryId);
254 }
255
256 public List<AssetCategory> getEntryCategories(long entryId)
257 throws SystemException {
258
259 return assetCategoryFinder.findByEntryId(entryId);
260 }
261
262 public List<AssetCategory> getVocabularyCategories(
263 long vocabularyId, int start, int end, OrderByComparator obc)
264 throws SystemException {
265
266 return assetCategoryPersistence.findByVocabularyId(
267 vocabularyId, start, end, obc);
268 }
269
270 public List<AssetCategory> getVocabularyCategories(
271 long parentCategoryId, long vocabularyId, int start, int end,
272 OrderByComparator obc)
273 throws SystemException {
274
275 return assetCategoryPersistence.findByP_V(
276 parentCategoryId, vocabularyId, start, end, obc);
277 }
278
279 public List<AssetCategory> getVocabularyRootCategories(
280 long vocabularyId, int start, int end, OrderByComparator obc)
281 throws SystemException {
282
283 return getVocabularyCategories(
284 AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID, vocabularyId,
285 start, end, obc);
286 }
287
288 public void mergeCategories(long fromCategoryId, long toCategoryId)
289 throws PortalException, SystemException {
290
291 List<AssetEntry> entries = assetCategoryPersistence.getAssetEntries(
292 fromCategoryId);
293
294 assetCategoryPersistence.addAssetEntries(toCategoryId, entries);
295
296 List<AssetCategoryProperty> categoryProperties =
297 assetCategoryPropertyPersistence.findByCategoryId(fromCategoryId);
298
299 for (AssetCategoryProperty fromCategoryProperty : categoryProperties) {
300 AssetCategoryProperty toCategoryProperty =
301 assetCategoryPropertyPersistence.fetchByCA_K(
302 toCategoryId, fromCategoryProperty.getKey());
303
304 if (toCategoryProperty == null) {
305 fromCategoryProperty.setCategoryId(toCategoryId);
306
307 assetCategoryPropertyPersistence.update(
308 fromCategoryProperty, false);
309 }
310 }
311
312 deleteCategory(fromCategoryId);
313 }
314
315 public JSONArray search(
316 long groupId, String name, String[] categoryProperties, int start,
317 int end)
318 throws SystemException {
319
320 List<AssetCategory> list = assetCategoryFinder.findByG_N_P(
321 groupId, name, categoryProperties, start, end);
322
323 return Autocomplete.listToJson(list, "name", "name");
324 }
325
326 public AssetCategory updateCategory(
327 long userId, long categoryId, long parentCategoryId,
328 Map<Locale, String> titleMap, long vocabularyId,
329 String[] categoryProperties, ServiceContext serviceContext)
330 throws PortalException, SystemException {
331
332
334 String name = titleMap.get(LocaleUtil.getDefault());
335
336 if (categoryProperties == null) {
337 categoryProperties = new String[0];
338 }
339
340 validate(categoryId, parentCategoryId, name, vocabularyId);
341
342 if (parentCategoryId > 0) {
343 assetCategoryPersistence.findByPrimaryKey(parentCategoryId);
344 }
345
346 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
347
348 AssetCategory category = assetCategoryPersistence.findByPrimaryKey(
349 categoryId);
350
351 category.setModifiedDate(new Date());
352 category.setParentCategoryId(parentCategoryId);
353 category.setName(name);
354 category.setTitleMap(titleMap);
355 category.setVocabularyId(vocabularyId);
356
357 assetCategoryPersistence.update(category, false);
358
359
361 List<AssetCategoryProperty> oldCategoryProperties =
362 assetCategoryPropertyPersistence.findByCategoryId(categoryId);
363
364 for (AssetCategoryProperty categoryProperty : oldCategoryProperties) {
365 assetCategoryPropertyLocalService.deleteAssetCategoryProperty(
366 categoryProperty);
367 }
368
369 for (int i = 0; i < categoryProperties.length; i++) {
370 String[] categoryProperty = StringUtil.split(
371 categoryProperties[i], StringPool.COLON);
372
373 String key = StringPool.BLANK;
374
375 if (categoryProperty.length > 0) {
376 key = GetterUtil.getString(categoryProperty[0]);
377 }
378
379 String value = StringPool.BLANK;
380
381 if (categoryProperty.length > 1) {
382 value = GetterUtil.getString(categoryProperty[1]);
383 }
384
385 if (Validator.isNotNull(key)) {
386 assetCategoryPropertyLocalService.addCategoryProperty(
387 userId, categoryId, key, value);
388 }
389 }
390
391 return category;
392 }
393
394 protected long[] getCategoryIds(List <AssetCategory>categories) {
395 return StringUtil.split(
396 ListUtil.toString(categories, "categoryId"), 0L);
397 }
398
399 protected void validate(
400 long categoryId, long parentCategoryId, String name,
401 long vocabularyId)
402 throws PortalException, SystemException {
403
404 List<AssetCategory> categories = null;
405
406 if (parentCategoryId == 0) {
407 categories = assetCategoryPersistence.findByN_V(
408 name, vocabularyId);
409 }
410 else {
411 categories = assetCategoryPersistence.findByP_N(
412 parentCategoryId, name);
413 }
414
415 if ((categories.size() > 0) &&
416 (categories.get(0).getCategoryId() != categoryId)) {
417
418 StringBundler sb = new StringBundler(4);
419
420 sb.append("There is another category named ");
421 sb.append(name);
422 sb.append(" as a child of category ");
423 sb.append(parentCategoryId);
424
425 throw new DuplicateCategoryException(sb.toString());
426 }
427 }
428
429 }