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 | 0 | public class GroupMembershipChangeQueueImpl implements GroupMembershipChangeQueue { |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
@Override |
56 | |
public void notifyMembershipChange(String operation, String groupId, String principalId) { |
57 | 0 | if (StringUtils.isBlank(operation)) { |
58 | 0 | throw new RiceIllegalArgumentException("operation was blank or null"); |
59 | |
} |
60 | |
|
61 | 0 | if (StringUtils.isBlank(groupId)) { |
62 | 0 | throw new RiceIllegalArgumentException("groupId was blank or null"); |
63 | |
} |
64 | |
|
65 | 0 | if (StringUtils.isBlank(principalId)) { |
66 | 0 | throw new RiceIllegalArgumentException("principalId was blank or null"); |
67 | |
} |
68 | |
|
69 | 0 | Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId); |
70 | 0 | if (principal == null) { |
71 | 0 | throw new RiceRuntimeException("Could not locate the user for the given principal id '" + principalId + "'"); |
72 | |
} |
73 | 0 | Group group = KimApiServiceLocator.getGroupService().getGroup(groupId); |
74 | 0 | if (group == null) { |
75 | 0 | throw new RiceRuntimeException("Could not locate the group with the given id '" + groupId + "'"); |
76 | |
} |
77 | 0 | if (KewApiConstants.GroupMembershipChangeOperations.ADDED.equalsIgnoreCase(operation)) { |
78 | 0 | updateActionListForUserAddedToGroup(principalId, groupId); |
79 | 0 | } else if (KewApiConstants.GroupMembershipChangeOperations.REMOVED.equalsIgnoreCase(operation)) { |
80 | 0 | updateActionListForUserRemovedFromGroup(principalId, groupId); |
81 | |
} else { |
82 | 0 | throw new WorkflowRuntimeException("Did not understand requested group membership change operation '" + operation + "'"); |
83 | |
} |
84 | 0 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
private void updateActionListForUserAddedToGroup(String principalId, String groupId) { |
91 | 0 | List<ActionRequestValue> actionRequests = new ArrayList<ActionRequestValue>(); |
92 | 0 | List<String> parentGroupIds = KimApiServiceLocator.getGroupService().getParentGroupIds(groupId); |
93 | 0 | List<String> allGroupsToCheck = new ArrayList<String>(); |
94 | 0 | allGroupsToCheck.add(0, groupId); |
95 | 0 | allGroupsToCheck.addAll(parentGroupIds); |
96 | 0 | for (String groupToCheckId : allGroupsToCheck) { |
97 | 0 | actionRequests.addAll(getActionRequestService().findActivatedByGroup(groupToCheckId)); |
98 | |
} |
99 | 0 | for (ActionRequestValue request : actionRequests) { |
100 | 0 | ActionItem item = getActionListService().createActionItemForActionRequest(request); |
101 | 0 | item.setPrincipalId(principalId); |
102 | 0 | getActionListService().saveActionItem(item); |
103 | 0 | } |
104 | 0 | } |
105 | |
|
106 | |
private void updateActionListForUserRemovedFromGroup(String principalId, String groupId) { |
107 | 0 | List<String> parentGroupIds = KimApiServiceLocator.getGroupService().getParentGroupIds(groupId); |
108 | 0 | List<String> allGroupsToCheck = new ArrayList<String>(); |
109 | 0 | allGroupsToCheck.add(0, groupId); |
110 | 0 | allGroupsToCheck.addAll(parentGroupIds); |
111 | 0 | Collection<ActionItem> actionItems = getActionListService().findByPrincipalId(principalId); |
112 | 0 | for (Iterator<ActionItem> itemIt = actionItems.iterator(); itemIt.hasNext();) { |
113 | 0 | ActionItem item = itemIt.next(); |
114 | 0 | if (item.isWorkgroupItem()) { |
115 | 0 | for (String groupIdToCheck : allGroupsToCheck) { |
116 | 0 | if (item.getGroupId().equals(groupIdToCheck)) { |
117 | 0 | getActionListService().deleteActionItem(item); |
118 | |
} |
119 | |
} |
120 | |
} |
121 | 0 | } |
122 | 0 | } |
123 | |
|
124 | |
public ActionRequestService getActionRequestService() { |
125 | 0 | return KEWServiceLocator.getActionRequestService(); |
126 | |
} |
127 | |
|
128 | |
public ActionListService getActionListService() { |
129 | 0 | return KEWServiceLocator.getActionListService(); |
130 | |
} |
131 | |
} |