1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  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(),
84              String.valueOf(userGroupId), null, 0, null, true, null);
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 userGroupPersistence.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> getUserGroups(long[] userGroupIds)
155         throws PortalException, SystemException {
156 
157         List<UserGroup> userGroups = new ArrayList<UserGroup>(
158             userGroupIds.length);
159 
160         for (long userGroupId : userGroupIds) {
161             UserGroup userGroup = getUserGroup(userGroupId);
162 
163             userGroups.add(userGroup);
164         }
165 
166         return userGroups;
167     }
168 
169     public List<UserGroup> getUserUserGroups(long userId)
170         throws SystemException {
171 
172         return userPersistence.getUserGroups(userId);
173     }
174 
175     public boolean hasGroupUserGroup(long groupId, long userGroupId)
176         throws SystemException {
177 
178         return groupPersistence.containsUserGroup(groupId, userGroupId);
179     }
180 
181     public List<UserGroup> search(
182             long companyId, String name, String description,
183             LinkedHashMap<String, Object> params, int start, int end,
184             OrderByComparator obc)
185         throws SystemException {
186 
187         return userGroupFinder.findByC_N_D(
188             companyId, name, description, params, start, end, obc);
189     }
190 
191     public int searchCount(
192             long companyId, String name, String description,
193             LinkedHashMap<String, Object> params)
194         throws SystemException {
195 
196         return userGroupFinder.countByC_N_D(
197             companyId, name, description, params);
198     }
199 
200     public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
201         throws SystemException {
202 
203         groupPersistence.removeUserGroups(groupId, userGroupIds);
204 
205         PermissionCacheUtil.clearCache();
206     }
207 
208     public UserGroup updateUserGroup(
209             long companyId, long userGroupId, String name,
210             String description)
211         throws PortalException, SystemException {
212 
213         validate(userGroupId, companyId, name);
214 
215         UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
216             userGroupId);
217 
218         userGroup.setName(name);
219         userGroup.setDescription(description);
220 
221         userGroupPersistence.update(userGroup, false);
222 
223         return userGroup;
224     }
225 
226     protected void validate(long userGroupId, long companyId, String name)
227         throws PortalException, SystemException {
228 
229         if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
230             (name.indexOf(StringPool.COMMA) != -1) ||
231             (name.indexOf(StringPool.STAR) != -1)) {
232 
233             throw new UserGroupNameException();
234         }
235 
236         try {
237             UserGroup userGroup = userGroupFinder.findByC_N(companyId, name);
238 
239             if (userGroup.getUserGroupId() != userGroupId) {
240                 throw new DuplicateUserGroupException();
241             }
242         }
243         catch (NoSuchUserGroupException nsuge) {
244         }
245     }
246 
247 }