001
014
015 package com.liferay.portlet.asset.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.json.JSONArray;
019 import com.liferay.portal.kernel.json.JSONFactoryUtil;
020 import com.liferay.portal.kernel.json.JSONObject;
021 import com.liferay.portal.kernel.search.BaseModelSearchResult;
022 import com.liferay.portal.kernel.search.Sort;
023 import com.liferay.portal.kernel.util.ListUtil;
024 import com.liferay.portal.kernel.util.LocaleUtil;
025 import com.liferay.portal.kernel.util.OrderByComparator;
026 import com.liferay.portal.kernel.util.Validator;
027 import com.liferay.portal.model.User;
028 import com.liferay.portal.security.permission.ActionKeys;
029 import com.liferay.portal.security.permission.PermissionChecker;
030 import com.liferay.portal.service.ServiceContext;
031 import com.liferay.portlet.asset.model.AssetCategory;
032 import com.liferay.portlet.asset.model.AssetCategoryConstants;
033 import com.liferay.portlet.asset.model.AssetCategoryDisplay;
034 import com.liferay.portlet.asset.model.AssetVocabulary;
035 import com.liferay.portlet.asset.service.base.AssetCategoryServiceBaseImpl;
036 import com.liferay.portlet.asset.service.permission.AssetCategoryPermission;
037 import com.liferay.util.Autocomplete;
038 import com.liferay.util.dao.orm.CustomSQLUtil;
039
040 import java.util.ArrayList;
041 import java.util.Iterator;
042 import java.util.List;
043 import java.util.Locale;
044 import java.util.Map;
045
046
056 public class AssetCategoryServiceImpl extends AssetCategoryServiceBaseImpl {
057
058 @Override
059 public AssetCategory addCategory(
060 long groupId, long parentCategoryId, Map<Locale, String> titleMap,
061 Map<Locale, String> descriptionMap, long vocabularyId,
062 String[] categoryProperties, ServiceContext serviceContext)
063 throws PortalException {
064
065 AssetCategoryPermission.check(
066 getPermissionChecker(), groupId, parentCategoryId,
067 ActionKeys.ADD_CATEGORY);
068
069 return assetCategoryLocalService.addCategory(
070 getUserId(), groupId, parentCategoryId, titleMap, descriptionMap,
071 vocabularyId, categoryProperties, serviceContext);
072 }
073
074 @Override
075 public AssetCategory addCategory(
076 long groupId, String title, long vocabularyId,
077 ServiceContext serviceContext)
078 throws PortalException {
079
080 AssetCategoryPermission.check(
081 getPermissionChecker(), groupId,
082 AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID,
083 ActionKeys.ADD_CATEGORY);
084
085 return assetCategoryLocalService.addCategory(
086 getUserId(), groupId, title, vocabularyId, serviceContext);
087 }
088
089 @Override
090 public void deleteCategories(long[] categoryIds) throws PortalException {
091 for (long categoryId : categoryIds) {
092 AssetCategoryPermission.check(
093 getPermissionChecker(), categoryId, ActionKeys.DELETE);
094 }
095
096 assetCategoryLocalService.deleteCategories(categoryIds);
097 }
098
099
102 @Deprecated
103 @Override
104 public List<AssetCategory> deleteCategories(
105 long[] categoryIds, ServiceContext serviceContext)
106 throws PortalException {
107
108 List<AssetCategory> failedCategories = new ArrayList<>();
109
110 for (long categoryId : categoryIds) {
111 try {
112 AssetCategoryPermission.check(
113 getPermissionChecker(), categoryId, ActionKeys.DELETE);
114
115 assetCategoryLocalService.deleteCategory(categoryId);
116 }
117 catch (PortalException pe) {
118 if (serviceContext == null) {
119 return null;
120 }
121
122 if (serviceContext.isFailOnPortalException()) {
123 throw pe;
124 }
125
126 AssetCategory category =
127 assetCategoryPersistence.fetchByPrimaryKey(categoryId);
128
129 if (category == null) {
130 category = assetCategoryPersistence.create(categoryId);
131 }
132
133 failedCategories.add(category);
134 }
135 }
136
137 return failedCategories;
138 }
139
140 @Override
141 public void deleteCategory(long categoryId) throws PortalException {
142 AssetCategoryPermission.check(
143 getPermissionChecker(), categoryId, ActionKeys.DELETE);
144
145 assetCategoryLocalService.deleteCategory(categoryId);
146 }
147
148 @Override
149 public AssetCategory fetchCategory(long categoryId) throws PortalException {
150 AssetCategory category = assetCategoryLocalService.fetchCategory(
151 categoryId);
152
153 if (category != null) {
154 AssetCategoryPermission.check(
155 getPermissionChecker(), category, ActionKeys.VIEW);
156 }
157
158 return category;
159 }
160
161 @Override
162 public List<AssetCategory> getCategories(String className, long classPK)
163 throws PortalException {
164
165 return filterCategories(
166 assetCategoryLocalService.getCategories(className, classPK));
167 }
168
169 @Override
170 public AssetCategory getCategory(long categoryId) throws PortalException {
171 AssetCategoryPermission.check(
172 getPermissionChecker(), categoryId, ActionKeys.VIEW);
173
174 return assetCategoryLocalService.getCategory(categoryId);
175 }
176
177 @Override
178 public String getCategoryPath(long categoryId) throws PortalException {
179 AssetCategoryPermission.check(
180 getPermissionChecker(), categoryId, ActionKeys.VIEW);
181
182 AssetCategory category = getCategory(categoryId);
183
184 return category.getPath(LocaleUtil.getMostRelevantLocale());
185 }
186
187 @Override
188 public List<AssetCategory> getChildCategories(long parentCategoryId)
189 throws PortalException {
190
191 return filterCategories(
192 assetCategoryLocalService.getChildCategories(parentCategoryId));
193 }
194
195 @Override
196 public List<AssetCategory> getChildCategories(
197 long parentCategoryId, int start, int end,
198 OrderByComparator<AssetCategory> obc)
199 throws PortalException {
200
201 return filterCategories(
202 assetCategoryLocalService.getChildCategories(
203 parentCategoryId, start, end, obc));
204 }
205
206
210 @Deprecated
211 @Override
212 public JSONArray getJSONSearch(
213 long groupId, String name, long[] vocabularyIds, int start, int end)
214 throws PortalException {
215
216 return search(new long[] {groupId}, name, vocabularyIds, start, end);
217 }
218
219
224 @Deprecated
225 @Override
226 public JSONObject getJSONVocabularyCategories(
227 long vocabularyId, int start, int end,
228 OrderByComparator<AssetCategory> obc)
229 throws PortalException {
230
231 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
232
233 List<AssetCategory> categories = filterCategories(
234 assetCategoryLocalService.getVocabularyCategories(
235 vocabularyId, start, end, obc));
236
237 jsonObject.put("categories", toJSONArray(categories));
238 jsonObject.put("total", categories.size());
239
240 return jsonObject;
241 }
242
243
248 @Deprecated
249 @Override
250 public JSONObject getJSONVocabularyCategories(
251 long groupId, String name, long vocabularyId, int start, int end,
252 OrderByComparator<AssetCategory> obc)
253 throws PortalException {
254
255 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
256
257 int page = 0;
258
259 if ((end > 0) && (start > 0)) {
260 page = end / (end - start);
261 }
262
263 jsonObject.put("page", page);
264
265 List<AssetCategory> categories;
266 int total = 0;
267
268 if (Validator.isNotNull(name)) {
269 name = (CustomSQLUtil.keywords(name))[0];
270
271 categories = getVocabularyCategories(
272 groupId, name, vocabularyId, start, end, obc);
273 total = getVocabularyCategoriesCount(groupId, name, vocabularyId);
274 }
275 else {
276 categories = getVocabularyCategories(vocabularyId, start, end, obc);
277 total = getVocabularyCategoriesCount(groupId, vocabularyId);
278 }
279
280 jsonObject.put("categories", toJSONArray(categories));
281 jsonObject.put("total", total);
282
283 return jsonObject;
284 }
285
286 @Override
287 public List<AssetCategory> getVocabularyCategories(
288 long vocabularyId, int start, int end,
289 OrderByComparator<AssetCategory> obc)
290 throws PortalException {
291
292 return filterCategories(
293 assetCategoryLocalService.getVocabularyCategories(
294 vocabularyId, start, end, obc));
295 }
296
297 @Override
298 public List<AssetCategory> getVocabularyCategories(
299 long parentCategoryId, long vocabularyId, int start, int end,
300 OrderByComparator<AssetCategory> obc)
301 throws PortalException {
302
303 return filterCategories(
304 assetCategoryLocalService.getVocabularyCategories(
305 parentCategoryId, vocabularyId, start, end, obc));
306 }
307
308 @Override
309 public List<AssetCategory> getVocabularyCategories(
310 long groupId, long parentCategoryId, long vocabularyId, int start,
311 int end, OrderByComparator<AssetCategory> obc) {
312
313 return assetCategoryPersistence.filterFindByG_P_V(
314 groupId, parentCategoryId, vocabularyId, start, end, obc);
315 }
316
317 @Override
318 public List<AssetCategory> getVocabularyCategories(
319 long groupId, String name, long vocabularyId, int start, int end,
320 OrderByComparator<AssetCategory> obc) {
321
322 if (Validator.isNull(name)) {
323 return assetCategoryPersistence.filterFindByG_V(
324 groupId, vocabularyId, start, end, obc);
325 }
326 else {
327 return assetCategoryPersistence.filterFindByG_LikeN_V(
328 groupId, name, vocabularyId, start, end, obc);
329 }
330 }
331
332 @Override
333 public int getVocabularyCategoriesCount(long groupId, long vocabularyId) {
334 return assetCategoryPersistence.filterCountByG_V(groupId, vocabularyId);
335 }
336
337 @Override
338 public int getVocabularyCategoriesCount(
339 long groupId, long parentCategory, long vocabularyId) {
340
341 return assetCategoryPersistence.filterCountByG_P_V(
342 groupId, parentCategory, vocabularyId);
343 }
344
345 @Override
346 public int getVocabularyCategoriesCount(
347 long groupId, String name, long vocabularyId) {
348
349 if (Validator.isNull(name)) {
350 return assetCategoryPersistence.filterCountByG_V(
351 groupId, vocabularyId);
352 }
353 else {
354 return assetCategoryPersistence.filterCountByG_LikeN_V(
355 groupId, name, vocabularyId);
356 }
357 }
358
359 @Override
360 public AssetCategoryDisplay getVocabularyCategoriesDisplay(
361 long vocabularyId, int start, int end,
362 OrderByComparator<AssetCategory> obc)
363 throws PortalException {
364
365 List<AssetCategory> categories = filterCategories(
366 assetCategoryLocalService.getVocabularyCategories(
367 vocabularyId, start, end, obc));
368
369 return new AssetCategoryDisplay(
370 categories, categories.size(), start, end);
371 }
372
373 @Override
374 public AssetCategoryDisplay getVocabularyCategoriesDisplay(
375 long groupId, String name, long vocabularyId, int start, int end,
376 OrderByComparator<AssetCategory> obc)
377 throws PortalException {
378
379 List<AssetCategory> categories = null;
380 int total = 0;
381
382 if (Validator.isNotNull(name)) {
383 name = (CustomSQLUtil.keywords(name))[0];
384
385 categories = getVocabularyCategories(
386 groupId, name, vocabularyId, start, end, obc);
387 total = getVocabularyCategoriesCount(groupId, name, vocabularyId);
388 }
389 else {
390 categories = getVocabularyCategories(vocabularyId, start, end, obc);
391 total = getVocabularyCategoriesCount(groupId, vocabularyId);
392 }
393
394 return new AssetCategoryDisplay(categories, total, start, end);
395 }
396
397
402 @Deprecated
403 @Override
404 public List<AssetCategory> getVocabularyRootCategories(
405 long vocabularyId, int start, int end,
406 OrderByComparator<AssetCategory> obc)
407 throws PortalException {
408
409 AssetVocabulary vocabulary = assetVocabularyLocalService.getVocabulary(
410 vocabularyId);
411
412 return getVocabularyRootCategories(
413 vocabulary.getGroupId(), vocabularyId, start, end, obc);
414 }
415
416 @Override
417 public List<AssetCategory> getVocabularyRootCategories(
418 long groupId, long vocabularyId, int start, int end,
419 OrderByComparator<AssetCategory> obc) {
420
421 return assetCategoryPersistence.filterFindByG_P_V(
422 groupId, AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID,
423 vocabularyId, start, end, obc);
424 }
425
426 @Override
427 public int getVocabularyRootCategoriesCount(
428 long groupId, long vocabularyId) {
429
430 return assetCategoryPersistence.filterCountByG_P_V(
431 groupId, AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID,
432 vocabularyId);
433 }
434
435 @Override
436 public AssetCategory moveCategory(
437 long categoryId, long parentCategoryId, long vocabularyId,
438 ServiceContext serviceContext)
439 throws PortalException {
440
441 AssetCategoryPermission.check(
442 getPermissionChecker(), categoryId, ActionKeys.UPDATE);
443
444 return assetCategoryLocalService.moveCategory(
445 categoryId, parentCategoryId, vocabularyId, serviceContext);
446 }
447
448 @Override
449 public List<AssetCategory> search(
450 long groupId, String keywords, long vocabularyId, int start, int end,
451 OrderByComparator<AssetCategory> obc) {
452
453 String name = CustomSQLUtil.keywords(keywords)[0];
454
455 if (Validator.isNull(name)) {
456 return assetCategoryPersistence.filterFindByG_V(
457 groupId, vocabularyId, start, end, obc);
458 }
459 else {
460 return assetCategoryPersistence.filterFindByG_LikeN_V(
461 groupId, name, vocabularyId, start, end, obc);
462 }
463 }
464
465 @Override
466 public JSONArray search(
467 long groupId, String name, String[] categoryProperties, int start,
468 int end)
469 throws PortalException {
470
471 List<AssetCategory> categories = assetCategoryLocalService.search(
472 groupId, name, categoryProperties, start, end);
473
474 categories = filterCategories(categories);
475
476 return Autocomplete.listToJson(categories, "name", "name");
477 }
478
479 @Override
480 public JSONArray search(
481 long[] groupIds, String name, long[] vocabularyIds, int start,
482 int end)
483 throws PortalException {
484
485 JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
486
487 for (long groupId : groupIds) {
488 JSONArray categoriesJSONArray = null;
489
490 if (Validator.isNull(name)) {
491 categoriesJSONArray = toJSONArray(
492 assetCategoryPersistence.filterFindByG_V(
493 groupId, vocabularyIds));
494 }
495 else {
496 categoriesJSONArray = toJSONArray(
497 assetCategoryPersistence.filterFindByG_LikeN_V(
498 groupId, name, vocabularyIds));
499 }
500
501 for (int j = 0; j < categoriesJSONArray.length(); j++) {
502 jsonArray.put(categoriesJSONArray.getJSONObject(j));
503 }
504 }
505
506 return jsonArray;
507 }
508
509 @Override
510 public AssetCategoryDisplay searchCategoriesDisplay(
511 long groupId, String title, long vocabularyId, int start, int end)
512 throws PortalException {
513
514 return searchCategoriesDisplay(
515 new long[] {groupId}, title, new long[] {vocabularyId}, start, end);
516 }
517
518 @Override
519 public AssetCategoryDisplay searchCategoriesDisplay(
520 long groupId, String title, long parentCategoryId,
521 long vocabularyId, int start, int end)
522 throws PortalException {
523
524 return searchCategoriesDisplay(
525 new long[] {groupId}, title, new long[] {parentCategoryId},
526 new long[] {vocabularyId}, start, end);
527 }
528
529 @Override
530 public AssetCategoryDisplay searchCategoriesDisplay(
531 long groupId, String title, long vocabularyId,
532 long parentCategoryId, int start, int end, Sort sort)
533 throws PortalException {
534
535 return searchCategoriesDisplay(
536 new long[] {groupId}, title, new long[] {vocabularyId},
537 new long[] {parentCategoryId}, start, end, sort);
538 }
539
540 @Override
541 public AssetCategoryDisplay searchCategoriesDisplay(
542 long[] groupIds, String title, long[] vocabularyIds, int start,
543 int end)
544 throws PortalException {
545
546 User user = getUser();
547
548 BaseModelSearchResult<AssetCategory> baseModelSearchResult =
549 assetCategoryLocalService.searchCategories(
550 user.getCompanyId(), groupIds, title, vocabularyIds, start,
551 end);
552
553 return new AssetCategoryDisplay(
554 baseModelSearchResult.getBaseModels(),
555 baseModelSearchResult.getLength(), start, end);
556 }
557
558 @Override
559 public AssetCategoryDisplay searchCategoriesDisplay(
560 long[] groupIds, String title, long[] parentCategoryIds,
561 long[] vocabularyIds, int start, int end)
562 throws PortalException {
563
564 User user = getUser();
565
566 BaseModelSearchResult<AssetCategory> baseModelSearchResult =
567 assetCategoryLocalService.searchCategories(
568 user.getCompanyId(), groupIds, title, parentCategoryIds,
569 vocabularyIds, start, end);
570
571 return new AssetCategoryDisplay(
572 baseModelSearchResult.getBaseModels(),
573 baseModelSearchResult.getLength(), start, end);
574 }
575
576 @Override
577 public AssetCategoryDisplay searchCategoriesDisplay(
578 long[] groupIds, String title, long[] vocabularyIds,
579 long[] parentCategoryIds, int start, int end, Sort sort)
580 throws PortalException {
581
582 User user = getUser();
583
584 BaseModelSearchResult<AssetCategory> baseModelSearchResult =
585 assetCategoryLocalService.searchCategories(
586 user.getCompanyId(), groupIds, title, vocabularyIds,
587 parentCategoryIds, start, end, sort);
588
589 return new AssetCategoryDisplay(
590 baseModelSearchResult.getBaseModels(),
591 baseModelSearchResult.getLength(), start, end);
592 }
593
594 @Override
595 public AssetCategory updateCategory(
596 long categoryId, long parentCategoryId,
597 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
598 long vocabularyId, String[] categoryProperties,
599 ServiceContext serviceContext)
600 throws PortalException {
601
602 AssetCategoryPermission.check(
603 getPermissionChecker(), categoryId, ActionKeys.UPDATE);
604
605 return assetCategoryLocalService.updateCategory(
606 getUserId(), categoryId, parentCategoryId, titleMap, descriptionMap,
607 vocabularyId, categoryProperties, serviceContext);
608 }
609
610 protected List<AssetCategory> filterCategories(
611 List<AssetCategory> categories)
612 throws PortalException {
613
614 PermissionChecker permissionChecker = getPermissionChecker();
615
616 categories = ListUtil.copy(categories);
617
618 Iterator<AssetCategory> itr = categories.iterator();
619
620 while (itr.hasNext()) {
621 AssetCategory category = itr.next();
622
623 if (!AssetCategoryPermission.contains(
624 permissionChecker, category, ActionKeys.VIEW)) {
625
626 itr.remove();
627 }
628 }
629
630 return categories;
631 }
632
633 protected JSONArray toJSONArray(List<AssetCategory> categories)
634 throws PortalException {
635
636 JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
637
638 for (AssetCategory category : categories) {
639 String categoryJSON = JSONFactoryUtil.looseSerialize(category);
640
641 JSONObject categoryJSONObject = JSONFactoryUtil.createJSONObject(
642 categoryJSON);
643
644 categoryJSONObject.put(
645 "path", getCategoryPath(category.getCategoryId()));
646
647 jsonArray.put(categoryJSONObject);
648 }
649
650 return jsonArray;
651 }
652
653 }