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.util.ListUtil;
021    import com.liferay.portal.kernel.util.OrderByComparator;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portlet.asset.model.AssetCategory;
026    import com.liferay.portlet.asset.service.base.AssetCategoryServiceBaseImpl;
027    import com.liferay.portlet.asset.service.permission.AssetCategoryPermission;
028    
029    import java.util.Iterator;
030    import java.util.List;
031    import java.util.Locale;
032    import java.util.Map;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     * @author Jorge Ferrer
037     * @author Alvaro del Castillo
038     * @author Eduardo Lundgren
039     * @author Bruno Farache
040     */
041    public class AssetCategoryServiceImpl extends AssetCategoryServiceBaseImpl {
042    
043            public AssetCategory addCategory(
044                            long parentCategoryId, Map<Locale, String> titleMap,
045                            long vocabularyId, String[] categoryProperties,
046                            ServiceContext serviceContext)
047                    throws PortalException, SystemException {
048    
049                    AssetCategoryPermission.check(
050                            getPermissionChecker(), serviceContext.getScopeGroupId(),
051                            parentCategoryId, ActionKeys.ADD_CATEGORY);
052    
053                    return assetCategoryLocalService.addCategory(
054                            getUserId(), parentCategoryId, titleMap, vocabularyId,
055                            categoryProperties, serviceContext);
056            }
057    
058            public void deleteCategory(long categoryId)
059                    throws PortalException, SystemException {
060    
061                    AssetCategoryPermission.check(
062                            getPermissionChecker(), categoryId, ActionKeys.DELETE);
063    
064                    assetCategoryLocalService.deleteCategory(categoryId);
065            }
066    
067            public List<AssetCategory> getCategories(String className, long classPK)
068                    throws PortalException, SystemException {
069    
070                    return filterCategories(
071                            assetCategoryLocalService.getCategories(className, classPK));
072            }
073    
074            public AssetCategory getCategory(long categoryId)
075                    throws PortalException, SystemException {
076    
077                    AssetCategoryPermission.check(
078                            getPermissionChecker(), categoryId, ActionKeys.VIEW);
079    
080                    return assetCategoryLocalService.getCategory(categoryId);
081            }
082    
083            public List<AssetCategory> getChildCategories(
084                            long parentCategoryId, int start, int end, OrderByComparator obc)
085                    throws PortalException, SystemException {
086    
087                    return filterCategories(
088                            assetCategoryLocalService.getChildCategories(
089                                    parentCategoryId, start, end, obc));
090            }
091    
092            public List<AssetCategory> getVocabularyCategories(
093                            long vocabularyId, int start, int end, OrderByComparator obc)
094                    throws PortalException, SystemException {
095    
096                    return filterCategories(
097                            assetCategoryLocalService.getVocabularyCategories(
098                                    vocabularyId, start, end, obc));
099            }
100    
101            public List<AssetCategory> getVocabularyRootCategories(
102                            long vocabularyId, int start, int end, OrderByComparator obc)
103                    throws PortalException, SystemException {
104    
105                    return filterCategories(
106                            assetCategoryLocalService.getVocabularyRootCategories(
107                                    vocabularyId, start, end, obc));
108            }
109    
110            public JSONArray search(
111                            long groupId, String name, String[] categoryProperties, int start,
112                            int end)
113                    throws SystemException {
114    
115                    return assetCategoryLocalService.search(
116                            groupId, name, categoryProperties, start, end);
117            }
118    
119            public AssetCategory updateCategory(
120                            long categoryId, long parentCategoryId,
121                            Map<Locale, String> titleMap, long vocabularyId,
122                            String[] categoryProperties, ServiceContext serviceContext)
123                    throws PortalException, SystemException {
124    
125                    AssetCategoryPermission.check(
126                            getPermissionChecker(), categoryId, ActionKeys.UPDATE);
127    
128                    return assetCategoryLocalService.updateCategory(
129                            getUserId(), categoryId, parentCategoryId, titleMap, vocabularyId,
130                            categoryProperties, serviceContext);
131            }
132    
133            protected List<AssetCategory> filterCategories(
134                            List<AssetCategory> categories)
135                    throws PortalException {
136    
137                    PermissionChecker permissionChecker = getPermissionChecker();
138    
139                    categories = ListUtil.copy(categories);
140    
141                    Iterator<AssetCategory> itr = categories.iterator();
142    
143                    while (itr.hasNext()) {
144                            AssetCategory category = itr.next();
145    
146                            if (!AssetCategoryPermission.contains(
147                                            permissionChecker, category, ActionKeys.VIEW)) {
148    
149                                    itr.remove();
150                            }
151                    }
152    
153                    return categories;
154            }
155    
156    }