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.exception.SystemException;
019 import com.liferay.portal.kernel.json.JSONArray;
020 import com.liferay.portal.kernel.json.JSONFactoryUtil;
021 import com.liferay.portal.kernel.json.JSONObject;
022 import com.liferay.portal.kernel.util.ListUtil;
023 import com.liferay.portal.kernel.util.OrderByComparator;
024 import com.liferay.portal.kernel.util.StringBundler;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.Validator;
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.Collections;
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 parentCategoryId, Map<Locale, String> titleMap,
061 Map<Locale, String> descriptionMap, long vocabularyId,
062 String[] categoryProperties, ServiceContext serviceContext)
063 throws PortalException, SystemException {
064
065 AssetCategoryPermission.check(
066 getPermissionChecker(), serviceContext.getScopeGroupId(),
067 parentCategoryId, ActionKeys.ADD_CATEGORY);
068
069 return assetCategoryLocalService.addCategory(
070 getUserId(), parentCategoryId, titleMap, descriptionMap,
071 vocabularyId, categoryProperties, serviceContext);
072 }
073
074 @Override
075 public AssetCategory addCategory(
076 String title, long vocabularyId, ServiceContext serviceContext)
077 throws PortalException, SystemException {
078
079 AssetCategoryPermission.check(
080 getPermissionChecker(), serviceContext.getScopeGroupId(),
081 AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID,
082 ActionKeys.ADD_CATEGORY);
083
084 return assetCategoryLocalService.addCategory(
085 getUserId(), title, vocabularyId, serviceContext);
086 }
087
088
092 @Override
093 public void deleteCategories(long[] categoryIds)
094 throws PortalException, SystemException {
095
096 deleteCategories(categoryIds, null);
097 }
098
099 @Override
100 public List<AssetCategory> deleteCategories(
101 long[] categoryIds, ServiceContext serviceContext)
102 throws PortalException, SystemException {
103
104 List<AssetCategory> failedCategories = new ArrayList<AssetCategory>();
105
106 for (long categoryId : categoryIds) {
107 try {
108 AssetCategoryPermission.check(
109 getPermissionChecker(), categoryId, ActionKeys.DELETE);
110
111 assetCategoryLocalService.deleteCategory(categoryId);
112 }
113 catch (PortalException pe) {
114 if (serviceContext == null) {
115 return null;
116 }
117
118 if (serviceContext.isFailOnPortalException()) {
119 throw pe;
120 }
121
122 AssetCategory category =
123 assetCategoryPersistence.fetchByPrimaryKey(categoryId);
124
125 if (category == null) {
126 category = assetCategoryPersistence.create(categoryId);
127 }
128
129 failedCategories.add(category);
130 }
131 }
132
133 return failedCategories;
134 }
135
136 @Override
137 public void deleteCategory(long categoryId)
138 throws PortalException, SystemException {
139
140 AssetCategoryPermission.check(
141 getPermissionChecker(), categoryId, ActionKeys.DELETE);
142
143 assetCategoryLocalService.deleteCategory(categoryId);
144 }
145
146 @Override
147 public List<AssetCategory> getCategories(String className, long classPK)
148 throws PortalException, SystemException {
149
150 return filterCategories(
151 assetCategoryLocalService.getCategories(className, classPK));
152 }
153
154 @Override
155 public AssetCategory getCategory(long categoryId)
156 throws PortalException, SystemException {
157
158 AssetCategoryPermission.check(
159 getPermissionChecker(), categoryId, ActionKeys.VIEW);
160
161 return assetCategoryLocalService.getCategory(categoryId);
162 }
163
164 @Override
165 public List<AssetCategory> getChildCategories(long parentCategoryId)
166 throws PortalException, SystemException {
167
168 return filterCategories(
169 assetCategoryLocalService.getChildCategories(parentCategoryId));
170 }
171
172 @Override
173 public List<AssetCategory> getChildCategories(
174 long parentCategoryId, int start, int end, OrderByComparator obc)
175 throws PortalException, SystemException {
176
177 return filterCategories(
178 assetCategoryLocalService.getChildCategories(
179 parentCategoryId, start, end, obc));
180 }
181
182
186 @Override
187 public JSONArray getJSONSearch(
188 long groupId, String name, long[] vocabularyIds, int start, int end)
189 throws PortalException, SystemException {
190
191 return search(new long[] {groupId}, name, vocabularyIds, start, end);
192 }
193
194
199 @Override
200 public JSONObject getJSONVocabularyCategories(
201 long vocabularyId, int start, int end, OrderByComparator obc)
202 throws PortalException, SystemException {
203
204 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
205
206 List<AssetCategory> categories = filterCategories(
207 assetCategoryLocalService.getVocabularyCategories(
208 vocabularyId, start, end, obc));
209
210 jsonObject.put("categories", toJSONArray(categories));
211 jsonObject.put("total", categories.size());
212
213 return jsonObject;
214 }
215
216
221 @Override
222 public JSONObject getJSONVocabularyCategories(
223 long groupId, String name, long vocabularyId, int start, int end,
224 OrderByComparator obc)
225 throws PortalException, SystemException {
226
227 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
228
229 int page = 0;
230
231 if ((end > 0) && (start > 0)) {
232 page = end / (end - start);
233 }
234
235 jsonObject.put("page", page);
236
237 List<AssetCategory> categories;
238 int total = 0;
239
240 if (Validator.isNotNull(name)) {
241 name = (CustomSQLUtil.keywords(name))[0];
242
243 categories = getVocabularyCategories(
244 groupId, name, vocabularyId, start, end, obc);
245 total = getVocabularyCategoriesCount(groupId, name, vocabularyId);
246 }
247 else {
248 categories = getVocabularyCategories(vocabularyId, start, end, obc);
249 total = getVocabularyCategoriesCount(groupId, vocabularyId);
250 }
251
252 jsonObject.put("categories", toJSONArray(categories));
253 jsonObject.put("total", total);
254
255 return jsonObject;
256 }
257
258 @Override
259 public List<AssetCategory> getVocabularyCategories(
260 long vocabularyId, int start, int end, OrderByComparator obc)
261 throws PortalException, SystemException {
262
263 return filterCategories(
264 assetCategoryLocalService.getVocabularyCategories(
265 vocabularyId, start, end, obc));
266 }
267
268 @Override
269 public List<AssetCategory> getVocabularyCategories(
270 long parentCategoryId, long vocabularyId, int start, int end,
271 OrderByComparator obc)
272 throws PortalException, SystemException {
273
274 return filterCategories(
275 assetCategoryLocalService.getVocabularyCategories(
276 parentCategoryId, vocabularyId, start, end, obc));
277 }
278
279 @Override
280 public List<AssetCategory> getVocabularyCategories(
281 long groupId, String name, long vocabularyId, int start, int end,
282 OrderByComparator obc)
283 throws SystemException {
284
285 if (Validator.isNull(name)) {
286 return assetCategoryPersistence.filterFindByG_V(
287 groupId, vocabularyId, start, end, obc);
288 }
289 else {
290 return assetCategoryPersistence.filterFindByG_LikeN_V(
291 groupId, name, vocabularyId, start, end, obc);
292 }
293 }
294
295 @Override
296 public int getVocabularyCategoriesCount(long groupId, long vocabularyId)
297 throws SystemException {
298
299 return assetCategoryPersistence.filterCountByG_V(groupId, vocabularyId);
300 }
301
302 @Override
303 public int getVocabularyCategoriesCount(
304 long groupId, String name, long vocabularyId)
305 throws SystemException {
306
307 if (Validator.isNull(name)) {
308 return assetCategoryPersistence.filterCountByG_V(
309 groupId, vocabularyId);
310 }
311 else {
312 return assetCategoryPersistence.filterCountByG_LikeN_V(
313 groupId, name, vocabularyId);
314 }
315 }
316
317 @Override
318 public AssetCategoryDisplay getVocabularyCategoriesDisplay(
319 long vocabularyId, int start, int end, OrderByComparator obc)
320 throws PortalException, SystemException {
321
322 List<AssetCategory> categories = filterCategories(
323 assetCategoryLocalService.getVocabularyCategories(
324 vocabularyId, start, end, obc));
325
326 return new AssetCategoryDisplay(
327 categories, categories.size(), start, end);
328 }
329
330 @Override
331 public AssetCategoryDisplay getVocabularyCategoriesDisplay(
332 long groupId, String name, long vocabularyId, int start, int end,
333 OrderByComparator obc)
334 throws PortalException, SystemException {
335
336 List<AssetCategory> categories = null;
337 int total = 0;
338
339 if (Validator.isNotNull(name)) {
340 name = (CustomSQLUtil.keywords(name))[0];
341
342 categories = getVocabularyCategories(
343 groupId, name, vocabularyId, start, end, obc);
344 total = getVocabularyCategoriesCount(groupId, name, vocabularyId);
345 }
346 else {
347 categories = getVocabularyCategories(vocabularyId, start, end, obc);
348 total = getVocabularyCategoriesCount(groupId, vocabularyId);
349 }
350
351 return new AssetCategoryDisplay(categories, total, start, end);
352 }
353
354
359 @Override
360 public List<AssetCategory> getVocabularyRootCategories(
361 long vocabularyId, int start, int end, OrderByComparator obc)
362 throws PortalException, SystemException {
363
364 AssetVocabulary vocabulary = assetVocabularyLocalService.getVocabulary(
365 vocabularyId);
366
367 return getVocabularyRootCategories(
368 vocabulary.getGroupId(), vocabularyId, start, end, obc);
369 }
370
371 @Override
372 public List<AssetCategory> getVocabularyRootCategories(
373 long groupId, long vocabularyId, int start, int end,
374 OrderByComparator obc)
375 throws SystemException {
376
377 return assetCategoryPersistence.filterFindByG_P_V(
378 groupId, AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID,
379 vocabularyId, start, end, obc);
380 }
381
382 @Override
383 public int getVocabularyRootCategoriesCount(long groupId, long vocabularyId)
384 throws SystemException {
385
386 return assetCategoryPersistence.filterCountByG_P_V(
387 groupId, AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID,
388 vocabularyId);
389 }
390
391 @Override
392 public AssetCategory moveCategory(
393 long categoryId, long parentCategoryId, long vocabularyId,
394 ServiceContext serviceContext)
395 throws PortalException, SystemException {
396
397 AssetCategoryPermission.check(
398 getPermissionChecker(), categoryId, ActionKeys.UPDATE);
399
400 return assetCategoryLocalService.moveCategory(
401 categoryId, parentCategoryId, vocabularyId, serviceContext);
402 }
403
404 @Override
405 public List<AssetCategory> search(
406 long groupId, String keywords, long vocabularyId, int start,
407 int end, OrderByComparator obc)
408 throws SystemException {
409
410 String name = CustomSQLUtil.keywords(keywords)[0];
411
412 if (Validator.isNull(name)) {
413 return assetCategoryPersistence.filterFindByG_V(
414 groupId, vocabularyId, start, end, obc);
415 }
416 else {
417 return assetCategoryPersistence.filterFindByG_LikeN_V(
418 groupId, name, vocabularyId, start, end, obc);
419 }
420 }
421
422 @Override
423 public JSONArray search(
424 long groupId, String name, String[] categoryProperties, int start,
425 int end)
426 throws PortalException, SystemException {
427
428 List<AssetCategory> categories = assetCategoryLocalService.search(
429 groupId, name, categoryProperties, start, end);
430
431 categories = filterCategories(categories);
432
433 return Autocomplete.listToJson(categories, "name", "name");
434 }
435
436 @Override
437 public JSONArray search(
438 long[] groupIds, String name, long[] vocabularyIds, int start,
439 int end)
440 throws PortalException, SystemException {
441
442 JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
443
444 for (long groupId : groupIds) {
445 JSONArray categoriesJSONArray = null;
446
447 if (Validator.isNull(name)) {
448 categoriesJSONArray = toJSONArray(
449 assetCategoryPersistence.filterFindByG_V(
450 groupId, vocabularyIds));
451 }
452 else {
453 categoriesJSONArray = toJSONArray(
454 assetCategoryPersistence.filterFindByG_LikeN_V(
455 groupId, name, vocabularyIds));
456 }
457
458 for (int j = 0; j < categoriesJSONArray.length(); j++) {
459 jsonArray.put(categoriesJSONArray.getJSONObject(j));
460 }
461 }
462
463 return jsonArray;
464 }
465
466 @Override
467 public AssetCategory updateCategory(
468 long categoryId, long parentCategoryId,
469 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
470 long vocabularyId, String[] categoryProperties,
471 ServiceContext serviceContext)
472 throws PortalException, SystemException {
473
474 AssetCategoryPermission.check(
475 getPermissionChecker(), categoryId, ActionKeys.UPDATE);
476
477 return assetCategoryLocalService.updateCategory(
478 getUserId(), categoryId, parentCategoryId, titleMap, descriptionMap,
479 vocabularyId, categoryProperties, serviceContext);
480 }
481
482 protected List<AssetCategory> filterCategories(
483 List<AssetCategory> categories)
484 throws PortalException, SystemException {
485
486 PermissionChecker permissionChecker = getPermissionChecker();
487
488 categories = ListUtil.copy(categories);
489
490 Iterator<AssetCategory> itr = categories.iterator();
491
492 while (itr.hasNext()) {
493 AssetCategory category = itr.next();
494
495 if (!AssetCategoryPermission.contains(
496 permissionChecker, category, ActionKeys.VIEW)) {
497
498 itr.remove();
499 }
500 }
501
502 return categories;
503 }
504
505 protected JSONArray toJSONArray(List<AssetCategory> categories)
506 throws PortalException, SystemException {
507
508 JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
509
510 for (AssetCategory category : categories) {
511 String categoryJSON = JSONFactoryUtil.looseSerialize(category);
512
513 JSONObject categoryJSONObject = JSONFactoryUtil.createJSONObject(
514 categoryJSON);
515
516 List<String> names = new ArrayList<String>();
517
518 AssetCategory curCategory = category;
519
520 while (curCategory.getParentCategoryId() > 0) {
521 AssetCategory parentCategory = getCategory(
522 curCategory.getParentCategoryId());
523
524 names.add(parentCategory.getName());
525 names.add(
526 StringPool.SPACE + StringPool.GREATER_THAN +
527 StringPool.SPACE);
528
529 curCategory = parentCategory;
530 }
531
532 Collections.reverse(names);
533
534 AssetVocabulary vocabulary = assetVocabularyService.getVocabulary(
535 category.getVocabularyId());
536
537 StringBundler sb = new StringBundler(1 + names.size());
538
539 sb.append(vocabulary.getName());
540 sb.append(names.toArray(new String[names.size()]));
541
542 categoryJSONObject.put("path", sb.toString());
543
544 jsonArray.put(categoryJSONObject);
545 }
546
547 return jsonArray;
548 }
549
550 }