1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kim.impl.group; |
18 | |
|
19 | |
import org.apache.commons.collections.CollectionUtils; |
20 | |
import org.apache.commons.lang.StringUtils; |
21 | |
import org.kuali.rice.core.api.criteria.QueryByCriteria; |
22 | |
import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; |
23 | |
import org.kuali.rice.kim.api.group.Group; |
24 | |
import org.kuali.rice.kim.api.group.GroupMember; |
25 | |
import org.kuali.rice.kim.api.group.GroupQueryResults; |
26 | |
import org.kuali.rice.kim.api.group.GroupService; |
27 | |
import org.kuali.rice.kim.util.KIMPropertyConstants; |
28 | |
import org.kuali.rice.kim.util.KimConstants; |
29 | |
|
30 | |
import java.sql.Timestamp; |
31 | |
import java.util.ArrayList; |
32 | |
import java.util.Collection; |
33 | |
import java.util.Collections; |
34 | |
import java.util.HashMap; |
35 | |
import java.util.HashSet; |
36 | |
import java.util.List; |
37 | |
import java.util.Map; |
38 | |
import java.util.Set; |
39 | |
|
40 | |
|
41 | 20 | public class GroupServiceImpl extends GroupServiceBase implements GroupService { |
42 | |
|
43 | |
@Override |
44 | |
public List<Group> getGroupsForPrincipal(String principalId) throws RiceIllegalArgumentException { |
45 | 1 | if ( StringUtils.isEmpty(principalId) ) { |
46 | 0 | throw new RiceIllegalArgumentException("principalId is blank"); |
47 | |
} |
48 | 1 | return getGroupsForPrincipalByNamespace( principalId, null ); |
49 | |
} |
50 | |
|
51 | |
@Override |
52 | |
public List<Group> getGroupsForPrincipalByNamespace(String principalId, String namespaceCode) throws RiceIllegalArgumentException { |
53 | 4 | if ( StringUtils.isEmpty(principalId) ) { |
54 | 0 | throw new RiceIllegalArgumentException("principalId is blank"); |
55 | |
} |
56 | 4 | Collection<Group> directGroups = getDirectGroupsForPrincipal( principalId, namespaceCode ); |
57 | 4 | Set<Group> groups = new HashSet<Group>(); |
58 | 4 | groups.addAll(directGroups); |
59 | 4 | for ( Group group : directGroups ) { |
60 | 4 | groups.add( group ); |
61 | 4 | groups.addAll( getParentGroups( group.getId() ) ); |
62 | |
} |
63 | 4 | return new ArrayList<Group>( groups ); |
64 | |
} |
65 | |
|
66 | |
@Override |
67 | |
public List<String> findGroupIds(final QueryByCriteria queryByCriteria) { |
68 | 1 | GroupQueryResults results = this.findGroups(queryByCriteria); |
69 | 1 | List<String> result = new ArrayList<String>(); |
70 | |
|
71 | 1 | for (Group group : results.getResults()) { |
72 | 3 | result.add(group.getId()); |
73 | |
} |
74 | |
|
75 | 1 | return result; |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
@Override |
89 | |
public boolean isDirectMemberOfGroup(String principalId, String groupId) throws RiceIllegalArgumentException { |
90 | 2 | if ( StringUtils.isEmpty(groupId) ) { |
91 | 0 | throw new RiceIllegalArgumentException("groupId is blank"); |
92 | |
} |
93 | 2 | if ( StringUtils.isEmpty(principalId) ) { |
94 | 0 | throw new RiceIllegalArgumentException("principalId is blank"); |
95 | |
} |
96 | 2 | Map<String,String> criteria = new HashMap<String,String>(); |
97 | 2 | criteria.put(KIMPropertyConstants.GroupMember.MEMBER_ID, principalId); |
98 | 2 | criteria.put(KIMPropertyConstants.GroupMember.MEMBER_TYPE_CODE, KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE); |
99 | 2 | criteria.put(KIMPropertyConstants.GroupMember.GROUP_ID, groupId); |
100 | |
|
101 | 2 | Collection<GroupMemberBo> groupMembers = businessObjectService.findMatching(GroupMemberBo.class, criteria); |
102 | 2 | for ( GroupMemberBo gm : groupMembers ) { |
103 | 1 | if ( gm.isActive(new Timestamp(System.currentTimeMillis())) ) { |
104 | 1 | return true; |
105 | |
} |
106 | |
} |
107 | 1 | return false; |
108 | |
} |
109 | |
|
110 | |
@Override |
111 | |
public List<String> getGroupIdsForPrincipal(String principalId) throws RiceIllegalArgumentException { |
112 | 1 | if ( StringUtils.isEmpty(principalId) ) { |
113 | 0 | throw new RiceIllegalArgumentException("principalId is blank"); |
114 | |
} |
115 | 1 | return getGroupIdsForPrincipalByNamespace(principalId, null); |
116 | |
} |
117 | |
|
118 | |
@Override |
119 | |
public List<String> getGroupIdsForPrincipalByNamespace(String principalId, String namespaceCode) throws RiceIllegalArgumentException { |
120 | 2 | if ( StringUtils.isEmpty(principalId) ) { |
121 | 0 | throw new RiceIllegalArgumentException("principalId is blank"); |
122 | |
} |
123 | 2 | List<String> result = new ArrayList<String>(); |
124 | |
|
125 | 2 | if (principalId != null) { |
126 | 2 | List<Group> groupList = getGroupsForPrincipalByNamespace(principalId, namespaceCode); |
127 | |
|
128 | 2 | for (Group group : groupList) { |
129 | 2 | result.add(group.getId()); |
130 | |
} |
131 | |
} |
132 | |
|
133 | 2 | return result; |
134 | |
} |
135 | |
|
136 | |
@Override |
137 | |
public List<String> getDirectGroupIdsForPrincipal(String principalId) throws RiceIllegalArgumentException { |
138 | 1 | if ( StringUtils.isEmpty(principalId) ) { |
139 | 0 | throw new RiceIllegalArgumentException("principalId is blank"); |
140 | |
} |
141 | 1 | List<String> result = new ArrayList<String>(); |
142 | |
|
143 | 1 | if (principalId != null) { |
144 | 1 | Collection<Group> groupList = getDirectGroupsForPrincipal(principalId); |
145 | |
|
146 | 1 | for (Group g : groupList) { |
147 | 1 | result.add(g.getId()); |
148 | |
} |
149 | |
} |
150 | |
|
151 | 1 | return result; |
152 | |
} |
153 | |
|
154 | |
@Override |
155 | |
public List<String> getMemberPrincipalIds(String groupId) throws RiceIllegalArgumentException { |
156 | 1 | if ( StringUtils.isEmpty(groupId) ) { |
157 | 0 | throw new RiceIllegalArgumentException("groupId is blank"); |
158 | |
} |
159 | 1 | Set<String> visitedGroupIds = new HashSet<String>(); |
160 | 1 | return getMemberPrincipalIdsInternal(groupId, visitedGroupIds); |
161 | |
} |
162 | |
|
163 | |
@Override |
164 | |
public List<String> getDirectMemberPrincipalIds(String groupId) throws RiceIllegalArgumentException { |
165 | 1 | if ( StringUtils.isEmpty(groupId) ) { |
166 | 0 | throw new RiceIllegalArgumentException("groupId is blank"); |
167 | |
} |
168 | |
|
169 | 1 | return this.getMemberIdsByType(getMembersOfGroup(groupId), KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE); |
170 | |
} |
171 | |
|
172 | |
@Override |
173 | |
public List<String> getMemberGroupIds(String groupId) throws RiceIllegalArgumentException { |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | 1 | if ( StringUtils.isEmpty(groupId) ) { |
182 | 0 | throw new RiceIllegalArgumentException("groupId is blank"); |
183 | |
} |
184 | 1 | List<GroupBo> groups = getMemberGroupBos( groupId ); |
185 | 1 | ArrayList<String> groupIds = new ArrayList<String>( groups.size() ); |
186 | 1 | for ( GroupBo group : groups ) { |
187 | 1 | if ( group.isActive() ) { |
188 | 1 | groupIds.add( group.getId() ); |
189 | |
} |
190 | |
} |
191 | 1 | return groupIds; |
192 | |
} |
193 | |
|
194 | |
|
195 | |
protected List<GroupBo> getMemberGroupBos(String groupId) { |
196 | 1 | if ( groupId == null ) { |
197 | 0 | return Collections.emptyList(); |
198 | |
} |
199 | 1 | Set<GroupBo> groups = new HashSet<GroupBo>(); |
200 | |
|
201 | 1 | GroupBo group = getGroupBo(groupId); |
202 | 1 | getMemberGroupsInternal(group, groups); |
203 | |
|
204 | 1 | return new ArrayList<GroupBo>(groups); |
205 | |
} |
206 | |
|
207 | |
protected void getMemberGroupsInternal( GroupBo group, Set<GroupBo> groups ) { |
208 | 2 | if ( group == null ) { |
209 | 0 | return; |
210 | |
} |
211 | 2 | List<String> groupIds = group.getMemberGroupIds(); |
212 | |
|
213 | 2 | for (String id : groupIds) { |
214 | 1 | GroupBo memberGroup = getGroupBo(id); |
215 | |
|
216 | 1 | if ( memberGroup.isActive() && !groups.contains( memberGroup ) ) { |
217 | 1 | groups.add(memberGroup); |
218 | 1 | getMemberGroupsInternal(memberGroup,groups); |
219 | |
} |
220 | 1 | } |
221 | |
|
222 | 2 | } |
223 | |
|
224 | |
@Override |
225 | |
public List<String> getDirectMemberGroupIds(String groupId) { |
226 | 1 | if ( groupId == null ) { |
227 | 0 | return Collections.emptyList(); |
228 | |
} |
229 | |
|
230 | 1 | return this.getMemberIdsByType(getMembersOfGroup(groupId), KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE); |
231 | |
} |
232 | |
|
233 | |
@Override |
234 | |
public List<String> getParentGroupIds(String groupId) throws RiceIllegalArgumentException { |
235 | 1 | if ( StringUtils.isEmpty(groupId) ) { |
236 | 0 | throw new RiceIllegalArgumentException("groupId is blank"); |
237 | |
} |
238 | 1 | List<String> result = new ArrayList<String>(); |
239 | 1 | if (groupId != null) { |
240 | 1 | List<Group> groupList = getParentGroups(groupId); |
241 | |
|
242 | 1 | for (Group group : groupList) { |
243 | 1 | result.add(group.getId()); |
244 | |
} |
245 | |
} |
246 | |
|
247 | 1 | return result; |
248 | |
} |
249 | |
|
250 | |
@Override |
251 | |
public List<String> getDirectParentGroupIds(String groupId) throws RiceIllegalArgumentException { |
252 | 1 | if ( StringUtils.isEmpty(groupId) ) { |
253 | 0 | throw new RiceIllegalArgumentException("groupId is blank"); |
254 | |
} |
255 | 1 | List<String> result = new ArrayList<String>(); |
256 | 1 | if (groupId != null) { |
257 | 1 | List<Group> groupList = getDirectParentGroups(groupId); |
258 | 1 | for (Group group : groupList) { |
259 | 1 | result.add(group.getId()); |
260 | |
} |
261 | |
} |
262 | |
|
263 | 1 | return result; |
264 | |
} |
265 | |
|
266 | |
@Override |
267 | |
public Map<String, String> getAttributes(String groupId) throws RiceIllegalArgumentException { |
268 | 0 | if ( StringUtils.isEmpty(groupId) ) { |
269 | 0 | throw new RiceIllegalArgumentException("groupId is blank"); |
270 | |
} |
271 | |
|
272 | 0 | if (groupId == null) { |
273 | 0 | return Collections.emptyMap(); |
274 | |
} |
275 | |
|
276 | 0 | Group group = getGroup(groupId); |
277 | 0 | if (group != null) { |
278 | 0 | return group.getAttributes(); |
279 | |
} |
280 | 0 | return null; |
281 | |
} |
282 | |
|
283 | |
@Override |
284 | |
public List<GroupMember> getMembers(List<String> groupIds) { |
285 | 1 | if (CollectionUtils.isEmpty(groupIds)) { |
286 | 0 | throw new RiceIllegalArgumentException("groupIds is empty"); |
287 | |
} |
288 | |
|
289 | |
|
290 | 1 | List<GroupMember> groupMembers = new ArrayList<GroupMember>(); |
291 | 1 | for (String groupId : groupIds) { |
292 | 2 | groupMembers.addAll(getMembersOfGroup(groupId)); |
293 | |
} |
294 | 1 | return groupMembers; |
295 | |
} |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
protected List<Group> getParentGroups(String groupId) throws RiceIllegalArgumentException { |
309 | 5 | if ( StringUtils.isEmpty(groupId) ) { |
310 | 0 | throw new RiceIllegalArgumentException("groupId is blank"); |
311 | |
} |
312 | 5 | Set<Group> groups = new HashSet<Group>(); |
313 | 5 | getParentGroupsInternal( groupId, groups ); |
314 | 5 | return new ArrayList<Group>( groups ); |
315 | |
} |
316 | |
|
317 | |
protected List<String> getMemberPrincipalIdsInternal(String groupId, Set<String> visitedGroupIds) { |
318 | 2 | if ( groupId == null ) { |
319 | 0 | return Collections.emptyList(); |
320 | |
} |
321 | 2 | Set<String> ids = new HashSet<String>(); |
322 | 2 | GroupBo group = getGroupBo(groupId); |
323 | 2 | if ( group == null || !group.isActive()) { |
324 | 0 | return Collections.emptyList(); |
325 | |
} |
326 | |
|
327 | |
|
328 | |
|
329 | 2 | ids.addAll( group.getMemberPrincipalIds()); |
330 | 2 | visitedGroupIds.add(group.getId()); |
331 | |
|
332 | 2 | for (String memberGroupId : group.getMemberGroupIds()) { |
333 | 1 | if (!visitedGroupIds.contains(memberGroupId)){ |
334 | 1 | ids.addAll(getMemberPrincipalIdsInternal(memberGroupId, visitedGroupIds)); |
335 | |
} |
336 | |
} |
337 | |
|
338 | 2 | return new ArrayList<String>(ids); |
339 | |
} |
340 | |
|
341 | |
protected Collection<Group> getDirectGroupsForPrincipal( String principalId ) { |
342 | 1 | return getDirectGroupsForPrincipal( principalId, null ); |
343 | |
} |
344 | |
|
345 | |
@SuppressWarnings("unchecked") |
346 | |
protected Collection<Group> getDirectGroupsForPrincipal( String principalId, String namespaceCode ) { |
347 | 5 | if ( principalId == null ) { |
348 | 0 | return Collections.emptyList(); |
349 | |
} |
350 | 5 | Map<String,Object> criteria = new HashMap<String,Object>(); |
351 | 5 | criteria.put(KIMPropertyConstants.GroupMember.MEMBER_ID, principalId); |
352 | 5 | criteria.put(KIMPropertyConstants.GroupMember.MEMBER_TYPE_CODE, KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE); |
353 | 5 | Collection<GroupMemberBo> groupMembers = businessObjectService.findMatching(GroupMemberBo.class, criteria); |
354 | 5 | Set<String> groupIds = new HashSet<String>( groupMembers.size() ); |
355 | |
|
356 | 5 | for ( GroupMemberBo gm : groupMembers ) { |
357 | 5 | if ( gm.isActive(new Timestamp(System.currentTimeMillis())) ) { |
358 | 5 | groupIds.add( gm.getGroupId() ); |
359 | |
} |
360 | |
} |
361 | |
|
362 | 5 | List<Group> groups = getGroups(groupIds); |
363 | 5 | List<Group> result = new ArrayList<Group>( groups.size() ); |
364 | |
|
365 | 5 | for ( Group group : groups ) { |
366 | 5 | if ( group.isActive() ) { |
367 | 5 | if ( StringUtils.isBlank(namespaceCode) || StringUtils.equals(namespaceCode, group.getNamespaceCode() ) ) { |
368 | 5 | result.add(group); |
369 | |
} |
370 | |
} |
371 | |
} |
372 | 5 | return result; |
373 | |
} |
374 | |
} |