| 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.bo.entity.KimPrincipal; |
| 34 | |
|
| 35 | |
import java.util.Collection; |
| 36 | |
import java.util.HashSet; |
| 37 | |
import java.util.List; |
| 38 | |
import java.util.Set; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public class MoveDocumentAction extends ActionTakenEvent { |
| 50 | |
|
| 51 | 0 | protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass()); |
| 52 | |
|
| 53 | |
private MovePoint movePoint; |
| 54 | |
|
| 55 | |
public MoveDocumentAction(DocumentRouteHeaderValue routeHeader, KimPrincipal principal) { |
| 56 | 0 | super(KEWConstants.ACTION_TAKEN_MOVE_CD, routeHeader, principal); |
| 57 | 0 | } |
| 58 | |
|
| 59 | |
public MoveDocumentAction(DocumentRouteHeaderValue routeHeader, KimPrincipal principal, String annotation, MovePoint movePoint) { |
| 60 | 0 | super(KEWConstants.ACTION_TAKEN_MOVE_CD, routeHeader, principal, annotation); |
| 61 | 0 | this.movePoint = movePoint; |
| 62 | 0 | } |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
@Override |
| 68 | |
public String validateActionRules() { |
| 69 | 0 | return validateActionRules(getActionRequestService().findAllPendingRequests(routeHeader.getRouteHeaderId()), KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(getRouteHeader().getRouteHeaderId())); |
| 70 | |
} |
| 71 | |
|
| 72 | |
@Override |
| 73 | |
public String validateActionRules(List<ActionRequestValue> actionRequests) { |
| 74 | 0 | return validateActionRules(actionRequests, KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(getRouteHeader().getRouteHeaderId())); |
| 75 | |
} |
| 76 | |
|
| 77 | |
private String validateActionRules(List<ActionRequestValue> actionRequests, Collection activeNodes) { |
| 78 | 0 | if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) { |
| 79 | 0 | return "Document is not in a state to be moved"; |
| 80 | |
} |
| 81 | 0 | if (activeNodes.isEmpty()) { |
| 82 | 0 | return "Document has no active nodes."; |
| 83 | |
} |
| 84 | 0 | List<ActionRequestValue> filteredActionRequests = filterActionRequestsByCode(actionRequests, KEWConstants.ACTION_REQUEST_COMPLETE_REQ); |
| 85 | 0 | if (!isActionCompatibleRequest(filteredActionRequests)) { |
| 86 | 0 | return "No request for the user is compatible with the MOVE action"; |
| 87 | |
} |
| 88 | 0 | return ""; |
| 89 | |
} |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
public boolean isActionCompatibleRequest(List<ActionRequestValue> requests) { |
| 96 | |
|
| 97 | 0 | return true; |
| 98 | |
} |
| 99 | |
|
| 100 | |
public void recordAction() throws InvalidActionTakenException { |
| 101 | 0 | MDC.put("docId", getRouteHeader().getRouteHeaderId()); |
| 102 | 0 | updateSearchableAttributesIfPossible(); |
| 103 | 0 | LOG.debug("Moving document " + getRouteHeader().getRouteHeaderId() + " to point: " + displayMovePoint(movePoint) + ", annotation: " + annotation); |
| 104 | |
|
| 105 | 0 | List actionRequests = getActionRequestService().findAllValidRequests(getPrincipal().getPrincipalId(), getRouteHeaderId(), KEWConstants.ACTION_REQUEST_COMPLETE_REQ); |
| 106 | 0 | Collection activeNodes = KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(getRouteHeader().getRouteHeaderId()); |
| 107 | 0 | String errorMessage = validateActionRules(actionRequests,activeNodes); |
| 108 | 0 | if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) { |
| 109 | 0 | throw new InvalidActionTakenException(errorMessage); |
| 110 | |
} |
| 111 | |
|
| 112 | 0 | RouteNodeInstance startNodeInstance = determineStartNode(activeNodes, movePoint); |
| 113 | |
|
| 114 | 0 | LOG.debug("Record the move action"); |
| 115 | 0 | Recipient delegator = findDelegatorForActionRequests(actionRequests); |
| 116 | 0 | ActionTakenValue actionTaken = saveActionTaken(delegator); |
| 117 | 0 | getActionRequestService().deactivateRequests(actionTaken, actionRequests); |
| 118 | 0 | notifyActionTaken(actionTaken); |
| 119 | |
|
| 120 | |
|
| 121 | 0 | if (movePoint.getStepsToMove() > 0) { |
| 122 | 0 | Set<String> targetNodeNames = new HashSet<String>(); |
| 123 | 0 | targetNodeNames.add(determineFutureNodeName(startNodeInstance, movePoint)); |
| 124 | |
|
| 125 | 0 | MoveDocumentService moveDocumentProcessor = MessageServiceNames.getMoveDocumentProcessorService(getRouteHeader()); |
| 126 | 0 | moveDocumentProcessor.moveDocument(getPrincipal().getPrincipalId(), getRouteHeader(), actionTaken, targetNodeNames); |
| 127 | |
|
| 128 | 0 | } else { |
| 129 | 0 | String targetNodeName = determineReturnNodeName(startNodeInstance, movePoint); |
| 130 | 0 | ReturnToPreviousNodeAction returnAction = new ReturnToPreviousNodeAction(KEWConstants.ACTION_TAKEN_MOVE_CD, getRouteHeader(), getPrincipal(), annotation, targetNodeName, false); |
| 131 | |
|
| 132 | 0 | returnAction.recordAction(); |
| 133 | |
} |
| 134 | 0 | } |
| 135 | |
|
| 136 | |
public void performDeferredMoveDocumentWork(ActionTakenValue actionTaken, Set<String> nodeNames) throws Exception { |
| 137 | |
|
| 138 | 0 | if (getRouteHeader().isInException()) { |
| 139 | 0 | LOG.debug("Moving document back to Enroute from Exception"); |
| 140 | |
|
| 141 | 0 | String oldStatus = getRouteHeader().getDocRouteStatus(); |
| 142 | 0 | getRouteHeader().markDocumentEnroute(); |
| 143 | |
|
| 144 | 0 | String newStatus = getRouteHeader().getDocRouteStatus(); |
| 145 | 0 | notifyStatusChange(newStatus, oldStatus); |
| 146 | |
} |
| 147 | 0 | OrchestrationConfig config = new OrchestrationConfig(); |
| 148 | 0 | config.setCause(actionTaken); |
| 149 | 0 | config.setDestinationNodeNames(nodeNames); |
| 150 | 0 | config.setSendNotifications(false); |
| 151 | 0 | BlanketApproveEngine blanketApproveEngine = KEWServiceLocator.getBlanketApproveEngineFactory().newEngine(config); |
| 152 | 0 | blanketApproveEngine.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 | |
} |