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