1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.asset.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.json.JSONArray;
20  import com.liferay.portal.kernel.util.ListUtil;
21  import com.liferay.portal.security.permission.ActionKeys;
22  import com.liferay.portal.security.permission.PermissionChecker;
23  import com.liferay.portal.service.ServiceContext;
24  import com.liferay.portlet.asset.model.AssetCategory;
25  import com.liferay.portlet.asset.service.base.AssetCategoryServiceBaseImpl;
26  import com.liferay.portlet.asset.service.permission.AssetCategoryPermission;
27  
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Locale;
31  import java.util.Map;
32  
33  /**
34   * <a href="AssetCategoryServiceImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   * @author Jorge Ferrer
38   * @author Alvaro del Castillo
39   * @author Eduardo Lundgren
40   * @author Bruno Farache
41   */
42  public class AssetCategoryServiceImpl extends AssetCategoryServiceBaseImpl {
43  
44      public AssetCategory addCategory(
45              long parentCategoryId, Map<Locale, String> titleMap,
46              long vocabularyId, String[] categoryProperties,
47              ServiceContext serviceContext)
48          throws PortalException, SystemException {
49  
50          AssetCategoryPermission.check(
51              getPermissionChecker(), serviceContext.getScopeGroupId(),
52              parentCategoryId, ActionKeys.ADD_CATEGORY);
53  
54          return assetCategoryLocalService.addCategory(
55              null, getUserId(), parentCategoryId, titleMap, vocabularyId,
56              categoryProperties, serviceContext);
57      }
58  
59      public void deleteCategory(long categoryId)
60          throws PortalException, SystemException {
61  
62          AssetCategoryPermission.check(
63              getPermissionChecker(), categoryId, ActionKeys.DELETE);
64  
65          assetCategoryLocalService.deleteCategory(categoryId);
66      }
67  
68      public List<AssetCategory> getCategories(String className, long classPK)
69          throws PortalException, SystemException {
70  
71          return filterCategories(
72              assetCategoryLocalService.getCategories(className, classPK));
73      }
74  
75      public AssetCategory getCategory(long categoryId)
76          throws PortalException, SystemException {
77  
78          AssetCategoryPermission.check(
79              getPermissionChecker(), categoryId, ActionKeys.VIEW);
80  
81          return assetCategoryLocalService.getCategory(categoryId);
82      }
83  
84      public List<AssetCategory> getChildCategories(long parentCategoryId)
85          throws PortalException, SystemException {
86  
87          return filterCategories(
88              assetCategoryLocalService.getChildCategories(parentCategoryId));
89      }
90  
91      public List<AssetCategory> getVocabularyCategories(long vocabularyId)
92          throws PortalException, SystemException {
93  
94          return filterCategories(
95              assetCategoryLocalService.getVocabularyCategories(vocabularyId));
96      }
97  
98      public List<AssetCategory> getVocabularyRootCategories(long vocabularyId)
99          throws PortalException, SystemException {
100 
101         return filterCategories(
102             assetCategoryLocalService.getVocabularyRootCategories(
103                 vocabularyId));
104     }
105 
106     public JSONArray search(
107             long groupId, String name, String[] categoryProperties, int start,
108             int end)
109         throws SystemException {
110 
111         return assetCategoryLocalService.search(
112             groupId, name, categoryProperties, start, end);
113     }
114 
115     public AssetCategory updateCategory(
116             long categoryId, long parentCategoryId,
117             Map<Locale, String> titleMap, long vocabularyId,
118             String[] categoryProperties, ServiceContext serviceContext)
119         throws PortalException, SystemException {
120 
121         AssetCategoryPermission.check(
122             getPermissionChecker(), categoryId, ActionKeys.UPDATE);
123 
124         return assetCategoryLocalService.updateCategory(
125             getUserId(), categoryId, parentCategoryId, titleMap, vocabularyId,
126             categoryProperties, serviceContext);
127     }
128 
129     protected List<AssetCategory> filterCategories(
130             List<AssetCategory> categories)
131         throws PortalException {
132 
133         PermissionChecker permissionChecker = getPermissionChecker();
134 
135         categories = ListUtil.copy(categories);
136 
137         Iterator<AssetCategory> itr = categories.iterator();
138 
139         while (itr.hasNext()) {
140             AssetCategory category = itr.next();
141 
142             if (!AssetCategoryPermission.contains(
143                     permissionChecker, category, ActionKeys.VIEW)) {
144 
145                 itr.remove();
146             }
147         }
148 
149         return categories;
150     }
151 
152 }