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