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 public class GroupInternalServiceImpl implements GroupInternalService {
40 protected BusinessObjectService getBusinessObjectService() {
41 return KRADServiceLocator.getBusinessObjectService();
42 }
43
44
45 public GroupService getGroupService(){
46 return KimApiServiceLocator.getGroupService();
47 }
48
49 @Override
50 public GroupBo saveWorkgroup(GroupBo group) {
51 GroupService ims = getGroupService();
52 List<String> oldIds = Collections.emptyList();
53 if (StringUtils.isNotEmpty(group.getId())) {
54 oldIds = ims.getMemberPrincipalIds(group.getId());
55 }
56 group = (GroupBo)getBusinessObjectService().save( group );
57 List<String> newIds = ims.getMemberPrincipalIds(group.getId());
58 updateForWorkgroupChange(group.getId(), oldIds, newIds);
59 return group;
60 }
61
62 @Override
63 public void updateForWorkgroupChange(String groupId,
64 List<String> oldPrincipalIds, List<String> newPrincipalIds) {
65 MembersDiff membersDiff = getMembersDiff(oldPrincipalIds, newPrincipalIds);
66 for (String removedPrincipalId : membersDiff.getRemovedPrincipalIds()) {
67 updateForUserRemovedFromGroup(removedPrincipalId, groupId);
68 }
69 for (String addedPrincipalId : membersDiff.getAddedPrincipalIds()) {
70 updateForUserAddedToGroup(addedPrincipalId, groupId);
71 }
72 }
73
74 @Override
75 public void updateForUserAddedToGroup(String principalId, String groupId) {
76
77 if(getGroupService().isMemberOfGroup(principalId, groupId))
78 {
79 KewApiServiceLocator.getGroupMembershipChangeQueue()
80 .notifyMembershipChange(KewApiConstants.GroupMembershipChangeOperations.ADDED, groupId, principalId);
81 }
82 }
83
84 @Override
85 public void updateForUserRemovedFromGroup(String principalId, String groupId) {
86
87 if(!getGroupService().isMemberOfGroup(principalId, groupId))
88 {
89 KewApiServiceLocator.getGroupMembershipChangeQueue()
90 .notifyMembershipChange(KewApiConstants.GroupMembershipChangeOperations.REMOVED, groupId, principalId);
91 }
92
93 }
94
95 private MembersDiff getMembersDiff(List<String> oldMemberPrincipalIds, List<String> newMemberPrincipalIds) {
96
97
98
99 if(oldMemberPrincipalIds == null) oldMemberPrincipalIds = new ArrayList<String>();
100 if(newMemberPrincipalIds == null) newMemberPrincipalIds = new ArrayList<String>();
101
102 Set<String> addedPrincipalIds = new HashSet<String>(ListUtils.subtract(newMemberPrincipalIds, oldMemberPrincipalIds));
103 Set<String> removedPrincipalIds = new HashSet<String>(ListUtils.subtract(oldMemberPrincipalIds, newMemberPrincipalIds));
104 return new MembersDiff(addedPrincipalIds, removedPrincipalIds);
105 }
106
107 private class MembersDiff {
108 private final Set<String> addedPrincipalIds;
109
110 private final Set<String> removedPrincipalIds;
111
112 public MembersDiff(Set<String> addedPrincipalIds, Set<String>removedPrincipalIds) {
113 this.addedPrincipalIds = addedPrincipalIds;
114 this.removedPrincipalIds = removedPrincipalIds;
115 }
116
117 public Set<String> getAddedPrincipalIds() {
118 return addedPrincipalIds;
119 }
120
121 public Set<String> getRemovedPrincipalIds() {
122 return removedPrincipalIds;
123 }
124 }
125
126 }