View Javadoc

1   /*
2    * Copyright 2005-2008 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.actionrequest.service.impl;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
25  import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
26  import org.kuali.rice.kew.actionrequest.service.DocumentRequeuerService;
27  import org.kuali.rice.kew.api.WorkflowRuntimeException;
28  import org.kuali.rice.kew.engine.RouteHelper;
29  import org.kuali.rice.kew.engine.node.RouteNodeInstance;
30  import org.kuali.rice.kew.engine.node.service.RouteNodeService;
31  import org.kuali.rice.kew.service.KEWServiceLocator;
32  import org.kuali.rice.kew.util.PerformanceLogger;
33  
34  
35  /**
36   * A service which effectively "refreshes" and requeues a document.  It first deletes any
37   * pending action requests on the documents and then requeues the document for standard routing.
38   * Addionally, it adds duplicate notification suppression state to RouteNodeInstanceS for 
39   * which ActionRequestS will be regenerated. 
40   * 
41   * Intended to be called async and wired that way in server/client spring beans.
42   * 
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  public class DocumentRequeuerImpl implements DocumentRequeuerService {
46  	
47  	private RouteHelper helper = new RouteHelper();
48      
49  	/**
50  	 * Requeues a document, and sets notification suppression data
51  	 * 
52  	 * @see org.kuali.rice.kew.actionrequest.service.DocumentRequeuerService#requeueDocument(java.lang.Long)
53  	 */
54  	@SuppressWarnings("unchecked")
55  	public void requeueDocument(String documentId) {
56  		PerformanceLogger performanceLogger = new PerformanceLogger();
57          KEWServiceLocator.getRouteHeaderService().lockRouteHeader(documentId, true);
58          Collection<RouteNodeInstance> activeNodes = getRouteNodeService().getActiveNodeInstances(documentId);
59          List<ActionRequestValue> requestsToDelete = new ArrayList<ActionRequestValue>();
60          
61  		NotificationSuppression notificationSuppression = new NotificationSuppression();
62  		
63          for (RouteNodeInstance nodeInstance : activeNodes) {
64              // only "requeue" if we're dealing with a request activation node
65              if (helper.isRequestActivationNode(nodeInstance.getRouteNode())) {
66              	List<ActionRequestValue> deletesForThisNode = 
67              		getActionRequestService().findPendingRootRequestsByDocIdAtRouteNode(documentId, nodeInstance.getRouteNodeInstanceId());
68              	
69              	for (ActionRequestValue deleteForThisNode : deletesForThisNode) {
70              		// only delete the request if it was generated by a route module (or the rules system)
71              		if (deleteForThisNode.isRouteModuleRequest()) {
72              			requestsToDelete.add(deleteForThisNode);
73  
74              			// suppress duplicate notifications
75              			notificationSuppression.addNotificationSuppression(nodeInstance, deleteForThisNode);
76              		}
77              	}
78                  // this will trigger a regeneration of requests
79                  nodeInstance.setInitial(true);
80                  getRouteNodeService().save(nodeInstance);
81              }
82          }
83          for (ActionRequestValue requestToDelete : requestsToDelete) {
84              getActionRequestService().deleteActionRequestGraph(requestToDelete);
85          }
86          try {
87          	KEWServiceLocator.getWorkflowEngine().process(documentId, null);
88          } catch (Exception e) {
89          	throw new WorkflowRuntimeException(e);
90          }
91          performanceLogger.log("Time to run DocumentRequeuer for document " + documentId);	
92  	}
93  
94      private ActionRequestService getActionRequestService() {
95          return KEWServiceLocator.getActionRequestService();
96      }
97      
98      private RouteNodeService getRouteNodeService() {
99          return KEWServiceLocator.getRouteNodeService();
100     }
101 }