001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.engine.node;
018:
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.apache.log4j.MDC;
025:
026: import edu.iu.uis.eden.EdenConstants;
027: import edu.iu.uis.eden.KEWServiceLocator;
028: import edu.iu.uis.eden.actionrequests.ActionRequestValue;
029: import edu.iu.uis.eden.engine.RouteContext;
030: import edu.iu.uis.eden.engine.RouteHelper;
031: import edu.iu.uis.eden.exception.EdenUserNotFoundException;
032: import edu.iu.uis.eden.exception.ResourceUnavailableException;
033: import edu.iu.uis.eden.exception.WorkflowException;
034: import edu.iu.uis.eden.routeheader.DocumentRouteHeaderValue;
035: import edu.iu.uis.eden.util.PerformanceLogger;
036: import edu.iu.uis.eden.util.Utilities;
037:
038: /**
039: * A node which will activate any requests on it, returning true when there are no more requests
040: * which require activation.
041: *
042: * @author ewestfal
043: */
044: public class RequestActivationNode implements SimpleNode {
045:
046: protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
047: .getLogger(getClass());
048: private static long generatedRequestPriority = 0;
049:
050: public SimpleResult process(RouteContext routeContext,
051: RouteHelper routeHelper) throws Exception {
052: DocumentRouteHeaderValue document = routeContext.getDocument();
053: RouteNodeInstance nodeInstance = routeContext.getNodeInstance();
054: if (routeContext.isSimulation()) {
055: if (routeContext.getActivationContext()
056: .isActionsToPerform()) {
057: activateRequests(routeContext, document, nodeInstance);
058: }
059: return new SimpleResult(true);
060: } else if (!activateRequests(routeContext, document,
061: nodeInstance)
062: && shouldTransition(document, nodeInstance)) {
063: return new SimpleResult(true);
064: } else {
065: return new SimpleResult(false);
066: }
067: }
068:
069: public boolean shouldTransition(DocumentRouteHeaderValue document,
070: RouteNodeInstance nodeInstance) {
071: List requests = KEWServiceLocator.getActionRequestService()
072: .findPendingRootRequestsByDocIdAtRouteNode(
073: document.getRouteHeaderId(),
074: nodeInstance.getRouteNodeInstanceId());
075: boolean shouldTransition = true;
076: for (Iterator iterator = requests.iterator(); iterator
077: .hasNext();) {
078: ActionRequestValue request = (ActionRequestValue) iterator
079: .next();
080: if (request.isApproveOrCompleteRequest()) {
081: shouldTransition = false;
082: break;
083: }
084: }
085: return shouldTransition;
086: }
087:
088: /**
089: * 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.
090: * <p>
091: * 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.
092: * <p>
093: * Exception routing and adhoc routing are processed slightly differently.
094: *
095: * @param rh
096: * RouteHeaderBean object for which requests are activated
097: * @return True if the any approval actions were activated.
098: * @throws ResourceUnavailableException
099: * @throws WorkflowException
100: */
101: public boolean activateRequests(RouteContext context,
102: DocumentRouteHeaderValue document,
103: RouteNodeInstance nodeInstance) throws WorkflowException {
104: MDC.put("docID", document.getRouteHeaderId());
105: PerformanceLogger performanceLogger = new PerformanceLogger(
106: document.getRouteHeaderId());
107: List generatedActionItems = new ArrayList();
108: List requests = KEWServiceLocator.getActionRequestService()
109: .findPendingRootRequestsByDocIdAtRouteNode(
110: document.getRouteHeaderId(),
111: nodeInstance.getRouteNodeInstanceId());
112: if (context.isSimulation()) {
113: requests.addAll(context.getEngineState()
114: .getGeneratedRequests());
115: }
116: Collections
117: .sort(requests, new Utilities().new PrioritySorter());
118: LOG.debug("Pending Root Requests " + requests.size());
119: String activationType = nodeInstance.getRouteNode()
120: .getActivationType();
121: boolean isParallel = EdenConstants.ROUTE_LEVEL_PARALLEL
122: .equals(activationType);
123: boolean activatedApproveRequest = false;
124: for (Iterator iter = requests.iterator(); iter.hasNext();) {
125: if (activatedApproveRequest && !isParallel) {
126: break;
127: }
128: ActionRequestValue request = (ActionRequestValue) iter
129: .next();
130: if (request.getParentActionRequest() != null
131: || request.getNodeInstance() == null) {
132: // 1. disregard request if it's not a top-level request
133: // 2. disregard request if it's a "future" request and hasn't been attached to a node instance yet
134: continue;
135: }
136: if (request.isActive()) {
137: activatedApproveRequest = activatedApproveRequest
138: || request.isApproveOrCompleteRequest();
139: continue;
140: }
141: logProcessingMessage(request);
142: LOG.debug("Activating request.");
143: activatedApproveRequest = activateRequest(context, request,
144: nodeInstance, generatedActionItems)
145: || activatedApproveRequest;
146: }
147: // now let's send notifications, since this code needs to be able to activate each request individually, we need
148: // to collection all action items and then notify after all have been generated
149: if (!context.isSimulation()) {
150: KEWServiceLocator.getNotificationService().notify(
151: generatedActionItems);
152: }
153: performanceLogger.log("Time to activate requests.");
154: return activatedApproveRequest;
155: }
156:
157: private boolean activateRequest(RouteContext context,
158: ActionRequestValue actionRequest,
159: RouteNodeInstance nodeInstance, List generatedActionItems)
160: throws EdenUserNotFoundException {
161: if (actionRequest.isRoleRequest()) {
162: List actionRequests = KEWServiceLocator
163: .getActionRequestService()
164: .findPendingRootRequestsByDocIdAtRouteNode(
165: actionRequest.getRouteHeaderId(),
166: nodeInstance.getRouteNodeInstanceId());
167: for (Iterator iterator = actionRequests.iterator(); iterator
168: .hasNext();) {
169: ActionRequestValue siblingRequest = (ActionRequestValue) iterator
170: .next();
171: if (actionRequest.getRoleName().equals(
172: siblingRequest.getRoleName())) {
173: generatedActionItems.addAll(KEWServiceLocator
174: .getActionRequestService()
175: .activateRequestNoNotification(
176: siblingRequest,
177: context.getActivationContext()));
178: }
179: }
180: }
181: generatedActionItems.addAll(KEWServiceLocator
182: .getActionRequestService()
183: .activateRequestNoNotification(actionRequest,
184: context.getActivationContext()));
185: return actionRequest.isApproveOrCompleteRequest()
186: && !actionRequest.isDone();
187: }
188:
189: protected void saveActionRequest(RouteContext context,
190: ActionRequestValue actionRequest)
191: throws EdenUserNotFoundException {
192: if (!context.isSimulation()) {
193: KEWServiceLocator.getActionRequestService()
194: .saveActionRequest(actionRequest);
195: } else {
196: actionRequest.setActionRequestId(new Long(
197: generatedRequestPriority++));
198: context.getEngineState().getGeneratedRequests().add(
199: actionRequest);
200: }
201:
202: }
203:
204: protected void saveDocument(RouteContext context,
205: DocumentRouteHeaderValue document) {
206: if (!context.isSimulation()) {
207: KEWServiceLocator.getRouteHeaderService().saveRouteHeader(
208: document);
209: }
210: }
211:
212: private void logProcessingMessage(ActionRequestValue request) {
213: if (LOG.isDebugEnabled()) {
214: RouteNodeInstance nodeInstance = request.getNodeInstance();
215: StringBuffer buffer = new StringBuffer();
216: buffer.append("Processing AR: ").append(
217: request.getActionRequestId()).append("\n");
218: buffer.append("AR Node Name: ").append(
219: nodeInstance != null ? nodeInstance.getName()
220: : "null").append("\n");
221: buffer.append("AR RouteLevel: ").append(
222: request.getRouteLevel()).append("\n");
223: buffer.append("AR Request Code: ").append(
224: request.getActionRequested()).append("\n");
225: buffer.append("AR Request priority: ").append(
226: request.getPriority()).append("\n");
227: LOG.debug(buffer);
228: }
229: }
230:
231: }
|