1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.tags.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.ListUtil;
28  import com.liferay.portal.security.permission.ActionKeys;
29  import com.liferay.portal.security.permission.PermissionChecker;
30  import com.liferay.portal.service.ServiceContext;
31  import com.liferay.portlet.tags.model.TagsVocabulary;
32  import com.liferay.portlet.tags.service.base.TagsVocabularyServiceBaseImpl;
33  import com.liferay.portlet.tags.service.permission.TagsPermission;
34  import com.liferay.portlet.tags.service.permission.TagsVocabularyPermission;
35  
36  import java.util.Iterator;
37  import java.util.List;
38  
39  /**
40   * <a href="TagsVocabularyServiceImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Alvaro del Castillo
43   * @author Eduardo Lundgren
44   */
45  public class TagsVocabularyServiceImpl extends TagsVocabularyServiceBaseImpl {
46  
47      public TagsVocabulary addVocabulary(
48              String name, boolean folksonomy, ServiceContext serviceContext)
49          throws PortalException, SystemException {
50  
51          TagsPermission.check(
52              getPermissionChecker(), serviceContext.getScopeGroupId(),
53              ActionKeys.ADD_VOCABULARY);
54  
55          return tagsVocabularyLocalService.addVocabulary(
56              getUserId(), name, folksonomy, serviceContext);
57      }
58  
59      public void deleteVocabulary(long vocabularyId)
60          throws PortalException, SystemException {
61  
62          TagsVocabularyPermission.check(
63              getPermissionChecker(), vocabularyId, ActionKeys.DELETE);
64  
65          tagsVocabularyLocalService.deleteVocabulary(vocabularyId);
66      }
67  
68      public List<TagsVocabulary> getCompanyVocabularies(
69              long companyId, boolean folksonomy)
70          throws PortalException, SystemException {
71  
72          return getVocabularies(
73              tagsVocabularyLocalService.getCompanyVocabularies(
74                  companyId, folksonomy));
75      }
76  
77      public List<TagsVocabulary> getGroupVocabularies(
78              long groupId, boolean folksonomy)
79          throws PortalException, SystemException {
80  
81          return getVocabularies(
82              tagsVocabularyLocalService.getGroupVocabularies(
83                  groupId, folksonomy));
84      }
85  
86      public TagsVocabulary getVocabulary(long vocabularyId)
87          throws PortalException, SystemException {
88  
89          TagsVocabularyPermission.check(
90              getPermissionChecker(), vocabularyId, ActionKeys.VIEW);
91  
92          return tagsVocabularyLocalService.getVocabulary(vocabularyId);
93      }
94  
95      public TagsVocabulary updateVocabulary(
96              long vocabularyId, String name, boolean folksonomy)
97          throws PortalException, SystemException {
98  
99          TagsVocabularyPermission.check(
100             getPermissionChecker(), vocabularyId, ActionKeys.UPDATE);
101 
102         return tagsVocabularyLocalService.updateVocabulary(
103             vocabularyId, name, folksonomy);
104     }
105 
106     protected List<TagsVocabulary> getVocabularies(
107             List<TagsVocabulary> vocabularies)
108         throws PortalException {
109 
110         PermissionChecker permissionChecker = getPermissionChecker();
111 
112         vocabularies = ListUtil.copy(vocabularies);
113 
114         Iterator<TagsVocabulary> itr = vocabularies.iterator();
115 
116         while (itr.hasNext()) {
117             TagsVocabulary vocabulary = itr.next();
118 
119             if (!TagsVocabularyPermission.contains(
120                     permissionChecker, vocabulary, ActionKeys.VIEW)) {
121 
122                 itr.remove();
123             }
124         }
125 
126         return vocabularies;
127     }
128 
129 }