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.engine.RouteContext;
 28  
 import org.kuali.rice.kew.engine.RouteHelper;
 29  
 import org.kuali.rice.kew.exception.ResourceUnavailableException;
 30  
 import org.kuali.rice.kew.exception.WorkflowException;
 31  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 32  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 33  
 import org.kuali.rice.kew.util.KEWConstants;
 34  
 import org.kuali.rice.kew.util.PerformanceLogger;
 35  
 import org.kuali.rice.kew.util.Utilities;
 36  
 
 37  
 
 38  
 /**
 39  
  * A node which will activate any requests on it, returning true when there are no more requests 
 40  
  * which require activation.
 41  
  * 
 42  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 43  
  */
 44  0
 public class RequestActivationNode extends RequestActivationNodeBase {
 45  
 
 46  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger( RequestActivationNode.class );
 47  0
     private static long generatedRequestPriority = 0;
 48  
 
 49  
     public SimpleResult process(RouteContext routeContext, RouteHelper routeHelper) throws Exception {
 50  0
         DocumentRouteHeaderValue document = routeContext.getDocument();
 51  0
         RouteNodeInstance nodeInstance = routeContext.getNodeInstance();
 52  0
         if (routeContext.isSimulation()) {
 53  0
             if (routeContext.getActivationContext().isActivateRequests()) {
 54  0
                 activateRequests(routeContext, document, nodeInstance);
 55  
             }
 56  0
             return new SimpleResult(true);
 57  0
         } else if (!activateRequests(routeContext, document, nodeInstance) && shouldTransition(document, nodeInstance)) {
 58  0
             return new SimpleResult(true);
 59  
         } else {
 60  0
             return new SimpleResult(false);
 61  
         }            
 62  
     }
 63  
     
 64  
     public boolean shouldTransition(DocumentRouteHeaderValue document, RouteNodeInstance nodeInstance) {
 65  0
         List requests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocIdAtRouteNode(document.getRouteHeaderId(), nodeInstance.getRouteNodeInstanceId());
 66  0
         boolean shouldTransition = true;
 67  0
         for (Iterator iterator = requests.iterator(); iterator.hasNext();) {
 68  0
             ActionRequestValue request = (ActionRequestValue) iterator.next();
 69  0
             if (request.isApproveOrCompleteRequest()) {
 70  0
                 shouldTransition = false;
 71  0
                 break;
 72  
             }
 73  0
         }
 74  0
         return shouldTransition;
 75  
     }
 76  
 
 77  
     /**
 78  
      * 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.
 79  
      * <p>
 80  
      * 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.
 81  
      * <p>
 82  
      * Exception routing and adhoc routing are processed slightly differently.
 83  
      * 
 84  
      * @param rh
 85  
      *            RouteHeaderBean object for which requests are activated
 86  
      * @return True if the any approval actions were activated.
 87  
      * @throws ResourceUnavailableException
 88  
      * @throws WorkflowException
 89  
      */
 90  
     public boolean activateRequests(RouteContext context, DocumentRouteHeaderValue document, RouteNodeInstance nodeInstance) throws WorkflowException {
 91  0
         MDC.put("docId", document.getRouteHeaderId());
 92  0
         PerformanceLogger performanceLogger = new PerformanceLogger(document.getRouteHeaderId());
 93  0
         List<ActionItem> generatedActionItems = new ArrayList<ActionItem>();
 94  0
         List<ActionRequestValue> requests = new ArrayList<ActionRequestValue>();
 95  0
         if (context.isSimulation()) {
 96  0
                 for (ActionRequestValue ar : context.getDocument().getActionRequests()) {
 97  
                         // logic check below duplicates behavior of the ActionRequestService.findPendingRootRequestsByDocIdAtRouteNode(routeHeaderId, routeNodeInstanceId) method
 98  0
                                 if (ar.getCurrentIndicator()
 99  
                                                 && (KEWConstants.ACTION_REQUEST_INITIALIZED.equals(ar.getStatus()) || KEWConstants.ACTION_REQUEST_ACTIVATED.equals(ar.getStatus()))
 100  
                                                 && ar.getNodeInstance().getRouteNodeInstanceId().equals(nodeInstance.getRouteNodeInstanceId())
 101  
                                                 && ar.getParentActionRequest() == null) {
 102  0
                                         requests.add(ar);
 103  
                                 }
 104  
                         }
 105  0
             requests.addAll(context.getEngineState().getGeneratedRequests());
 106  
         } else {
 107  0
             requests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocIdAtRouteNode(document.getRouteHeaderId(), nodeInstance.getRouteNodeInstanceId());
 108  
         }
 109  0
         if ( LOG.isDebugEnabled() ) {
 110  0
                 LOG.debug("Pending Root Requests " + requests.size());
 111  
         }
 112  0
         boolean activatedApproveRequest = activateRequestsCustom( context, requests, generatedActionItems, document, nodeInstance );
 113  
 
 114  
         // now let's send notifications, since this code needs to be able to activate each request individually, we need
 115  
         // to collection all action items and then notify after all have been generated
 116  0
         notify(context, generatedActionItems, nodeInstance);
 117  
 
 118  0
         performanceLogger.log("Time to activate requests.");
 119  0
         return activatedApproveRequest;
 120  
     }
 121  
 
 122  
     protected boolean activateRequestsCustom( RouteContext context, List<ActionRequestValue> requests, List<ActionItem> generatedActionItems, 
 123  
                     DocumentRouteHeaderValue document, RouteNodeInstance nodeInstance) throws WorkflowException {
 124  0
         Collections.sort(requests, new Utilities.PrioritySorter());
 125  0
         String activationType = nodeInstance.getRouteNode().getActivationType();
 126  0
         boolean isParallel = KEWConstants.ROUTE_LEVEL_PARALLEL.equals(activationType);
 127  0
         boolean activatedApproveRequest = false;
 128  0
         for (ActionRequestValue request : requests ) {
 129  0
             if (activatedApproveRequest && !isParallel && (!context.isSimulation() || !context.getActivationContext().isActivateRequests() )) {
 130  0
                 break;
 131  
             }
 132  0
             if (request.getParentActionRequest() != null || request.getNodeInstance() == null) {
 133  
                 // 1. disregard request if it's not a top-level request
 134  
                 // 2. disregard request if it's a "future" request and hasn't been attached to a node instance yet
 135  0
                 continue; 
 136  
             }
 137  0
             if (request.isActive()) {
 138  0
                 activatedApproveRequest = activatedApproveRequest || request.isApproveOrCompleteRequest();
 139  0
                 continue;
 140  
             }
 141  0
             logProcessingMessage(request);   
 142  0
             if ( LOG.isDebugEnabled() ) {
 143  0
                     LOG.debug("Activating request: " + request);
 144  
             }
 145  0
             activatedApproveRequest = activateRequest(context, request, nodeInstance, generatedActionItems) || activatedApproveRequest;
 146  
         }
 147  0
         return activatedApproveRequest;
 148  
     }
 149  
     
 150  
     protected boolean activateRequest(RouteContext context, ActionRequestValue actionRequest, RouteNodeInstance nodeInstance, List generatedActionItems) {
 151  0
         if (actionRequest.isRoleRequest()) {
 152  0
             List actionRequests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocIdAtRouteNode(actionRequest.getRouteHeaderId(), nodeInstance.getRouteNodeInstanceId());
 153  0
             for (Iterator iterator = actionRequests.iterator(); iterator.hasNext();) {
 154  0
                 ActionRequestValue siblingRequest = (ActionRequestValue) iterator.next();
 155  0
                 if (actionRequest.getRoleName().equals(siblingRequest.getRoleName())) {
 156  0
                     generatedActionItems.addAll(KEWServiceLocator.getActionRequestService().activateRequestNoNotification(siblingRequest, context.getActivationContext()));
 157  
                 }
 158  0
             }
 159  
         }
 160  0
         generatedActionItems.addAll(KEWServiceLocator.getActionRequestService().activateRequestNoNotification(actionRequest, context.getActivationContext()));
 161  0
         return actionRequest.isApproveOrCompleteRequest() && ! actionRequest.isDone();
 162  
     }
 163  
     
 164  
     protected void saveActionRequest(RouteContext context, ActionRequestValue actionRequest) {
 165  0
         if (!context.isSimulation()) {
 166  0
             KEWServiceLocator.getActionRequestService().saveActionRequest(actionRequest);
 167  
         } else {
 168  0
             actionRequest.setActionRequestId(new Long(generatedRequestPriority++));
 169  0
             context.getEngineState().getGeneratedRequests().add(actionRequest);    
 170  
         }
 171  
         
 172  0
     }
 173  
     
 174  
     protected void saveDocument(RouteContext context, DocumentRouteHeaderValue document) {
 175  0
         if (!context.isSimulation()) {
 176  0
             KEWServiceLocator.getRouteHeaderService().saveRouteHeader(document);
 177  
         }
 178  0
     }
 179  
 
 180  
     protected void logProcessingMessage(ActionRequestValue request) {
 181  0
         if (LOG.isDebugEnabled()) {
 182  0
                 RouteNodeInstance nodeInstance = request.getNodeInstance();
 183  0
             StringBuffer buffer = new StringBuffer();
 184  0
             buffer.append("Processing AR: ").append(request.getActionRequestId()).append("\n");
 185  0
             buffer.append("AR Node Name: ").append(nodeInstance != null ? nodeInstance.getName() : "null").append("\n");
 186  0
             buffer.append("AR RouteLevel: ").append(request.getRouteLevel()).append("\n");
 187  0
             buffer.append("AR Request Code: ").append(request.getActionRequested()).append("\n");
 188  0
             buffer.append("AR Request priority: ").append(request.getPriority()).append("\n");
 189  0
             LOG.debug(buffer);
 190  
         }
 191  0
     }
 192  
             
 193  
 }