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.model.ResourceConstants;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.service.ServiceContext;
30  import com.liferay.portlet.tags.DuplicateVocabularyException;
31  import com.liferay.portlet.tags.model.TagsVocabulary;
32  import com.liferay.portlet.tags.service.base.TagsVocabularyLocalServiceBaseImpl;
33  
34  import java.util.Date;
35  import java.util.List;
36  
37  /**
38   * <a href="TagsVocabularyLocalServiceImpl.java.html"><b><i>View Source</i></b>
39   * </a>
40   *
41   * @author Alvaro del Castillo
42   * @author Eduardo Lundgren
43   *
44   */
45  public class TagsVocabularyLocalServiceImpl
46      extends TagsVocabularyLocalServiceBaseImpl {
47  
48      public TagsVocabulary addVocabulary(
49              long userId, String name, boolean folksonomy,
50              ServiceContext serviceContext)
51          throws PortalException, SystemException {
52  
53          // Vocabulary
54  
55          User user = userPersistence.findByPrimaryKey(userId);
56          long groupId = serviceContext.getScopeGroupId();
57          name = name.trim();
58          Date now = new Date();
59  
60          if (hasVocabulary(groupId, name)) {
61              throw new DuplicateVocabularyException(
62                  "A vocabulary with the name " + name + " already exists");
63          }
64  
65          long vocabularyId = counterLocalService.increment();
66  
67          TagsVocabulary vocabulary = tagsVocabularyPersistence.create(
68              vocabularyId);
69  
70          vocabulary.setGroupId(groupId);
71          vocabulary.setCompanyId(user.getCompanyId());
72          vocabulary.setUserId(user.getUserId());
73          vocabulary.setUserName(user.getFullName());
74          vocabulary.setCreateDate(now);
75          vocabulary.setModifiedDate(now);
76          vocabulary.setName(name);
77          vocabulary.setFolksonomy(folksonomy);
78  
79          tagsVocabularyPersistence.update(vocabulary, false);
80  
81          // Resources
82  
83          if (serviceContext.getAddCommunityPermissions() ||
84              serviceContext.getAddGuestPermissions()) {
85  
86              addVocabularyResources(
87                  vocabulary, serviceContext.getAddCommunityPermissions(),
88                  serviceContext.getAddGuestPermissions());
89          }
90          else {
91              addVocabularyResources(
92                  vocabulary, serviceContext.getCommunityPermissions(),
93                  serviceContext.getGuestPermissions());
94          }
95  
96          return vocabulary;
97  
98      }
99  
100     public void addVocabularyResources(
101             TagsVocabulary vocabulary, boolean addCommunityPermissions,
102             boolean addGuestPermissions)
103         throws PortalException, SystemException {
104 
105         resourceLocalService.addResources(
106             vocabulary.getCompanyId(), vocabulary.getGroupId(),
107             vocabulary.getUserId(), TagsVocabulary.class.getName(),
108             vocabulary.getVocabularyId(), false, addCommunityPermissions,
109             addGuestPermissions);
110     }
111 
112     public void addVocabularyResources(
113             TagsVocabulary vocabulary, String[] communityPermissions,
114             String[] guestPermissions)
115         throws PortalException, SystemException {
116 
117         resourceLocalService.addModelResources(
118             vocabulary.getCompanyId(), vocabulary.getGroupId(),
119             vocabulary.getUserId(), TagsVocabulary.class.getName(),
120             vocabulary.getVocabularyId(), communityPermissions,
121             guestPermissions);
122     }
123 
124     public void deleteVocabulary(long vocabularyId)
125         throws PortalException, SystemException {
126 
127         TagsVocabulary vocabulary = tagsVocabularyPersistence.findByPrimaryKey(
128             vocabularyId);
129 
130         deleteVocabulary(vocabulary);
131     }
132 
133     public void deleteVocabulary(TagsVocabulary vocabulary)
134         throws PortalException, SystemException {
135 
136         // Entries
137 
138         tagsEntryLocalService.deleteVocabularyEntries(
139             vocabulary.getVocabularyId());
140 
141         // Resources
142 
143         resourceLocalService.deleteResource(
144             vocabulary.getCompanyId(), TagsVocabulary.class.getName(),
145             ResourceConstants.SCOPE_INDIVIDUAL, vocabulary.getVocabularyId());
146 
147         tagsVocabularyPersistence.remove(vocabulary);
148     }
149 
150     public List<TagsVocabulary> getCompanyVocabularies(
151             long companyId, boolean folksonomy)
152         throws SystemException {
153 
154         return tagsVocabularyPersistence.findByC_F(companyId, folksonomy);
155     }
156 
157     public List<TagsVocabulary> getGroupVocabularies(
158             long groupId, boolean folksonomy)
159         throws SystemException {
160 
161         return tagsVocabularyPersistence.findByG_F(groupId, folksonomy);
162     }
163 
164     public TagsVocabulary getGroupVocabulary(long groupId, String name)
165         throws PortalException, SystemException {
166 
167         return tagsVocabularyPersistence.findByG_N(groupId, name);
168     }
169 
170     public TagsVocabulary getVocabulary(long vocabularyId)
171         throws PortalException, SystemException {
172 
173         return tagsVocabularyPersistence.findByPrimaryKey(vocabularyId);
174     }
175 
176     public TagsVocabulary updateVocabulary(
177             long vocabularyId, String name, boolean folksonomy)
178         throws PortalException, SystemException {
179 
180         name = name.trim();
181 
182         TagsVocabulary vocabulary = tagsVocabularyPersistence.findByPrimaryKey(
183             vocabularyId);
184 
185         if (!vocabulary.getName().equals(name) &&
186             hasVocabulary(vocabulary.getGroupId(), name)) {
187 
188             throw new DuplicateVocabularyException(name);
189         }
190 
191         vocabulary.setModifiedDate(new Date());
192         vocabulary.setName(name);
193         vocabulary.setFolksonomy(folksonomy);
194 
195         tagsVocabularyPersistence.update(vocabulary, false);
196 
197         return vocabulary;
198     }
199 
200     protected boolean hasVocabulary(long groupId, String name)
201         throws SystemException {
202 
203         if (tagsVocabularyPersistence.countByG_N(groupId, name) == 0) {
204             return false;
205         }
206         else {
207             return true;
208         }
209     }
210 
211 }