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