001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            /**
130             * @deprecated
131             */
132            public JSONArray getJSONSearch(
133                            long groupId, String keywords, long vocabularyId, int start,
134                            int end, OrderByComparator obc)
135                    throws PortalException, SystemException {
136    
137                    List<AssetCategory> categories = getVocabularyCategories(
138                            groupId, keywords, vocabularyId, start, end, obc);
139    
140                    return toJSONArray(categories);
141            }
142    
143            public JSONArray getJSONSearch(
144                            long groupId, String name, long[] vocabularyIds, int start, int end)
145                    throws PortalException, SystemException {
146    
147                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
148    
149                    for (AssetVocabulary vocabulary :
150                                    assetVocabularyService.getVocabularies(vocabularyIds)) {
151    
152                            List<AssetCategory> vocabularyCategory =
153                                    assetCategoryFinder.findByG_N_V(
154                                            groupId, name, vocabulary.getVocabularyId(), start, end,
155                                            null);
156    
157                            JSONArray vocabularyCategoryJSONArray = toJSONArray(
158                                    vocabularyCategory);
159    
160                            for (int i = 0; i < vocabularyCategoryJSONArray.length(); ++i) {
161                                    JSONObject vocabularyCategoryJSONObject =
162                                            vocabularyCategoryJSONArray.getJSONObject(i);
163    
164                                    jsonArray.put(vocabularyCategoryJSONObject);
165                            }
166                    }
167    
168                    return jsonArray;
169            }
170    
171            public JSONObject getJSONVocabularyCategories(
172                            long vocabularyId, int start, int end, OrderByComparator obc)
173                    throws PortalException, SystemException {
174    
175                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
176    
177                    List<AssetCategory> categories = filterCategories(
178                            assetCategoryLocalService.getVocabularyCategories(
179                                    vocabularyId, start, end, obc));
180    
181                    jsonObject.put("categories", toJSONArray(categories));
182                    jsonObject.put("total", categories.size());
183    
184                    return jsonObject;
185            }
186    
187            public JSONObject getJSONVocabularyCategories(
188                            long groupId, String name, long vocabularyId, int start, int end,
189                            OrderByComparator obc)
190                    throws PortalException, SystemException {
191    
192                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
193    
194                    int page = 0;
195    
196                    if ((end > 0) && (start > 0)) {
197                            page = end / (end - start);
198                    }
199    
200                    jsonObject.put("page", page);
201    
202                    List<AssetCategory> categories;
203                    int total = 0;
204    
205                    if (Validator.isNotNull(name)) {
206                            name = (CustomSQLUtil.keywords(name))[0];
207    
208                            categories = getVocabularyCategories(
209                                    groupId, name, vocabularyId, start, end, obc);
210                            total = getVocabularyCategoriesCount(groupId, name, vocabularyId);
211                    }
212                    else {
213                            categories = getVocabularyCategories(vocabularyId, start, end, obc);
214                            total = getVocabularyCategoriesCount(groupId, vocabularyId);
215                    }
216    
217                    jsonObject.put("categories", toJSONArray(categories));
218                    jsonObject.put("total", total);
219    
220                    return jsonObject;
221            }
222    
223            public List<AssetCategory> getVocabularyCategories(
224                            long vocabularyId, int start, int end, OrderByComparator obc)
225                    throws PortalException, SystemException {
226    
227                    return filterCategories(
228                            assetCategoryLocalService.getVocabularyCategories(
229                                    vocabularyId, start, end, obc));
230            }
231    
232            public List<AssetCategory> getVocabularyCategories(
233                            long parentCategoryId, long vocabularyId, int start, int end,
234                            OrderByComparator obc)
235                    throws PortalException, SystemException {
236    
237                    return filterCategories(
238                            assetCategoryLocalService.getVocabularyCategories(
239                                    parentCategoryId, vocabularyId, start, end, obc));
240            }
241    
242            public List<AssetCategory> getVocabularyCategories(
243                            long groupId, String name, long vocabularyId, int start, int end,
244                            OrderByComparator obc)
245                    throws SystemException {
246    
247                    return assetCategoryFinder.filterFindByG_N_V(
248                            groupId, name, vocabularyId, start, end, obc);
249            }
250    
251            public int getVocabularyCategoriesCount(long groupId, long vocabularyId)
252                    throws SystemException {
253    
254                    return assetCategoryPersistence.filterCountByG_V(groupId, vocabularyId);
255            }
256    
257            public int getVocabularyCategoriesCount(
258                            long groupId, String name, long vocabularyId)
259                    throws SystemException {
260    
261                    return assetCategoryFinder.filterCountByG_N_V(
262                            groupId, name, vocabularyId);
263            }
264    
265            public List<AssetCategory> getVocabularyRootCategories(
266                            long vocabularyId, int start, int end, OrderByComparator obc)
267                    throws PortalException, SystemException {
268    
269                    return filterCategories(
270                            assetCategoryLocalService.getVocabularyRootCategories(
271                                    vocabularyId, start, end, obc));
272            }
273    
274            public AssetCategory moveCategory(
275                            long categoryId, long parentCategoryId, long vocabularyId,
276                            ServiceContext serviceContext)
277                    throws PortalException, SystemException {
278    
279                    AssetCategoryPermission.check(
280                            getPermissionChecker(), categoryId, ActionKeys.UPDATE);
281    
282                    return assetCategoryLocalService.moveCategory(
283                            categoryId, parentCategoryId, vocabularyId, serviceContext);
284            }
285    
286            public List<AssetCategory> search(
287                            long groupId, String keywords, long vocabularyId, int start,
288                            int end, OrderByComparator obc)
289                    throws PortalException, SystemException {
290    
291                    return filterCategories(
292                            assetCategoryFinder.findByG_N_V(
293                                    groupId, CustomSQLUtil.keywords(keywords)[0], vocabularyId,
294                                    start, end, obc));
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 AssetCategory updateCategory(
311                            long categoryId, long parentCategoryId,
312                            Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
313                            long vocabularyId, String[] categoryProperties,
314                            ServiceContext serviceContext)
315                    throws PortalException, SystemException {
316    
317                    AssetCategoryPermission.check(
318                            getPermissionChecker(), categoryId, ActionKeys.UPDATE);
319    
320                    return assetCategoryLocalService.updateCategory(
321                            getUserId(), categoryId, parentCategoryId, titleMap, descriptionMap,
322                            vocabularyId, categoryProperties, serviceContext);
323            }
324    
325            protected List<AssetCategory> filterCategories(
326                            List<AssetCategory> categories)
327                    throws PortalException {
328    
329                    PermissionChecker permissionChecker = getPermissionChecker();
330    
331                    categories = ListUtil.copy(categories);
332    
333                    Iterator<AssetCategory> itr = categories.iterator();
334    
335                    while (itr.hasNext()) {
336                            AssetCategory category = itr.next();
337    
338                            if (!AssetCategoryPermission.contains(
339                                            permissionChecker, category, ActionKeys.VIEW)) {
340    
341                                    itr.remove();
342                            }
343                    }
344    
345                    return categories;
346            }
347    
348            protected JSONArray toJSONArray(List<AssetCategory> categories)
349                    throws PortalException, SystemException {
350    
351                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
352    
353                    for (AssetCategory category : categories) {
354                            String categoryJSON = JSONFactoryUtil.looseSerialize(category);
355    
356                            JSONObject categoryJSONObject = JSONFactoryUtil.createJSONObject(
357                                    categoryJSON);
358    
359                            List<String> names = new ArrayList<String>();
360    
361                            AssetCategory curCategory = category;
362    
363                            while (curCategory.getParentCategoryId() > 0) {
364                                    AssetCategory parentCategory = getCategory(
365                                            curCategory.getParentCategoryId());
366    
367                                    names.add(parentCategory.getName());
368                                    names.add(
369                                            StringPool.SPACE + StringPool.GREATER_THAN +
370                                                    StringPool.SPACE);
371    
372                                    curCategory = parentCategory;
373                            }
374    
375                            Collections.reverse(names);
376    
377                            AssetVocabulary vocabulary = assetVocabularyService.getVocabulary(
378                                    category.getVocabularyId());
379    
380                            StringBundler sb = new StringBundler(1 + names.size());
381    
382                            sb.append(vocabulary.getName());
383                            sb.append(names.toArray(new String[names.size()]));
384    
385                            categoryJSONObject.put("path", sb.toString());
386    
387                            jsonArray.put(categoryJSONObject);
388                    }
389    
390                    return jsonArray;
391            }
392    
393    }