1
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
50 public class UserGroupLocalServiceImpl extends UserGroupLocalServiceBaseImpl {
51
52 public void addGroupUserGroups(long groupId, long[] userGroupIds)
53 throws PortalException, 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
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
82 groupLocalService.addGroup(
83 userId, UserGroup.class.getName(), userGroup.getUserGroupId(), null,
84 null, 0, null, true);
85
86
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)
96 throws PortalException, SystemException {
97
98 userPersistence.clearUserGroups(userId);
99
100 PermissionCacheUtil.clearCache();
101 }
102
103 public void deleteUserGroup(long userGroupId)
104 throws PortalException, SystemException {
105
106 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
107 userGroupId);
108
109 if (userLocalService.getUserGroupUsersCount(userGroupId, true) > 0) {
110 throw new RequiredUserGroupException();
111 }
112
113
115 clearUserUserGroups(userGroupId);
116
117
119 Group group = userGroup.getGroup();
120
121 groupLocalService.deleteGroup(group.getGroupId());
122
123
125 resourceLocalService.deleteResource(
126 userGroup.getCompanyId(), UserGroup.class.getName(),
127 ResourceConstants.SCOPE_INDIVIDUAL, userGroup.getUserGroupId());
128
129
131 userGroupPersistence.remove(userGroupId);
132
133
135 PermissionCacheUtil.clearCache();
136 }
137
138 public UserGroup getUserGroup(long userGroupId)
139 throws PortalException, SystemException {
140
141 return userGroupPersistence.findByPrimaryKey(userGroupId);
142 }
143
144 public UserGroup getUserGroup(long companyId, String name)
145 throws PortalException, SystemException {
146
147 return userGroupFinder.findByC_N(companyId, name);
148 }
149
150 public List<UserGroup> getUserGroups(long companyId)
151 throws PortalException, SystemException {
152
153 return userGroupPersistence.findByCompanyId(companyId);
154 }
155
156 public List<UserGroup> getUserUserGroups(long userId)
157 throws PortalException, SystemException {
158
159 return userPersistence.getUserGroups(userId);
160 }
161
162 public boolean hasGroupUserGroup(long groupId, long userGroupId)
163 throws PortalException, SystemException {
164
165 return groupPersistence.containsUserGroup(groupId, userGroupId);
166 }
167
168 public List<UserGroup> search(
169 long companyId, String name, String description,
170 LinkedHashMap<String, Object> params, int begin, int end,
171 OrderByComparator obc)
172 throws SystemException {
173
174 return userGroupFinder.findByC_N_D(
175 companyId, name, description, params, begin, end, obc);
176 }
177
178 public int searchCount(
179 long companyId, String name, String description,
180 LinkedHashMap<String, Object> 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 groupPersistence.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 = userGroupPersistence.findByPrimaryKey(
203 userGroupId);
204
205 userGroup.setName(name);
206 userGroup.setDescription(description);
207
208 userGroupPersistence.update(userGroup, false);
209
210 return userGroup;
211 }
212
213 protected void validate(long userGroupId, long companyId, String name)
214 throws PortalException, SystemException {
215
216 if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
217 (name.indexOf(StringPool.COMMA) != -1) ||
218 (name.indexOf(StringPool.STAR) != -1)) {
219
220 throw new UserGroupNameException();
221 }
222
223 try {
224 UserGroup userGroup = userGroupFinder.findByC_N(companyId, name);
225
226 if (userGroup.getUserGroupId() != userGroupId) {
227 throw new DuplicateUserGroupException();
228 }
229 }
230 catch (NoSuchUserGroupException nsuge) {
231 }
232 }
233
234 }