1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.impl;
21  
22  import com.liferay.portal.DuplicateUserGroupException;
23  import com.liferay.portal.NoSuchUserGroupException;
24  import com.liferay.portal.PortalException;
25  import com.liferay.portal.RequiredUserGroupException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.UserGroupNameException;
28  import com.liferay.portal.kernel.util.OrderByComparator;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Group;
32  import com.liferay.portal.model.ResourceConstants;
33  import com.liferay.portal.model.UserGroup;
34  import com.liferay.portal.model.impl.UserGroupImpl;
35  import com.liferay.portal.security.permission.PermissionCacheUtil;
36  import com.liferay.portal.service.base.UserGroupLocalServiceBaseImpl;
37  
38  import java.util.ArrayList;
39  import java.util.LinkedHashMap;
40  import java.util.List;
41  
42  /**
43   * <a href="UserGroupLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Charles May
46   *
47   */
48  public class UserGroupLocalServiceImpl extends UserGroupLocalServiceBaseImpl {
49  
50      public void addGroupUserGroups(long groupId, long[] userGroupIds)
51          throws SystemException {
52  
53          groupPersistence.addUserGroups(groupId, userGroupIds);
54  
55          PermissionCacheUtil.clearCache();
56      }
57  
58      public UserGroup addUserGroup(
59              long userId, long companyId, String name, String description)
60          throws PortalException, SystemException {
61  
62          // User Group
63  
64          validate(0, companyId, name);
65  
66          long userGroupId = counterLocalService.increment();
67  
68          UserGroup userGroup = userGroupPersistence.create(userGroupId);
69  
70          userGroup.setCompanyId(companyId);
71          userGroup.setParentUserGroupId(
72              UserGroupImpl.DEFAULT_PARENT_USER_GROUP_ID);
73          userGroup.setName(name);
74          userGroup.setDescription(description);
75  
76          userGroupPersistence.update(userGroup, false);
77  
78          // Group
79  
80          groupLocalService.addGroup(
81              userId, UserGroup.class.getName(), userGroup.getUserGroupId(),
82              String.valueOf(userGroupId), null, 0, null, true, null);
83  
84          // Resources
85  
86          resourceLocalService.addResources(
87              companyId, 0, userId, UserGroup.class.getName(),
88              userGroup.getUserGroupId(), false, false, false);
89  
90          return userGroup;
91      }
92  
93      public void clearUserUserGroups(long userId) throws SystemException {
94          userPersistence.clearUserGroups(userId);
95  
96          PermissionCacheUtil.clearCache();
97      }
98  
99      public void deleteUserGroup(long userGroupId)
100         throws PortalException, SystemException {
101 
102         UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
103             userGroupId);
104 
105         if (userLocalService.getUserGroupUsersCount(userGroupId, true) > 0) {
106             throw new RequiredUserGroupException();
107         }
108 
109         // Users
110 
111         clearUserUserGroups(userGroupId);
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             ResourceConstants.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 userGroupPersistence.findByC_N(companyId, name);
144     }
145 
146     public List<UserGroup> getUserGroups(long companyId)
147         throws SystemException {
148 
149         return userGroupPersistence.findByCompanyId(companyId);
150     }
151 
152     public List<UserGroup> getUserGroups(long[] userGroupIds)
153         throws PortalException, SystemException {
154 
155         List<UserGroup> userGroups = new ArrayList<UserGroup>(
156             userGroupIds.length);
157 
158         for (long userGroupId : userGroupIds) {
159             UserGroup userGroup = getUserGroup(userGroupId);
160 
161             userGroups.add(userGroup);
162         }
163 
164         return userGroups;
165     }
166 
167     public List<UserGroup> getUserUserGroups(long userId)
168         throws SystemException {
169 
170         return userPersistence.getUserGroups(userId);
171     }
172 
173     public boolean hasGroupUserGroup(long groupId, long userGroupId)
174         throws SystemException {
175 
176         return groupPersistence.containsUserGroup(groupId, userGroupId);
177     }
178 
179     public List<UserGroup> search(
180             long companyId, String name, String description,
181             LinkedHashMap<String, Object> params, int start, int end,
182             OrderByComparator obc)
183         throws SystemException {
184 
185         return userGroupFinder.findByC_N_D(
186             companyId, name, description, params, start, end, obc);
187     }
188 
189     public int searchCount(
190             long companyId, String name, String description,
191             LinkedHashMap<String, Object> params)
192         throws SystemException {
193 
194         return userGroupFinder.countByC_N_D(
195             companyId, name, description, params);
196     }
197 
198     public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
199         throws SystemException {
200 
201         groupPersistence.removeUserGroups(groupId, userGroupIds);
202 
203         PermissionCacheUtil.clearCache();
204     }
205 
206     public UserGroup updateUserGroup(
207             long companyId, long userGroupId, String name,
208             String description)
209         throws PortalException, SystemException {
210 
211         validate(userGroupId, companyId, name);
212 
213         UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
214             userGroupId);
215 
216         userGroup.setName(name);
217         userGroup.setDescription(description);
218 
219         userGroupPersistence.update(userGroup, false);
220 
221         return userGroup;
222     }
223 
224     protected void validate(long userGroupId, long companyId, String name)
225         throws PortalException, SystemException {
226 
227         if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
228             (name.indexOf(StringPool.COMMA) != -1) ||
229             (name.indexOf(StringPool.STAR) != -1)) {
230 
231             throw new UserGroupNameException();
232         }
233 
234         try {
235             UserGroup userGroup = userGroupFinder.findByC_N(companyId, name);
236 
237             if (userGroup.getUserGroupId() != userGroupId) {
238                 throw new DuplicateUserGroupException();
239             }
240         }
241         catch (NoSuchUserGroupException nsuge) {
242         }
243     }
244 
245 }