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