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.util.CharPool;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.ListUtil;
023 import com.liferay.portal.kernel.util.LocaleUtil;
024 import com.liferay.portal.kernel.util.OrderByComparator;
025 import com.liferay.portal.kernel.util.StringBundler;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.kernel.util.StringUtil;
028 import com.liferay.portal.kernel.util.Validator;
029 import com.liferay.portal.model.ModelHintsUtil;
030 import com.liferay.portal.model.ResourceConstants;
031 import com.liferay.portal.model.User;
032 import com.liferay.portal.service.ServiceContext;
033 import com.liferay.portal.util.PortalUtil;
034 import com.liferay.portlet.asset.AssetCategoryNameException;
035 import com.liferay.portlet.asset.DuplicateCategoryException;
036 import com.liferay.portlet.asset.model.AssetCategory;
037 import com.liferay.portlet.asset.model.AssetCategoryConstants;
038 import com.liferay.portlet.asset.model.AssetCategoryProperty;
039 import com.liferay.portlet.asset.model.AssetEntry;
040 import com.liferay.portlet.asset.service.base.AssetCategoryLocalServiceBaseImpl;
041
042 import java.util.Date;
043 import java.util.List;
044 import java.util.Locale;
045 import java.util.Map;
046
047
053 public class AssetCategoryLocalServiceImpl
054 extends AssetCategoryLocalServiceBaseImpl {
055
056 public AssetCategory addCategory(
057 long userId, long parentCategoryId, Map<Locale, String> titleMap,
058 Map<Locale, String> descriptionMap, long vocabularyId,
059 String[] categoryProperties, ServiceContext serviceContext)
060 throws PortalException, SystemException {
061
062
063
064 User user = userPersistence.findByPrimaryKey(userId);
065 long groupId = serviceContext.getScopeGroupId();
066
067 String name = titleMap.get(LocaleUtil.getDefault());
068
069 name = ModelHintsUtil.trimString(
070 AssetCategory.class.getName(), "name", name);
071
072 if (categoryProperties == null) {
073 categoryProperties = new String[0];
074 }
075
076 Date now = new Date();
077
078 validate(0, parentCategoryId, name, vocabularyId);
079
080 if (parentCategoryId > 0) {
081 assetCategoryPersistence.findByPrimaryKey(parentCategoryId);
082 }
083
084 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
085
086 long categoryId = counterLocalService.increment();
087
088 AssetCategory category = assetCategoryPersistence.create(categoryId);
089
090 category.setUuid(serviceContext.getUuid());
091 category.setGroupId(groupId);
092 category.setCompanyId(user.getCompanyId());
093 category.setUserId(user.getUserId());
094 category.setUserName(user.getFullName());
095 category.setCreateDate(now);
096 category.setModifiedDate(now);
097 category.setParentCategoryId(parentCategoryId);
098 category.setName(name);
099 category.setTitleMap(titleMap);
100 category.setDescriptionMap(descriptionMap);
101 category.setVocabularyId(vocabularyId);
102
103 assetCategoryPersistence.update(category, false);
104
105
106
107 if (serviceContext.isAddGroupPermissions() ||
108 serviceContext.isAddGuestPermissions()) {
109
110 addCategoryResources(
111 category, serviceContext.isAddGroupPermissions(),
112 serviceContext.isAddGuestPermissions());
113 }
114 else {
115 addCategoryResources(
116 category, serviceContext.getGroupPermissions(),
117 serviceContext.getGuestPermissions());
118 }
119
120
121
122 for (int i = 0; i < categoryProperties.length; i++) {
123 String[] categoryProperty = StringUtil.split(
124 categoryProperties[i], CharPool.COLON);
125
126 String key = StringPool.BLANK;
127 String value = StringPool.BLANK;
128
129 if (categoryProperty.length > 1) {
130 key = GetterUtil.getString(categoryProperty[0]);
131 value = GetterUtil.getString(categoryProperty[1]);
132 }
133
134 if (Validator.isNotNull(key)) {
135 assetCategoryPropertyLocalService.addCategoryProperty(
136 userId, categoryId, key, value);
137 }
138 }
139
140 return category;
141 }
142
143 public void addCategoryResources(
144 AssetCategory category, boolean addGroupPermissions,
145 boolean addGuestPermissions)
146 throws PortalException, SystemException {
147
148 resourceLocalService.addResources(
149 category.getCompanyId(), category.getGroupId(),
150 category.getUserId(), AssetCategory.class.getName(),
151 category.getCategoryId(), false, addGroupPermissions,
152 addGuestPermissions);
153 }
154
155 public void addCategoryResources(
156 AssetCategory category, String[] groupPermissions,
157 String[] guestPermissions)
158 throws PortalException, SystemException {
159
160 resourceLocalService.addModelResources(
161 category.getCompanyId(), category.getGroupId(),
162 category.getUserId(), AssetCategory.class.getName(),
163 category.getCategoryId(), groupPermissions, guestPermissions);
164 }
165
166 public void deleteCategory(AssetCategory category)
167 throws PortalException, SystemException {
168
169
170
171 List<AssetEntry> entries = assetTagPersistence.getAssetEntries(
172 category.getCategoryId());
173
174
175
176 assetCategoryPersistence.remove(category);
177
178
179
180 resourceLocalService.deleteResource(
181 category.getCompanyId(), AssetCategory.class.getName(),
182 ResourceConstants.SCOPE_INDIVIDUAL, category.getCategoryId());
183
184
185
186 List<AssetCategory> categories =
187 assetCategoryPersistence.findByParentCategoryId(
188 category.getCategoryId());
189
190 for (AssetCategory curCategory : categories) {
191 deleteCategory(curCategory);
192 }
193
194
195
196 assetCategoryPropertyLocalService.deleteCategoryProperties(
197 category.getCategoryId());
198
199
200
201 assetEntryLocalService.reindex(entries);
202 }
203
204 public void deleteCategory(long categoryId)
205 throws PortalException, SystemException {
206
207 AssetCategory category = assetCategoryPersistence.findByPrimaryKey(
208 categoryId);
209
210 deleteCategory(category);
211 }
212
213 public void deleteVocabularyCategories(long vocabularyId)
214 throws PortalException, SystemException {
215
216 List<AssetCategory> categories =
217 assetCategoryPersistence.findByVocabularyId(vocabularyId);
218
219 for (AssetCategory category : categories) {
220 deleteCategory(category);
221 }
222 }
223
224 public AssetCategory fetchCategory(long categoryId) throws SystemException {
225 return assetCategoryPersistence.fetchByPrimaryKey(categoryId);
226 }
227
228 public List<AssetCategory> getCategories() throws SystemException {
229 return assetCategoryPersistence.findAll();
230 }
231
232 @ThreadLocalCachable
233 public List<AssetCategory> getCategories(long classNameId, long classPK)
234 throws SystemException {
235
236 return assetCategoryFinder.findByC_C(classNameId, classPK);
237 }
238
239 public List<AssetCategory> getCategories(String className, long classPK)
240 throws SystemException {
241
242 long classNameId = PortalUtil.getClassNameId(className);
243
244 return getCategories(classNameId, classPK);
245 }
246
247 public AssetCategory getCategory(long categoryId)
248 throws PortalException, SystemException {
249
250 return assetCategoryPersistence.findByPrimaryKey(categoryId);
251 }
252
253 public long[] getCategoryIds(String className, long classPK)
254 throws SystemException {
255
256 return getCategoryIds(getCategories(className, classPK));
257 }
258
259 public String[] getCategoryNames() throws SystemException {
260 return getCategoryNames(getCategories());
261 }
262
263 public String[] getCategoryNames(long classNameId, long classPK)
264 throws SystemException {
265
266 return getCategoryNames(getCategories(classNameId, classPK));
267 }
268
269 public String[] getCategoryNames(String className, long classPK)
270 throws SystemException {
271
272 return getCategoryNames(getCategories(className, classPK));
273 }
274
275 public List<AssetCategory> getChildCategories(long parentCategoryId)
276 throws SystemException {
277
278 return assetCategoryPersistence.findByParentCategoryId(
279 parentCategoryId);
280 }
281
282 public List<AssetCategory> getChildCategories(
283 long parentCategoryId, int start, int end, OrderByComparator obc)
284 throws SystemException {
285
286 return assetCategoryPersistence.findByParentCategoryId(
287 parentCategoryId, start, end, obc);
288 }
289
290 public int getChildCategoriesCount(long parentCategoryId)
291 throws SystemException {
292
293 return assetCategoryPersistence.countByParentCategoryId(
294 parentCategoryId);
295 }
296
297 public List<AssetCategory> getEntryCategories(long entryId)
298 throws SystemException {
299
300 return assetCategoryFinder.findByEntryId(entryId);
301 }
302
303 public List<AssetCategory> getVocabularyCategories(
304 long vocabularyId, int start, int end, OrderByComparator obc)
305 throws SystemException {
306
307 return assetCategoryPersistence.findByVocabularyId(
308 vocabularyId, start, end, obc);
309 }
310
311 public List<AssetCategory> getVocabularyCategories(
312 long parentCategoryId, long vocabularyId, int start, int end,
313 OrderByComparator obc)
314 throws SystemException {
315
316 return assetCategoryPersistence.findByP_V(
317 parentCategoryId, vocabularyId, start, end, obc);
318 }
319
320 public int getVocabularyCategoriesCount(long vocabularyId)
321 throws SystemException {
322
323 return assetCategoryPersistence.countByVocabularyId(vocabularyId);
324 }
325
326 public List<AssetCategory> getVocabularyRootCategories(
327 long vocabularyId, int start, int end, OrderByComparator obc)
328 throws SystemException {
329
330 return getVocabularyCategories(
331 AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID, vocabularyId,
332 start, end, obc);
333 }
334
335 public void mergeCategories(long fromCategoryId, long toCategoryId)
336 throws PortalException, SystemException {
337
338 List<AssetEntry> entries = assetCategoryPersistence.getAssetEntries(
339 fromCategoryId);
340
341 assetCategoryPersistence.addAssetEntries(toCategoryId, entries);
342
343 List<AssetCategoryProperty> categoryProperties =
344 assetCategoryPropertyPersistence.findByCategoryId(fromCategoryId);
345
346 for (AssetCategoryProperty fromCategoryProperty : categoryProperties) {
347 AssetCategoryProperty toCategoryProperty =
348 assetCategoryPropertyPersistence.fetchByCA_K(
349 toCategoryId, fromCategoryProperty.getKey());
350
351 if (toCategoryProperty == null) {
352 fromCategoryProperty.setCategoryId(toCategoryId);
353
354 assetCategoryPropertyPersistence.update(
355 fromCategoryProperty, false);
356 }
357 }
358
359 deleteCategory(fromCategoryId);
360 }
361
362 public AssetCategory moveCategory(
363 long categoryId, long parentCategoryId, long vocabularyId,
364 ServiceContext serviceContext)
365 throws PortalException, SystemException {
366
367 AssetCategory category = assetCategoryPersistence.findByPrimaryKey(
368 categoryId);
369
370 validate(
371 categoryId, parentCategoryId, category.getName(), vocabularyId);
372
373 if (parentCategoryId > 0) {
374 assetCategoryPersistence.findByPrimaryKey(parentCategoryId);
375 }
376
377 if (vocabularyId != category.getVocabularyId()) {
378 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
379
380 category.setVocabularyId(vocabularyId);
381
382 updateChildrenVocabularyId(category, vocabularyId);
383 }
384
385 category.setModifiedDate(new Date());
386 category.setParentCategoryId(parentCategoryId);
387
388 assetCategoryPersistence.update(category, false);
389
390 return category;
391 }
392
393 public void rebuildTree(long groupId, boolean force)
394 throws SystemException {
395
396 assetCategoryPersistence.rebuildTree(groupId, force);
397 }
398
399 public List<AssetCategory> search(
400 long groupId, String name, String[] categoryProperties, int start,
401 int end)
402 throws SystemException {
403
404 return assetCategoryFinder.findByG_N_P(
405 groupId, name, categoryProperties, start, end);
406 }
407
408 public AssetCategory updateCategory(
409 long userId, long categoryId, long parentCategoryId,
410 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
411 long vocabularyId, String[] categoryProperties,
412 ServiceContext serviceContext)
413 throws PortalException, SystemException {
414
415
416
417 String name = titleMap.get(LocaleUtil.getDefault());
418
419 name = ModelHintsUtil.trimString(
420 AssetCategory.class.getName(), "name", name);
421
422 if (categoryProperties == null) {
423 categoryProperties = new String[0];
424 }
425
426 validate(categoryId, parentCategoryId, name, vocabularyId);
427
428 if (parentCategoryId > 0) {
429 assetCategoryPersistence.findByPrimaryKey(parentCategoryId);
430 }
431
432 AssetCategory category = assetCategoryPersistence.findByPrimaryKey(
433 categoryId);
434
435 String oldName = category.getName();
436
437 if (vocabularyId != category.getVocabularyId()) {
438 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
439
440 parentCategoryId =
441 AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
442
443 category.setVocabularyId(vocabularyId);
444
445 updateChildrenVocabularyId(category, vocabularyId);
446 }
447
448 category.setModifiedDate(new Date());
449 category.setParentCategoryId(parentCategoryId);
450 category.setName(name);
451 category.setTitleMap(titleMap);
452 category.setDescriptionMap(descriptionMap);
453
454 assetCategoryPersistence.update(category, false);
455
456
457
458 List<AssetCategoryProperty> oldCategoryProperties =
459 assetCategoryPropertyPersistence.findByCategoryId(categoryId);
460
461 for (AssetCategoryProperty categoryProperty : oldCategoryProperties) {
462 assetCategoryPropertyLocalService.deleteAssetCategoryProperty(
463 categoryProperty);
464 }
465
466 for (int i = 0; i < categoryProperties.length; i++) {
467 String[] categoryProperty = StringUtil.split(
468 categoryProperties[i], CharPool.COLON);
469
470 String key = StringPool.BLANK;
471
472 if (categoryProperty.length > 0) {
473 key = GetterUtil.getString(categoryProperty[0]);
474 }
475
476 String value = StringPool.BLANK;
477
478 if (categoryProperty.length > 1) {
479 value = GetterUtil.getString(categoryProperty[1]);
480 }
481
482 if (Validator.isNotNull(key)) {
483 assetCategoryPropertyLocalService.addCategoryProperty(
484 userId, categoryId, key, value);
485 }
486 }
487
488
489
490 if (!oldName.equals(name)) {
491 List<AssetEntry> entries = assetCategoryPersistence.getAssetEntries(
492 category.getCategoryId());
493
494 assetEntryLocalService.reindex(entries);
495 }
496
497 return category;
498 }
499
500 protected long[] getCategoryIds(List<AssetCategory> categories) {
501 return StringUtil.split(
502 ListUtil.toString(categories, AssetCategory.CATEGORY_ID_ACCESSOR),
503 0L);
504 }
505
506 protected String[] getCategoryNames(List<AssetCategory> categories) {
507 return StringUtil.split(
508 ListUtil.toString(categories, AssetCategory.NAME_ACCESSOR));
509 }
510
511 protected void updateChildrenVocabularyId(
512 AssetCategory category, long vocabularyId)
513 throws SystemException {
514
515 List<AssetCategory> childrenCategories =
516 assetCategoryPersistence.findByParentCategoryId(
517 category.getCategoryId());
518
519 if (!childrenCategories.isEmpty()) {
520 for (AssetCategory childCategory : childrenCategories) {
521 childCategory.setVocabularyId(vocabularyId);
522 childCategory.setModifiedDate(new Date());
523
524 assetCategoryPersistence.update(childCategory, false);
525
526 updateChildrenVocabularyId (childCategory, vocabularyId);
527 }
528 }
529 }
530
531 protected void validate(
532 long categoryId, long parentCategoryId, String name,
533 long vocabularyId)
534 throws PortalException, SystemException {
535
536 if (Validator.isNull(name)) {
537 throw new AssetCategoryNameException();
538 }
539
540 AssetCategory category = assetCategoryPersistence.fetchByP_N_V(
541 parentCategoryId, name, vocabularyId);
542
543 if ((category != null) && (category.getCategoryId() != categoryId)) {
544 StringBundler sb = new StringBundler(4);
545
546 sb.append("There is another category named ");
547 sb.append(name);
548 sb.append(" as a child of category ");
549 sb.append(parentCategoryId);
550
551 throw new DuplicateCategoryException(sb.toString());
552 }
553 }
554
555 }