001    /**
002     * Copyright (c) 2000-2011 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.Validator;
025    import com.liferay.portal.security.permission.ActionKeys;
026    import com.liferay.portal.security.permission.PermissionChecker;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portlet.asset.model.AssetCategory;
029    import com.liferay.portlet.asset.service.base.AssetCategoryServiceBaseImpl;
030    import com.liferay.portlet.asset.service.permission.AssetCategoryPermission;
031    import com.liferay.util.Autocomplete;
032    import com.liferay.util.dao.orm.CustomSQLUtil;
033    
034    import java.util.Iterator;
035    import java.util.List;
036    import java.util.Locale;
037    import java.util.Map;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Jorge Ferrer
042     * @author Alvaro del Castillo
043     * @author Eduardo Lundgren
044     * @author Bruno Farache
045     */
046    public class AssetCategoryServiceImpl extends AssetCategoryServiceBaseImpl {
047    
048            public AssetCategory addCategory(
049                            long parentCategoryId, Map<Locale, String> titleMap,
050                            Map<Locale, String> descriptionMap, long vocabularyId,
051                            String[] categoryProperties, ServiceContext serviceContext)
052                    throws PortalException, SystemException {
053    
054                    AssetCategoryPermission.check(
055                            getPermissionChecker(), serviceContext.getScopeGroupId(),
056                            parentCategoryId, ActionKeys.ADD_CATEGORY);
057    
058                    return assetCategoryLocalService.addCategory(
059                            getUserId(), parentCategoryId, titleMap, descriptionMap,
060                            vocabularyId, categoryProperties, serviceContext);
061            }
062    
063            public void deleteCategories(long[] categoryIds)
064                    throws PortalException, SystemException {
065    
066                    PermissionChecker permissionChecker = getPermissionChecker();
067    
068                    for (long categoryId : categoryIds) {
069                            AssetCategory category = assetCategoryPersistence.fetchByPrimaryKey(
070                                    categoryId);
071    
072                            if (category == null) {
073                                    continue;
074                            }
075    
076                            AssetCategoryPermission.check(
077                                    permissionChecker, categoryId, ActionKeys.DELETE);
078    
079                            assetCategoryLocalService.deleteCategory(category);
080                    }
081            }
082    
083            public void deleteCategory(long categoryId)
084                    throws PortalException, SystemException {
085    
086                    AssetCategoryPermission.check(
087                            getPermissionChecker(), categoryId, ActionKeys.DELETE);
088    
089                    assetCategoryLocalService.deleteCategory(categoryId);
090            }
091    
092            public List<AssetCategory> getCategories(String className, long classPK)
093                    throws PortalException, SystemException {
094    
095                    return filterCategories(
096                            assetCategoryLocalService.getCategories(className, classPK));
097            }
098    
099            public AssetCategory getCategory(long categoryId)
100                    throws PortalException, SystemException {
101    
102                    AssetCategoryPermission.check(
103                            getPermissionChecker(), categoryId, ActionKeys.VIEW);
104    
105                    return assetCategoryLocalService.getCategory(categoryId);
106            }
107    
108            public List<AssetCategory> getChildCategories(long parentCategoryId)
109                    throws PortalException, SystemException {
110    
111                    return filterCategories(
112                            assetCategoryLocalService.getChildCategories(parentCategoryId));
113            }
114    
115            public List<AssetCategory> getChildCategories(
116                            long parentCategoryId, int start, int end, OrderByComparator obc)
117                    throws PortalException, SystemException {
118    
119                    return filterCategories(
120                            assetCategoryLocalService.getChildCategories(
121                                    parentCategoryId, start, end, obc));
122            }
123    
124            public JSONObject getJSONVocabularyCategories(
125                            long groupId, String name, long vocabularyId, int start, int end,
126                            OrderByComparator obc)
127                    throws PortalException, SystemException {
128    
129                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
130    
131                    int page = end / (end - start);
132    
133                    jsonObject.put("page", page);
134    
135                    List<AssetCategory> categories;
136                    int total = 0;
137    
138                    if (Validator.isNotNull(name)) {
139                            name = (CustomSQLUtil.keywords(name))[0];
140    
141                            categories = getVocabularyCategories(
142                                    groupId, name, vocabularyId, start, end, obc);
143                            total = getVocabularyCategoriesCount(groupId, name, vocabularyId);
144                    }
145                    else {
146                            categories = getVocabularyCategories(vocabularyId, start, end, obc);
147                            total = getVocabularyCategoriesCount(groupId, vocabularyId);
148                    }
149    
150                    String categoriesJSON = JSONFactoryUtil.looseSerialize(categories);
151    
152                    JSONArray categoriesJSONArray =
153                            JSONFactoryUtil.createJSONArray(categoriesJSON);
154    
155                    jsonObject.put("categories", categoriesJSONArray);
156    
157                    jsonObject.put("total", total);
158    
159                    return jsonObject;
160            }
161    
162            public List<AssetCategory> getVocabularyCategories(
163                            long vocabularyId, int start, int end, OrderByComparator obc)
164                    throws PortalException, SystemException {
165    
166                    return filterCategories(
167                            assetCategoryLocalService.getVocabularyCategories(
168                                    vocabularyId, start, end, obc));
169            }
170    
171            public List<AssetCategory> getVocabularyCategories(
172                            long parentCategoryId, long vocabularyId, int start, int end,
173                            OrderByComparator obc)
174                    throws PortalException, SystemException {
175    
176                    return filterCategories(
177                            assetCategoryLocalService.getVocabularyCategories(
178                                    parentCategoryId, vocabularyId, start, end, obc));
179            }
180    
181            public List<AssetCategory> getVocabularyCategories(
182                            long groupId, String name, long vocabularyId, int start, int end,
183                            OrderByComparator obc)
184                    throws SystemException {
185    
186                    return assetCategoryFinder.filterFindByG_N_V(
187                            groupId, name, vocabularyId, start, end, obc);
188            }
189    
190            public int getVocabularyCategoriesCount(long groupId, long vocabularyId)
191                    throws SystemException {
192    
193                    return assetCategoryPersistence.filterCountByG_V(groupId, vocabularyId);
194            }
195    
196            public int getVocabularyCategoriesCount(
197                            long groupId, String name, long vocabularyId)
198                    throws SystemException {
199    
200                    return assetCategoryFinder.filterCountByG_N_V(
201                            groupId, name, vocabularyId);
202            }
203    
204            public List<AssetCategory> getVocabularyRootCategories(
205                            long vocabularyId, int start, int end, OrderByComparator obc)
206                    throws PortalException, SystemException {
207    
208                    return filterCategories(
209                            assetCategoryLocalService.getVocabularyRootCategories(
210                                    vocabularyId, start, end, obc));
211            }
212    
213            public AssetCategory moveCategory(
214                            long categoryId, long parentCategoryId, long vocabularyId,
215                            ServiceContext serviceContext)
216                    throws PortalException, SystemException {
217    
218                    AssetCategoryPermission.check(
219                            getPermissionChecker(), categoryId, ActionKeys.UPDATE);
220    
221                    return assetCategoryLocalService.moveCategory(
222                            categoryId, parentCategoryId, vocabularyId, serviceContext);
223            }
224    
225            public JSONArray search(
226                            long groupId, String name, String[] categoryProperties, int start,
227                            int end)
228                    throws PortalException, SystemException {
229    
230                    List<AssetCategory> categories = assetCategoryLocalService.search(
231                            groupId, name, categoryProperties, start, end);
232    
233                    categories = filterCategories(categories);
234    
235                    return Autocomplete.listToJson(categories, "name", "name");
236            }
237    
238            public AssetCategory updateCategory(
239                            long categoryId, long parentCategoryId,
240                            Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
241                            long vocabularyId, String[] categoryProperties,
242                            ServiceContext serviceContext)
243                    throws PortalException, SystemException {
244    
245                    AssetCategoryPermission.check(
246                            getPermissionChecker(), categoryId, ActionKeys.UPDATE);
247    
248                    return assetCategoryLocalService.updateCategory(
249                            getUserId(), categoryId, parentCategoryId, titleMap, descriptionMap,
250                            vocabularyId, categoryProperties, serviceContext);
251            }
252    
253            protected List<AssetCategory> filterCategories(
254                            List<AssetCategory> categories)
255                    throws PortalException {
256    
257                    PermissionChecker permissionChecker = getPermissionChecker();
258    
259                    categories = ListUtil.copy(categories);
260    
261                    Iterator<AssetCategory> itr = categories.iterator();
262    
263                    while (itr.hasNext()) {
264                            AssetCategory category = itr.next();
265    
266                            if (!AssetCategoryPermission.contains(
267                                            permissionChecker, category, ActionKeys.VIEW)) {
268    
269                                    itr.remove();
270                            }
271                    }
272    
273                    return categories;
274            }
275    
276    }