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