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