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 java.util.Collections; |
20 | |
import java.util.HashSet; |
21 | |
import java.util.Iterator; |
22 | |
import java.util.List; |
23 | |
import java.util.Set; |
24 | |
|
25 | |
import org.apache.log4j.MDC; |
26 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
27 | |
import org.kuali.rice.kew.actionrequest.Recipient; |
28 | |
import org.kuali.rice.kew.actions.asyncservices.BlanketApproveProcessorService; |
29 | |
import org.kuali.rice.kew.actiontaken.ActionTakenValue; |
30 | |
import org.kuali.rice.kew.api.WorkflowRuntimeException; |
31 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
32 | |
import org.kuali.rice.kew.engine.BlanketApproveEngine; |
33 | |
import org.kuali.rice.kew.engine.CompatUtils; |
34 | |
import org.kuali.rice.kew.engine.OrchestrationConfig; |
35 | |
import org.kuali.rice.kew.engine.OrchestrationConfig.EngineCapability; |
36 | |
import org.kuali.rice.kew.engine.RouteContext; |
37 | |
import org.kuali.rice.kew.engine.node.RouteNode; |
38 | |
import org.kuali.rice.kew.engine.node.service.RouteNodeService; |
39 | |
import org.kuali.rice.kew.exception.InvalidActionTakenException; |
40 | |
import org.kuali.rice.kew.messaging.MessageServiceNames; |
41 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
42 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
43 | |
import org.kuali.rice.kew.util.KEWConstants; |
44 | |
import org.kuali.rice.kim.api.identity.principal.PrincipalContract; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public class BlanketApproveAction extends ActionTakenEvent { |
53 | |
|
54 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BlanketApproveAction.class); |
55 | |
private Set<String> nodeNames; |
56 | |
|
57 | |
public BlanketApproveAction(DocumentRouteHeaderValue rh, PrincipalContract principal) { |
58 | 0 | super(KEWConstants.ACTION_TAKEN_BLANKET_APPROVE_CD, rh, principal); |
59 | |
|
60 | 0 | setQueueDocumentAfterAction(false); |
61 | |
|
62 | 0 | } |
63 | |
|
64 | |
public BlanketApproveAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation, Integer routeLevel) { |
65 | 0 | this(rh, principal, annotation, convertRouteLevel(rh.getDocumentType(), routeLevel)); |
66 | 0 | } |
67 | |
|
68 | |
public BlanketApproveAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation, String nodeName) { |
69 | 0 | this(rh, principal, annotation, Collections.singleton(nodeName)); |
70 | |
|
71 | 0 | } |
72 | |
|
73 | |
public BlanketApproveAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation, Set<String> nodeNames) { |
74 | 0 | super(KEWConstants.ACTION_TAKEN_BLANKET_APPROVE_CD, rh, principal, annotation); |
75 | 0 | this.nodeNames = (nodeNames == null ? new HashSet<String>() : nodeNames); |
76 | 0 | setQueueDocumentAfterAction(false); |
77 | 0 | } |
78 | |
|
79 | |
private static Set<String> convertRouteLevel(DocumentType documentType, Integer routeLevel) { |
80 | 0 | Set<String> nodeNames = new HashSet<String>(); |
81 | 0 | if (routeLevel == null) { |
82 | 0 | return nodeNames; |
83 | |
} |
84 | 0 | RouteNode node = CompatUtils.getNodeForLevel(documentType, routeLevel); |
85 | 0 | if (node == null) { |
86 | 0 | throw new WorkflowRuntimeException("Could not locate a valid node for the given route level: " + routeLevel); |
87 | |
} |
88 | 0 | nodeNames.add(node.getRouteNodeName()); |
89 | 0 | return nodeNames; |
90 | |
} |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
@Override |
96 | |
public String validateActionRules() { |
97 | 0 | return validateActionRules(getActionRequestService().findAllPendingRequests(routeHeader.getDocumentId())); |
98 | |
} |
99 | |
|
100 | |
public String validateActionRules(List<ActionRequestValue> actionRequests) { |
101 | 0 | if ( (nodeNames != null) && (!nodeNames.isEmpty()) ) { |
102 | 0 | String nodeName = isGivenNodeListValid(); |
103 | 0 | if (!org.apache.commons.lang.StringUtils.isEmpty(nodeName)) { |
104 | 0 | return "Document already at or beyond route node " + nodeName; |
105 | |
} |
106 | |
} |
107 | 0 | if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) { |
108 | 0 | return "Document is not in a state to be approved"; |
109 | |
} |
110 | 0 | List<ActionRequestValue> filteredActionRequests = filterActionRequestsByCode(actionRequests, KEWConstants.ACTION_REQUEST_COMPLETE_REQ); |
111 | 0 | if (!isActionCompatibleRequest(filteredActionRequests)) { |
112 | 0 | return "No request for the user is compatible with the BlanketApprove Action"; |
113 | |
} |
114 | |
|
115 | 0 | if (! KEWServiceLocator.getDocumentTypePermissionService().canBlanketApprove(getPrincipal().getPrincipalId(), getRouteHeader().getDocumentType(), getRouteHeader().getDocRouteStatus(), getRouteHeader().getInitiatorWorkflowId())) { |
116 | 0 | return "User is not authorized to BlanketApprove document"; |
117 | |
} |
118 | 0 | return ""; |
119 | |
} |
120 | |
|
121 | |
private String isGivenNodeListValid() { |
122 | 0 | for (Iterator<String> iterator = nodeNames.iterator(); iterator.hasNext();) { |
123 | 0 | String nodeName = (String) iterator.next(); |
124 | 0 | if (nodeName == null) { |
125 | 0 | iterator.remove(); |
126 | 0 | continue; |
127 | |
} |
128 | 0 | if (!getRouteNodeService().isNodeInPath(getRouteHeader(), nodeName)) { |
129 | 0 | return nodeName; |
130 | |
} |
131 | 0 | } |
132 | 0 | return ""; |
133 | |
} |
134 | |
|
135 | |
public void recordAction() throws InvalidActionTakenException { |
136 | 0 | MDC.put("docId", getRouteHeader().getDocumentId()); |
137 | 0 | updateSearchableAttributesIfPossible(); |
138 | |
|
139 | 0 | List<ActionRequestValue> actionRequests = getActionRequestService().findAllValidRequests(getPrincipal().getPrincipalId(), getDocumentId(), KEWConstants.ACTION_REQUEST_COMPLETE_REQ); |
140 | 0 | String errorMessage = validateActionRules(actionRequests); |
141 | 0 | if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) { |
142 | 0 | throw new InvalidActionTakenException(errorMessage); |
143 | |
} |
144 | |
|
145 | 0 | LOG.debug("Checking to see if the action is legal"); |
146 | |
|
147 | 0 | LOG.debug("Blanket approving document : " + annotation); |
148 | |
|
149 | 0 | if (getRouteHeader().isStateInitiated() || getRouteHeader().isStateSaved()) { |
150 | 0 | markDocumentEnroute(getRouteHeader()); |
151 | 0 | getRouteHeader().setRoutedByUserWorkflowId(getPrincipal().getPrincipalId()); |
152 | |
} |
153 | |
|
154 | 0 | LOG.debug("Record the blanket approval action"); |
155 | 0 | Recipient delegator = findDelegatorForActionRequests(actionRequests); |
156 | 0 | ActionTakenValue actionTaken = saveActionTaken(delegator); |
157 | |
|
158 | 0 | LOG.debug("Deactivate pending action requests for user"); |
159 | 0 | getActionRequestService().deactivateRequests(actionTaken, actionRequests); |
160 | 0 | notifyActionTaken(actionTaken); |
161 | |
|
162 | 0 | KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader()); |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | 0 | queueDeferredWork(actionTaken); |
170 | 0 | } |
171 | |
|
172 | |
protected void queueDeferredWork(ActionTakenValue actionTaken) { |
173 | |
try { |
174 | 0 | final boolean shouldIndex = getRouteHeader().getDocumentType().hasSearchableAttributesOld() && RouteContext.getCurrentRouteContext().isSearchIndexingRequestedForContext(); |
175 | |
|
176 | 0 | BlanketApproveProcessorService blanketApprove = MessageServiceNames.getBlanketApproveProcessorService(routeHeader); |
177 | 0 | blanketApprove.doBlanketApproveWork(routeHeader.getDocumentId(), getPrincipal().getPrincipalId(), actionTaken.getActionTakenId(), nodeNames, shouldIndex); |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | 0 | } catch (Exception e) { |
187 | 0 | LOG.error(e); |
188 | 0 | throw new WorkflowRuntimeException(e); |
189 | 0 | } |
190 | |
|
191 | |
|
192 | 0 | } |
193 | |
|
194 | |
public void performDeferredBlanketApproveWork(ActionTakenValue actionTaken) throws Exception { |
195 | |
|
196 | 0 | if (getRouteHeader().isInException()) { |
197 | 0 | LOG.debug("Moving document back to Enroute from Exception"); |
198 | |
|
199 | 0 | String oldStatus = getRouteHeader().getDocRouteStatus(); |
200 | 0 | getRouteHeader().markDocumentEnroute(); |
201 | |
|
202 | 0 | String newStatus = getRouteHeader().getDocRouteStatus(); |
203 | 0 | notifyStatusChange(newStatus, oldStatus); |
204 | |
} |
205 | 0 | OrchestrationConfig config = new OrchestrationConfig(EngineCapability.BLANKET_APPROVAL, nodeNames, actionTaken); |
206 | 0 | BlanketApproveEngine blanketApproveEngine = KEWServiceLocator.getWorkflowEngineFactory().newEngine(config); |
207 | 0 | blanketApproveEngine.process(getRouteHeader().getDocumentId(), null); |
208 | |
|
209 | 0 | queueDocumentProcessing(); |
210 | 0 | } |
211 | |
|
212 | |
protected void markDocumentEnroute(DocumentRouteHeaderValue routeHeader) throws InvalidActionTakenException { |
213 | 0 | String oldStatus = getRouteHeader().getDocRouteStatus(); |
214 | 0 | getRouteHeader().markDocumentEnroute(); |
215 | |
|
216 | 0 | String newStatus = getRouteHeader().getDocRouteStatus(); |
217 | 0 | notifyStatusChange(newStatus, oldStatus); |
218 | 0 | KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader()); |
219 | 0 | } |
220 | |
|
221 | |
private RouteNodeService getRouteNodeService() { |
222 | 0 | return KEWServiceLocator.getRouteNodeService(); |
223 | |
} |
224 | |
} |