1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.actions;
17
18 import org.kuali.rice.kew.actionitem.ActionItem;
19 import org.kuali.rice.kew.actionlist.service.ActionListService;
20 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
21 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
22 import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
23 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
24 import org.kuali.rice.kew.service.KEWServiceLocator;
25 import org.kuali.rice.kew.api.KewApiConstants;
26 import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
27 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
28
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.List;
32
33
34
35
36
37
38
39
40
41
42 public class TakeWorkgroupAuthority extends ActionTakenEvent {
43
44 private String groupId;
45
46
47
48
49
50 public TakeWorkgroupAuthority(DocumentRouteHeaderValue routeHeader, PrincipalContract principal) {
51 super(KewApiConstants.ACTION_TAKEN_TAKE_WORKGROUP_AUTHORITY_CD, routeHeader, principal);
52 }
53
54
55
56
57
58
59
60 public TakeWorkgroupAuthority(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation, String groupId) {
61 super(KewApiConstants.ACTION_TAKEN_TAKE_WORKGROUP_AUTHORITY_CD, routeHeader, principal, annotation);
62 this.groupId = groupId;
63 }
64
65
66
67
68 @Override
69 public String validateActionRules() {
70 if ( (groupId != null) && (!KimApiServiceLocator.getGroupService().isMemberOfGroup(getPrincipal().getPrincipalId(), groupId))) {
71 return (getPrincipal().getPrincipalName() + " not a member of workgroup " + groupId);
72 }
73 return "";
74 }
75
76 @Override
77 public String validateActionRules(List<ActionRequestValue> actionRequests) {
78 return validateActionRules();
79 }
80
81 public void recordAction() throws InvalidActionTakenException {
82
83 String errorMessage = validateActionRules();
84 if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
85 throw new InvalidActionTakenException(errorMessage);
86 }
87
88
89
90
91 List<ActionRequestValue> documentRequests = getActionRequestService().findPendingByDoc(getDocumentId());
92 List<ActionRequestValue> workgroupRequests = new ArrayList<ActionRequestValue>();
93 for (ActionRequestValue actionRequest : documentRequests)
94 {
95 if (actionRequest.isGroupRequest() && actionRequest.getGroup().getId().equals(groupId))
96 {
97 workgroupRequests.add(actionRequest);
98 }
99 }
100
101 ActionTakenValue actionTaken = saveActionTaken(findDelegatorForActionRequests(workgroupRequests));
102 notifyActionTaken(actionTaken);
103
104 ActionListService actionListService = KEWServiceLocator.getActionListService();
105 Collection<ActionItem> actionItems = actionListService.findByDocumentId(getDocumentId());
106 for (ActionItem actionItem : actionItems)
107 {
108
109 if (actionItem.isWorkgroupItem() && actionItem.getGroupId().equals(groupId) &&
110 !actionItem.getPrincipalId().equals(getPrincipal().getPrincipalId()))
111 {
112 actionListService.deleteActionItem(actionItem);
113 }
114 }
115 }
116 }