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.AssetVocabulary;
032    import com.liferay.portlet.asset.service.base.AssetCategoryServiceBaseImpl;
033    import com.liferay.portlet.asset.service.permission.AssetCategoryPermission;
034    import com.liferay.util.Autocomplete;
035    import com.liferay.util.dao.orm.CustomSQLUtil;
036    
037    import java.util.ArrayList;
038    import java.util.Collections;
039    import java.util.Iterator;
040    import java.util.List;
041    import java.util.Locale;
042    import java.util.Map;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     * @author Jorge Ferrer
047     * @author Alvaro del Castillo
048     * @author Eduardo Lundgren
049     * @author Bruno Farache
050     */
051    public class AssetCategoryServiceImpl extends AssetCategoryServiceBaseImpl {
052    
053            public AssetCategory addCategory(
054                            long parentCategoryId, Map<Locale, String> titleMap,
055                            Map<Locale, String> descriptionMap, long vocabularyId,
056                            String[] categoryProperties, ServiceContext serviceContext)
057                    throws PortalException, SystemException {
058    
059                    AssetCategoryPermission.check(
060                            getPermissionChecker(), serviceContext.getScopeGroupId(),
061                            parentCategoryId, ActionKeys.ADD_CATEGORY);
062    
063                    return assetCategoryLocalService.addCategory(
064                            getUserId(), parentCategoryId, titleMap, descriptionMap,
065                            vocabularyId, categoryProperties, serviceContext);
066            }
067    
068            public void deleteCategories(long[] categoryIds)
069                    throws PortalException, SystemException {
070    
071                    PermissionChecker permissionChecker = getPermissionChecker();
072    
073                    for (long categoryId : categoryIds) {
074                            AssetCategory category = assetCategoryPersistence.fetchByPrimaryKey(
075                                    categoryId);
076    
077                            if (category == null) {
078                                    continue;
079                            }
080    
081                            AssetCategoryPermission.check(
082                                    permissionChecker, categoryId, ActionKeys.DELETE);
083    
084                            assetCategoryLocalService.deleteCategory(category);
085                    }
086            }
087    
088            public void deleteCategory(long categoryId)
089                    throws PortalException, SystemException {
090    
091                    AssetCategoryPermission.check(
092                            getPermissionChecker(), categoryId, ActionKeys.DELETE);
093    
094                    assetCategoryLocalService.deleteCategory(categoryId);
095            }
096    
097            public List<AssetCategory> getCategories(String className, long classPK)
098                    throws PortalException, SystemException {
099    
100                    return filterCategories(
101                            assetCategoryLocalService.getCategories(className, classPK));
102            }
103    
104            public AssetCategory getCategory(long categoryId)
105                    throws PortalException, SystemException {
106    
107                    AssetCategoryPermission.check(
108                            getPermissionChecker(), categoryId, ActionKeys.VIEW);
109    
110                    return assetCategoryLocalService.getCategory(categoryId);
111            }
112    
113            public List<AssetCategory> getChildCategories(long parentCategoryId)
114                    throws PortalException, SystemException {
115    
116                    return filterCategories(
117                            assetCategoryLocalService.getChildCategories(parentCategoryId));
118            }
119    
120            public List<AssetCategory> getChildCategories(
121                            long parentCategoryId, int start, int end, OrderByComparator obc)
122                    throws PortalException, SystemException {
123    
124                    return filterCategories(
125                            assetCategoryLocalService.getChildCategories(
126                                    parentCategoryId, start, end, obc));
127            }
128    
129            public JSONArray getJSONSearch(
130                            long groupId, String keywords, long vocabularyId, int start,
131                            int end, OrderByComparator obc)
132                    throws PortalException, SystemException {
133    
134                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
135    
136                    List<AssetCategory> categories = search(
137                            groupId, keywords, vocabularyId, start, end, obc);
138    
139                    for (AssetCategory category : categories) {
140                            String categoryJSON = JSONFactoryUtil.looseSerialize(category);
141    
142                            JSONObject categoryJSONObject = JSONFactoryUtil.createJSONObject(
143                                    categoryJSON);
144    
145                            List<String> names = new ArrayList<String>();
146    
147                            AssetCategory curCategory = category;
148    
149                            while (curCategory.getParentCategoryId() > 0) {
150                                    AssetCategory parentCategory = getCategory(
151                                            curCategory.getParentCategoryId());
152    
153                                    names.add(parentCategory.getName());
154                                    names.add(StringPool.SLASH);
155    
156                                    curCategory = parentCategory;
157                            }
158    
159                            Collections.reverse(names);
160    
161                            AssetVocabulary vocabulary = assetVocabularyService.getVocabulary(
162                                    category.getVocabularyId());
163    
164                            StringBundler sb = new StringBundler(2 + names.size());
165    
166                            sb.append(" - ");
167                            sb.append(vocabulary.getName());
168                            sb.append(names.toArray(new String[names.size()]));
169    
170                            categoryJSONObject.put("path", sb.toString());
171    
172                            jsonArray.put(categoryJSONObject);
173                    }
174    
175                    return jsonArray;
176            }
177    
178            public JSONObject getJSONVocabularyCategories(
179                            long groupId, String name, long vocabularyId, int start, int end,
180                            OrderByComparator obc)
181                    throws PortalException, SystemException {
182    
183                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
184    
185                    int page = end / (end - start);
186    
187                    jsonObject.put("page", page);
188    
189                    List<AssetCategory> categories;
190                    int total = 0;
191    
192                    if (Validator.isNotNull(name)) {
193                            name = (CustomSQLUtil.keywords(name))[0];
194    
195                            categories = getVocabularyCategories(
196                                    groupId, name, vocabularyId, start, end, obc);
197                            total = getVocabularyCategoriesCount(groupId, name, vocabularyId);
198                    }
199                    else {
200                            categories = getVocabularyCategories(vocabularyId, start, end, obc);
201                            total = getVocabularyCategoriesCount(groupId, vocabularyId);
202                    }
203    
204                    String categoriesJSON = JSONFactoryUtil.looseSerialize(categories);
205    
206                    JSONArray categoriesJSONArray = JSONFactoryUtil.createJSONArray(
207                            categoriesJSON);
208    
209                    jsonObject.put("categories", categoriesJSONArray);
210    
211                    jsonObject.put("total", total);
212    
213                    return jsonObject;
214            }
215    
216            public List<AssetCategory> getVocabularyCategories(
217                            long vocabularyId, int start, int end, OrderByComparator obc)
218                    throws PortalException, SystemException {
219    
220                    return filterCategories(
221                            assetCategoryLocalService.getVocabularyCategories(
222                                    vocabularyId, start, end, obc));
223            }
224    
225            public List<AssetCategory> getVocabularyCategories(
226                            long parentCategoryId, long vocabularyId, int start, int end,
227                            OrderByComparator obc)
228                    throws PortalException, SystemException {
229    
230                    return filterCategories(
231                            assetCategoryLocalService.getVocabularyCategories(
232                                    parentCategoryId, vocabularyId, start, end, obc));
233            }
234    
235            public List<AssetCategory> getVocabularyCategories(
236                            long groupId, String name, long vocabularyId, int start, int end,
237                            OrderByComparator obc)
238                    throws SystemException {
239    
240                    return assetCategoryFinder.filterFindByG_N_V(
241                            groupId, name, vocabularyId, start, end, obc);
242            }
243    
244            public int getVocabularyCategoriesCount(long groupId, long vocabularyId)
245                    throws SystemException {
246    
247                    return assetCategoryPersistence.filterCountByG_V(groupId, vocabularyId);
248            }
249    
250            public int getVocabularyCategoriesCount(
251                            long groupId, String name, long vocabularyId)
252                    throws SystemException {
253    
254                    return assetCategoryFinder.filterCountByG_N_V(
255                            groupId, name, vocabularyId);
256            }
257    
258            public List<AssetCategory> getVocabularyRootCategories(
259                            long vocabularyId, int start, int end, OrderByComparator obc)
260                    throws PortalException, SystemException {
261    
262                    return filterCategories(
263                            assetCategoryLocalService.getVocabularyRootCategories(
264                                    vocabularyId, start, end, obc));
265            }
266    
267            public AssetCategory moveCategory(
268                            long categoryId, long parentCategoryId, long vocabularyId,
269                            ServiceContext serviceContext)
270                    throws PortalException, SystemException {
271    
272                    AssetCategoryPermission.check(
273                            getPermissionChecker(), categoryId, ActionKeys.UPDATE);
274    
275                    return assetCategoryLocalService.moveCategory(
276                            categoryId, parentCategoryId, vocabularyId, serviceContext);
277            }
278    
279            public List<AssetCategory> search(
280                            long groupId, String keywords, long vocabularyId, int start,
281                            int end, OrderByComparator obc)
282                    throws PortalException, SystemException {
283    
284                    return filterCategories(
285                            assetCategoryFinder.findByG_N_V(
286                                    groupId, CustomSQLUtil.keywords(keywords)[0], vocabularyId,
287                                    start, end, obc));
288            }
289    
290            public JSONArray search(
291                            long groupId, String name, String[] categoryProperties, int start,
292                            int end)
293                    throws PortalException, SystemException {
294    
295                    List<AssetCategory> categories = assetCategoryLocalService.search(
296                            groupId, name, categoryProperties, start, end);
297    
298                    categories = filterCategories(categories);
299    
300                    return Autocomplete.listToJson(categories, "name", "name");
301            }
302    
303            public AssetCategory updateCategory(
304                            long categoryId, long parentCategoryId,
305                            Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
306                            long vocabularyId, String[] categoryProperties,
307                            ServiceContext serviceContext)
308                    throws PortalException, SystemException {
309    
310                    AssetCategoryPermission.check(
311                            getPermissionChecker(), categoryId, ActionKeys.UPDATE);
312    
313                    return assetCategoryLocalService.updateCategory(
314                            getUserId(), categoryId, parentCategoryId, titleMap, descriptionMap,
315                            vocabularyId, categoryProperties, serviceContext);
316            }
317    
318            protected List<AssetCategory> filterCategories(
319                            List<AssetCategory> categories)
320                    throws PortalException {
321    
322                    PermissionChecker permissionChecker = getPermissionChecker();
323    
324                    categories = ListUtil.copy(categories);
325    
326                    Iterator<AssetCategory> itr = categories.iterator();
327    
328                    while (itr.hasNext()) {
329                            AssetCategory category = itr.next();
330    
331                            if (!AssetCategoryPermission.contains(
332                                            permissionChecker, category, ActionKeys.VIEW)) {
333    
334                                    itr.remove();
335                            }
336                    }
337    
338                    return categories;
339            }
340    
341    }