1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.actions; |
18 | |
|
19 | |
import org.apache.cxf.common.util.StringUtils; |
20 | |
import org.kuali.rice.kew.actionrequest.*; |
21 | |
import org.kuali.rice.kew.engine.node.RouteNodeInstance; |
22 | |
import org.kuali.rice.kew.exception.InvalidActionTakenException; |
23 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
24 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
25 | |
import org.kuali.rice.kew.util.KEWConstants; |
26 | |
import org.kuali.rice.kim.api.entity.principal.PrincipalContract; |
27 | |
import org.kuali.rice.kim.api.group.Group; |
28 | |
|
29 | |
|
30 | |
import java.util.List; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class AdHocAction extends ActionTakenEvent { |
39 | |
|
40 | |
|
41 | |
|
42 | 0 | private static final String NO_ACTION_TAKEN_CODE = null; |
43 | |
|
44 | |
private String actionRequested; |
45 | |
private String nodeName; |
46 | |
private String responsibilityDesc; |
47 | |
private Boolean forceAction; |
48 | |
private Recipient recipient; |
49 | |
private String annotation; |
50 | |
private String requestLabel; |
51 | |
|
52 | |
public AdHocAction(DocumentRouteHeaderValue routeHeader, PrincipalContract principal) { |
53 | 0 | super(NO_ACTION_TAKEN_CODE, routeHeader, principal); |
54 | 0 | } |
55 | |
|
56 | |
public AdHocAction(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation, String actionRequested, String nodeName, Recipient recipient, String responsibilityDesc, Boolean forceAction, String requestLabel) { |
57 | 0 | super(NO_ACTION_TAKEN_CODE, routeHeader, principal, annotation); |
58 | 0 | this.actionRequested = actionRequested; |
59 | 0 | this.nodeName = nodeName; |
60 | 0 | this.responsibilityDesc = responsibilityDesc; |
61 | 0 | this.forceAction = forceAction; |
62 | 0 | this.recipient = recipient; |
63 | 0 | this.annotation = annotation; |
64 | 0 | this.requestLabel = requestLabel; |
65 | 0 | } |
66 | |
|
67 | |
public void recordAction() throws InvalidActionTakenException { |
68 | 0 | String errorMessage = validateActionRules(); |
69 | 0 | if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) { |
70 | 0 | throw new InvalidActionTakenException(errorMessage); |
71 | |
} |
72 | 0 | List targetNodes = KEWServiceLocator.getRouteNodeService().getCurrentNodeInstances(getDocumentId()); |
73 | 0 | String error = adhocRouteAction(targetNodes, false); |
74 | 0 | if (!org.apache.commons.lang.StringUtils.isEmpty(error)) { |
75 | 0 | throw new InvalidActionTakenException(error); |
76 | |
} |
77 | 0 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
@Override |
83 | |
public String validateActionRules() { |
84 | 0 | List targetNodes = KEWServiceLocator.getRouteNodeService().getCurrentNodeInstances(getDocumentId()); |
85 | 0 | return validateActionRulesInternal(targetNodes); |
86 | |
} |
87 | |
|
88 | |
@Override |
89 | |
public String validateActionRules(List<ActionRequestValue> actionRequests) { |
90 | 0 | return validateActionRules(); |
91 | |
} |
92 | |
|
93 | |
private String validateActionRulesInternal(List<RouteNodeInstance> targetNodes) { |
94 | |
|
95 | 0 | if (recipient != null) { |
96 | 0 | if (recipient instanceof KimPrincipalRecipient) { |
97 | 0 | KimPrincipalRecipient principalRecipient = (KimPrincipalRecipient)recipient; |
98 | 0 | if (!KEWServiceLocator.getDocumentTypePermissionService().canReceiveAdHocRequest(principalRecipient.getPrincipalId(), getRouteHeader().getDocumentType(), actionRequested)) { |
99 | 0 | return "The principal '" + principalRecipient.getPrincipal().getPrincipalName() + "' does not have permission to recieve ad hoc requests on DocumentType '" + getRouteHeader().getDocumentType().getName() + "'"; |
100 | |
} |
101 | 0 | } else if (recipient instanceof KimGroupRecipient) { |
102 | 0 | Group group = ((KimGroupRecipient)recipient).getGroup(); |
103 | 0 | if (!KEWServiceLocator.getDocumentTypePermissionService().canGroupReceiveAdHocRequest("" + group.getId(), getRouteHeader().getDocumentType(), actionRequested)) { |
104 | 0 | return "The group '" + group.getName() + "' does not have permission to recieve ad hoc requests on DocumentType '" + getRouteHeader().getDocumentType().getName() + "'"; |
105 | |
} |
106 | 0 | } else { |
107 | 0 | return "Invalid Recipient type encountered: " + recipient.getClass(); |
108 | |
} |
109 | |
} |
110 | 0 | return adhocRouteAction(targetNodes, true); |
111 | |
} |
112 | |
|
113 | |
private String adhocRouteAction(List<RouteNodeInstance> targetNodes, boolean forValidationOnly) { |
114 | |
|
115 | |
|
116 | |
|
117 | 0 | boolean requestCreated = false; |
118 | 0 | for (RouteNodeInstance routeNode : targetNodes) |
119 | |
{ |
120 | |
|
121 | 0 | if (nodeName == null || routeNode.getName().equals(nodeName)) |
122 | |
{ |
123 | 0 | String message = createAdHocRequest(routeNode, forValidationOnly); |
124 | 0 | if (!StringUtils.isEmpty(message)) |
125 | |
{ |
126 | 0 | return message; |
127 | |
} |
128 | 0 | requestCreated = true; |
129 | 0 | if (nodeName == null) |
130 | |
{ |
131 | 0 | break; |
132 | |
} |
133 | 0 | } |
134 | |
} |
135 | |
|
136 | 0 | if (!requestCreated && targetNodes.isEmpty()) { |
137 | 0 | String message = createAdHocRequest(null, forValidationOnly); |
138 | 0 | if (!StringUtils.isEmpty(message)) { |
139 | 0 | return message; |
140 | |
} |
141 | 0 | requestCreated = true; |
142 | |
} |
143 | |
|
144 | 0 | if (!requestCreated) { |
145 | 0 | return "Didn't create request. The node name " + nodeName + " given is probably invalid "; |
146 | |
} |
147 | 0 | return ""; |
148 | |
} |
149 | |
|
150 | |
private String createAdHocRequest(RouteNodeInstance routeNode, boolean forValidationOnly) { |
151 | 0 | ActionRequestValue adhocRequest = new ActionRequestValue(); |
152 | 0 | if (!forValidationOnly) { |
153 | 0 | ActionRequestFactory arFactory = new ActionRequestFactory(routeHeader, routeNode); |
154 | 0 | adhocRequest = arFactory.createActionRequest(actionRequested, recipient, responsibilityDesc, forceAction, annotation); |
155 | 0 | adhocRequest.setResponsibilityId(KEWConstants.ADHOC_REQUEST_RESPONSIBILITY_ID); |
156 | 0 | adhocRequest.setRequestLabel(requestLabel); |
157 | 0 | } else { |
158 | 0 | adhocRequest.setActionRequested(actionRequested); |
159 | |
} |
160 | 0 | if (adhocRequest.isApproveOrCompleteRequest() && ! (routeHeader.isEnroute() || routeHeader.isStateInitiated() || |
161 | |
routeHeader.isStateSaved())) { |
162 | 0 | return "Cannot AdHoc a Complete or Approve request when document is in state '" + routeHeader.getDocRouteStatusLabel() + "'."; |
163 | |
} |
164 | 0 | if (!forValidationOnly) { |
165 | |
|
166 | 0 | if (routeHeader.isDisaproved() || routeHeader.isCanceled() || routeHeader.isFinal() || routeHeader.isProcessed()) { |
167 | 0 | getActionRequestService().activateRequest(adhocRequest); |
168 | |
} else { |
169 | 0 | KEWServiceLocator.getActionRequestService().saveActionRequest(adhocRequest); |
170 | |
} |
171 | |
} |
172 | 0 | return ""; |
173 | |
} |
174 | |
} |