1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.impl.group;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Iterator;
21 import java.util.List;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
25 import org.kuali.rice.core.api.exception.RiceRuntimeException;
26 import org.kuali.rice.kew.actionitem.ActionItem;
27 import org.kuali.rice.kew.actionlist.service.ActionListService;
28 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
29 import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
30 import org.kuali.rice.kew.api.WorkflowRuntimeException;
31 import org.kuali.rice.kew.api.group.GroupMembershipChangeQueue;
32 import org.kuali.rice.kew.service.KEWServiceLocator;
33 import org.kuali.rice.kew.api.KewApiConstants;
34 import org.kuali.rice.kim.api.group.Group;
35 import org.kuali.rice.kim.api.identity.principal.Principal;
36 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class GroupMembershipChangeQueueImpl implements GroupMembershipChangeQueue {
51
52
53
54
55 @Override
56 public void notifyMembershipChange(String operation, String groupId, String principalId) {
57 if (StringUtils.isBlank(operation)) {
58 throw new RiceIllegalArgumentException("operation was blank or null");
59 }
60
61 if (StringUtils.isBlank(groupId)) {
62 throw new RiceIllegalArgumentException("groupId was blank or null");
63 }
64
65 if (StringUtils.isBlank(principalId)) {
66 throw new RiceIllegalArgumentException("principalId was blank or null");
67 }
68
69 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
70 if (principal == null) {
71 throw new RiceRuntimeException("Could not locate the user for the given principal id '" + principalId + "'");
72 }
73 Group group = KimApiServiceLocator.getGroupService().getGroup(groupId);
74 if (group == null) {
75 throw new RiceRuntimeException("Could not locate the group with the given id '" + groupId + "'");
76 }
77 if (KewApiConstants.GroupMembershipChangeOperations.ADDED.equalsIgnoreCase(operation)) {
78 updateActionListForUserAddedToGroup(principalId, groupId);
79 } else if (KewApiConstants.GroupMembershipChangeOperations.REMOVED.equalsIgnoreCase(operation)) {
80 updateActionListForUserRemovedFromGroup(principalId, groupId);
81 } else {
82 throw new WorkflowRuntimeException("Did not understand requested group membership change operation '" + operation + "'");
83 }
84 }
85
86
87
88
89
90 private void updateActionListForUserAddedToGroup(String principalId, String groupId) {
91 List<ActionRequestValue> actionRequests = new ArrayList<ActionRequestValue>();
92 List<String> parentGroupIds = KimApiServiceLocator.getGroupService().getParentGroupIds(groupId);
93 List<String> allGroupsToCheck = new ArrayList<String>();
94 allGroupsToCheck.add(0, groupId);
95 allGroupsToCheck.addAll(parentGroupIds);
96 for (String groupToCheckId : allGroupsToCheck) {
97 actionRequests.addAll(getActionRequestService().findActivatedByGroup(groupToCheckId));
98 }
99 for (ActionRequestValue request : actionRequests) {
100 ActionItem item = getActionListService().createActionItemForActionRequest(request);
101 item.setPrincipalId(principalId);
102 getActionListService().saveActionItem(item);
103 }
104 }
105
106 private void updateActionListForUserRemovedFromGroup(String principalId, String groupId) {
107 List<String> parentGroupIds = KimApiServiceLocator.getGroupService().getParentGroupIds(groupId);
108 List<String> allGroupsToCheck = new ArrayList<String>();
109 allGroupsToCheck.add(0, groupId);
110 allGroupsToCheck.addAll(parentGroupIds);
111 Collection<ActionItem> actionItems = getActionListService().findByPrincipalId(principalId);
112 for (Iterator<ActionItem> itemIt = actionItems.iterator(); itemIt.hasNext();) {
113 ActionItem item = itemIt.next();
114 if (item.isWorkgroupItem()) {
115 for (String groupIdToCheck : allGroupsToCheck) {
116 if (item.getGroupId().equals(groupIdToCheck)) {
117 getActionListService().deleteActionItem(item);
118 }
119 }
120 }
121 }
122 }
123
124 public ActionRequestService getActionRequestService() {
125 return KEWServiceLocator.getActionRequestService();
126 }
127
128 public ActionListService getActionListService() {
129 return KEWServiceLocator.getActionListService();
130 }
131 }