View Javadoc

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.routemodule;
18  
19  import java.rmi.RemoteException;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.kuali.rice.core.api.exception.RiceRuntimeException;
24  import org.kuali.rice.kew.dto.ActionRequestDTO;
25  import org.kuali.rice.kew.dto.DTOConverter;
26  import org.kuali.rice.kew.dto.DocumentContentDTO;
27  import org.kuali.rice.kew.dto.RouteHeaderDTO;
28  import org.kuali.rice.kew.engine.RouteContext;
29  import org.kuali.rice.kew.exception.WorkflowException;
30  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
31  import org.kuali.rice.kew.util.ResponsibleParty;
32  
33  
34  /**
35   * Adapts a {@link RouteModuleRemote} to the {@link RouteModule} interface.
36   *
37   * @see RouteModuleRemote
38   * @see RouteModule
39   *
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  public class RouteModuleRemoteAdapter implements RouteModule {
43  
44      private RouteModuleRemote routeModule;
45  
46      public RouteModuleRemoteAdapter(RouteModuleRemote routeModule) {
47          if (routeModule == null) {
48              throw new IllegalArgumentException("RouteModuleRemoteAdapter cannot adapt a null RouteModuleRemote");
49          }
50          this.routeModule = routeModule;
51      }
52  
53      public List findActionRequests(RouteContext context) throws Exception {
54      	// TODO add new findActionRequests to RouteModuleRemote
55      	return findActionRequests(context.getDocument());
56      }
57  
58      public List findActionRequests(DocumentRouteHeaderValue routeHeader) throws WorkflowException {
59          try {
60              List actionRequests = new ArrayList();
61              RouteHeaderDTO routeHeaderVO = DTOConverter.convertRouteHeader(routeHeader, null);
62              DocumentContentDTO documentContentVO = DTOConverter.convertDocumentContent(routeHeader.getDocContent(), routeHeaderVO.getDocumentId());
63              ActionRequestDTO[] actionRequestVOs = routeModule.findActionRequests(routeHeaderVO, documentContentVO);
64              if (actionRequestVOs != null && actionRequestVOs.length > 0) {
65              	assertRootRequests(actionRequestVOs);
66  
67                  for (ActionRequestDTO actionRequestVO : actionRequestVOs) {
68                      actionRequestVO.setDocumentId(routeHeader.getDocumentId());
69                      
70                      // TODO this should be moved to a validate somewhere's...
71                      if (actionRequestVO.getPrincipalId() == null && actionRequestVO.getGroupId() == null) {
72                      	throw new RiceRuntimeException("Post processor didn't set a user or workgroup on the request");
73                      }
74                      
75                      actionRequests.add(DTOConverter.convertActionRequestDTO(actionRequestVO));
76                  }
77              }
78              return actionRequests;
79          } catch (RemoteException e) {
80              if (e.getCause() instanceof WorkflowException) {
81                  throw (WorkflowException)e.getCause();
82              }
83              throw new WorkflowException("Remote exception when finding action requests from route module "+routeModule.toString(), e);
84          }
85      }
86  
87      public ResponsibleParty resolveResponsibilityId(String responsibilityId) throws WorkflowException {
88          try {
89              return DTOConverter.convertResponsiblePartyVO(routeModule.resolveResponsibilityId(responsibilityId));
90          } catch (RemoteException e) {
91              if (e.getCause() instanceof WorkflowException) {
92                  throw (WorkflowException)e.getCause();
93              }
94              throw new WorkflowException("Remote exception when resolving responsibility ids from route module "+routeModule.toString(), e);
95          }
96      }
97      
98      /**
99       * Asserts that the given array of ActionRequestDTOs are only root requests (as required by the RouteModuleRemote API)
100      */
101     private void assertRootRequests(ActionRequestDTO[] actionRequestVOs) throws WorkflowException {
102     	for (ActionRequestDTO actionRequest : actionRequestVOs) {
103     		if (actionRequest.getParentActionRequestId() != null) {
104     			throw new WorkflowException("Encountered an action request in the graph which was NOT a root request.  RouteModuleRemote.findActionRequests should only produce root ActionRequestDTO objects.");
105     		}
106     	}
107     }
108 
109 }