001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
046     * Provides the remote service for accessing, adding, deleting, moving, and
047     * updating asset categories. Its methods include permission checks.
048     *
049     * @author Brian Wing Shun Chan
050     * @author Jorge Ferrer
051     * @author Alvaro del Castillo
052     * @author Eduardo Lundgren
053     * @author Bruno Farache
054     */
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            /**
099             * @deprecated As of 7.0.0, Replaced by {@link #deleteCategories(long[])}
100             */
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            /**
206             * @deprecated As of 6.2.0, replaced by {@link #search(long[], String,
207             *             long[], int, int)}
208             */
209            @Deprecated
210            @Override
211            public JSONArray getJSONSearch(
212                            long groupId, String name, long[] vocabularyIds, int start, int end)
213                    throws PortalException {
214    
215                    return search(new long[] {groupId}, name, vocabularyIds, start, end);
216            }
217    
218            /**
219             * @deprecated As of 6.2.0, replaced by {@link
220             *             #getVocabularyCategoriesDisplay(long, int, int,
221             *             OrderByComparator)}
222             */
223            @Deprecated
224            @Override
225            public JSONObject getJSONVocabularyCategories(
226                            long vocabularyId, int start, int end,
227                            OrderByComparator<AssetCategory> obc)
228                    throws PortalException {
229    
230                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
231    
232                    List<AssetCategory> categories = filterCategories(
233                            assetCategoryLocalService.getVocabularyCategories(
234                                    vocabularyId, start, end, obc));
235    
236                    jsonObject.put("categories", toJSONArray(categories));
237                    jsonObject.put("total", categories.size());
238    
239                    return jsonObject;
240            }
241    
242            /**
243             * @deprecated As of 6.2.0, replaced by {@link
244             *             #getVocabularyCategoriesDisplay(long, String, long, int, int,
245             *             OrderByComparator)}
246             */
247            @Deprecated
248            @Override
249            public JSONObject getJSONVocabularyCategories(
250                            long groupId, String name, long vocabularyId, int start, int end,
251                            OrderByComparator<AssetCategory> obc)
252                    throws PortalException {
253    
254                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
255    
256                    int page = 0;
257    
258                    if ((end > 0) && (start > 0)) {
259                            page = end / (end - start);
260                    }
261    
262                    jsonObject.put("page", page);
263    
264                    List<AssetCategory> categories;
265                    int total = 0;
266    
267                    if (Validator.isNotNull(name)) {
268                            name = (CustomSQLUtil.keywords(name))[0];
269    
270                            categories = getVocabularyCategories(
271                                    groupId, name, vocabularyId, start, end, obc);
272                            total = getVocabularyCategoriesCount(groupId, name, vocabularyId);
273                    }
274                    else {
275                            categories = getVocabularyCategories(vocabularyId, start, end, obc);
276                            total = getVocabularyCategoriesCount(groupId, vocabularyId);
277                    }
278    
279                    jsonObject.put("categories", toJSONArray(categories));
280                    jsonObject.put("total", total);
281    
282                    return jsonObject;
283            }
284    
285            @Override
286            public List<AssetCategory> getVocabularyCategories(
287                            long vocabularyId, int start, int end,
288                            OrderByComparator<AssetCategory> obc)
289                    throws PortalException {
290    
291                    return filterCategories(
292                            assetCategoryLocalService.getVocabularyCategories(
293                                    vocabularyId, start, end, obc));
294            }
295    
296            @Override
297            public List<AssetCategory> getVocabularyCategories(
298                            long parentCategoryId, long vocabularyId, int start, int end,
299                            OrderByComparator<AssetCategory> obc)
300                    throws PortalException {
301    
302                    return filterCategories(
303                            assetCategoryLocalService.getVocabularyCategories(
304                                    parentCategoryId, vocabularyId, start, end, obc));
305            }
306    
307            @Override
308            public List<AssetCategory> getVocabularyCategories(
309                    long groupId, long parentCategoryId, long vocabularyId, int start,
310                    int end, OrderByComparator<AssetCategory> obc) {
311    
312                    return assetCategoryPersistence.filterFindByG_P_V(
313                            groupId, parentCategoryId, vocabularyId, start, end, obc);
314            }
315    
316            @Override
317            public List<AssetCategory> getVocabularyCategories(
318                    long groupId, String name, long vocabularyId, int start, int end,
319                    OrderByComparator<AssetCategory> obc) {
320    
321                    if (Validator.isNull(name)) {
322                            return assetCategoryPersistence.filterFindByG_V(
323                                    groupId, vocabularyId, start, end, obc);
324                    }
325                    else {
326                            return assetCategoryPersistence.filterFindByG_LikeN_V(
327                                    groupId, name, vocabularyId, start, end, obc);
328                    }
329            }
330    
331            @Override
332            public int getVocabularyCategoriesCount(long groupId, long vocabularyId) {
333                    return assetCategoryPersistence.filterCountByG_V(groupId, vocabularyId);
334            }
335    
336            @Override
337            public int getVocabularyCategoriesCount(
338                    long groupId, long parentCategory, long vocabularyId) {
339    
340                    return assetCategoryPersistence.filterCountByG_P_V(
341                            groupId, parentCategory, vocabularyId);
342            }
343    
344            @Override
345            public int getVocabularyCategoriesCount(
346                    long groupId, String name, long vocabularyId) {
347    
348                    if (Validator.isNull(name)) {
349                            return assetCategoryPersistence.filterCountByG_V(
350                                    groupId, vocabularyId);
351                    }
352                    else {
353                            return assetCategoryPersistence.filterCountByG_LikeN_V(
354                                    groupId, name, vocabularyId);
355                    }
356            }
357    
358            @Override
359            public AssetCategoryDisplay getVocabularyCategoriesDisplay(
360                            long vocabularyId, int start, int end,
361                            OrderByComparator<AssetCategory> obc)
362                    throws PortalException {
363    
364                    List<AssetCategory> categories = filterCategories(
365                            assetCategoryLocalService.getVocabularyCategories(
366                                    vocabularyId, start, end, obc));
367    
368                    return new AssetCategoryDisplay(
369                            categories, categories.size(), start, end);
370            }
371    
372            @Override
373            public AssetCategoryDisplay getVocabularyCategoriesDisplay(
374                            long groupId, String name, long vocabularyId, int start, int end,
375                            OrderByComparator<AssetCategory> obc)
376                    throws PortalException {
377    
378                    List<AssetCategory> categories = null;
379                    int total = 0;
380    
381                    if (Validator.isNotNull(name)) {
382                            name = (CustomSQLUtil.keywords(name))[0];
383    
384                            categories = getVocabularyCategories(
385                                    groupId, name, vocabularyId, start, end, obc);
386                            total = getVocabularyCategoriesCount(groupId, name, vocabularyId);
387                    }
388                    else {
389                            categories = getVocabularyCategories(vocabularyId, start, end, obc);
390                            total = getVocabularyCategoriesCount(groupId, vocabularyId);
391                    }
392    
393                    return new AssetCategoryDisplay(categories, total, start, end);
394            }
395    
396            /**
397             * @deprecated As of 6.2.0, replaced by {@link
398             *             #getVocabularyRootCategories(long, long, int, int,
399             *             OrderByComparator)}
400             */
401            @Deprecated
402            @Override
403            public List<AssetCategory> getVocabularyRootCategories(
404                            long vocabularyId, int start, int end,
405                            OrderByComparator<AssetCategory> obc)
406                    throws PortalException {
407    
408                    AssetVocabulary vocabulary = assetVocabularyLocalService.getVocabulary(
409                            vocabularyId);
410    
411                    return getVocabularyRootCategories(
412                            vocabulary.getGroupId(), vocabularyId, start, end, obc);
413            }
414    
415            @Override
416            public List<AssetCategory> getVocabularyRootCategories(
417                    long groupId, long vocabularyId, int start, int end,
418                    OrderByComparator<AssetCategory> obc) {
419    
420                    return assetCategoryPersistence.filterFindByG_P_V(
421                            groupId, AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID,
422                            vocabularyId, start, end, obc);
423            }
424    
425            @Override
426            public int getVocabularyRootCategoriesCount(
427                    long groupId, long vocabularyId) {
428    
429                    return assetCategoryPersistence.filterCountByG_P_V(
430                            groupId, AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID,
431                            vocabularyId);
432            }
433    
434            @Override
435            public AssetCategory moveCategory(
436                            long categoryId, long parentCategoryId, long vocabularyId,
437                            ServiceContext serviceContext)
438                    throws PortalException {
439    
440                    AssetCategoryPermission.check(
441                            getPermissionChecker(), categoryId, ActionKeys.UPDATE);
442    
443                    return assetCategoryLocalService.moveCategory(
444                            categoryId, parentCategoryId, vocabularyId, serviceContext);
445            }
446    
447            @Override
448            public List<AssetCategory> search(
449                    long groupId, String keywords, long vocabularyId, int start, int end,
450                    OrderByComparator<AssetCategory> obc) {
451    
452                    String name = CustomSQLUtil.keywords(keywords)[0];
453    
454                    if (Validator.isNull(name)) {
455                            return assetCategoryPersistence.filterFindByG_V(
456                                    groupId, vocabularyId, start, end, obc);
457                    }
458                    else {
459                            return assetCategoryPersistence.filterFindByG_LikeN_V(
460                                    groupId, name, vocabularyId, start, end, obc);
461                    }
462            }
463    
464            @Override
465            public JSONArray search(
466                            long groupId, String name, String[] categoryProperties, int start,
467                            int end)
468                    throws PortalException {
469    
470                    List<AssetCategory> categories = assetCategoryLocalService.search(
471                            groupId, name, categoryProperties, start, end);
472    
473                    categories = filterCategories(categories);
474    
475                    return Autocomplete.listToJson(categories, "name", "name");
476            }
477    
478            @Override
479            public JSONArray search(
480                            long[] groupIds, String name, long[] vocabularyIds, int start,
481                            int end)
482                    throws PortalException {
483    
484                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
485    
486                    for (long groupId : groupIds) {
487                            JSONArray categoriesJSONArray = null;
488    
489                            if (Validator.isNull(name)) {
490                                    categoriesJSONArray = toJSONArray(
491                                            assetCategoryPersistence.filterFindByG_V(
492                                                    groupId, vocabularyIds));
493                            }
494                            else {
495                                    categoriesJSONArray = toJSONArray(
496                                            assetCategoryPersistence.filterFindByG_LikeN_V(
497                                                    groupId, name, vocabularyIds));
498                            }
499    
500                            for (int j = 0; j < categoriesJSONArray.length(); j++) {
501                                    jsonArray.put(categoriesJSONArray.getJSONObject(j));
502                            }
503                    }
504    
505                    return jsonArray;
506            }
507    
508            @Override
509            public AssetCategoryDisplay searchCategoriesDisplay(
510                            long groupId, String title, long vocabularyId, int start, int end)
511                    throws PortalException {
512    
513                    return searchCategoriesDisplay(
514                            new long[] {groupId}, title, new long[] {vocabularyId}, start, end);
515            }
516    
517            @Override
518            public AssetCategoryDisplay searchCategoriesDisplay(
519                            long groupId, String title, long parentCategoryId,
520                            long vocabularyId, int start, int end)
521                    throws PortalException {
522    
523                    return searchCategoriesDisplay(
524                            new long[] {groupId}, title, new long[] {parentCategoryId},
525                            new long[] {vocabularyId}, start, end);
526            }
527    
528            @Override
529            public AssetCategoryDisplay searchCategoriesDisplay(
530                            long[] groupIds, String title, long[] vocabularyIds, int start,
531                            int end)
532                    throws PortalException {
533    
534                    User user = getUser();
535    
536                    BaseModelSearchResult<AssetCategory> baseModelSearchResult =
537                            assetCategoryLocalService.searchCategories(
538                                    user.getCompanyId(), groupIds, title, vocabularyIds, start,
539                                    end);
540    
541                    return new AssetCategoryDisplay(
542                            baseModelSearchResult.getBaseModels(),
543                            baseModelSearchResult.getLength(), start, end);
544            }
545    
546            @Override
547            public AssetCategoryDisplay searchCategoriesDisplay(
548                            long[] groupIds, String title, long[] parentCategoryIds,
549                            long[] vocabularyIds, int start, int end)
550                    throws PortalException {
551    
552                    User user = getUser();
553    
554                    BaseModelSearchResult<AssetCategory> baseModelSearchResult =
555                            assetCategoryLocalService.searchCategories(
556                                    user.getCompanyId(), groupIds, title, parentCategoryIds,
557                                    vocabularyIds, start, end);
558    
559                    return new AssetCategoryDisplay(
560                            baseModelSearchResult.getBaseModels(),
561                            baseModelSearchResult.getLength(), start, end);
562            }
563    
564            @Override
565            public AssetCategory updateCategory(
566                            long categoryId, long parentCategoryId,
567                            Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
568                            long vocabularyId, String[] categoryProperties,
569                            ServiceContext serviceContext)
570                    throws PortalException {
571    
572                    AssetCategoryPermission.check(
573                            getPermissionChecker(), categoryId, ActionKeys.UPDATE);
574    
575                    return assetCategoryLocalService.updateCategory(
576                            getUserId(), categoryId, parentCategoryId, titleMap, descriptionMap,
577                            vocabularyId, categoryProperties, serviceContext);
578            }
579    
580            protected List<AssetCategory> filterCategories(
581                            List<AssetCategory> categories)
582                    throws PortalException {
583    
584                    PermissionChecker permissionChecker = getPermissionChecker();
585    
586                    categories = ListUtil.copy(categories);
587    
588                    Iterator<AssetCategory> itr = categories.iterator();
589    
590                    while (itr.hasNext()) {
591                            AssetCategory category = itr.next();
592    
593                            if (!AssetCategoryPermission.contains(
594                                            permissionChecker, category, ActionKeys.VIEW)) {
595    
596                                    itr.remove();
597                            }
598                    }
599    
600                    return categories;
601            }
602    
603            protected JSONArray toJSONArray(List<AssetCategory> categories)
604                    throws PortalException {
605    
606                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
607    
608                    for (AssetCategory category : categories) {
609                            String categoryJSON = JSONFactoryUtil.looseSerialize(category);
610    
611                            JSONObject categoryJSONObject = JSONFactoryUtil.createJSONObject(
612                                    categoryJSON);
613    
614                            categoryJSONObject.put(
615                                    "path", getCategoryPath(category.getCategoryId()));
616    
617                            jsonArray.put(categoryJSONObject);
618                    }
619    
620                    return jsonArray;
621            }
622    
623    }