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.util.ListUtil;
20  import com.liferay.portal.security.permission.ActionKeys;
21  import com.liferay.portal.security.permission.PermissionChecker;
22  import com.liferay.portal.service.ServiceContext;
23  import com.liferay.portlet.asset.model.AssetVocabulary;
24  import com.liferay.portlet.asset.service.base.AssetVocabularyServiceBaseImpl;
25  import com.liferay.portlet.asset.service.permission.AssetPermission;
26  import com.liferay.portlet.asset.service.permission.AssetVocabularyPermission;
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="AssetVocabularyServiceImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Alvaro del Castillo
37   * @author Eduardo Lundgren
38   * @author Jorge Ferrer
39   */
40  public class AssetVocabularyServiceImpl
41      extends AssetVocabularyServiceBaseImpl {
42  
43      public AssetVocabulary addVocabulary(
44              Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
45              String settings, ServiceContext serviceContext)
46          throws PortalException, SystemException {
47  
48          AssetPermission.check(
49              getPermissionChecker(), serviceContext.getScopeGroupId(),
50              ActionKeys.ADD_VOCABULARY);
51  
52          return assetVocabularyLocalService.addVocabulary(
53              null, getUserId(), titleMap, descriptionMap, settings,
54                  serviceContext);
55      }
56  
57      public void deleteVocabulary(long vocabularyId)
58          throws PortalException, SystemException {
59  
60          AssetVocabularyPermission.check(
61              getPermissionChecker(), vocabularyId, ActionKeys.DELETE);
62  
63          assetVocabularyLocalService.deleteVocabulary(vocabularyId);
64      }
65  
66      public List<AssetVocabulary> getCompanyVocabularies(long companyId)
67          throws PortalException, SystemException {
68  
69          return filterVocabularies(
70              assetVocabularyLocalService.getCompanyVocabularies(companyId));
71      }
72  
73      public List<AssetVocabulary> getGroupsVocabularies(long[] groupIds)
74          throws PortalException, SystemException {
75  
76          return filterVocabularies(
77              assetVocabularyLocalService.getGroupsVocabularies(groupIds));
78      }
79  
80      public List<AssetVocabulary> getGroupVocabularies(long groupId)
81          throws PortalException, SystemException {
82  
83          return filterVocabularies(
84              assetVocabularyLocalService.getGroupVocabularies(groupId));
85      }
86  
87      public AssetVocabulary getVocabulary(long vocabularyId)
88          throws PortalException, SystemException {
89  
90          AssetVocabularyPermission.check(
91              getPermissionChecker(), vocabularyId, ActionKeys.VIEW);
92  
93          return assetVocabularyLocalService.getVocabulary(vocabularyId);
94      }
95  
96      public AssetVocabulary updateVocabulary(
97              long vocabularyId, Map<Locale, String> titleMap,
98              Map<Locale, String> descriptionMap, String settings,
99              ServiceContext serviceContext)
100         throws PortalException, SystemException {
101 
102         AssetVocabularyPermission.check(
103             getPermissionChecker(), vocabularyId, ActionKeys.UPDATE);
104 
105         return assetVocabularyLocalService.updateVocabulary(
106             vocabularyId, titleMap, descriptionMap, settings, serviceContext);
107     }
108 
109     protected List<AssetVocabulary> filterVocabularies(
110             List<AssetVocabulary> vocabularies)
111         throws PortalException {
112 
113         PermissionChecker permissionChecker = getPermissionChecker();
114 
115         vocabularies = ListUtil.copy(vocabularies);
116 
117         Iterator<AssetVocabulary> itr = vocabularies.iterator();
118 
119         while (itr.hasNext()) {
120             AssetVocabulary vocabulary = itr.next();
121 
122             if (!AssetVocabularyPermission.contains(
123                     permissionChecker, vocabulary, ActionKeys.VIEW)) {
124 
125                 itr.remove();
126             }
127         }
128 
129         return vocabularies;
130     }
131 
132 }