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