1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.service.impl;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26
27 import javax.jws.WebService;
28
29 import org.apache.commons.lang.StringUtils;
30 import org.kuali.rice.core.xml.dto.AttributeSet;
31 import org.kuali.rice.kim.bo.Group;
32 import org.kuali.rice.kim.bo.group.dto.GroupInfo;
33 import org.kuali.rice.kim.bo.group.dto.GroupMembershipInfo;
34 import org.kuali.rice.kim.bo.group.impl.GroupAttributeDataImpl;
35 import org.kuali.rice.kim.bo.group.impl.GroupMemberImpl;
36 import org.kuali.rice.kim.bo.impl.GroupImpl;
37 import org.kuali.rice.kim.dao.KimGroupDao;
38 import org.kuali.rice.kim.service.GroupService;
39 import org.kuali.rice.kim.util.KIMPropertyConstants;
40 import org.kuali.rice.kim.util.KIMWebServiceConstants;
41 import org.kuali.rice.kim.util.KimConstants.KimGroupMemberTypes;
42 import org.kuali.rice.kns.util.KNSPropertyConstants;
43
44 @WebService(endpointInterface = KIMWebServiceConstants.GroupService.INTERFACE_CLASS, serviceName = KIMWebServiceConstants.GroupService.WEB_SERVICE_NAME, portName = KIMWebServiceConstants.GroupService.WEB_SERVICE_PORT, targetNamespace = KIMWebServiceConstants.MODULE_TARGET_NAMESPACE)
45 public class GroupServiceImpl extends GroupServiceBase implements GroupService {
46
47 private KimGroupDao kimGroupDao;
48
49
50
51
52 public List<String> getGroupIdsForPrincipal(String principalId) {
53 return getGroupIdsForPrincipalByNamespace(principalId, null);
54 }
55
56
57
58
59 public List<String> getGroupIdsForPrincipalByNamespace(String principalId, String namespaceCode) {
60 List<String> result = new ArrayList<String>();
61
62 if (principalId != null) {
63 List<GroupInfo> groupList = getGroupsForPrincipalByNamespace(principalId, namespaceCode);
64
65 for (GroupInfo group : groupList) {
66 result.add(group.getGroupId());
67 }
68 }
69
70 return result;
71 }
72
73
74
75
76 public List<String> getDirectGroupIdsForPrincipal(String principalId) {
77 List<String> result = new ArrayList<String>();
78
79 if (principalId != null) {
80 Collection<GroupInfo> groupList = getDirectGroupsForPrincipal(principalId);
81
82 for (GroupInfo g : groupList) {
83 result.add(g.getGroupId());
84 }
85 }
86
87 return result;
88 }
89
90
91
92
93 public List<GroupInfo> getGroupsForPrincipal(String principalId) {
94 return getGroupsForPrincipalByNamespace( principalId, null );
95 }
96
97
98
99
100 public List<GroupInfo> getGroupsForPrincipalByNamespace(String principalId, String namespaceCode) {
101 Collection<GroupInfo> directGroups = getDirectGroupsForPrincipal( principalId, namespaceCode );
102 Set<GroupInfo> groups = new HashSet<GroupInfo>();
103 for ( GroupInfo group : directGroups ) {
104 groups.add( group );
105 groups.addAll( getParentGroups( group.getGroupId() ) );
106 }
107 return new ArrayList<GroupInfo>( groups );
108 }
109
110 protected Collection<GroupInfo> getDirectGroupsForPrincipal( String principalId ) {
111 return getDirectGroupsForPrincipal( principalId, null );
112 }
113
114 @SuppressWarnings("unchecked")
115 protected Collection<GroupInfo> getDirectGroupsForPrincipal( String principalId, String namespaceCode ) {
116 if ( principalId == null ) {
117 return Collections.emptyList();
118 }
119 Map<String,Object> criteria = new HashMap<String,Object>();
120 criteria.put(KIMPropertyConstants.GroupMember.MEMBER_ID, principalId);
121 criteria.put(KIMPropertyConstants.GroupMember.MEMBER_TYPE_CODE, KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE);
122 Collection<GroupMemberImpl> groupMembers = getBusinessObjectService().findMatching(GroupMemberImpl.class, criteria);
123 Set<String> groupIds = new HashSet<String>( groupMembers.size() );
124
125 for ( GroupMemberImpl gm : groupMembers ) {
126 if ( gm.isActive() ) {
127 groupIds.add( gm.getGroupId() );
128 }
129 }
130
131 Map<String,GroupInfo> groups = getGroupInfos(groupIds);
132 List<GroupInfo> result = new ArrayList<GroupInfo>( groups.size() );
133
134 for ( GroupInfo gi : groups.values() ) {
135 if ( gi.isActive() ) {
136 if ( StringUtils.isBlank( namespaceCode ) || StringUtils.equals(namespaceCode, gi.getNamespaceCode() ) ) {
137 result.add(gi);
138 }
139 }
140 }
141 return result;
142 }
143
144
145
146
147 protected List<GroupImpl> getMemberGroups(String groupId) {
148 if ( groupId == null ) {
149 return Collections.emptyList();
150 }
151 Set<GroupImpl> groups = new HashSet<GroupImpl>();
152
153 GroupImpl group = getGroupImpl(groupId);
154 getMemberGroupsInternal(group, groups);
155
156 return new ArrayList<GroupImpl>(groups);
157 }
158
159 protected void getMemberGroupsInternal( GroupImpl group, Set<GroupImpl> groups ) {
160 if ( group == null ) {
161 return;
162 }
163 List<String> memberGroupIds = group.getMemberGroupIds();
164
165 for (String groupId : memberGroupIds) {
166 GroupImpl memberGroup = getGroupImpl(groupId);
167
168 if ( memberGroup.isActive() && !groups.contains( memberGroup ) ) {
169 groups.add(memberGroup);
170 getMemberGroupsInternal(memberGroup,groups);
171 }
172 }
173
174 }
175
176
177
178
179 public List<String> lookupGroupIds(Map<String, String> searchCriteria) {
180 List<? extends Group> groupList = this.lookupGroups(searchCriteria);
181 List<String> result = new ArrayList<String>();
182
183 for (Group group : groupList) {
184 result.add(group.getGroupId());
185 }
186
187 return result;
188 }
189
190
191
192
193 @SuppressWarnings("unchecked")
194 public List<? extends Group> lookupGroups(Map<String, String> searchCriteria) {
195 return this.toGroupInfo( kimGroupDao.getGroups(searchCriteria));
196 }
197
198
199
200
201 public List<String> getDirectMemberPrincipalIds(String groupId) {
202 if ( groupId == null ) {
203 return Collections.emptyList();
204 }
205 GroupImpl group = getGroupImpl(groupId);
206 if ( group == null ) {
207 return Collections.emptyList();
208 }
209
210 return group.getMemberPrincipalIds();
211 }
212
213
214
215
216 public List<String> getMemberPrincipalIds(String groupId) {
217 if ( groupId == null ) {
218 return Collections.emptyList();
219 }
220 Set<String> visitedGroupIds = new HashSet<String>();
221 return getMemberPrincipalIdsInternal(groupId, visitedGroupIds);
222 }
223
224
225
226
227
228 public boolean isMemberOfGroup(String principalId, String groupId) {
229 if ( principalId == null || groupId == null ) {
230 return false;
231 }
232 Set<String> visitedGroupIds = new HashSet<String>();
233 return isMemberOfGroupInternal(principalId, groupId, visitedGroupIds);
234 }
235
236
237
238
239 public List<String> getDirectParentGroupIds(String groupId) {
240
241
242 List<String> result = new ArrayList<String>();
243 if (groupId != null) {
244 Map<String,GroupInfo> groupList = getDirectParentGroups(groupId);
245 result.addAll(groupList.keySet());
246 }
247
248 return result;
249 }
250
251
252
253
254 public List<String> getParentGroupIds(String groupId) {
255 List<String> result = new ArrayList<String>();
256 if (groupId != null) {
257 List<GroupInfo> groupList = getParentGroups(groupId);
258
259 for (GroupInfo group : groupList) {
260 result.add(group.getGroupId());
261 }
262 }
263
264 return result;
265 }
266
267
268
269
270
271 public List<String> getDirectMemberGroupIds(String groupId) {
272 if ( groupId == null ) {
273 return Collections.emptyList();
274 }
275 GroupImpl group = getGroupImpl( groupId );
276 if ( group == null ) {
277 return Collections.emptyList();
278 }
279 return group.getMemberGroupIds();
280 }
281
282
283
284
285 public boolean isGroupActive( String groupId ) {
286 Map<String,String> criteria = new HashMap<String,String>();
287 criteria.put(KIMPropertyConstants.Group.GROUP_ID, groupId);
288 criteria.put(KNSPropertyConstants.ACTIVE, "Y");
289 return getBusinessObjectService().countMatching(GroupImpl.class, criteria) > 0;
290 }
291
292
293
294
295 public List<String> getMemberGroupIds(String groupId) {
296 if ( groupId == null ) {
297 return Collections.emptyList();
298 }
299 List<GroupImpl> groups = getMemberGroups( groupId );
300 ArrayList<String> groupIds = new ArrayList<String>( groups.size() );
301 for ( GroupImpl group : groups ) {
302 if ( group.isActive() ) {
303 groupIds.add( group.getGroupId() );
304 }
305 }
306 return groupIds;
307 }
308
309
310
311
312 @SuppressWarnings("unchecked")
313 public boolean isDirectMemberOfGroup(String principalId, String groupId) {
314 if ( principalId == null || groupId == null ) {
315 return false;
316 }
317 Map<String,String> criteria = new HashMap<String,String>();
318 criteria.put(KIMPropertyConstants.GroupMember.MEMBER_ID, principalId);
319 criteria.put(KIMPropertyConstants.GroupMember.MEMBER_TYPE_CODE, KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE);
320 criteria.put(KIMPropertyConstants.GroupMember.GROUP_ID, groupId);
321
322 Collection<GroupMemberImpl> groupMembers = getBusinessObjectService().findMatching(GroupMemberImpl.class, criteria);
323 for ( GroupMemberImpl gm : groupMembers ) {
324 if ( gm.isActive() ) {
325 return true;
326 }
327 }
328 return false;
329 }
330
331
332
333
334
335
336 @SuppressWarnings("unchecked")
337 public AttributeSet getGroupAttributes(String groupId) {
338 if ( groupId == null ) {
339 return new AttributeSet(0);
340 }
341 Map<String,String> criteria = new HashMap<String,String>();
342 criteria.put(KIMPropertyConstants.Group.GROUP_ID, groupId);
343 List<GroupAttributeDataImpl> groupAttributes = (List<GroupAttributeDataImpl>)getBusinessObjectService().findMatching(GroupAttributeDataImpl.class, criteria);
344 AttributeSet attributes = new AttributeSet( groupAttributes.size() );
345 for ( GroupAttributeDataImpl attr : groupAttributes ) {
346 attributes.put(attr.getKimAttribute().getAttributeName(), attr.getAttributeValue());
347 }
348 return attributes;
349 }
350
351
352
353
354
355
356
357 public Collection<GroupMembershipInfo> getGroupMembers( List<String> groupIds ) {
358 if ( groupIds == null ) {
359 return Collections.emptyList();
360 }
361 List<GroupMembershipInfo> groupMembers = new ArrayList<GroupMembershipInfo>();
362 for (String groupId : groupIds) {
363 groupMembers.addAll( getGroupMembersOfGroup(groupId) );
364 }
365 return groupMembers;
366 }
367
368
369 @SuppressWarnings("unchecked")
370 public Collection<GroupMembershipInfo> getGroupMembersOfGroup( String groupId ) {
371 if ( groupId == null ) {
372 return Collections.emptyList();
373 }
374 Map<String,String> criteria = new HashMap<String,String>( 1 );
375 criteria.put(KIMPropertyConstants.GroupMember.GROUP_ID, groupId);
376 List<GroupMemberImpl> groupMemberImpls = (List<GroupMemberImpl>)getBusinessObjectService().findMatching(GroupMemberImpl.class, criteria);
377 List<GroupMembershipInfo> groupMembers = new ArrayList<GroupMembershipInfo>( groupMemberImpls.size() );
378 for (GroupMemberImpl groupMember : groupMemberImpls) {
379 if (groupMember != null && groupMember.isActive()) {
380 groupMembers.add(toGroupMemberInfo(groupMember));
381 }
382 }
383 return groupMembers;
384 }
385
386 protected GroupMembershipInfo toGroupMemberInfo(GroupMemberImpl kimGroupMember) {
387 GroupMembershipInfo groupMemberinfo = null;
388
389 if (kimGroupMember != null) {
390 groupMemberinfo = new GroupMembershipInfo(kimGroupMember.getGroupId(), kimGroupMember.getGroupMemberId(),kimGroupMember.getMemberId(),kimGroupMember.getMemberTypeCode(), kimGroupMember.getActiveFromDate(), kimGroupMember.getActiveToDate());
391 groupMemberinfo.setVersionNumber(kimGroupMember.getVersionNumber());
392 }
393
394 return groupMemberinfo;
395 }
396
397 public KimGroupDao getKimGroupDao() {
398 return kimGroupDao;
399 }
400
401 public void setKimGroupDao(KimGroupDao kimGroupDao) {
402 this.kimGroupDao = kimGroupDao;
403 }
404 }