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.LinkedHashMap;
39  import java.util.List;
40  
41  /**
42   * <a href="UserGroupLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Charles May
45   *
46   */
47  public class UserGroupLocalServiceImpl extends UserGroupLocalServiceBaseImpl {
48  
49      public void addGroupUserGroups(long groupId, long[] userGroupIds)
50          throws SystemException {
51  
52          groupPersistence.addUserGroups(groupId, userGroupIds);
53  
54          PermissionCacheUtil.clearCache();
55      }
56  
57      public UserGroup addUserGroup(
58              long userId, long companyId, String name, String description)
59          throws PortalException, SystemException {
60  
61          // User Group
62  
63          validate(0, companyId, name);
64  
65          long userGroupId = counterLocalService.increment();
66  
67          UserGroup userGroup = userGroupPersistence.create(userGroupId);
68  
69          userGroup.setCompanyId(companyId);
70          userGroup.setParentUserGroupId(
71              UserGroupImpl.DEFAULT_PARENT_USER_GROUP_ID);
72          userGroup.setName(name);
73          userGroup.setDescription(description);
74  
75          userGroupPersistence.update(userGroup, false);
76  
77          // Group
78  
79          groupLocalService.addGroup(
80              userId, UserGroup.class.getName(), userGroup.getUserGroupId(), null,
81              null, 0, null, true);
82  
83          // Resources
84  
85          resourceLocalService.addResources(
86              companyId, 0, userId, UserGroup.class.getName(),
87              userGroup.getUserGroupId(), false, false, false);
88  
89          return userGroup;
90      }
91  
92      public void clearUserUserGroups(long userId) throws SystemException {
93          userPersistence.clearUserGroups(userId);
94  
95          PermissionCacheUtil.clearCache();
96      }
97  
98      public void deleteUserGroup(long userGroupId)
99          throws PortalException, SystemException {
100 
101         UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
102             userGroupId);
103 
104         if (userLocalService.getUserGroupUsersCount(userGroupId, true) > 0) {
105             throw new RequiredUserGroupException();
106         }
107 
108         // Users
109 
110         clearUserUserGroups(userGroupId);
111 
112         // Group
113 
114         Group group = userGroup.getGroup();
115 
116         groupLocalService.deleteGroup(group.getGroupId());
117 
118         // Resources
119 
120         resourceLocalService.deleteResource(
121             userGroup.getCompanyId(), UserGroup.class.getName(),
122             ResourceConstants.SCOPE_INDIVIDUAL, userGroup.getUserGroupId());
123 
124         // User Group
125 
126         userGroupPersistence.remove(userGroupId);
127 
128         // Permission cache
129 
130         PermissionCacheUtil.clearCache();
131     }
132 
133     public UserGroup getUserGroup(long userGroupId)
134         throws PortalException, SystemException {
135 
136         return userGroupPersistence.findByPrimaryKey(userGroupId);
137     }
138 
139     public UserGroup getUserGroup(long companyId, String name)
140         throws PortalException, SystemException {
141 
142         return userGroupPersistence.findByC_N(companyId, name);
143     }
144 
145     public List<UserGroup> getUserGroups(long companyId)
146         throws SystemException {
147 
148         return userGroupPersistence.findByCompanyId(companyId);
149     }
150 
151     public List<UserGroup> getUserUserGroups(long userId)
152         throws SystemException {
153 
154         return userPersistence.getUserGroups(userId);
155     }
156 
157     public boolean hasGroupUserGroup(long groupId, long userGroupId)
158         throws SystemException {
159 
160         return groupPersistence.containsUserGroup(groupId, userGroupId);
161     }
162 
163     public List<UserGroup> search(
164             long companyId, String name, String description,
165             LinkedHashMap<String, Object> params, int start, int end,
166             OrderByComparator obc)
167         throws SystemException {
168 
169         return userGroupFinder.findByC_N_D(
170             companyId, name, description, params, start, end, obc);
171     }
172 
173     public int searchCount(
174             long companyId, String name, String description,
175             LinkedHashMap<String, Object> 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 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, false);
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 }