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.kew.util.Utilities; |
34 | |
import org.kuali.rice.kim.bo.entity.KimPrincipal; |
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, KimPrincipal principal) { |
57 | 0 | super(KEWConstants.ACTION_TAKEN_MOVE_CD, routeHeader, principal); |
58 | 0 | } |
59 | |
|
60 | |
public MoveDocumentAction(DocumentRouteHeaderValue routeHeader, KimPrincipal 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.getRouteHeaderId()), KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(getRouteHeader().getRouteHeaderId())); |
71 | |
} |
72 | |
|
73 | |
@Override |
74 | |
public String validateActionRules(List<ActionRequestValue> actionRequests) { |
75 | 0 | return validateActionRules(actionRequests, KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(getRouteHeader().getRouteHeaderId())); |
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().getRouteHeaderId()); |
103 | 0 | updateSearchableAttributesIfPossible(); |
104 | 0 | LOG.debug("Moving document " + getRouteHeader().getRouteHeaderId() + " to point: " + displayMovePoint(movePoint) + ", annotation: " + annotation); |
105 | |
|
106 | 0 | List actionRequests = getActionRequestService().findAllValidRequests(getPrincipal().getPrincipalId(), getRouteHeaderId(), KEWConstants.ACTION_REQUEST_COMPLETE_REQ); |
107 | 0 | Collection activeNodes = KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(getRouteHeader().getRouteHeaderId()); |
108 | 0 | String errorMessage = validateActionRules(actionRequests,activeNodes); |
109 | 0 | if (!Utilities.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 | new BlanketApproveEngine(config).process(getRouteHeader().getRouteHeaderId(), null); |
153 | |
|
154 | 0 | queueDocumentProcessing(); |
155 | 0 | } |
156 | |
|
157 | |
private RouteNodeInstance determineStartNode(Collection<RouteNodeInstance> activeNodes, MovePoint movePoint) throws InvalidActionTakenException { |
158 | 0 | RouteNodeInstance startNodeInstance = null; |
159 | 0 | for (RouteNodeInstance nodeInstance : activeNodes) |
160 | |
{ |
161 | 0 | if (nodeInstance.getName().equals(movePoint.getStartNodeName())) |
162 | |
{ |
163 | 0 | if (startNodeInstance != null) |
164 | |
{ |
165 | 0 | throw new InvalidActionTakenException("More than one active node exists with the given name: " + movePoint.getStartNodeName()); |
166 | |
} |
167 | 0 | startNodeInstance = nodeInstance; |
168 | |
} |
169 | |
} |
170 | 0 | if (startNodeInstance == null) { |
171 | 0 | throw new InvalidActionTakenException("Could not locate an active node with the given name: " + movePoint.getStartNodeName()); |
172 | |
} |
173 | 0 | return startNodeInstance; |
174 | |
} |
175 | |
|
176 | |
private String determineFutureNodeName(RouteNodeInstance startNodeInstance, MovePoint movePoint) throws InvalidActionTakenException { |
177 | 0 | return determineFutureNodeName(startNodeInstance.getRouteNode(), movePoint, 0, new HashSet()); |
178 | |
} |
179 | |
|
180 | |
private String determineFutureNodeName(RouteNode node, MovePoint movePoint, int currentStep, Set nodesProcessed) throws InvalidActionTakenException { |
181 | 0 | if (nodesProcessed.contains(node.getRouteNodeId())) { |
182 | 0 | throw new InvalidActionTakenException("Detected a cycle at node " + node.getRouteNodeName() + " when attempting to move document."); |
183 | |
} |
184 | 0 | nodesProcessed.add(node.getRouteNodeId()); |
185 | 0 | if (currentStep == movePoint.getStepsToMove()) { |
186 | 0 | return node.getRouteNodeName(); |
187 | |
} |
188 | 0 | List nextNodes = node.getNextNodes(); |
189 | 0 | if (nextNodes.size() == 0) { |
190 | 0 | throw new InvalidActionTakenException("Could not proceed forward, there are no more nodes in the route. Halted on step " + currentStep); |
191 | |
} |
192 | 0 | if (nextNodes.size() != 1) { |
193 | 0 | throw new InvalidActionTakenException("Cannot move forward in a multi-branch path. Located "+nextNodes.size()+" branches. Halted on step " + currentStep); |
194 | |
} |
195 | 0 | return determineFutureNodeName((RouteNode)nextNodes.get(0), movePoint, currentStep+1, nodesProcessed); |
196 | |
} |
197 | |
|
198 | |
private String determineReturnNodeName(RouteNodeInstance startNodeInstance, MovePoint movePoint) throws InvalidActionTakenException { |
199 | 0 | return determineReturnNodeName(startNodeInstance.getRouteNode(), movePoint, 0); |
200 | |
} |
201 | |
|
202 | |
private String determineReturnNodeName(RouteNode node, MovePoint movePoint, int currentStep) throws InvalidActionTakenException { |
203 | 0 | if (currentStep == movePoint.getStepsToMove()) { |
204 | 0 | return node.getRouteNodeName(); |
205 | |
} |
206 | 0 | List previousNodes = node.getPreviousNodes(); |
207 | 0 | if (previousNodes.size() == 0) { |
208 | 0 | throw new InvalidActionTakenException("Could not locate the named target node in the document's past route. Halted on step " + currentStep); |
209 | |
} |
210 | 0 | if (previousNodes.size() != 1) { |
211 | 0 | throw new InvalidActionTakenException("Located a multi-branch path, could not proceed backward past this point. Halted on step " + currentStep); |
212 | |
} |
213 | 0 | return determineReturnNodeName((RouteNode)previousNodes.get(0), movePoint, currentStep-1); |
214 | |
} |
215 | |
|
216 | |
private String displayMovePoint(MovePoint movePoint) { |
217 | 0 | return "fromNode="+movePoint.getStartNodeName()+", stepsToMove="+movePoint.getStepsToMove(); |
218 | |
} |
219 | |
|
220 | |
} |