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.UserGroup;
36  import com.liferay.portal.model.impl.ResourceImpl;
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 PortalException, 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);
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)
96          throws PortalException, SystemException {
97  
98          userPersistence.clearUserGroups(userId);
99  
100         PermissionCacheUtil.clearCache();
101     }
102 
103     public void deleteUserGroup(long userGroupId)
104         throws PortalException, SystemException {
105 
106         UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
107             userGroupId);
108 
109         if (userGroupPersistence.containsUsers(userGroup.getUserGroupId())) {
110             throw new RequiredUserGroupException();
111         }
112 
113         // Group
114 
115         Group group = userGroup.getGroup();
116 
117         groupLocalService.deleteGroup(group.getGroupId());
118 
119         // Resources
120 
121         resourceLocalService.deleteResource(
122             userGroup.getCompanyId(), UserGroup.class.getName(),
123             ResourceImpl.SCOPE_INDIVIDUAL, userGroup.getUserGroupId());
124 
125         // User Group
126 
127         userGroupPersistence.remove(userGroupId);
128 
129         // Permission cache
130 
131         PermissionCacheUtil.clearCache();
132     }
133 
134     public UserGroup getUserGroup(long userGroupId)
135         throws PortalException, SystemException {
136 
137         return userGroupPersistence.findByPrimaryKey(userGroupId);
138     }
139 
140     public UserGroup getUserGroup(long companyId, String name)
141         throws PortalException, SystemException {
142 
143         return userGroupFinder.findByC_N(companyId, name);
144     }
145 
146     public List getUserGroups(long companyId)
147         throws PortalException, SystemException {
148 
149         return userGroupPersistence.findByCompanyId(companyId);
150     }
151 
152     public List getUserUserGroups(long userId)
153         throws PortalException, SystemException {
154 
155         return userPersistence.getUserGroups(userId);
156     }
157 
158     public boolean hasGroupUserGroup(long groupId, long userGroupId)
159         throws PortalException, SystemException {
160 
161         return groupPersistence.containsUserGroup(groupId, userGroupId);
162     }
163 
164     public List search(
165             long companyId, String name, String description,
166             LinkedHashMap params, int begin, int end, OrderByComparator obc)
167         throws SystemException {
168 
169         return userGroupFinder.findByC_N_D(
170             companyId, name, description, params, begin, end, obc);
171     }
172 
173     public int searchCount(
174             long companyId, String name, String description,
175             LinkedHashMap params)
176         throws SystemException {
177 
178         return userGroupFinder.countByC_N_D(
179             companyId, name, description, params);
180     }
181 
182     public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
183         throws PortalException, SystemException {
184 
185         groupPersistence.removeUserGroups(groupId, userGroupIds);
186 
187         PermissionCacheUtil.clearCache();
188     }
189 
190     public UserGroup updateUserGroup(
191             long companyId, long userGroupId, String name,
192             String description)
193         throws PortalException, SystemException {
194 
195         validate(userGroupId, companyId, name);
196 
197         UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
198             userGroupId);
199 
200         userGroup.setName(name);
201         userGroup.setDescription(description);
202 
203         userGroupPersistence.update(userGroup);
204 
205         return userGroup;
206     }
207 
208     protected void validate(long userGroupId, long companyId, String name)
209         throws PortalException, SystemException {
210 
211         if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
212             (name.indexOf(StringPool.COMMA) != -1) ||
213             (name.indexOf(StringPool.STAR) != -1)) {
214 
215             throw new UserGroupNameException();
216         }
217 
218         try {
219             UserGroup userGroup = userGroupFinder.findByC_N(companyId, name);
220 
221             if (userGroup.getUserGroupId() != userGroupId) {
222                 throw new DuplicateUserGroupException();
223             }
224         }
225         catch (NoSuchUserGroupException nsuge) {
226         }
227     }
228 
229 }