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