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