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 java.util.Collections; |
19 | |
import java.util.HashSet; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.List; |
22 | |
import java.util.Set; |
23 | |
|
24 | |
import org.apache.log4j.MDC; |
25 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
26 | |
import org.kuali.rice.kew.actionrequest.Recipient; |
27 | |
import org.kuali.rice.kew.api.document.DocumentOrchestrationQueue; |
28 | |
import org.kuali.rice.kew.actiontaken.ActionTakenValue; |
29 | |
import org.kuali.rice.kew.api.WorkflowRuntimeException; |
30 | |
import org.kuali.rice.kew.api.document.DocumentProcessingOptions; |
31 | |
import org.kuali.rice.kew.api.exception.InvalidActionTakenException; |
32 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
33 | |
import org.kuali.rice.kew.engine.BlanketApproveEngine; |
34 | |
import org.kuali.rice.kew.engine.CompatUtils; |
35 | |
import org.kuali.rice.kew.engine.OrchestrationConfig; |
36 | |
import org.kuali.rice.kew.engine.OrchestrationConfig.EngineCapability; |
37 | |
import org.kuali.rice.kew.engine.RouteContext; |
38 | |
import org.kuali.rice.kew.engine.node.RouteNode; |
39 | |
import org.kuali.rice.kew.engine.node.service.RouteNodeService; |
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.api.KewApiConstants; |
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(KewApiConstants.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(KewApiConstants.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, KewApiConstants.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(), KewApiConstants.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().hasSearchableAttributes() && RouteContext.getCurrentRouteContext().isSearchIndexingRequestedForContext(); |
175 | |
|
176 | 0 | DocumentOrchestrationQueue blanketApprove = MessageServiceNames.getDocumentOrchestrationQueue(routeHeader); |
177 | 0 | org.kuali.rice.kew.api.document.OrchestrationConfig orchestrationConfig = |
178 | |
org.kuali.rice.kew.api.document.OrchestrationConfig.create(actionTaken.getActionTakenId(), nodeNames); |
179 | 0 | DocumentProcessingOptions options = DocumentProcessingOptions.create(true, shouldIndex); |
180 | 0 | blanketApprove.orchestrateDocument(routeHeader.getDocumentId(), getPrincipal().getPrincipalId(), |
181 | |
orchestrationConfig, options); |
182 | 0 | } catch (Exception e) { |
183 | 0 | LOG.error(e); |
184 | 0 | throw new WorkflowRuntimeException(e); |
185 | 0 | } |
186 | 0 | } |
187 | |
|
188 | |
public void performDeferredBlanketApproveWork(ActionTakenValue actionTaken, DocumentProcessingOptions processingOptions) throws Exception { |
189 | |
|
190 | 0 | if (getRouteHeader().isInException()) { |
191 | 0 | LOG.debug("Moving document back to Enroute from Exception"); |
192 | |
|
193 | 0 | String oldStatus = getRouteHeader().getDocRouteStatus(); |
194 | 0 | getRouteHeader().markDocumentEnroute(); |
195 | |
|
196 | 0 | String newStatus = getRouteHeader().getDocRouteStatus(); |
197 | 0 | notifyStatusChange(newStatus, oldStatus); |
198 | |
} |
199 | 0 | OrchestrationConfig config = new OrchestrationConfig(EngineCapability.BLANKET_APPROVAL, nodeNames, actionTaken, processingOptions.isSendNotifications(), processingOptions.isRunPostProcessor()); |
200 | 0 | BlanketApproveEngine blanketApproveEngine = KEWServiceLocator.getWorkflowEngineFactory().newEngine(config); |
201 | 0 | blanketApproveEngine.process(getRouteHeader().getDocumentId(), null); |
202 | |
|
203 | 0 | queueDocumentProcessing(); |
204 | 0 | } |
205 | |
|
206 | |
protected void markDocumentEnroute(DocumentRouteHeaderValue routeHeader) throws InvalidActionTakenException { |
207 | 0 | String oldStatus = getRouteHeader().getDocRouteStatus(); |
208 | 0 | getRouteHeader().markDocumentEnroute(); |
209 | |
|
210 | 0 | String newStatus = getRouteHeader().getDocRouteStatus(); |
211 | 0 | notifyStatusChange(newStatus, oldStatus); |
212 | 0 | KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader()); |
213 | 0 | } |
214 | |
|
215 | |
private RouteNodeService getRouteNodeService() { |
216 | 0 | return KEWServiceLocator.getRouteNodeService(); |
217 | |
} |
218 | |
} |