1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.tags.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.model.ResourceConstants;
20  import com.liferay.portal.model.User;
21  import com.liferay.portal.service.ServiceContext;
22  import com.liferay.portlet.tags.DuplicateVocabularyException;
23  import com.liferay.portlet.tags.model.TagsVocabulary;
24  import com.liferay.portlet.tags.service.base.TagsVocabularyLocalServiceBaseImpl;
25  
26  import java.util.Date;
27  import java.util.List;
28  
29  /**
30   * <a href="TagsVocabularyLocalServiceImpl.java.html"><b><i>View Source</i></b>
31   * </a>
32   *
33   * @author Alvaro del Castillo
34   * @author Eduardo Lundgren
35   */
36  public class TagsVocabularyLocalServiceImpl
37      extends TagsVocabularyLocalServiceBaseImpl {
38  
39      public TagsVocabulary addVocabulary(
40              long userId, String name, boolean folksonomy,
41              ServiceContext serviceContext)
42          throws PortalException, SystemException {
43  
44          // Vocabulary
45  
46          User user = userPersistence.findByPrimaryKey(userId);
47          long groupId = serviceContext.getScopeGroupId();
48          name = name.trim();
49          Date now = new Date();
50  
51          if (hasVocabulary(groupId, name)) {
52              throw new DuplicateVocabularyException(
53                  "A vocabulary with the name " + name + " already exists");
54          }
55  
56          long vocabularyId = counterLocalService.increment();
57  
58          TagsVocabulary vocabulary = tagsVocabularyPersistence.create(
59              vocabularyId);
60  
61          vocabulary.setGroupId(groupId);
62          vocabulary.setCompanyId(user.getCompanyId());
63          vocabulary.setUserId(user.getUserId());
64          vocabulary.setUserName(user.getFullName());
65          vocabulary.setCreateDate(now);
66          vocabulary.setModifiedDate(now);
67          vocabulary.setName(name);
68          vocabulary.setFolksonomy(folksonomy);
69  
70          tagsVocabularyPersistence.update(vocabulary, false);
71  
72          // Resources
73  
74          if (serviceContext.getAddCommunityPermissions() ||
75              serviceContext.getAddGuestPermissions()) {
76  
77              addVocabularyResources(
78                  vocabulary, serviceContext.getAddCommunityPermissions(),
79                  serviceContext.getAddGuestPermissions());
80          }
81          else {
82              addVocabularyResources(
83                  vocabulary, serviceContext.getCommunityPermissions(),
84                  serviceContext.getGuestPermissions());
85          }
86  
87          return vocabulary;
88  
89      }
90  
91      public void addVocabularyResources(
92              TagsVocabulary vocabulary, boolean addCommunityPermissions,
93              boolean addGuestPermissions)
94          throws PortalException, SystemException {
95  
96          resourceLocalService.addResources(
97              vocabulary.getCompanyId(), vocabulary.getGroupId(),
98              vocabulary.getUserId(), TagsVocabulary.class.getName(),
99              vocabulary.getVocabularyId(), false, addCommunityPermissions,
100             addGuestPermissions);
101     }
102 
103     public void addVocabularyResources(
104             TagsVocabulary vocabulary, String[] communityPermissions,
105             String[] guestPermissions)
106         throws PortalException, SystemException {
107 
108         resourceLocalService.addModelResources(
109             vocabulary.getCompanyId(), vocabulary.getGroupId(),
110             vocabulary.getUserId(), TagsVocabulary.class.getName(),
111             vocabulary.getVocabularyId(), communityPermissions,
112             guestPermissions);
113     }
114 
115     public void deleteVocabulary(long vocabularyId)
116         throws PortalException, SystemException {
117 
118         TagsVocabulary vocabulary = tagsVocabularyPersistence.findByPrimaryKey(
119             vocabularyId);
120 
121         deleteVocabulary(vocabulary);
122     }
123 
124     public void deleteVocabulary(TagsVocabulary vocabulary)
125         throws PortalException, SystemException {
126 
127         // Vocabulary
128 
129         tagsVocabularyPersistence.remove(vocabulary);
130 
131         // Resources
132 
133         resourceLocalService.deleteResource(
134             vocabulary.getCompanyId(), TagsVocabulary.class.getName(),
135             ResourceConstants.SCOPE_INDIVIDUAL, vocabulary.getVocabularyId());
136 
137         // Entries
138 
139         tagsEntryLocalService.deleteVocabularyEntries(
140             vocabulary.getVocabularyId());
141     }
142 
143     public List<TagsVocabulary> getCompanyVocabularies(
144             long companyId, boolean folksonomy)
145         throws SystemException {
146 
147         return tagsVocabularyPersistence.findByC_F(companyId, folksonomy);
148     }
149 
150     public List<TagsVocabulary> getGroupVocabularies(
151             long groupId, boolean folksonomy)
152         throws SystemException {
153 
154         return tagsVocabularyPersistence.findByG_F(groupId, folksonomy);
155     }
156 
157     public TagsVocabulary getGroupVocabulary(long groupId, String name)
158         throws PortalException, SystemException {
159 
160         return tagsVocabularyPersistence.findByG_N(groupId, name);
161     }
162 
163     public TagsVocabulary getVocabulary(long vocabularyId)
164         throws PortalException, SystemException {
165 
166         return tagsVocabularyPersistence.findByPrimaryKey(vocabularyId);
167     }
168 
169     public TagsVocabulary updateVocabulary(
170             long vocabularyId, String name, boolean folksonomy)
171         throws PortalException, SystemException {
172 
173         name = name.trim();
174 
175         TagsVocabulary vocabulary = tagsVocabularyPersistence.findByPrimaryKey(
176             vocabularyId);
177 
178         if (!vocabulary.getName().equals(name) &&
179             hasVocabulary(vocabulary.getGroupId(), name)) {
180 
181             throw new DuplicateVocabularyException(name);
182         }
183 
184         vocabulary.setModifiedDate(new Date());
185         vocabulary.setName(name);
186         vocabulary.setFolksonomy(folksonomy);
187 
188         tagsVocabularyPersistence.update(vocabulary, false);
189 
190         return vocabulary;
191     }
192 
193     protected boolean hasVocabulary(long groupId, String name)
194         throws SystemException {
195 
196         if (tagsVocabularyPersistence.countByG_N(groupId, name) == 0) {
197             return false;
198         }
199         else {
200             return true;
201         }
202     }
203 
204 }