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