001/**
002 * Copyright 2005-2015 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 */
016package org.kuali.rice.krad.service.impl;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
022import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
023import org.kuali.rice.coreservice.api.namespace.Namespace;
024import org.kuali.rice.coreservice.framework.parameter.ParameterConstants;
025import org.kuali.rice.krad.bo.BusinessObject;
026import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
027import org.kuali.rice.krad.document.TransactionalDocument;
028import org.kuali.rice.krad.exception.ModuleServiceNotFoundException;
029import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
030import org.kuali.rice.krad.service.KualiModuleService;
031import org.kuali.rice.krad.service.ModuleService;
032import org.kuali.rice.krad.util.KRADConstants;
033import org.springframework.beans.factory.InitializingBean;
034import org.springframework.beans.factory.NoSuchBeanDefinitionException;
035import org.springframework.context.ApplicationContext;
036import org.springframework.context.ApplicationContextAware;
037
038/**
039 * @author Kuali Rice Team (rice.collab@kuali.org)
040 */
041public class KualiModuleServiceImpl implements KualiModuleService, InitializingBean, ApplicationContextAware {
042
043    private List<ModuleService> installedModuleServices = new ArrayList<ModuleService>();
044    private boolean loadRiceInstalledModuleServices;
045    private ApplicationContext applicationContext;
046
047    /**
048         * @param applicationContext the applicationContext to set
049         */
050        @Override
051        public void setApplicationContext(ApplicationContext applicationContext) {
052                this.applicationContext = applicationContext;
053        }
054
055        @Override
056        public List<ModuleService> getInstalledModuleServices() {
057        return installedModuleServices;
058    }
059
060    @Override
061        public ModuleService getModuleService(String moduleId) {
062        for (ModuleService moduleService : installedModuleServices) {
063            if ( moduleService.getModuleConfiguration().getNamespaceCode().equals( moduleId ) ) {
064                return moduleService;
065            }
066        }
067        return null;
068    }
069
070    @Override
071        public ModuleService getModuleServiceByNamespaceCode(String namespaceCode) {
072        for (ModuleService moduleService : installedModuleServices) {
073            if ( moduleService.getModuleConfiguration().getNamespaceCode().equals( namespaceCode ) ) {
074                return moduleService;
075            }
076        }
077        return null;
078    }
079
080    @Override
081        public boolean isModuleServiceInstalled(String namespaceCode) {
082        for (ModuleService moduleService : installedModuleServices) {
083            if ( moduleService.getModuleConfiguration().getNamespaceCode().equals( namespaceCode ) ) {
084                return true;
085            }
086        }
087        return false;
088    }
089
090    @Override
091        public ModuleService getResponsibleModuleService(Class boClass) {
092        if(boClass==null) {
093                        return null;
094                }
095        for (ModuleService moduleService : installedModuleServices) {
096            if ( moduleService.isResponsibleFor( boClass ) ) {
097                return moduleService;
098            }
099        }
100        //Throwing exception only for externalizable business objects
101        if(ExternalizableBusinessObject.class.isAssignableFrom(boClass)){
102            String message;
103                if(!boClass.isInterface()) {
104                                message = "There is no responsible module for the externalized business object class: "+boClass;
105                        } else {
106                                message = "There is no responsible module for the externalized business object interface: "+boClass;
107                        }
108                throw new ModuleServiceNotFoundException(message);
109        }
110        //Returning null for business objects other than externalizable to keep the framework backward compatible
111        return null;
112    }
113
114    @Override
115        public void setInstalledModuleServices(List<ModuleService> installedModuleServices) {
116        this.installedModuleServices = installedModuleServices;
117    }
118
119    @Override
120        public List<String> getDataDictionaryPackages() {
121        List<String> packages  = new ArrayList<String>();
122        for ( ModuleService moduleService : installedModuleServices ) {
123            if ( moduleService.getModuleConfiguration().getDataDictionaryPackages() != null ) {
124                packages.addAll( moduleService.getModuleConfiguration().getDataDictionaryPackages() );
125            }
126        }
127        return packages;
128    }
129
130    @Override
131        public String getNamespaceName(final String namespaceCode){
132        Namespace parameterNamespace = CoreServiceApiServiceLocator.getNamespaceService().getNamespace(namespaceCode);
133        return parameterNamespace==null ? "" : parameterNamespace.getName();
134    }
135
136        /**
137         * @param loadRiceInstalledModuleServices the loadRiceInstalledModuleServices to set
138         */
139        public void setLoadRiceInstalledModuleServices(
140                        boolean loadRiceInstalledModuleServices) {
141                this.loadRiceInstalledModuleServices = loadRiceInstalledModuleServices;
142        }
143
144        /***
145         * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
146         */
147        @Override
148        public void afterPropertiesSet() throws Exception {
149                if(loadRiceInstalledModuleServices){
150                        try {
151                                installedModuleServices.addAll(
152                                                GlobalResourceLoader.<KualiModuleService>getService(KualiModuleService.class.getSimpleName().substring(0, 1).toLowerCase() + KualiModuleService.class.getSimpleName().substring(1)).getInstalledModuleServices());
153                        } catch ( NoSuchBeanDefinitionException ex ) {
154                                installedModuleServices.addAll( ((KualiModuleService)applicationContext.getBean( KRADServiceLocatorWeb.KUALI_MODULE_SERVICE )).getInstalledModuleServices() );
155                        }
156                }
157        }
158
159    @Override
160    public String getNamespaceCode(Class<?> documentClass) {
161        if (documentClass == null) {
162            throw new IllegalArgumentException("documentClass must not be null");
163        }
164
165        if (documentClass.isAnnotationPresent(ParameterConstants.NAMESPACE.class)) {
166            return (documentClass.getAnnotation(ParameterConstants.NAMESPACE.class)).namespace();
167        }
168        ModuleService moduleService = getResponsibleModuleService(documentClass);
169        if (moduleService != null) {
170            return moduleService.getModuleConfiguration().getNamespaceCode();
171        }
172        if (documentClass.getName().startsWith("org.kuali.rice.krad")) {
173            return KRADConstants.KNS_NAMESPACE;
174        }
175        if (documentClass.getName().startsWith("org.kuali.rice.edl")) {
176            return "KR-EDL";
177        }
178        if (documentClass.getName().startsWith("org.kuali.rice.kew")) {
179            return "KR-WKFLW";
180        }
181        if (documentClass.getName().startsWith("org.kuali.rice.edl")) {
182                return "KR-WKFLW";
183        }
184        if (documentClass.getName().startsWith("org.kuali.rice.kim")) {
185            return "KR-IDM";
186        }
187        if (documentClass.getName().startsWith("org.kuali.rice.core")) {
188            return "KR-CORE";
189        }
190        throw new IllegalArgumentException("Unable to determine the namespace code for documentClass " + documentClass.getName());
191    }
192
193    @Override
194    public String getComponentCode(Class<?> documentClass) {
195        if (documentClass == null) {
196            throw new IllegalArgumentException("documentClass must not be null");
197        }
198
199        if (documentClass.isAnnotationPresent(ParameterConstants.COMPONENT.class)) {
200            return documentClass.getAnnotation(ParameterConstants.COMPONENT.class).component();
201        } else if (TransactionalDocument.class.isAssignableFrom(documentClass)) {
202            return documentClass.getSimpleName().replace("Document", "");
203        } else {
204            return documentClass.getSimpleName();
205        }
206    }
207
208}
209