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.HashSet; |
20 | |
import java.util.List; |
21 | |
import java.util.Set; |
22 | |
|
23 | |
import javax.xml.namespace.QName; |
24 | |
|
25 | |
import org.apache.commons.collections.ListUtils; |
26 | |
import org.kuali.rice.kew.exception.WorkflowRuntimeException; |
27 | |
import org.kuali.rice.kew.messaging.MessageServiceNames; |
28 | |
import org.kuali.rice.kew.util.KEWConstants; |
29 | |
import org.kuali.rice.kew.workgroup.WorkgroupMembershipChangeProcessor; |
30 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
31 | |
import org.kuali.rice.kim.impl.group.GroupBo; |
32 | |
import org.kuali.rice.kim.api.group.GroupService; |
33 | |
import org.kuali.rice.kim.service.GroupInternalService; |
34 | |
import org.kuali.rice.kns.service.BusinessObjectService; |
35 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
36 | |
import org.kuali.rice.ksb.api.KsbApiServiceLocator; |
37 | |
import org.kuali.rice.ksb.messaging.service.KSBXMLService; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public class GroupInternalServiceImpl implements GroupInternalService { |
46 | |
protected BusinessObjectService getBusinessObjectService() { |
47 | 0 | return KNSServiceLocator.getBusinessObjectService(); |
48 | |
} |
49 | |
|
50 | |
|
51 | |
public GroupService getGroupService(){ |
52 | 0 | return KimApiServiceLocator.getGroupService(); |
53 | |
} |
54 | |
|
55 | |
public GroupBo saveWorkgroup(GroupBo group) { |
56 | 0 | GroupService ims = getGroupService(); |
57 | 0 | List<String> oldIds = ims.getMemberPrincipalIds(group.getId()); |
58 | 0 | group = (GroupBo)getBusinessObjectService().save( group ); |
59 | 0 | List<String> newIds = ims.getMemberPrincipalIds(group.getId()); |
60 | 0 | updateForWorkgroupChange(group.getId(), oldIds, newIds); |
61 | 0 | return group; |
62 | |
} |
63 | |
|
64 | |
public void updateForWorkgroupChange(String groupId, |
65 | |
List<String> oldPrincipalIds, List<String> newPrincipalIds) { |
66 | 0 | MembersDiff membersDiff = getMembersDiff(oldPrincipalIds, newPrincipalIds); |
67 | 0 | for (String removedPrincipalId : membersDiff.getRemovedPrincipalIds()) { |
68 | 0 | updateForUserRemovedFromGroup(removedPrincipalId, groupId); |
69 | |
} |
70 | 0 | for (String addedPrincipalId : membersDiff.getAddedPrincipalIds()) { |
71 | 0 | updateForUserAddedToGroup(addedPrincipalId, groupId); |
72 | |
} |
73 | 0 | } |
74 | |
|
75 | |
public void updateForUserAddedToGroup(String principalId, String groupId) { |
76 | |
|
77 | 0 | if(getGroupService().isMemberOfGroup(principalId, groupId)) |
78 | |
{ |
79 | 0 | KSBXMLService workgroupMembershipChangeProcessor = (KSBXMLService) KsbApiServiceLocator.getMessageHelper() |
80 | |
.getServiceAsynchronously(new QName(KEWConstants.KEW_MODULE_NAMESPACE, MessageServiceNames.WORKGROUP_MEMBERSHIP_CHANGE_SERVICE)); |
81 | |
try { |
82 | 0 | workgroupMembershipChangeProcessor.invoke(WorkgroupMembershipChangeProcessor |
83 | |
.getMemberAddedMessageContents(principalId, groupId)); |
84 | 0 | } catch (Exception e) { |
85 | 0 | throw new WorkflowRuntimeException(e); |
86 | 0 | } |
87 | |
} |
88 | 0 | } |
89 | |
|
90 | |
public void updateForUserRemovedFromGroup(String principalId, String groupId) { |
91 | |
|
92 | 0 | if(!getGroupService().isMemberOfGroup(principalId, groupId)) |
93 | |
{ |
94 | 0 | KSBXMLService workgroupMembershipChangeProcessor = (KSBXMLService) KsbApiServiceLocator.getMessageHelper() |
95 | |
.getServiceAsynchronously(new QName(KEWConstants.KEW_MODULE_NAMESPACE, MessageServiceNames.WORKGROUP_MEMBERSHIP_CHANGE_SERVICE)); |
96 | |
try { |
97 | 0 | workgroupMembershipChangeProcessor.invoke(WorkgroupMembershipChangeProcessor |
98 | |
.getMemberRemovedMessageContents(principalId, groupId)); |
99 | 0 | } catch (Exception e) { |
100 | 0 | throw new WorkflowRuntimeException(e); |
101 | 0 | } |
102 | |
} |
103 | |
|
104 | 0 | } |
105 | |
|
106 | |
private MembersDiff getMembersDiff(List<String> oldMemberPrincipalIds, List<String> newMemberPrincipalIds) { |
107 | |
|
108 | |
|
109 | |
|
110 | 0 | if(oldMemberPrincipalIds == null) oldMemberPrincipalIds = new ArrayList<String>(); |
111 | 0 | if(newMemberPrincipalIds == null) newMemberPrincipalIds = new ArrayList<String>(); |
112 | |
|
113 | 0 | Set<String> addedPrincipalIds = new HashSet<String>(ListUtils.subtract(newMemberPrincipalIds, oldMemberPrincipalIds)); |
114 | 0 | Set<String> removedPrincipalIds = new HashSet<String>(ListUtils.subtract(oldMemberPrincipalIds, newMemberPrincipalIds)); |
115 | 0 | return new MembersDiff(addedPrincipalIds, removedPrincipalIds); |
116 | |
} |
117 | |
|
118 | 0 | private class MembersDiff { |
119 | |
private final Set<String> addedPrincipalIds; |
120 | |
|
121 | |
private final Set<String> removedPrincipalIds; |
122 | |
|
123 | 0 | public MembersDiff(Set<String> addedPrincipalIds, Set<String>removedPrincipalIds) { |
124 | 0 | this.addedPrincipalIds = addedPrincipalIds; |
125 | 0 | this.removedPrincipalIds = removedPrincipalIds; |
126 | 0 | } |
127 | |
|
128 | |
public Set<String> getAddedPrincipalIds() { |
129 | 0 | return addedPrincipalIds; |
130 | |
} |
131 | |
|
132 | |
public Set<String> getRemovedPrincipalIds() { |
133 | 0 | return removedPrincipalIds; |
134 | |
} |
135 | |
} |
136 | |
|
137 | |
} |