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.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  public class UserGroupLocalServiceImpl extends UserGroupLocalServiceBaseImpl {
50  
51      public void addGroupUserGroups(long groupId, long[] userGroupIds)
52          throws SystemException {
53  
54          groupPersistence.addUserGroups(groupId, userGroupIds);
55  
56          PermissionCacheUtil.clearCache();
57      }
58  
59      public UserGroup addUserGroup(
60              long userId, long companyId, String name, String description)
61          throws PortalException, SystemException {
62  
63          // User Group
64  
65          validate(0, companyId, name);
66  
67          long userGroupId = counterLocalService.increment();
68  
69          UserGroup userGroup = userGroupPersistence.create(userGroupId);
70  
71          userGroup.setCompanyId(companyId);
72          userGroup.setParentUserGroupId(
73              UserGroupImpl.DEFAULT_PARENT_USER_GROUP_ID);
74          userGroup.setName(name);
75          userGroup.setDescription(description);
76  
77          userGroupPersistence.update(userGroup, false);
78  
79          // Group
80  
81          groupLocalService.addGroup(
82              userId, UserGroup.class.getName(), userGroup.getUserGroupId(), null,
83              null, 0, null, true);
84  
85          // Resources
86  
87          resourceLocalService.addResources(
88              companyId, 0, userId, UserGroup.class.getName(),
89              userGroup.getUserGroupId(), false, false, false);
90  
91          return userGroup;
92      }
93  
94      public void clearUserUserGroups(long userId) throws SystemException {
95          userPersistence.clearUserGroups(userId);
96  
97          PermissionCacheUtil.clearCache();
98      }
99  
100     public void deleteUserGroup(long userGroupId)
101         throws PortalException, SystemException {
102 
103         UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
104             userGroupId);
105 
106         if (userLocalService.getUserGroupUsersCount(userGroupId, true) > 0) {
107             throw new RequiredUserGroupException();
108         }
109 
110         // Users
111 
112         clearUserUserGroups(userGroupId);
113 
114         // Group
115 
116         Group group = userGroup.getGroup();
117 
118         groupLocalService.deleteGroup(group.getGroupId());
119 
120         // Resources
121 
122         resourceLocalService.deleteResource(
123             userGroup.getCompanyId(), UserGroup.class.getName(),
124             ResourceConstants.SCOPE_INDIVIDUAL, userGroup.getUserGroupId());
125 
126         // User Group
127 
128         userGroupPersistence.remove(userGroupId);
129 
130         // Permission cache
131 
132         PermissionCacheUtil.clearCache();
133     }
134 
135     public UserGroup getUserGroup(long userGroupId)
136         throws PortalException, SystemException {
137 
138         return userGroupPersistence.findByPrimaryKey(userGroupId);
139     }
140 
141     public UserGroup getUserGroup(long companyId, String name)
142         throws PortalException, SystemException {
143 
144         return userGroupPersistence.findByC_N(companyId, name);
145     }
146 
147     public List<UserGroup> getUserGroups(long companyId)
148         throws SystemException {
149 
150         return userGroupPersistence.findByCompanyId(companyId);
151     }
152 
153     public List<UserGroup> getUserUserGroups(long userId)
154         throws SystemException {
155 
156         return userPersistence.getUserGroups(userId);
157     }
158 
159     public boolean hasGroupUserGroup(long groupId, long userGroupId)
160         throws SystemException {
161 
162         return groupPersistence.containsUserGroup(groupId, userGroupId);
163     }
164 
165     public List<UserGroup> search(
166             long companyId, String name, String description,
167             LinkedHashMap<String, Object> params, int start, int end,
168             OrderByComparator obc)
169         throws SystemException {
170 
171         return userGroupFinder.findByC_N_D(
172             companyId, name, description, params, start, end, obc);
173     }
174 
175     public int searchCount(
176             long companyId, String name, String description,
177             LinkedHashMap<String, Object> params)
178         throws SystemException {
179 
180         return userGroupFinder.countByC_N_D(
181             companyId, name, description, params);
182     }
183 
184     public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
185         throws SystemException {
186 
187         groupPersistence.removeUserGroups(groupId, userGroupIds);
188 
189         PermissionCacheUtil.clearCache();
190     }
191 
192     public UserGroup updateUserGroup(
193             long companyId, long userGroupId, String name,
194             String description)
195         throws PortalException, SystemException {
196 
197         validate(userGroupId, companyId, name);
198 
199         UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
200             userGroupId);
201 
202         userGroup.setName(name);
203         userGroup.setDescription(description);
204 
205         userGroupPersistence.update(userGroup, false);
206 
207         return userGroup;
208     }
209 
210     protected void validate(long userGroupId, long companyId, String name)
211         throws PortalException, SystemException {
212 
213         if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
214             (name.indexOf(StringPool.COMMA) != -1) ||
215             (name.indexOf(StringPool.STAR) != -1)) {
216 
217             throw new UserGroupNameException();
218         }
219 
220         try {
221             UserGroup userGroup = userGroupFinder.findByC_N(companyId, name);
222 
223             if (userGroup.getUserGroupId() != userGroupId) {
224                 throw new DuplicateUserGroupException();
225             }
226         }
227         catch (NoSuchUserGroupException nsuge) {
228         }
229     }
230 
231 }