001    /**
002     * Copyright (c) 2000-2012 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.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    /**
046     * @author Brian Wing Shun Chan
047     * @author Jorge Ferrer
048     * @author Alvaro del Castillo
049     * @author Eduardo Lundgren
050     * @author Bruno Farache
051     */
052    public class AssetCategoryServiceImpl extends AssetCategoryServiceBaseImpl {
053    
054            public AssetCategory addCategory(
055                            long parentCategoryId, Map<Locale, String> titleMap,
056                            Map<Locale, String> descriptionMap, long vocabularyId,
057                            String[] categoryProperties, ServiceContext serviceContext)
058                    throws PortalException, SystemException {
059    
060                    AssetCategoryPermission.check(
061                            getPermissionChecker(), serviceContext.getScopeGroupId(),
062                            parentCategoryId, ActionKeys.ADD_CATEGORY);
063    
064                    return assetCategoryLocalService.addCategory(
065                            getUserId(), parentCategoryId, titleMap, descriptionMap,
066                            vocabularyId, categoryProperties, serviceContext);
067            }
068    
069            public AssetCategory addCategory(
070                            String title, long vocabularyId, ServiceContext serviceContext)
071                    throws PortalException, SystemException {
072    
073                    AssetCategoryPermission.check(
074                            getPermissionChecker(), serviceContext.getScopeGroupId(),
075                            AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID,
076                            ActionKeys.ADD_CATEGORY);
077    
078                    return assetCategoryLocalService.addCategory(
079                            getUserId(), title, vocabularyId, serviceContext);
080            }
081    
082            public void deleteCategories(long[] categoryIds)
083                    throws PortalException, SystemException {
084    
085                    PermissionChecker permissionChecker = getPermissionChecker();
086    
087                    for (long categoryId : categoryIds) {
088                            AssetCategory category = assetCategoryPersistence.fetchByPrimaryKey(
089                                    categoryId);
090    
091                            if (category == null) {
092                                    continue;
093                            }
094    
095                            AssetCategoryPermission.check(
096                                    permissionChecker, categoryId, ActionKeys.DELETE);
097    
098                            assetCategoryLocalService.deleteCategory(category);
099                    }
100            }
101    
102            public void deleteCategory(long categoryId)
103                    throws PortalException, SystemException {
104    
105                    AssetCategoryPermission.check(
106                            getPermissionChecker(), categoryId, ActionKeys.DELETE);
107    
108                    assetCategoryLocalService.deleteCategory(categoryId);
109            }
110    
111            public List<AssetCategory> getCategories(String className, long classPK)
112                    throws PortalException, SystemException {
113    
114                    return filterCategories(
115                            assetCategoryLocalService.getCategories(className, classPK));
116            }
117    
118            public AssetCategory getCategory(long categoryId)
119                    throws PortalException, SystemException {
120    
121                    AssetCategoryPermission.check(
122                            getPermissionChecker(), categoryId, ActionKeys.VIEW);
123    
124                    return assetCategoryLocalService.getCategory(categoryId);
125            }
126    
127            public List<AssetCategory> getChildCategories(long parentCategoryId)
128                    throws PortalException, SystemException {
129    
130                    return filterCategories(
131                            assetCategoryLocalService.getChildCategories(parentCategoryId));
132            }
133    
134            public List<AssetCategory> getChildCategories(
135                            long parentCategoryId, int start, int end, OrderByComparator obc)
136                    throws PortalException, SystemException {
137    
138                    return filterCategories(
139                            assetCategoryLocalService.getChildCategories(
140                                    parentCategoryId, start, end, obc));
141            }
142    
143            /**
144             * @deprecated {@link #search(long[], String, long[], int, int)}
145             */
146            public JSONArray getJSONSearch(
147                            long groupId, String name, long[] vocabularyIds, int start, int end)
148                    throws PortalException, SystemException {
149    
150                    return search(new long[]{groupId}, name, vocabularyIds, start, end);
151            }
152    
153            public JSONObject getJSONVocabularyCategories(
154                            long vocabularyId, int start, int end, OrderByComparator obc)
155                    throws PortalException, SystemException {
156    
157                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
158    
159                    List<AssetCategory> categories = filterCategories(
160                            assetCategoryLocalService.getVocabularyCategories(
161                                    vocabularyId, start, end, obc));
162    
163                    jsonObject.put("categories", toJSONArray(categories));
164                    jsonObject.put("total", categories.size());
165    
166                    return jsonObject;
167            }
168    
169            public JSONObject getJSONVocabularyCategories(
170                            long groupId, String name, long vocabularyId, int start, int end,
171                            OrderByComparator obc)
172                    throws PortalException, SystemException {
173    
174                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
175    
176                    int page = 0;
177    
178                    if ((end > 0) && (start > 0)) {
179                            page = end / (end - start);
180                    }
181    
182                    jsonObject.put("page", page);
183    
184                    List<AssetCategory> categories;
185                    int total = 0;
186    
187                    if (Validator.isNotNull(name)) {
188                            name = (CustomSQLUtil.keywords(name))[0];
189    
190                            categories = getVocabularyCategories(
191                                    groupId, name, vocabularyId, start, end, obc);
192                            total = getVocabularyCategoriesCount(groupId, name, vocabularyId);
193                    }
194                    else {
195                            categories = getVocabularyCategories(vocabularyId, start, end, obc);
196                            total = getVocabularyCategoriesCount(groupId, vocabularyId);
197                    }
198    
199                    jsonObject.put("categories", toJSONArray(categories));
200                    jsonObject.put("total", total);
201    
202                    return jsonObject;
203            }
204    
205            public List<AssetCategory> getVocabularyCategories(
206                            long vocabularyId, int start, int end, OrderByComparator obc)
207                    throws PortalException, SystemException {
208    
209                    return filterCategories(
210                            assetCategoryLocalService.getVocabularyCategories(
211                                    vocabularyId, start, end, obc));
212            }
213    
214            public List<AssetCategory> getVocabularyCategories(
215                            long parentCategoryId, long vocabularyId, int start, int end,
216                            OrderByComparator obc)
217                    throws PortalException, SystemException {
218    
219                    return filterCategories(
220                            assetCategoryLocalService.getVocabularyCategories(
221                                    parentCategoryId, vocabularyId, start, end, obc));
222            }
223    
224            public List<AssetCategory> getVocabularyCategories(
225                            long groupId, String name, long vocabularyId, int start, int end,
226                            OrderByComparator obc)
227                    throws SystemException {
228    
229                    if (Validator.isNull(name)) {
230                            return assetCategoryPersistence.filterFindByG_V(
231                                    groupId, vocabularyId, start, end, obc);
232                    }
233                    else {
234                            return assetCategoryPersistence.filterFindByG_LikeN_V(
235                                    groupId, name, vocabularyId, start, end, obc);
236                    }
237            }
238    
239            public int getVocabularyCategoriesCount(long groupId, long vocabularyId)
240                    throws SystemException {
241    
242                    return assetCategoryPersistence.filterCountByG_V(groupId, vocabularyId);
243            }
244    
245            public int getVocabularyCategoriesCount(
246                            long groupId, String name, long vocabularyId)
247                    throws SystemException {
248    
249                    if (Validator.isNull(name)) {
250                            return assetCategoryPersistence.filterCountByG_V(
251                                    groupId, vocabularyId);
252                    }
253                    else {
254                            return assetCategoryPersistence.filterCountByG_LikeN_V(
255                                    groupId, name, vocabularyId);
256                    }
257            }
258    
259            public List<AssetCategory> getVocabularyRootCategories(
260                            long vocabularyId, int start, int end, OrderByComparator obc)
261                    throws PortalException, SystemException {
262    
263                    return filterCategories(
264                            assetCategoryLocalService.getVocabularyRootCategories(
265                                    vocabularyId, start, end, obc));
266            }
267    
268            public AssetCategory moveCategory(
269                            long categoryId, long parentCategoryId, long vocabularyId,
270                            ServiceContext serviceContext)
271                    throws PortalException, SystemException {
272    
273                    AssetCategoryPermission.check(
274                            getPermissionChecker(), categoryId, ActionKeys.UPDATE);
275    
276                    return assetCategoryLocalService.moveCategory(
277                            categoryId, parentCategoryId, vocabularyId, serviceContext);
278            }
279    
280            public List<AssetCategory> search(
281                            long groupId, String keywords, long vocabularyId, int start,
282                            int end, OrderByComparator obc)
283                    throws SystemException {
284    
285                    String name = CustomSQLUtil.keywords(keywords)[0];
286    
287                    if (Validator.isNull(name)) {
288                            return assetCategoryPersistence.filterFindByG_V(
289                                    groupId, vocabularyId, start, end, obc);
290                    }
291                    else {
292                            return assetCategoryPersistence.filterFindByG_LikeN_V(
293                                    groupId, name, vocabularyId, start, end, obc);
294                    }
295            }
296    
297            public JSONArray search(
298                            long groupId, String name, String[] categoryProperties, int start,
299                            int end)
300                    throws PortalException, SystemException {
301    
302                    List<AssetCategory> categories = assetCategoryLocalService.search(
303                            groupId, name, categoryProperties, start, end);
304    
305                    categories = filterCategories(categories);
306    
307                    return Autocomplete.listToJson(categories, "name", "name");
308            }
309    
310            public JSONArray search(
311                            long[] groupIds, String name, long[] vocabularyIds, int start,
312                            int end)
313                    throws PortalException, SystemException {
314    
315                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
316    
317                    for (long groupId : groupIds) {
318                            JSONArray categoriesJSONArray = null;
319    
320                            if (Validator.isNull(name)) {
321                                    categoriesJSONArray = toJSONArray(
322                                            assetCategoryPersistence.filterFindByG_V(
323                                                    groupId, vocabularyIds));
324                            }
325                            else {
326                                    categoriesJSONArray = toJSONArray(
327                                            assetCategoryPersistence.filterFindByG_LikeN_V(
328                                                    groupId, name, vocabularyIds));
329                            }
330    
331                            for (int j = 0; j < categoriesJSONArray.length(); j++) {
332                                    jsonArray.put(categoriesJSONArray.getJSONObject(j));
333                            }
334                    }
335    
336                    return jsonArray;
337            }
338    
339            public AssetCategory updateCategory(
340                            long categoryId, long parentCategoryId,
341                            Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
342                            long vocabularyId, String[] categoryProperties,
343                            ServiceContext serviceContext)
344                    throws PortalException, SystemException {
345    
346                    AssetCategoryPermission.check(
347                            getPermissionChecker(), categoryId, ActionKeys.UPDATE);
348    
349                    return assetCategoryLocalService.updateCategory(
350                            getUserId(), categoryId, parentCategoryId, titleMap, descriptionMap,
351                            vocabularyId, categoryProperties, serviceContext);
352            }
353    
354            protected List<AssetCategory> filterCategories(
355                            List<AssetCategory> categories)
356                    throws PortalException {
357    
358                    PermissionChecker permissionChecker = getPermissionChecker();
359    
360                    categories = ListUtil.copy(categories);
361    
362                    Iterator<AssetCategory> itr = categories.iterator();
363    
364                    while (itr.hasNext()) {
365                            AssetCategory category = itr.next();
366    
367                            if (!AssetCategoryPermission.contains(
368                                            permissionChecker, category, ActionKeys.VIEW)) {
369    
370                                    itr.remove();
371                            }
372                    }
373    
374                    return categories;
375            }
376    
377            protected JSONArray toJSONArray(List<AssetCategory> categories)
378                    throws PortalException, SystemException {
379    
380                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
381    
382                    for (AssetCategory category : categories) {
383                            String categoryJSON = JSONFactoryUtil.looseSerialize(category);
384    
385                            JSONObject categoryJSONObject = JSONFactoryUtil.createJSONObject(
386                                    categoryJSON);
387    
388                            List<String> names = new ArrayList<String>();
389    
390                            AssetCategory curCategory = category;
391    
392                            while (curCategory.getParentCategoryId() > 0) {
393                                    AssetCategory parentCategory = getCategory(
394                                            curCategory.getParentCategoryId());
395    
396                                    names.add(parentCategory.getName());
397                                    names.add(
398                                            StringPool.SPACE + StringPool.GREATER_THAN +
399                                                    StringPool.SPACE);
400    
401                                    curCategory = parentCategory;
402                            }
403    
404                            Collections.reverse(names);
405    
406                            AssetVocabulary vocabulary = assetVocabularyService.getVocabulary(
407                                    category.getVocabularyId());
408    
409                            StringBundler sb = new StringBundler(1 + names.size());
410    
411                            sb.append(vocabulary.getName());
412                            sb.append(names.toArray(new String[names.size()]));
413    
414                            categoryJSONObject.put("path", sb.toString());
415    
416                            jsonArray.put(categoryJSONObject);
417                    }
418    
419                    return jsonArray;
420            }
421    
422    }