1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kim.impl.group; |
17 | |
|
18 | |
import org.apache.commons.collections.ListUtils; |
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.kuali.rice.kew.api.KewApiServiceLocator; |
21 | |
import org.kuali.rice.kew.api.KewApiConstants; |
22 | |
import org.kuali.rice.kim.api.group.GroupService; |
23 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
24 | |
import org.kuali.rice.krad.service.BusinessObjectService; |
25 | |
import org.kuali.rice.krad.service.KRADServiceLocator; |
26 | |
|
27 | |
import java.util.ArrayList; |
28 | |
import java.util.Collections; |
29 | |
import java.util.HashSet; |
30 | |
import java.util.List; |
31 | |
import java.util.Set; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | public class GroupInternalServiceImpl implements GroupInternalService { |
40 | |
protected BusinessObjectService getBusinessObjectService() { |
41 | 0 | return KRADServiceLocator.getBusinessObjectService(); |
42 | |
} |
43 | |
|
44 | |
|
45 | |
public GroupService getGroupService(){ |
46 | 0 | return KimApiServiceLocator.getGroupService(); |
47 | |
} |
48 | |
|
49 | |
@Override |
50 | |
public GroupBo saveWorkgroup(GroupBo group) { |
51 | 0 | GroupService ims = getGroupService(); |
52 | 0 | List<String> oldIds = Collections.emptyList(); |
53 | 0 | if (StringUtils.isNotEmpty(group.getId())) { |
54 | 0 | oldIds = ims.getMemberPrincipalIds(group.getId()); |
55 | |
} |
56 | 0 | group = (GroupBo)getBusinessObjectService().save( group ); |
57 | 0 | List<String> newIds = ims.getMemberPrincipalIds(group.getId()); |
58 | 0 | updateForWorkgroupChange(group.getId(), oldIds, newIds); |
59 | 0 | return group; |
60 | |
} |
61 | |
|
62 | |
@Override |
63 | |
public void updateForWorkgroupChange(String groupId, |
64 | |
List<String> oldPrincipalIds, List<String> newPrincipalIds) { |
65 | 0 | MembersDiff membersDiff = getMembersDiff(oldPrincipalIds, newPrincipalIds); |
66 | 0 | for (String removedPrincipalId : membersDiff.getRemovedPrincipalIds()) { |
67 | 0 | updateForUserRemovedFromGroup(removedPrincipalId, groupId); |
68 | |
} |
69 | 0 | for (String addedPrincipalId : membersDiff.getAddedPrincipalIds()) { |
70 | 0 | updateForUserAddedToGroup(addedPrincipalId, groupId); |
71 | |
} |
72 | 0 | } |
73 | |
|
74 | |
@Override |
75 | |
public void updateForUserAddedToGroup(String principalId, String groupId) { |
76 | |
|
77 | 0 | if(getGroupService().isMemberOfGroup(principalId, groupId)) |
78 | |
{ |
79 | 0 | KewApiServiceLocator.getGroupMembershipChangeQueue() |
80 | |
.notifyMembershipChange(KewApiConstants.GroupMembershipChangeOperations.ADDED, groupId, principalId); |
81 | |
} |
82 | 0 | } |
83 | |
|
84 | |
@Override |
85 | |
public void updateForUserRemovedFromGroup(String principalId, String groupId) { |
86 | |
|
87 | 0 | if(!getGroupService().isMemberOfGroup(principalId, groupId)) |
88 | |
{ |
89 | 0 | KewApiServiceLocator.getGroupMembershipChangeQueue() |
90 | |
.notifyMembershipChange(KewApiConstants.GroupMembershipChangeOperations.REMOVED, groupId, principalId); |
91 | |
} |
92 | |
|
93 | 0 | } |
94 | |
|
95 | |
private MembersDiff getMembersDiff(List<String> oldMemberPrincipalIds, List<String> newMemberPrincipalIds) { |
96 | |
|
97 | |
|
98 | |
|
99 | 0 | if(oldMemberPrincipalIds == null) oldMemberPrincipalIds = new ArrayList<String>(); |
100 | 0 | if(newMemberPrincipalIds == null) newMemberPrincipalIds = new ArrayList<String>(); |
101 | |
|
102 | 0 | Set<String> addedPrincipalIds = new HashSet<String>(ListUtils.subtract(newMemberPrincipalIds, oldMemberPrincipalIds)); |
103 | 0 | Set<String> removedPrincipalIds = new HashSet<String>(ListUtils.subtract(oldMemberPrincipalIds, newMemberPrincipalIds)); |
104 | 0 | return new MembersDiff(addedPrincipalIds, removedPrincipalIds); |
105 | |
} |
106 | |
|
107 | 0 | private class MembersDiff { |
108 | |
private final Set<String> addedPrincipalIds; |
109 | |
|
110 | |
private final Set<String> removedPrincipalIds; |
111 | |
|
112 | 0 | public MembersDiff(Set<String> addedPrincipalIds, Set<String>removedPrincipalIds) { |
113 | 0 | this.addedPrincipalIds = addedPrincipalIds; |
114 | 0 | this.removedPrincipalIds = removedPrincipalIds; |
115 | 0 | } |
116 | |
|
117 | |
public Set<String> getAddedPrincipalIds() { |
118 | 0 | return addedPrincipalIds; |
119 | |
} |
120 | |
|
121 | |
public Set<String> getRemovedPrincipalIds() { |
122 | 0 | return removedPrincipalIds; |
123 | |
} |
124 | |
} |
125 | |
|
126 | |
} |