Coverage Report - org.kuali.rice.kew.actions.MoveDocumentAction
 
Classes in this File Line Coverage Branch Coverage Complexity
MoveDocumentAction
0%
0/75
0%
0/36
3.308
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kew.actions;
 17  
 
 18  
 import java.util.Collection;
 19  
 import java.util.HashSet;
 20  
 import java.util.List;
 21  
 import java.util.Set;
 22  
 
 23  
 import org.apache.log4j.MDC;
 24  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 25  
 import org.kuali.rice.kew.actionrequest.Recipient;
 26  
 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
 27  
 import org.kuali.rice.kew.api.action.MovePoint;
 28  
 import org.kuali.rice.kew.api.document.DocumentOrchestrationQueue;
 29  
 import org.kuali.rice.kew.api.document.DocumentProcessingOptions;
 30  
 import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
 31  
 import org.kuali.rice.kew.engine.RouteContext;
 32  
 import org.kuali.rice.kew.engine.node.RouteNode;
 33  
 import org.kuali.rice.kew.engine.node.RouteNodeInstance;
 34  
 import org.kuali.rice.kew.messaging.MessageServiceNames;
 35  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 36  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 37  
 import org.kuali.rice.kew.api.KewApiConstants;
 38  
 import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
 39  
 
 40  
 
 41  
 /**
 42  
  * Returns a document to a previous node in the route.
 43  
  *
 44  
  * Current implementation only supports returning to a node on the main branch of the
 45  
  * document.
 46  
  *
 47  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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, PrincipalContract principal) {
 56  0
         super(KewApiConstants.ACTION_TAKEN_MOVE_CD, routeHeader, principal);
 57  0
     }
 58  
 
 59  
     public MoveDocumentAction(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation, MovePoint movePoint) {
 60  0
         super(KewApiConstants.ACTION_TAKEN_MOVE_CD, routeHeader, principal, annotation);
 61  0
         this.movePoint = movePoint;
 62  0
     }
 63  
 
 64  
     /* (non-Javadoc)
 65  
      * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
 66  
      */
 67  
     @Override
 68  
     public String validateActionRules() {
 69  0
         return validateActionRules(getActionRequestService().findAllPendingRequests(routeHeader.getDocumentId()), KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(getRouteHeader().getDocumentId()));
 70  
     }
 71  
 
 72  
     @Override
 73  
         public String validateActionRules(List<ActionRequestValue> actionRequests) {
 74  0
         return validateActionRules(actionRequests, KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(getRouteHeader().getDocumentId()));
 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, KewApiConstants.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  
     /* (non-Javadoc)
 93  
      * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
 94  
      */
 95  
     public boolean isActionCompatibleRequest(List<ActionRequestValue> requests) {
 96  
         //Move is always correct because the client application has authorized it
 97  0
         return true;
 98  
     }
 99  
 
 100  
     public void recordAction() throws InvalidActionTakenException {
 101  0
         MDC.put("docId", getRouteHeader().getDocumentId());
 102  0
         updateSearchableAttributesIfPossible();
 103  0
         LOG.debug("Moving document " + getRouteHeader().getDocumentId() + " to point: " + displayMovePoint(movePoint) + ", annotation: " + annotation);
 104  
 
 105  0
         List actionRequests = getActionRequestService().findAllValidRequests(getPrincipal().getPrincipalId(), getDocumentId(), KewApiConstants.ACTION_REQUEST_COMPLETE_REQ);
 106  0
         Collection activeNodes = KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(getRouteHeader().getDocumentId());
 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  
             // TODO this whole bit is a bit hacky at the moment
 121  0
             if (movePoint.getStepsToMove() > 0) {
 122  0
                 Set<String> targetNodeNames = new HashSet<String>();
 123  0
                 targetNodeNames.add(determineFutureNodeName(startNodeInstance, movePoint));
 124  
 
 125  0
                     final boolean shouldIndex = getRouteHeader().getDocumentType().hasSearchableAttributes() && RouteContext.getCurrentRouteContext().isSearchIndexingRequestedForContext();
 126  0
                     DocumentOrchestrationQueue orchestrationQueue = MessageServiceNames.getDocumentOrchestrationQueue(
 127  
                         routeHeader);
 128  0
                 org.kuali.rice.kew.api.document.OrchestrationConfig orchestrationConfig =
 129  
                     org.kuali.rice.kew.api.document.OrchestrationConfig.create(actionTaken.getActionTakenId(), targetNodeNames);
 130  0
                 DocumentProcessingOptions options = DocumentProcessingOptions.create(true, shouldIndex, false);
 131  0
                 orchestrationQueue.orchestrateDocument(routeHeader.getDocumentId(), getPrincipal().getPrincipalId(), orchestrationConfig, options);
 132  0
             } else {
 133  0
                 String targetNodeName = determineReturnNodeName(startNodeInstance, movePoint);
 134  0
                 ReturnToPreviousNodeAction returnAction = new ReturnToPreviousNodeAction(KewApiConstants.ACTION_TAKEN_MOVE_CD, getRouteHeader(), getPrincipal(), annotation, targetNodeName, false);
 135  
                 
 136  0
                 returnAction.recordAction();
 137  
             }
 138  0
     }
 139  
 
 140  
     private RouteNodeInstance determineStartNode(Collection<RouteNodeInstance> activeNodes, MovePoint movePoint) throws InvalidActionTakenException {
 141  0
         RouteNodeInstance startNodeInstance = null;
 142  0
         for (RouteNodeInstance nodeInstance : activeNodes)
 143  
         {
 144  0
             if (nodeInstance.getName().equals(movePoint.getStartNodeName()))
 145  
             {
 146  0
                 if (startNodeInstance != null)
 147  
                 {
 148  0
                     throw new InvalidActionTakenException("More than one active node exists with the given name:  " + movePoint.getStartNodeName());
 149  
                 }
 150  0
                 startNodeInstance = nodeInstance;
 151  
             }
 152  
         }
 153  0
         if (startNodeInstance == null) {
 154  0
             throw new InvalidActionTakenException("Could not locate an active node with the given name: " + movePoint.getStartNodeName());
 155  
         }
 156  0
         return startNodeInstance;
 157  
     }
 158  
 
 159  
     private String determineFutureNodeName(RouteNodeInstance startNodeInstance, MovePoint movePoint) throws InvalidActionTakenException {
 160  0
         return determineFutureNodeName(startNodeInstance.getRouteNode(), movePoint, 0, new HashSet());
 161  
     }
 162  
 
 163  
     private String determineFutureNodeName(RouteNode node, MovePoint movePoint, int currentStep, Set nodesProcessed) throws InvalidActionTakenException {
 164  0
         if (nodesProcessed.contains(node.getRouteNodeId())) {
 165  0
             throw new InvalidActionTakenException("Detected a cycle at node " + node.getRouteNodeName() + " when attempting to move document.");
 166  
         }
 167  0
         nodesProcessed.add(node.getRouteNodeId());
 168  0
         if (currentStep == movePoint.getStepsToMove()) {
 169  0
             return node.getRouteNodeName();
 170  
         }
 171  0
         List nextNodes = node.getNextNodes();
 172  0
         if (nextNodes.size() == 0) {
 173  0
             throw new InvalidActionTakenException("Could not proceed forward, there are no more nodes in the route.  Halted on step " + currentStep);
 174  
         }
 175  0
         if (nextNodes.size() != 1) {
 176  0
             throw new InvalidActionTakenException("Cannot move forward in a multi-branch path.  Located "+nextNodes.size()+" branches.  Halted on step " + currentStep);
 177  
         }
 178  0
         return determineFutureNodeName((RouteNode)nextNodes.get(0), movePoint, currentStep+1, nodesProcessed);
 179  
     }
 180  
 
 181  
     private String determineReturnNodeName(RouteNodeInstance startNodeInstance, MovePoint movePoint) throws InvalidActionTakenException {
 182  0
         return determineReturnNodeName(startNodeInstance.getRouteNode(), movePoint, 0);
 183  
     }
 184  
 
 185  
     private String determineReturnNodeName(RouteNode node, MovePoint movePoint, int currentStep) throws InvalidActionTakenException {
 186  0
         if (currentStep == movePoint.getStepsToMove()) {
 187  0
             return node.getRouteNodeName();
 188  
         }
 189  0
         List previousNodes = node.getPreviousNodes();
 190  0
         if (previousNodes.size() == 0) {
 191  0
             throw new InvalidActionTakenException("Could not locate the named target node in the document's past route.  Halted on step " + currentStep);
 192  
         }
 193  0
         if (previousNodes.size() != 1) {
 194  0
             throw new InvalidActionTakenException("Located a multi-branch path, could not proceed backward past this point.  Halted on step " + currentStep);
 195  
         }
 196  0
         return determineReturnNodeName((RouteNode)previousNodes.get(0), movePoint, currentStep-1);
 197  
     }
 198  
 
 199  
     private String displayMovePoint(MovePoint movePoint) {
 200  0
         return "fromNode="+movePoint.getStartNodeName()+", stepsToMove="+movePoint.getStepsToMove();
 201  
     }
 202  
 
 203  
 }