001
014
015 package com.liferay.portlet.asset.service.impl;
016
017 import com.liferay.portal.kernel.cache.ThreadLocalCachable;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.exception.SystemException;
020 import com.liferay.portal.kernel.search.Indexer;
021 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
022 import com.liferay.portal.kernel.transaction.TransactionCommitCallbackRegistryUtil;
023 import com.liferay.portal.kernel.util.CharPool;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.ListUtil;
026 import com.liferay.portal.kernel.util.LocaleUtil;
027 import com.liferay.portal.kernel.util.OrderByComparator;
028 import com.liferay.portal.kernel.util.StringBundler;
029 import com.liferay.portal.kernel.util.StringPool;
030 import com.liferay.portal.kernel.util.StringUtil;
031 import com.liferay.portal.kernel.util.Validator;
032 import com.liferay.portal.model.ModelHintsUtil;
033 import com.liferay.portal.model.ResourceConstants;
034 import com.liferay.portal.model.User;
035 import com.liferay.portal.service.ServiceContext;
036 import com.liferay.portal.util.PortalUtil;
037 import com.liferay.portlet.asset.AssetCategoryNameException;
038 import com.liferay.portlet.asset.DuplicateCategoryException;
039 import com.liferay.portlet.asset.model.AssetCategory;
040 import com.liferay.portlet.asset.model.AssetCategoryConstants;
041 import com.liferay.portlet.asset.model.AssetCategoryProperty;
042 import com.liferay.portlet.asset.model.AssetEntry;
043 import com.liferay.portlet.asset.service.base.AssetCategoryLocalServiceBaseImpl;
044
045 import java.util.Collections;
046 import java.util.Date;
047 import java.util.HashMap;
048 import java.util.Iterator;
049 import java.util.List;
050 import java.util.Locale;
051 import java.util.Map;
052 import java.util.concurrent.Callable;
053
054
063 public class AssetCategoryLocalServiceImpl
064 extends AssetCategoryLocalServiceBaseImpl {
065
066 @Override
067 public AssetCategory addCategory(
068 long userId, long parentCategoryId, Map<Locale, String> titleMap,
069 Map<Locale, String> descriptionMap, long vocabularyId,
070 String[] categoryProperties, ServiceContext serviceContext)
071 throws PortalException, SystemException {
072
073
074
075 User user = userPersistence.findByPrimaryKey(userId);
076 long groupId = serviceContext.getScopeGroupId();
077
078 String name = titleMap.get(LocaleUtil.getSiteDefault());
079
080 name = ModelHintsUtil.trimString(
081 AssetCategory.class.getName(), "name", name);
082
083 if (categoryProperties == null) {
084 categoryProperties = new String[0];
085 }
086
087 Date now = new Date();
088
089 validate(0, parentCategoryId, name, vocabularyId);
090
091 if (parentCategoryId > 0) {
092 assetCategoryPersistence.findByPrimaryKey(parentCategoryId);
093 }
094
095 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
096
097 long categoryId = counterLocalService.increment();
098
099 AssetCategory category = assetCategoryPersistence.create(categoryId);
100
101 category.setUuid(serviceContext.getUuid());
102 category.setGroupId(groupId);
103 category.setCompanyId(user.getCompanyId());
104 category.setUserId(user.getUserId());
105 category.setUserName(user.getFullName());
106 category.setCreateDate(now);
107 category.setModifiedDate(now);
108 category.setParentCategoryId(parentCategoryId);
109 category.setName(name);
110 category.setTitleMap(titleMap);
111 category.setDescriptionMap(descriptionMap);
112 category.setVocabularyId(vocabularyId);
113
114 assetCategoryPersistence.update(category);
115
116
117
118 if (serviceContext.isAddGroupPermissions() ||
119 serviceContext.isAddGuestPermissions()) {
120
121 addCategoryResources(
122 category, serviceContext.isAddGroupPermissions(),
123 serviceContext.isAddGuestPermissions());
124 }
125 else {
126 addCategoryResources(
127 category, serviceContext.getGroupPermissions(),
128 serviceContext.getGuestPermissions());
129 }
130
131
132
133 for (int i = 0; i < categoryProperties.length; i++) {
134 String[] categoryProperty = StringUtil.split(
135 categoryProperties[i],
136 AssetCategoryConstants.PROPERTY_KEY_VALUE_SEPARATOR);
137
138 if (categoryProperty.length <= 1) {
139 categoryProperty = StringUtil.split(
140 categoryProperties[i], CharPool.COLON);
141 }
142
143 String key = StringPool.BLANK;
144 String value = StringPool.BLANK;
145
146 if (categoryProperty.length > 1) {
147 key = GetterUtil.getString(categoryProperty[0]);
148 value = GetterUtil.getString(categoryProperty[1]);
149 }
150
151 if (Validator.isNotNull(key)) {
152 assetCategoryPropertyLocalService.addCategoryProperty(
153 userId, categoryId, key, value);
154 }
155 }
156
157
158
159 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
160 AssetCategory.class);
161
162 indexer.reindex(category);
163
164 return category;
165 }
166
167 @Override
168 public AssetCategory addCategory(
169 long userId, String title, long vocabularyId,
170 ServiceContext serviceContext)
171 throws PortalException, SystemException {
172
173 Map<Locale, String> titleMap = new HashMap<Locale, String>();
174
175 Locale locale = LocaleUtil.getSiteDefault();
176
177 titleMap.put(locale, title);
178
179 Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
180
181 descriptionMap.put(locale, StringPool.BLANK);
182
183 return addCategory(
184 userId, AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID, titleMap,
185 descriptionMap, vocabularyId, null, serviceContext);
186 }
187
188 @Override
189 public void addCategoryResources(
190 AssetCategory category, boolean addGroupPermissions,
191 boolean addGuestPermissions)
192 throws PortalException, SystemException {
193
194 resourceLocalService.addResources(
195 category.getCompanyId(), category.getGroupId(),
196 category.getUserId(), AssetCategory.class.getName(),
197 category.getCategoryId(), false, addGroupPermissions,
198 addGuestPermissions);
199 }
200
201 @Override
202 public void addCategoryResources(
203 AssetCategory category, String[] groupPermissions,
204 String[] guestPermissions)
205 throws PortalException, SystemException {
206
207 resourceLocalService.addModelResources(
208 category.getCompanyId(), category.getGroupId(),
209 category.getUserId(), AssetCategory.class.getName(),
210 category.getCategoryId(), groupPermissions, guestPermissions);
211 }
212
213 @Override
214 public void deleteCategory(AssetCategory category)
215 throws PortalException, SystemException {
216
217
218
219 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
220 AssetCategory.class);
221
222 indexer.delete(category);
223
224 deleteCategory(category, false);
225 }
226
227 @Override
228 public void deleteCategory(long categoryId)
229 throws PortalException, SystemException {
230
231 AssetCategory category = assetCategoryPersistence.findByPrimaryKey(
232 categoryId);
233
234 deleteCategory(category);
235 }
236
237 @Override
238 public void deleteVocabularyCategories(long vocabularyId)
239 throws PortalException, SystemException {
240
241 List<AssetCategory> categories =
242 assetCategoryPersistence.findByVocabularyId(vocabularyId);
243
244 for (AssetCategory category : categories) {
245 if (category.getParentCategoryId() ==
246 AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
247
248 deleteCategory(category.getCategoryId());
249 }
250 }
251 }
252
253 @Override
254 public AssetCategory fetchCategory(long categoryId) throws SystemException {
255 return assetCategoryPersistence.fetchByPrimaryKey(categoryId);
256 }
257
258 @Override
259 public List<AssetCategory> getCategories() throws SystemException {
260 return assetCategoryPersistence.findAll();
261 }
262
263 @Override
264 @ThreadLocalCachable
265 public List<AssetCategory> getCategories(long classNameId, long classPK)
266 throws SystemException {
267
268 AssetEntry entry = assetEntryPersistence.fetchByC_C(
269 classNameId, classPK);
270
271 if (entry == null) {
272 return Collections.emptyList();
273 }
274
275 return assetEntryPersistence.getAssetCategories(entry.getEntryId());
276 }
277
278 @Override
279 public List<AssetCategory> getCategories(String className, long classPK)
280 throws SystemException {
281
282 long classNameId = PortalUtil.getClassNameId(className);
283
284 return getCategories(classNameId, classPK);
285 }
286
287 @Override
288 public AssetCategory getCategory(long categoryId)
289 throws PortalException, SystemException {
290
291 return assetCategoryPersistence.findByPrimaryKey(categoryId);
292 }
293
294 @Override
295 public AssetCategory getCategory(String uuid, long groupId)
296 throws PortalException, SystemException {
297
298 return assetCategoryPersistence.findByUUID_G(uuid, groupId);
299 }
300
301 @Override
302 public long[] getCategoryIds(String className, long classPK)
303 throws SystemException {
304
305 return getCategoryIds(getCategories(className, classPK));
306 }
307
308 @Override
309 public String[] getCategoryNames() throws SystemException {
310 return getCategoryNames(getCategories());
311 }
312
313 @Override
314 public String[] getCategoryNames(long classNameId, long classPK)
315 throws SystemException {
316
317 return getCategoryNames(getCategories(classNameId, classPK));
318 }
319
320 @Override
321 public String[] getCategoryNames(String className, long classPK)
322 throws SystemException {
323
324 return getCategoryNames(getCategories(className, classPK));
325 }
326
327 @Override
328 public List<AssetCategory> getChildCategories(long parentCategoryId)
329 throws SystemException {
330
331 return assetCategoryPersistence.findByParentCategoryId(
332 parentCategoryId);
333 }
334
335 @Override
336 public List<AssetCategory> getChildCategories(
337 long parentCategoryId, int start, int end, OrderByComparator obc)
338 throws SystemException {
339
340 return assetCategoryPersistence.findByParentCategoryId(
341 parentCategoryId, start, end, obc);
342 }
343
344 @Override
345 public int getChildCategoriesCount(long parentCategoryId)
346 throws SystemException {
347
348 return assetCategoryPersistence.countByParentCategoryId(
349 parentCategoryId);
350 }
351
352 @Override
353 public List<AssetCategory> getEntryCategories(long entryId)
354 throws SystemException {
355
356 return assetEntryPersistence.getAssetCategories(entryId);
357 }
358
359 @Override
360 public List<Long> getSubcategoryIds(long parentCategoryId)
361 throws SystemException {
362
363 return assetCategoryFinder.findByG_L(parentCategoryId);
364 }
365
366 @Override
367 public List<AssetCategory> getVocabularyCategories(
368 long vocabularyId, int start, int end, OrderByComparator obc)
369 throws SystemException {
370
371 return assetCategoryPersistence.findByVocabularyId(
372 vocabularyId, start, end, obc);
373 }
374
375 @Override
376 public List<AssetCategory> getVocabularyCategories(
377 long parentCategoryId, long vocabularyId, int start, int end,
378 OrderByComparator obc)
379 throws SystemException {
380
381 return assetCategoryPersistence.findByP_V(
382 parentCategoryId, vocabularyId, start, end, obc);
383 }
384
385 @Override
386 public int getVocabularyCategoriesCount(long vocabularyId)
387 throws SystemException {
388
389 return assetCategoryPersistence.countByVocabularyId(vocabularyId);
390 }
391
392 @Override
393 public List<AssetCategory> getVocabularyRootCategories(
394 long vocabularyId, int start, int end, OrderByComparator obc)
395 throws SystemException {
396
397 return getVocabularyCategories(
398 AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID, vocabularyId,
399 start, end, obc);
400 }
401
402 @Override
403 public int getVocabularyRootCategoriesCount(long vocabularyId)
404 throws SystemException {
405
406 return assetCategoryPersistence.countByP_V(
407 AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID, vocabularyId);
408 }
409
410 @Override
411 public void mergeCategories(long fromCategoryId, long toCategoryId)
412 throws PortalException, SystemException {
413
414 List<AssetEntry> entries = assetCategoryPersistence.getAssetEntries(
415 fromCategoryId);
416
417 assetCategoryPersistence.addAssetEntries(toCategoryId, entries);
418
419 List<AssetCategoryProperty> categoryProperties =
420 assetCategoryPropertyPersistence.findByCategoryId(fromCategoryId);
421
422 for (AssetCategoryProperty fromCategoryProperty : categoryProperties) {
423 AssetCategoryProperty toCategoryProperty =
424 assetCategoryPropertyPersistence.fetchByCA_K(
425 toCategoryId, fromCategoryProperty.getKey());
426
427 if (toCategoryProperty == null) {
428 fromCategoryProperty.setCategoryId(toCategoryId);
429
430 assetCategoryPropertyPersistence.update(fromCategoryProperty);
431 }
432 }
433
434
435
436 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
437 AssetCategory.class);
438
439 indexer.reindex(toCategoryId);
440
441 deleteCategory(fromCategoryId);
442 }
443
444 @Override
445 public AssetCategory moveCategory(
446 long categoryId, long parentCategoryId, long vocabularyId,
447 ServiceContext serviceContext)
448 throws PortalException, SystemException {
449
450 AssetCategory category = assetCategoryPersistence.findByPrimaryKey(
451 categoryId);
452
453 validate(
454 categoryId, parentCategoryId, category.getName(), vocabularyId);
455
456 if (parentCategoryId > 0) {
457 assetCategoryPersistence.findByPrimaryKey(parentCategoryId);
458 }
459
460 if (vocabularyId != category.getVocabularyId()) {
461 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
462
463 category.setVocabularyId(vocabularyId);
464
465 updateChildrenVocabularyId(category, vocabularyId);
466 }
467
468 category.setModifiedDate(new Date());
469 category.setParentCategoryId(parentCategoryId);
470
471 assetCategoryPersistence.update(category);
472
473
474
475 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
476 AssetCategory.class);
477
478 indexer.reindex(category);
479
480 return category;
481 }
482
483 @Override
484 public void rebuildTree(long groupId, boolean force)
485 throws SystemException {
486
487 assetCategoryPersistence.rebuildTree(groupId, force);
488 }
489
490 @Override
491 public List<AssetCategory> search(
492 long groupId, String name, String[] categoryProperties, int start,
493 int end)
494 throws SystemException {
495
496 return assetCategoryFinder.findByG_N_P(
497 groupId, name, categoryProperties, start, end);
498 }
499
500 @Override
501 public AssetCategory updateCategory(
502 long userId, long categoryId, long parentCategoryId,
503 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
504 long vocabularyId, String[] categoryProperties,
505 ServiceContext serviceContext)
506 throws PortalException, SystemException {
507
508
509
510 String name = titleMap.get(LocaleUtil.getSiteDefault());
511
512 name = ModelHintsUtil.trimString(
513 AssetCategory.class.getName(), "name", name);
514
515 if (categoryProperties == null) {
516 categoryProperties = new String[0];
517 }
518
519 validate(categoryId, parentCategoryId, name, vocabularyId);
520
521 if (parentCategoryId > 0) {
522 assetCategoryPersistence.findByPrimaryKey(parentCategoryId);
523 }
524
525 AssetCategory category = assetCategoryPersistence.findByPrimaryKey(
526 categoryId);
527
528 String oldName = category.getName();
529
530 if (vocabularyId != category.getVocabularyId()) {
531 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
532
533 parentCategoryId =
534 AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
535
536 category.setVocabularyId(vocabularyId);
537
538 updateChildrenVocabularyId(category, vocabularyId);
539 }
540
541 category.setModifiedDate(new Date());
542 category.setParentCategoryId(parentCategoryId);
543 category.setName(name);
544 category.setTitleMap(titleMap);
545 category.setDescriptionMap(descriptionMap);
546
547 assetCategoryPersistence.update(category);
548
549
550
551 List<AssetCategoryProperty> oldCategoryProperties =
552 assetCategoryPropertyPersistence.findByCategoryId(categoryId);
553
554 oldCategoryProperties = ListUtil.copy(oldCategoryProperties);
555
556 for (int i = 0; i < categoryProperties.length; i++) {
557 String[] categoryProperty = StringUtil.split(
558 categoryProperties[i],
559 AssetCategoryConstants.PROPERTY_KEY_VALUE_SEPARATOR);
560
561 if (categoryProperty.length <= 1) {
562 categoryProperty = StringUtil.split(
563 categoryProperties[i], CharPool.COLON);
564 }
565
566 String key = StringPool.BLANK;
567
568 if (categoryProperty.length > 0) {
569 key = GetterUtil.getString(categoryProperty[0]);
570 }
571
572 String value = StringPool.BLANK;
573
574 if (categoryProperty.length > 1) {
575 value = GetterUtil.getString(categoryProperty[1]);
576 }
577
578 if (Validator.isNotNull(key)) {
579 boolean addCategoryProperty = true;
580
581 AssetCategoryProperty oldCategoryProperty = null;
582
583 Iterator<AssetCategoryProperty> iterator =
584 oldCategoryProperties.iterator();
585
586 while (iterator.hasNext()) {
587 oldCategoryProperty = iterator.next();
588
589 if ((categoryId == oldCategoryProperty.getCategoryId()) &&
590 key.equals(oldCategoryProperty.getKey())) {
591
592 addCategoryProperty = false;
593
594 if (userId != oldCategoryProperty.getUserId()) {
595 User user = userPersistence.findByPrimaryKey(
596 userId);
597
598 oldCategoryProperty.setUserId(userId);
599 oldCategoryProperty.setUserName(user.getFullName());
600
601 assetCategoryPropertyPersistence.update(
602 oldCategoryProperty);
603 }
604
605 if (!value.equals(oldCategoryProperty.getValue())) {
606 assetCategoryPropertyLocalService.
607 updateCategoryProperty(
608 oldCategoryProperty.getCategoryPropertyId(),
609 key, value);
610 }
611
612 iterator.remove();
613
614 break;
615 }
616 }
617
618 if (addCategoryProperty) {
619 assetCategoryPropertyLocalService.addCategoryProperty(
620 userId, categoryId, key, value);
621 }
622 }
623 }
624
625 for (AssetCategoryProperty categoryProperty : oldCategoryProperties) {
626 assetCategoryPropertyLocalService.deleteAssetCategoryProperty(
627 categoryProperty);
628 }
629
630
631
632 if (!oldName.equals(name)) {
633 List<AssetEntry> entries = assetCategoryPersistence.getAssetEntries(
634 category.getCategoryId());
635
636 assetEntryLocalService.reindex(entries);
637 }
638
639 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
640 AssetCategory.class);
641
642 indexer.reindex(category);
643
644 return category;
645 }
646
647 protected void deleteCategory(AssetCategory category, boolean childCategory)
648 throws PortalException, SystemException {
649
650
651
652 List<AssetCategory> categories =
653 assetCategoryPersistence.findByParentCategoryId(
654 category.getCategoryId());
655
656 for (AssetCategory curCategory : categories) {
657 deleteCategory(curCategory, true);
658 }
659
660 if (!categories.isEmpty() && !childCategory) {
661 final long groupId = category.getGroupId();
662
663 TransactionCommitCallbackRegistryUtil.registerCallback(
664 new Callable<Void>() {
665
666 @Override
667 public Void call() throws Exception {
668 assetCategoryLocalService.rebuildTree(groupId, true);
669
670 return null;
671 }
672
673 });
674 }
675
676
677
678 assetCategoryPersistence.remove(category);
679
680
681
682 resourceLocalService.deleteResource(
683 category.getCompanyId(), AssetCategory.class.getName(),
684 ResourceConstants.SCOPE_INDIVIDUAL, category.getCategoryId());
685
686
687
688 List<AssetEntry> entries = assetTagPersistence.getAssetEntries(
689 category.getCategoryId());
690
691
692
693 assetCategoryPropertyLocalService.deleteCategoryProperties(
694 category.getCategoryId());
695
696
697
698 assetEntryLocalService.reindex(entries);
699
700 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
701 AssetCategory.class);
702
703 indexer.delete(category);
704 }
705
706 protected long[] getCategoryIds(List<AssetCategory> categories) {
707 return StringUtil.split(
708 ListUtil.toString(categories, AssetCategory.CATEGORY_ID_ACCESSOR),
709 0L);
710 }
711
712 protected String[] getCategoryNames(List<AssetCategory> categories) {
713 return StringUtil.split(
714 ListUtil.toString(categories, AssetCategory.NAME_ACCESSOR));
715 }
716
717 protected void updateChildrenVocabularyId(
718 AssetCategory category, long vocabularyId)
719 throws SystemException {
720
721 List<AssetCategory> childrenCategories =
722 assetCategoryPersistence.findByParentCategoryId(
723 category.getCategoryId());
724
725 if (!childrenCategories.isEmpty()) {
726 for (AssetCategory childCategory : childrenCategories) {
727 childCategory.setVocabularyId(vocabularyId);
728 childCategory.setModifiedDate(new Date());
729
730 assetCategoryPersistence.update(childCategory);
731
732 updateChildrenVocabularyId (childCategory, vocabularyId);
733 }
734 }
735 }
736
737 protected void validate(
738 long categoryId, long parentCategoryId, String name,
739 long vocabularyId)
740 throws PortalException, SystemException {
741
742 if (Validator.isNull(name)) {
743 throw new AssetCategoryNameException();
744 }
745
746 AssetCategory category = assetCategoryPersistence.fetchByP_N_V(
747 parentCategoryId, name, vocabularyId);
748
749 if ((category != null) && (category.getCategoryId() != categoryId)) {
750 StringBundler sb = new StringBundler(4);
751
752 sb.append("There is another category named ");
753 sb.append(name);
754 sb.append(" as a child of category ");
755 sb.append(parentCategoryId);
756
757 throw new DuplicateCategoryException(sb.toString());
758 }
759 }
760
761 }