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.routemodule;
018:
019: import java.rmi.RemoteException;
020: import java.util.ArrayList;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Set;
025:
026: import edu.iu.uis.eden.clientapp.RouteModuleRemote;
027: import edu.iu.uis.eden.clientapp.vo.ActionRequestVO;
028: import edu.iu.uis.eden.engine.RouteContext;
029: import edu.iu.uis.eden.exception.WorkflowException;
030: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
031: import edu.iu.uis.eden.routeheader.DocumentRouteHeaderValue;
032: import edu.iu.uis.eden.server.BeanConverter;
033: import edu.iu.uis.eden.util.ResponsibleParty;
034:
035: /**
036: * Adapts a {@link RouteModuleRemote} to the {@link RouteModule} interface.
037: *
038: * @see RouteModuleRemote
039: * @see RouteModule
040: *
041: * @author ewestfal
042: */
043: public class RouteModuleRemoteAdapter implements RouteModule {
044:
045: private RouteModuleRemote routeModule;
046:
047: public RouteModuleRemoteAdapter(RouteModuleRemote routeModule) {
048: if (routeModule == null) {
049: throw new IllegalArgumentException(
050: "RouteModuleRemoteAdapter cannot adapt a null RouteModuleRemote");
051: }
052: this .routeModule = routeModule;
053: }
054:
055: public List findActionRequests(RouteContext context)
056: throws Exception {
057: // TODO add new findActionRequests to RouteModuleRemote
058: return findActionRequests(context.getDocument());
059: }
060:
061: public List findActionRequests(DocumentRouteHeaderValue routeHeader)
062: throws WorkflowException {
063: try {
064: List actionRequests = new ArrayList();
065: ActionRequestVO[] actionRequestVOs = routeModule
066: .findActionRequests(BeanConverter
067: .convertRouteHeader(routeHeader, null));
068: if (actionRequestVOs != null && actionRequestVOs.length > 0) {
069: Set rootRequests = findRootRequests(actionRequestVOs);
070:
071: for (Iterator iterator = rootRequests.iterator(); iterator
072: .hasNext();) {
073: ActionRequestVO actionRequestVO = (ActionRequestVO) iterator
074: .next();
075: actionRequestVO.setRouteHeaderId(routeHeader
076: .getRouteHeaderId());
077: actionRequests.add(BeanConverter
078: .convertActionRequestVO(actionRequestVO));
079: }
080: }
081: return actionRequests;
082: } catch (RemoteException e) {
083: if (e.getCause() instanceof WorkflowException) {
084: throw (WorkflowException) e.getCause();
085: }
086: throw new WorkflowException(
087: "Remote exception when finding action requests from route module "
088: + routeModule.toString(), e);
089: }
090: }
091:
092: public ResponsibleParty resolveResponsibilityId(
093: Long responsibilityId) throws WorkflowException {
094: try {
095: return BeanConverter.convertResponsiblePartyVO(routeModule
096: .resolveResponsibilityId(responsibilityId));
097: } catch (RemoteException e) {
098: if (e.getCause() instanceof WorkflowException) {
099: throw (WorkflowException) e.getCause();
100: }
101: throw new WorkflowException(
102: "Remote exception when resolving responsibility ids from route module "
103: + routeModule.toString(), e);
104: }
105: }
106:
107: private Set findRootRequests(ActionRequestVO[] actionRequestVOs) {
108: Set rootRequests = new HashSet();
109: for (int index = 0; index < actionRequestVOs.length; index++) {
110: rootRequests.add(findRootRequest(actionRequestVOs[index],
111: new HashSet()));
112: }
113: return rootRequests;
114: }
115:
116: /**
117: * Walks to the top of the request graph and returns the root request. Also attempts to
118: * avoid bad data by detecting cycles in the graph.
119: */
120: private ActionRequestVO findRootRequest(
121: ActionRequestVO actionRequestVO, Set parents) {
122: if (actionRequestVO.getParentActionRequest() != null) {
123: if (parents.contains(actionRequestVO
124: .getParentActionRequest())) {
125: throw new WorkflowRuntimeException(
126: "Detected a cycle in action request graph returned from route module "
127: + routeModule.getClass());
128: }
129: parents.add(actionRequestVO.getParentActionRequest());
130: return findRootRequest(actionRequestVO
131: .getParentActionRequest(), parents);
132: }
133: return actionRequestVO;
134: }
135:
136: }
|