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