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         // Entries
136 
137         tagsEntryLocalService.deleteVocabularyEntries(
138             vocabulary.getVocabularyId());
139 
140         // Resources
141 
142         resourceLocalService.deleteResource(
143             vocabulary.getCompanyId(), TagsVocabulary.class.getName(),
144             ResourceConstants.SCOPE_INDIVIDUAL, vocabulary.getVocabularyId());
145 
146         tagsVocabularyPersistence.remove(vocabulary);
147     }
148 
149     public List<TagsVocabulary> getCompanyVocabularies(
150             long companyId, boolean folksonomy)
151         throws SystemException {
152 
153         return tagsVocabularyPersistence.findByC_F(companyId, folksonomy);
154     }
155 
156     public List<TagsVocabulary> getGroupVocabularies(
157             long groupId, boolean folksonomy)
158         throws SystemException {
159 
160         return tagsVocabularyPersistence.findByG_F(groupId, folksonomy);
161     }
162 
163     public TagsVocabulary getGroupVocabulary(long groupId, String name)
164         throws PortalException, SystemException {
165 
166         return tagsVocabularyPersistence.findByG_N(groupId, name);
167     }
168 
169     public TagsVocabulary getVocabulary(long vocabularyId)
170         throws PortalException, SystemException {
171 
172         return tagsVocabularyPersistence.findByPrimaryKey(vocabularyId);
173     }
174 
175     public TagsVocabulary updateVocabulary(
176             long vocabularyId, String name, boolean folksonomy)
177         throws PortalException, SystemException {
178 
179         name = name.trim();
180 
181         TagsVocabulary vocabulary = tagsVocabularyPersistence.findByPrimaryKey(
182             vocabularyId);
183 
184         if (!vocabulary.getName().equals(name) &&
185             hasVocabulary(vocabulary.getGroupId(), name)) {
186 
187             throw new DuplicateVocabularyException(name);
188         }
189 
190         vocabulary.setModifiedDate(new Date());
191         vocabulary.setName(name);
192         vocabulary.setFolksonomy(folksonomy);
193 
194         tagsVocabularyPersistence.update(vocabulary, false);
195 
196         return vocabulary;
197     }
198 
199     protected boolean hasVocabulary(long groupId, String name)
200         throws SystemException {
201 
202         if (tagsVocabularyPersistence.countByG_N(groupId, name) == 0) {
203             return false;
204         }
205         else {
206             return true;
207         }
208     }
209 
210 }