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.MoveDocumentService; |
23 | |
import org.kuali.rice.kew.actiontaken.ActionTakenValue; |
24 | |
import org.kuali.rice.kew.engine.BlanketApproveEngine; |
25 | |
import org.kuali.rice.kew.engine.OrchestrationConfig; |
26 | |
import org.kuali.rice.kew.engine.node.RouteNode; |
27 | |
import org.kuali.rice.kew.engine.node.RouteNodeInstance; |
28 | |
import org.kuali.rice.kew.exception.InvalidActionTakenException; |
29 | |
import org.kuali.rice.kew.messaging.MessageServiceNames; |
30 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
31 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
32 | |
import org.kuali.rice.kew.util.KEWConstants; |
33 | |
import org.kuali.rice.kim.api.entity.principal.PrincipalContract; |
34 | |
|
35 | |
|
36 | |
import java.util.Collection; |
37 | |
import java.util.HashSet; |
38 | |
import java.util.List; |
39 | |
import java.util.Set; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public class MoveDocumentAction extends ActionTakenEvent { |
51 | |
|
52 | 0 | protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass()); |
53 | |
|
54 | |
private MovePoint movePoint; |
55 | |
|
56 | |
public MoveDocumentAction(DocumentRouteHeaderValue routeHeader, PrincipalContract principal) { |
57 | 0 | super(KEWConstants.ACTION_TAKEN_MOVE_CD, routeHeader, principal); |
58 | 0 | } |
59 | |
|
60 | |
public MoveDocumentAction(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation, MovePoint movePoint) { |
61 | 0 | super(KEWConstants.ACTION_TAKEN_MOVE_CD, routeHeader, principal, annotation); |
62 | 0 | this.movePoint = movePoint; |
63 | 0 | } |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
@Override |
69 | |
public String validateActionRules() { |
70 | 0 | return validateActionRules(getActionRequestService().findAllPendingRequests(routeHeader.getDocumentId()), KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(getRouteHeader().getDocumentId())); |
71 | |
} |
72 | |
|
73 | |
@Override |
74 | |
public String validateActionRules(List<ActionRequestValue> actionRequests) { |
75 | 0 | return validateActionRules(actionRequests, KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(getRouteHeader().getDocumentId())); |
76 | |
} |
77 | |
|
78 | |
private String validateActionRules(List<ActionRequestValue> actionRequests, Collection activeNodes) { |
79 | 0 | if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) { |
80 | 0 | return "Document is not in a state to be moved"; |
81 | |
} |
82 | 0 | if (activeNodes.isEmpty()) { |
83 | 0 | return "Document has no active nodes."; |
84 | |
} |
85 | 0 | List<ActionRequestValue> filteredActionRequests = filterActionRequestsByCode(actionRequests, KEWConstants.ACTION_REQUEST_COMPLETE_REQ); |
86 | 0 | if (!isActionCompatibleRequest(filteredActionRequests)) { |
87 | 0 | return "No request for the user is compatible with the MOVE action"; |
88 | |
} |
89 | 0 | return ""; |
90 | |
} |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public boolean isActionCompatibleRequest(List<ActionRequestValue> requests) { |
97 | |
|
98 | 0 | return true; |
99 | |
} |
100 | |
|
101 | |
public void recordAction() throws InvalidActionTakenException { |
102 | 0 | MDC.put("docId", getRouteHeader().getDocumentId()); |
103 | 0 | updateSearchableAttributesIfPossible(); |
104 | 0 | LOG.debug("Moving document " + getRouteHeader().getDocumentId() + " to point: " + displayMovePoint(movePoint) + ", annotation: " + annotation); |
105 | |
|
106 | 0 | List actionRequests = getActionRequestService().findAllValidRequests(getPrincipal().getPrincipalId(), getDocumentId(), KEWConstants.ACTION_REQUEST_COMPLETE_REQ); |
107 | 0 | Collection activeNodes = KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(getRouteHeader().getDocumentId()); |
108 | 0 | String errorMessage = validateActionRules(actionRequests,activeNodes); |
109 | 0 | if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) { |
110 | 0 | throw new InvalidActionTakenException(errorMessage); |
111 | |
} |
112 | |
|
113 | 0 | RouteNodeInstance startNodeInstance = determineStartNode(activeNodes, movePoint); |
114 | |
|
115 | 0 | LOG.debug("Record the move action"); |
116 | 0 | Recipient delegator = findDelegatorForActionRequests(actionRequests); |
117 | 0 | ActionTakenValue actionTaken = saveActionTaken(delegator); |
118 | 0 | getActionRequestService().deactivateRequests(actionTaken, actionRequests); |
119 | 0 | notifyActionTaken(actionTaken); |
120 | |
|
121 | |
|
122 | 0 | if (movePoint.getStepsToMove() > 0) { |
123 | 0 | Set<String> targetNodeNames = new HashSet<String>(); |
124 | 0 | targetNodeNames.add(determineFutureNodeName(startNodeInstance, movePoint)); |
125 | |
|
126 | 0 | MoveDocumentService moveDocumentProcessor = MessageServiceNames.getMoveDocumentProcessorService(getRouteHeader()); |
127 | 0 | moveDocumentProcessor.moveDocument(getPrincipal().getPrincipalId(), getRouteHeader(), actionTaken, targetNodeNames); |
128 | |
|
129 | 0 | } else { |
130 | 0 | String targetNodeName = determineReturnNodeName(startNodeInstance, movePoint); |
131 | 0 | ReturnToPreviousNodeAction returnAction = new ReturnToPreviousNodeAction(KEWConstants.ACTION_TAKEN_MOVE_CD, getRouteHeader(), getPrincipal(), annotation, targetNodeName, false); |
132 | |
|
133 | 0 | returnAction.recordAction(); |
134 | |
} |
135 | 0 | } |
136 | |
|
137 | |
public void performDeferredMoveDocumentWork(ActionTakenValue actionTaken, Set<String> nodeNames) throws Exception { |
138 | |
|
139 | 0 | if (getRouteHeader().isInException()) { |
140 | 0 | LOG.debug("Moving document back to Enroute from Exception"); |
141 | |
|
142 | 0 | String oldStatus = getRouteHeader().getDocRouteStatus(); |
143 | 0 | getRouteHeader().markDocumentEnroute(); |
144 | |
|
145 | 0 | String newStatus = getRouteHeader().getDocRouteStatus(); |
146 | 0 | notifyStatusChange(newStatus, oldStatus); |
147 | |
} |
148 | 0 | OrchestrationConfig config = new OrchestrationConfig(); |
149 | 0 | config.setCause(actionTaken); |
150 | 0 | config.setDestinationNodeNames(nodeNames); |
151 | 0 | config.setSendNotifications(false); |
152 | 0 | BlanketApproveEngine blanketApproveEngine = KEWServiceLocator.getBlanketApproveEngineFactory().newEngine(config); |
153 | 0 | blanketApproveEngine.process(getRouteHeader().getDocumentId(), null); |
154 | |
|
155 | 0 | queueDocumentProcessing(); |
156 | 0 | } |
157 | |
|
158 | |
private RouteNodeInstance determineStartNode(Collection<RouteNodeInstance> activeNodes, MovePoint movePoint) throws InvalidActionTakenException { |
159 | 0 | RouteNodeInstance startNodeInstance = null; |
160 | 0 | for (RouteNodeInstance nodeInstance : activeNodes) |
161 | |
{ |
162 | 0 | if (nodeInstance.getName().equals(movePoint.getStartNodeName())) |
163 | |
{ |
164 | 0 | if (startNodeInstance != null) |
165 | |
{ |
166 | 0 | throw new InvalidActionTakenException("More than one active node exists with the given name: " + movePoint.getStartNodeName()); |
167 | |
} |
168 | 0 | startNodeInstance = nodeInstance; |
169 | |
} |
170 | |
} |
171 | 0 | if (startNodeInstance == null) { |
172 | 0 | throw new InvalidActionTakenException("Could not locate an active node with the given name: " + movePoint.getStartNodeName()); |
173 | |
} |
174 | 0 | return startNodeInstance; |
175 | |
} |
176 | |
|
177 | |
private String determineFutureNodeName(RouteNodeInstance startNodeInstance, MovePoint movePoint) throws InvalidActionTakenException { |
178 | 0 | return determineFutureNodeName(startNodeInstance.getRouteNode(), movePoint, 0, new HashSet()); |
179 | |
} |
180 | |
|
181 | |
private String determineFutureNodeName(RouteNode node, MovePoint movePoint, int currentStep, Set nodesProcessed) throws InvalidActionTakenException { |
182 | 0 | if (nodesProcessed.contains(node.getRouteNodeId())) { |
183 | 0 | throw new InvalidActionTakenException("Detected a cycle at node " + node.getRouteNodeName() + " when attempting to move document."); |
184 | |
} |
185 | 0 | nodesProcessed.add(node.getRouteNodeId()); |
186 | 0 | if (currentStep == movePoint.getStepsToMove()) { |
187 | 0 | return node.getRouteNodeName(); |
188 | |
} |
189 | 0 | List nextNodes = node.getNextNodes(); |
190 | 0 | if (nextNodes.size() == 0) { |
191 | 0 | throw new InvalidActionTakenException("Could not proceed forward, there are no more nodes in the route. Halted on step " + currentStep); |
192 | |
} |
193 | 0 | if (nextNodes.size() != 1) { |
194 | 0 | throw new InvalidActionTakenException("Cannot move forward in a multi-branch path. Located "+nextNodes.size()+" branches. Halted on step " + currentStep); |
195 | |
} |
196 | 0 | return determineFutureNodeName((RouteNode)nextNodes.get(0), movePoint, currentStep+1, nodesProcessed); |
197 | |
} |
198 | |
|
199 | |
private String determineReturnNodeName(RouteNodeInstance startNodeInstance, MovePoint movePoint) throws InvalidActionTakenException { |
200 | 0 | return determineReturnNodeName(startNodeInstance.getRouteNode(), movePoint, 0); |
201 | |
} |
202 | |
|
203 | |
private String determineReturnNodeName(RouteNode node, MovePoint movePoint, int currentStep) throws InvalidActionTakenException { |
204 | 0 | if (currentStep == movePoint.getStepsToMove()) { |
205 | 0 | return node.getRouteNodeName(); |
206 | |
} |
207 | 0 | List previousNodes = node.getPreviousNodes(); |
208 | 0 | if (previousNodes.size() == 0) { |
209 | 0 | throw new InvalidActionTakenException("Could not locate the named target node in the document's past route. Halted on step " + currentStep); |
210 | |
} |
211 | 0 | if (previousNodes.size() != 1) { |
212 | 0 | throw new InvalidActionTakenException("Located a multi-branch path, could not proceed backward past this point. Halted on step " + currentStep); |
213 | |
} |
214 | 0 | return determineReturnNodeName((RouteNode)previousNodes.get(0), movePoint, currentStep-1); |
215 | |
} |
216 | |
|
217 | |
private String displayMovePoint(MovePoint movePoint) { |
218 | 0 | return "fromNode="+movePoint.getStartNodeName()+", stepsToMove="+movePoint.getStepsToMove(); |
219 | |
} |
220 | |
|
221 | |
} |