Coverage Report - org.kuali.rice.kew.engine.node.RequestActivationNode
 
Classes in this File Line Coverage Branch Coverage Complexity
RequestActivationNode
0%
0/81
0%
0/72
5.375
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  * 
 4  
  * 
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.engine.node;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.Collections;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 
 24  
 import org.apache.log4j.MDC;
 25  
 import org.kuali.rice.kew.actionitem.ActionItem;
 26  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 27  
 import org.kuali.rice.kew.api.action.ActionRequestStatus;
 28  
 import org.kuali.rice.kew.engine.RouteContext;
 29  
 import org.kuali.rice.kew.engine.RouteHelper;
 30  
 import org.kuali.rice.kew.exception.ResourceUnavailableException;
 31  
 import org.kuali.rice.kew.exception.WorkflowException;
 32  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 33  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 34  
 import org.kuali.rice.kew.util.KEWConstants;
 35  
 import org.kuali.rice.kew.util.PerformanceLogger;
 36  
 import org.kuali.rice.kew.util.Utilities;
 37  
 
 38  
 
 39  
 /**
 40  
  * A node which will activate any requests on it, returning true when there are no more requests 
 41  
  * which require activation.
 42  
  * 
 43  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 44  
  */
 45  0
 public class RequestActivationNode extends RequestActivationNodeBase {
 46  
 
 47  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger( RequestActivationNode.class );
 48  0
     private static long generatedRequestPriority = 0;
 49  
 
 50  
     public SimpleResult process(RouteContext routeContext, RouteHelper routeHelper) throws Exception {
 51  0
         DocumentRouteHeaderValue document = routeContext.getDocument();
 52  0
         RouteNodeInstance nodeInstance = routeContext.getNodeInstance();
 53  0
         if (routeContext.isSimulation()) {
 54  0
             if (routeContext.getActivationContext().isActivateRequests()) {
 55  0
                 activateRequests(routeContext, document, nodeInstance);
 56  
             }
 57  0
             return new SimpleResult(true);
 58  0
         } else if (!activateRequests(routeContext, document, nodeInstance) && shouldTransition(document, nodeInstance)) {
 59  0
             return new SimpleResult(true);
 60  
         } else {
 61  0
             return new SimpleResult(false);
 62  
         }            
 63  
     }
 64  
     
 65  
     public boolean shouldTransition(DocumentRouteHeaderValue document, RouteNodeInstance nodeInstance) {
 66  0
         List requests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocIdAtRouteNode(document.getDocumentId(), nodeInstance.getRouteNodeInstanceId());
 67  0
         boolean shouldTransition = true;
 68  0
         for (Iterator iterator = requests.iterator(); iterator.hasNext();) {
 69  0
             ActionRequestValue request = (ActionRequestValue) iterator.next();
 70  0
             if (request.isApproveOrCompleteRequest()) {
 71  0
                 shouldTransition = false;
 72  0
                 break;
 73  
             }
 74  0
         }
 75  0
         return shouldTransition;
 76  
     }
 77  
 
 78  
     /**
 79  
      * Activates the action requests that are pending at this routelevel of the document. The requests are processed by priority and then request ID. It is implicit in the access that the requests are activated according to the route level above all.
 80  
      * <p>
 81  
      * FYI and acknowledement requests do not cause the processing to stop. Only action requests for approval or completion cause the processing to stop and then only for route level with a serialized activation policy. Only requests at the current document's current route level are activated. Inactive requests at a lower level cause a routing exception.
 82  
      * <p>
 83  
      * Exception routing and adhoc routing are processed slightly differently.
 84  
      * 
 85  
      * @param rh
 86  
      *            RouteHeaderBean object for which requests are activated
 87  
      * @return True if the any approval actions were activated.
 88  
      * @throws ResourceUnavailableException
 89  
      * @throws WorkflowException
 90  
      */
 91  
     public boolean activateRequests(RouteContext context, DocumentRouteHeaderValue document, RouteNodeInstance nodeInstance) throws WorkflowException {
 92  0
         MDC.put("docId", document.getDocumentId());
 93  0
         PerformanceLogger performanceLogger = new PerformanceLogger(document.getDocumentId());
 94  0
         List<ActionItem> generatedActionItems = new ArrayList<ActionItem>();
 95  0
         List<ActionRequestValue> requests = new ArrayList<ActionRequestValue>();
 96  0
         if (context.isSimulation()) {
 97  0
                 for (ActionRequestValue ar : context.getDocument().getActionRequests()) {
 98  
                         // logic check below duplicates behavior of the ActionRequestService.findPendingRootRequestsByDocIdAtRouteNode(documentId, routeNodeInstanceId) method
 99  0
                                 if (ar.getCurrentIndicator()
 100  
                                                 && (ActionRequestStatus.INITIALIZED.getCode().equals(ar.getStatus()) || ActionRequestStatus.ACTIVATED.getCode().equals(ar.getStatus()))
 101  
                                                 && ar.getNodeInstance().getRouteNodeInstanceId().equals(nodeInstance.getRouteNodeInstanceId())
 102  
                                                 && ar.getParentActionRequest() == null) {
 103  0
                                         requests.add(ar);
 104  
                                 }
 105  
                         }
 106  0
             requests.addAll(context.getEngineState().getGeneratedRequests());
 107  
         } else {
 108  0
             requests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocIdAtRouteNode(document.getDocumentId(), nodeInstance.getRouteNodeInstanceId());
 109  
         }
 110  0
         if ( LOG.isDebugEnabled() ) {
 111  0
                 LOG.debug("Pending Root Requests " + requests.size());
 112  
         }
 113  0
         boolean activatedApproveRequest = activateRequestsCustom( context, requests, generatedActionItems, document, nodeInstance );
 114  
 
 115  
         // now let's send notifications, since this code needs to be able to activate each request individually, we need
 116  
         // to collection all action items and then notify after all have been generated
 117  0
         notify(context, generatedActionItems, nodeInstance);
 118  
 
 119  0
         performanceLogger.log("Time to activate requests.");
 120  0
         return activatedApproveRequest;
 121  
     }
 122  
 
 123  
     protected boolean activateRequestsCustom( RouteContext context, List<ActionRequestValue> requests, List<ActionItem> generatedActionItems, 
 124  
                     DocumentRouteHeaderValue document, RouteNodeInstance nodeInstance) throws WorkflowException {
 125  0
         Collections.sort(requests, new Utilities.PrioritySorter());
 126  0
         String activationType = nodeInstance.getRouteNode().getActivationType();
 127  0
         boolean isParallel = KEWConstants.ROUTE_LEVEL_PARALLEL.equals(activationType);
 128  0
         boolean activatedApproveRequest = false;
 129  0
         for (ActionRequestValue request : requests ) {
 130  0
             if (activatedApproveRequest && !isParallel && (!context.isSimulation() || !context.getActivationContext().isActivateRequests() )) {
 131  0
                 break;
 132  
             }
 133  0
             if (request.getParentActionRequest() != null || request.getNodeInstance() == null) {
 134  
                 // 1. disregard request if it's not a top-level request
 135  
                 // 2. disregard request if it's a "future" request and hasn't been attached to a node instance yet
 136  0
                 continue; 
 137  
             }
 138  0
             if (request.isActive()) {
 139  0
                 activatedApproveRequest = activatedApproveRequest || request.isApproveOrCompleteRequest();
 140  0
                 continue;
 141  
             }
 142  0
             logProcessingMessage(request);   
 143  0
             if ( LOG.isDebugEnabled() ) {
 144  0
                     LOG.debug("Activating request: " + request);
 145  
             }
 146  0
             activatedApproveRequest = activateRequest(context, request, nodeInstance, generatedActionItems) || activatedApproveRequest;
 147  
         }
 148  0
         return activatedApproveRequest;
 149  
     }
 150  
     
 151  
     protected boolean activateRequest(RouteContext context, ActionRequestValue actionRequest, RouteNodeInstance nodeInstance, List generatedActionItems) {
 152  0
         if (actionRequest.isRoleRequest()) {
 153  0
             List actionRequests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocIdAtRouteNode(actionRequest.getDocumentId(), nodeInstance.getRouteNodeInstanceId());
 154  0
             for (Iterator iterator = actionRequests.iterator(); iterator.hasNext();) {
 155  0
                 ActionRequestValue siblingRequest = (ActionRequestValue) iterator.next();
 156  0
                 if (actionRequest.getRoleName().equals(siblingRequest.getRoleName())) {
 157  0
                     generatedActionItems.addAll(KEWServiceLocator.getActionRequestService().activateRequestNoNotification(siblingRequest, context.getActivationContext()));
 158  
                 }
 159  0
             }
 160  
         }
 161  0
         generatedActionItems.addAll(KEWServiceLocator.getActionRequestService().activateRequestNoNotification(actionRequest, context.getActivationContext()));
 162  0
         return actionRequest.isApproveOrCompleteRequest() && ! actionRequest.isDone();
 163  
     }
 164  
     
 165  
     protected void saveActionRequest(RouteContext context, ActionRequestValue actionRequest) {
 166  0
         if (!context.isSimulation()) {
 167  0
             KEWServiceLocator.getActionRequestService().saveActionRequest(actionRequest);
 168  
         } else {
 169  0
             actionRequest.setActionRequestId(String.valueOf(generatedRequestPriority++));
 170  0
             context.getEngineState().getGeneratedRequests().add(actionRequest);    
 171  
         }
 172  
         
 173  0
     }
 174  
     
 175  
     protected void saveDocument(RouteContext context, DocumentRouteHeaderValue document) {
 176  0
         if (!context.isSimulation()) {
 177  0
             KEWServiceLocator.getRouteHeaderService().saveRouteHeader(document);
 178  
         }
 179  0
     }
 180  
 
 181  
     protected void logProcessingMessage(ActionRequestValue request) {
 182  0
         if (LOG.isDebugEnabled()) {
 183  0
                 RouteNodeInstance nodeInstance = request.getNodeInstance();
 184  0
             StringBuffer buffer = new StringBuffer();
 185  0
             buffer.append("Processing AR: ").append(request.getActionRequestId()).append("\n");
 186  0
             buffer.append("AR Node Name: ").append(nodeInstance != null ? nodeInstance.getName() : "null").append("\n");
 187  0
             buffer.append("AR RouteLevel: ").append(request.getRouteLevel()).append("\n");
 188  0
             buffer.append("AR Request Code: ").append(request.getActionRequested()).append("\n");
 189  0
             buffer.append("AR Request priority: ").append(request.getPriority()).append("\n");
 190  0
             LOG.debug(buffer);
 191  
         }
 192  0
     }
 193  
             
 194  
 }