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.portal.service.impl;
24  
25  import com.liferay.portal.DuplicateUserGroupException;
26  import com.liferay.portal.NoSuchUserGroupException;
27  import com.liferay.portal.PortalException;
28  import com.liferay.portal.RequiredUserGroupException;
29  import com.liferay.portal.SystemException;
30  import com.liferay.portal.UserGroupNameException;
31  import com.liferay.portal.kernel.util.OrderByComparator;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.model.Group;
35  import com.liferay.portal.model.ResourceConstants;
36  import com.liferay.portal.model.UserGroup;
37  import com.liferay.portal.model.impl.UserGroupImpl;
38  import com.liferay.portal.security.permission.PermissionCacheUtil;
39  import com.liferay.portal.service.base.UserGroupLocalServiceBaseImpl;
40  
41  import java.util.ArrayList;
42  import java.util.LinkedHashMap;
43  import java.util.List;
44  
45  /**
46   * <a href="UserGroupLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Charles May
49   *
50   */
51  public class UserGroupLocalServiceImpl extends UserGroupLocalServiceBaseImpl {
52  
53      public void addGroupUserGroups(long groupId, long[] userGroupIds)
54          throws SystemException {
55  
56          groupPersistence.addUserGroups(groupId, userGroupIds);
57  
58          PermissionCacheUtil.clearCache();
59      }
60  
61      public UserGroup addUserGroup(
62              long userId, long companyId, String name, String description)
63          throws PortalException, SystemException {
64  
65          // User Group
66  
67          validate(0, companyId, name);
68  
69          long userGroupId = counterLocalService.increment();
70  
71          UserGroup userGroup = userGroupPersistence.create(userGroupId);
72  
73          userGroup.setCompanyId(companyId);
74          userGroup.setParentUserGroupId(
75              UserGroupImpl.DEFAULT_PARENT_USER_GROUP_ID);
76          userGroup.setName(name);
77          userGroup.setDescription(description);
78  
79          userGroupPersistence.update(userGroup, false);
80  
81          // Group
82  
83          groupLocalService.addGroup(
84              userId, UserGroup.class.getName(), userGroup.getUserGroupId(),
85              String.valueOf(userGroupId), null, 0, null, true);
86  
87          // Resources
88  
89          resourceLocalService.addResources(
90              companyId, 0, userId, UserGroup.class.getName(),
91              userGroup.getUserGroupId(), false, false, false);
92  
93          return userGroup;
94      }
95  
96      public void clearUserUserGroups(long userId) throws SystemException {
97          userPersistence.clearUserGroups(userId);
98  
99          PermissionCacheUtil.clearCache();
100     }
101 
102     public void deleteUserGroup(long userGroupId)
103         throws PortalException, SystemException {
104 
105         UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
106             userGroupId);
107 
108         if (userLocalService.getUserGroupUsersCount(userGroupId, true) > 0) {
109             throw new RequiredUserGroupException();
110         }
111 
112         // Users
113 
114         clearUserUserGroups(userGroupId);
115 
116         // Group
117 
118         Group group = userGroup.getGroup();
119 
120         groupLocalService.deleteGroup(group.getGroupId());
121 
122         // Resources
123 
124         resourceLocalService.deleteResource(
125             userGroup.getCompanyId(), UserGroup.class.getName(),
126             ResourceConstants.SCOPE_INDIVIDUAL, userGroup.getUserGroupId());
127 
128         // User Group
129 
130         userGroupPersistence.remove(userGroupId);
131 
132         // Permission cache
133 
134         PermissionCacheUtil.clearCache();
135     }
136 
137     public UserGroup getUserGroup(long userGroupId)
138         throws PortalException, SystemException {
139 
140         return userGroupPersistence.findByPrimaryKey(userGroupId);
141     }
142 
143     public UserGroup getUserGroup(long companyId, String name)
144         throws PortalException, SystemException {
145 
146         return userGroupFinder.findByC_N(companyId, name);
147     }
148 
149     public List<UserGroup> getUserGroups(long companyId)
150         throws SystemException {
151 
152         return userGroupPersistence.findByCompanyId(companyId);
153     }
154 
155     public List<UserGroup> getUserGroups(long[] userGroupIds)
156         throws PortalException, SystemException {
157 
158         List<UserGroup> userGroups = new ArrayList<UserGroup>(
159             userGroupIds.length);
160 
161         for (long userGroupId : userGroupIds) {
162             UserGroup userGroup = getUserGroup(userGroupId);
163 
164             userGroups.add(userGroup);
165         }
166 
167         return userGroups;
168     }
169 
170     public List<UserGroup> getUserUserGroups(long userId)
171         throws SystemException {
172 
173         return userPersistence.getUserGroups(userId);
174     }
175 
176     public boolean hasGroupUserGroup(long groupId, long userGroupId)
177         throws SystemException {
178 
179         return groupPersistence.containsUserGroup(groupId, userGroupId);
180     }
181 
182     public List<UserGroup> search(
183             long companyId, String name, String description,
184             LinkedHashMap<String, Object> params, int start, int end,
185             OrderByComparator obc)
186         throws SystemException {
187 
188         return userGroupFinder.findByC_N_D(
189             companyId, name, description, params, start, end, obc);
190     }
191 
192     public int searchCount(
193             long companyId, String name, String description,
194             LinkedHashMap<String, Object> params)
195         throws SystemException {
196 
197         return userGroupFinder.countByC_N_D(
198             companyId, name, description, params);
199     }
200 
201     public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
202         throws SystemException {
203 
204         groupPersistence.removeUserGroups(groupId, userGroupIds);
205 
206         PermissionCacheUtil.clearCache();
207     }
208 
209     public UserGroup updateUserGroup(
210             long companyId, long userGroupId, String name,
211             String description)
212         throws PortalException, SystemException {
213 
214         validate(userGroupId, companyId, name);
215 
216         UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
217             userGroupId);
218 
219         userGroup.setName(name);
220         userGroup.setDescription(description);
221 
222         userGroupPersistence.update(userGroup, false);
223 
224         return userGroup;
225     }
226 
227     protected void validate(long userGroupId, long companyId, String name)
228         throws PortalException, SystemException {
229 
230         if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
231             (name.indexOf(StringPool.COMMA) != -1) ||
232             (name.indexOf(StringPool.STAR) != -1)) {
233 
234             throw new UserGroupNameException();
235         }
236 
237         try {
238             UserGroup userGroup = userGroupFinder.findByC_N(companyId, name);
239 
240             if (userGroup.getUserGroupId() != userGroupId) {
241                 throw new DuplicateUserGroupException();
242             }
243         }
244         catch (NoSuchUserGroupException nsuge) {
245         }
246     }
247 
248 }