001    /**
002     * Copyright 2005-2014 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.routemodule.service.impl;
017    
018    import org.apache.log4j.Logger;
019    import org.kuali.rice.core.api.reflect.ObjectDefinition;
020    import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
021    import org.kuali.rice.kew.actionrequest.ActionRequestValue;
022    import org.kuali.rice.kew.api.WorkflowRuntimeException;
023    import org.kuali.rice.kew.api.exception.ResourceUnavailableException;
024    import org.kuali.rice.kew.engine.node.RouteNode;
025    import org.kuali.rice.kew.role.RoleRouteModule;
026    import org.kuali.rice.kew.routemodule.FlexRMAdapter;
027    import org.kuali.rice.kew.routemodule.RouteModule;
028    import org.kuali.rice.kew.routemodule.service.RouteModuleService;
029    import org.kuali.rice.kew.api.KewApiConstants;
030    import org.springframework.beans.BeansException;
031    import org.springframework.beans.factory.BeanFactory;
032    import org.springframework.beans.factory.BeanFactoryAware;
033    
034    /**
035     * @author Kuali Rice Team (rice.collab@kuali.org)
036     */
037    public class RouteModuleServiceImpl implements RouteModuleService, BeanFactoryAware {
038    
039        private static final Logger LOG = Logger.getLogger(RouteModuleServiceImpl.class);
040    
041        private BeanFactory beanFactory;
042        private String rulesEngineRouteModuleId;
043    
044        private RouteModule peopleFlowRouteModule;
045        private volatile RouteModule rulesEngineRouteModule;
046    
047        public RouteModule findRouteModule(RouteNode node) throws ResourceUnavailableException {
048            String routeMethodName = node.getRouteMethodName();
049            LOG.debug("Finding route module for routeMethodName="+routeMethodName+" at route level "+node.getRouteNodeName());
050            RouteModule routeModule = null;
051            // default to FlexRM module if the routeMethodName is null
052            if (node.isRulesEngineNode()) {
053                routeModule = getRulesEngineRouteModule();
054            } else if (node.isPeopleFlowNode()) {
055                routeModule = getPeopleFlowRouteModule();
056            } else if (routeMethodName == null || node.isFlexRM()) {
057                routeModule = getFlexRMRouteModule(routeMethodName);
058            } else {
059                routeModule = getRouteModule(routeMethodName);
060            }
061            return routeModule;
062        }
063    
064        public RouteModule findRouteModule(ActionRequestValue actionRequest) throws ResourceUnavailableException {
065            if (!actionRequest.getResolveResponsibility()) {
066                    return new RoleRouteModule();
067            }
068            if (actionRequest.getNodeInstance() == null) {
069                return null;
070            }
071            return findRouteModule(actionRequest.getNodeInstance().getRouteNode());
072        }
073    
074        private RouteModule getRouteModule(String routeMethodName) throws ResourceUnavailableException {
075            if (routeMethodName == null) {
076                return null;
077            } else if ("".equals(routeMethodName.trim()) || KewApiConstants.ROUTE_LEVEL_NO_ROUTE_MODULE.equals(routeMethodName)) {
078                    return null;
079            }
080            Object routeModule = GlobalResourceLoader.getObject(new ObjectDefinition(routeMethodName));//SpringServiceLocator.getExtensionService().getRouteModule(routeMethodName);
081            if (routeModule instanceof RouteModule) {
082                return (RouteModule)routeModule;
083            }
084            throw new WorkflowRuntimeException("Could not locate the Route Module with the given name: " + routeMethodName);
085        }
086    
087        private RouteModule getFlexRMRouteModule(String ruleTemplateName) {
088            return new FlexRMAdapter();
089        }
090    
091        public void setPeopleFlowRouteModule(RouteModule peopleFlowRouteModule) {
092            this.peopleFlowRouteModule = peopleFlowRouteModule;
093        }
094    
095        public RouteModule getPeopleFlowRouteModule() {
096            return peopleFlowRouteModule;
097        }
098    
099        public String getRulesEngineRouteModuleId() {
100            return rulesEngineRouteModuleId;
101        }
102    
103        public void setRulesEngineRouteModuleId(String rulesEngineRouteModuleId) {
104            this.rulesEngineRouteModuleId = rulesEngineRouteModuleId;
105        }
106    
107        @Override
108        public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
109            this.beanFactory = beanFactory;
110        }
111    
112        /**
113         * Loaded lazily by id so as not to introduce a runtime dependency on KRMS when it is not in use.
114         */
115        protected RouteModule getRulesEngineRouteModule() {
116            if (rulesEngineRouteModule == null) {
117                // this should initialize the route module in spring
118                rulesEngineRouteModule = (RouteModule)beanFactory.getBean(getRulesEngineRouteModuleId());
119            }
120            return rulesEngineRouteModule;
121        }
122    
123    }