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