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