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