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