| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| KualiModuleServiceImpl |
|
| 4.571428571428571;4.571 |
| 1 | /* | |
| 2 | * Copyright 2006-2007 The Kuali Foundation | |
| 3 | * | |
| 4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.opensource.org/licenses/ecl2.php | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | package org.kuali.rice.krad.service.impl; | |
| 17 | ||
| 18 | import org.kuali.rice.core.api.CoreApiServiceLocator; | |
| 19 | import org.kuali.rice.core.api.namespace.Namespace; | |
| 20 | import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; | |
| 21 | import org.kuali.rice.core.api.util.ClassLoaderUtils; | |
| 22 | import org.kuali.rice.core.framework.parameter.ParameterConstants; | |
| 23 | import org.kuali.rice.krad.bo.BusinessObject; | |
| 24 | import org.kuali.rice.krad.bo.ExternalizableBusinessObject; | |
| 25 | import org.kuali.rice.krad.document.TransactionalDocument; | |
| 26 | import org.kuali.rice.krad.service.KRADServiceLocatorWeb; | |
| 27 | import org.kuali.rice.krad.service.KualiModuleService; | |
| 28 | import org.kuali.rice.krad.service.ModuleService; | |
| 29 | import org.kuali.rice.krad.service.ModuleServiceNotFoundException; | |
| 30 | import org.kuali.rice.krad.util.KRADConstants; | |
| 31 | import org.springframework.beans.factory.InitializingBean; | |
| 32 | import org.springframework.beans.factory.NoSuchBeanDefinitionException; | |
| 33 | import org.springframework.context.ApplicationContext; | |
| 34 | import org.springframework.context.ApplicationContextAware; | |
| 35 | ||
| 36 | import java.util.ArrayList; | |
| 37 | import java.util.List; | |
| 38 | ||
| 39 | 0 | public class KualiModuleServiceImpl implements KualiModuleService, InitializingBean, ApplicationContextAware { |
| 40 | ||
| 41 | 0 | private List<ModuleService> installedModuleServices = new ArrayList<ModuleService>(); |
| 42 | private boolean loadRiceInstalledModuleServices; | |
| 43 | private ApplicationContext applicationContext; | |
| 44 | ||
| 45 | /** | |
| 46 | * @param applicationContext the applicationContext to set | |
| 47 | */ | |
| 48 | @Override | |
| 49 | public void setApplicationContext(ApplicationContext applicationContext) { | |
| 50 | 0 | this.applicationContext = applicationContext; |
| 51 | 0 | } |
| 52 | ||
| 53 | @Override | |
| 54 | public List<ModuleService> getInstalledModuleServices() { | |
| 55 | 0 | return installedModuleServices; |
| 56 | } | |
| 57 | ||
| 58 | @Override | |
| 59 | public ModuleService getModuleService(String moduleId) { | |
| 60 | 0 | for (ModuleService moduleService : installedModuleServices) { |
| 61 | 0 | if ( moduleService.getModuleConfiguration().getNamespaceCode().equals( moduleId ) ) { |
| 62 | 0 | return moduleService; |
| 63 | } | |
| 64 | } | |
| 65 | 0 | return null; |
| 66 | } | |
| 67 | ||
| 68 | @Override | |
| 69 | public ModuleService getModuleServiceByNamespaceCode(String namespaceCode) { | |
| 70 | 0 | for (ModuleService moduleService : installedModuleServices) { |
| 71 | 0 | if ( moduleService.getModuleConfiguration().getNamespaceCode().equals( namespaceCode ) ) { |
| 72 | 0 | return moduleService; |
| 73 | } | |
| 74 | } | |
| 75 | 0 | return null; |
| 76 | } | |
| 77 | ||
| 78 | @Override | |
| 79 | public boolean isModuleServiceInstalled(String namespaceCode) { | |
| 80 | 0 | for (ModuleService moduleService : installedModuleServices) { |
| 81 | 0 | if ( moduleService.getModuleConfiguration().getNamespaceCode().equals( namespaceCode ) ) { |
| 82 | 0 | return true; |
| 83 | } | |
| 84 | } | |
| 85 | 0 | return false; |
| 86 | } | |
| 87 | ||
| 88 | @Override | |
| 89 | public ModuleService getResponsibleModuleService(Class boClass) { | |
| 90 | 0 | if(boClass==null) { |
| 91 | 0 | return null; |
| 92 | } | |
| 93 | 0 | for (ModuleService moduleService : installedModuleServices) { |
| 94 | 0 | if ( moduleService.isResponsibleFor( boClass ) ) { |
| 95 | 0 | return moduleService; |
| 96 | } | |
| 97 | } | |
| 98 | //Throwing exception only for externalizable business objects | |
| 99 | 0 | if(ExternalizableBusinessObject.class.isAssignableFrom(boClass)){ |
| 100 | String message; | |
| 101 | 0 | if(!boClass.isInterface()) { |
| 102 | 0 | message = "There is no responsible module for the externalized business object class: "+boClass; |
| 103 | } else { | |
| 104 | 0 | message = "There is no responsible module for the externalized business object interface: "+boClass; |
| 105 | } | |
| 106 | 0 | throw new ModuleServiceNotFoundException(message); |
| 107 | } | |
| 108 | //Returning null for business objects other than externalizable to keep the framework backward compatible | |
| 109 | 0 | return null; |
| 110 | } | |
| 111 | ||
| 112 | @Override | |
| 113 | public ModuleService getResponsibleModuleServiceForJob(String jobName){ | |
| 114 | 0 | for(ModuleService moduleService : installedModuleServices){ |
| 115 | 0 | if(moduleService.isResponsibleForJob(jobName)){ |
| 116 | 0 | return moduleService; |
| 117 | } | |
| 118 | } | |
| 119 | 0 | return null; |
| 120 | } | |
| 121 | ||
| 122 | @Override | |
| 123 | public void setInstalledModuleServices(List<ModuleService> installedModuleServices) { | |
| 124 | 0 | this.installedModuleServices = installedModuleServices; |
| 125 | 0 | } |
| 126 | ||
| 127 | @Override | |
| 128 | public List<String> getDataDictionaryPackages() { | |
| 129 | 0 | List<String> packages = new ArrayList<String>(); |
| 130 | 0 | for ( ModuleService moduleService : installedModuleServices ) { |
| 131 | 0 | if ( moduleService.getModuleConfiguration().getDataDictionaryPackages() != null ) { |
| 132 | 0 | packages.addAll( moduleService.getModuleConfiguration().getDataDictionaryPackages() ); |
| 133 | } | |
| 134 | } | |
| 135 | 0 | return packages; |
| 136 | } | |
| 137 | ||
| 138 | @Override | |
| 139 | public String getNamespaceName(final String namespaceCode){ | |
| 140 | 0 | Namespace parameterNamespace = CoreApiServiceLocator.getNamespaceService().getNamespace(namespaceCode); |
| 141 | 0 | return parameterNamespace==null ? "" : parameterNamespace.getName(); |
| 142 | } | |
| 143 | ||
| 144 | /** | |
| 145 | * @param loadRiceInstalledModuleServices the loadRiceInstalledModuleServices to set | |
| 146 | */ | |
| 147 | public void setLoadRiceInstalledModuleServices( | |
| 148 | boolean loadRiceInstalledModuleServices) { | |
| 149 | 0 | this.loadRiceInstalledModuleServices = loadRiceInstalledModuleServices; |
| 150 | 0 | } |
| 151 | ||
| 152 | /*** | |
| 153 | * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() | |
| 154 | */ | |
| 155 | @Override | |
| 156 | public void afterPropertiesSet() throws Exception { | |
| 157 | 0 | if(loadRiceInstalledModuleServices){ |
| 158 | try { | |
| 159 | 0 | installedModuleServices.addAll( |
| 160 | GlobalResourceLoader.<KualiModuleService>getService(KualiModuleService.class.getSimpleName().substring(0, 1).toLowerCase() + KualiModuleService.class.getSimpleName().substring(1)).getInstalledModuleServices()); | |
| 161 | 0 | } catch ( NoSuchBeanDefinitionException ex ) { |
| 162 | 0 | installedModuleServices.addAll( ((KualiModuleService)applicationContext.getBean( KRADServiceLocatorWeb.KUALI_MODULE_SERVICE )).getInstalledModuleServices() ); |
| 163 | 0 | } |
| 164 | } | |
| 165 | 0 | } |
| 166 | ||
| 167 | @Override | |
| 168 | public String getNamespaceCode(Class<?> documentOrStepClass) { | |
| 169 | 0 | if (documentOrStepClass == null) { |
| 170 | 0 | throw new IllegalArgumentException("documentOrStepClass must not be null"); |
| 171 | } | |
| 172 | ||
| 173 | 0 | if (documentOrStepClass.isAnnotationPresent(ParameterConstants.NAMESPACE.class)) { |
| 174 | 0 | return (documentOrStepClass.getAnnotation(ParameterConstants.NAMESPACE.class)).namespace(); |
| 175 | } | |
| 176 | 0 | ModuleService moduleService = getResponsibleModuleService(documentOrStepClass); |
| 177 | 0 | if (moduleService != null) { |
| 178 | 0 | return moduleService.getModuleConfiguration().getNamespaceCode(); |
| 179 | } | |
| 180 | 0 | if (documentOrStepClass.getName().startsWith("org.kuali.rice.krad")) { |
| 181 | 0 | return KRADConstants.KRAD_NAMESPACE; |
| 182 | } | |
| 183 | 0 | if (documentOrStepClass.getName().startsWith("org.kuali.rice.kew")) { |
| 184 | 0 | return "KR-WKFLW"; |
| 185 | } | |
| 186 | 0 | if (documentOrStepClass.getName().startsWith("org.kuali.rice.kim")) { |
| 187 | 0 | return "KR-IDM"; |
| 188 | } | |
| 189 | 0 | throw new IllegalArgumentException("Unable to determine the namespace code for documentOrStepClass " + documentOrStepClass.getName()); |
| 190 | } | |
| 191 | ||
| 192 | @Override | |
| 193 | public String getComponentCode(Class<?> documentOrStepClass) { | |
| 194 | 0 | if (documentOrStepClass == null) { |
| 195 | 0 | throw new IllegalArgumentException("documentOrStepClass must not be null"); |
| 196 | } | |
| 197 | ||
| 198 | 0 | if (documentOrStepClass.isAnnotationPresent(ParameterConstants.COMPONENT.class)) { |
| 199 | 0 | return documentOrStepClass.getAnnotation(ParameterConstants.COMPONENT.class).component(); |
| 200 | 0 | } else if (TransactionalDocument.class.isAssignableFrom(documentOrStepClass)) { |
| 201 | 0 | return documentOrStepClass.getSimpleName().replace("Document", ""); |
| 202 | 0 | } else if (BusinessObject.class.isAssignableFrom(documentOrStepClass)) { |
| 203 | 0 | return documentOrStepClass.getSimpleName(); |
| 204 | } else { | |
| 205 | 0 | if (STEP_CLASS != null && STEP_CLASS.isAssignableFrom(documentOrStepClass)) { |
| 206 | 0 | return documentOrStepClass.getSimpleName(); |
| 207 | } | |
| 208 | } | |
| 209 | 0 | throw new IllegalArgumentException("Unable to determine the component code for documentOrStepClass " + documentOrStepClass.getName()); |
| 210 | } | |
| 211 | ||
| 212 | private static final Class<?> STEP_CLASS; | |
| 213 | static { | |
| 214 | Class<?> clazz; | |
| 215 | try { | |
| 216 | 0 | ClassLoader cl = ClassLoaderUtils.getDefaultClassLoader(); |
| 217 | // TODO: Warning! Kludge! Hack! Will be replaced! KULRICE-2921 | |
| 218 | 0 | clazz = Class.forName("org.kuali.kfs.sys.batch.Step", true, cl); |
| 219 | 0 | } catch (Exception e) { |
| 220 | //swallowing: really what do we do here? This is basically asking - are we on kfs? | |
| 221 | 0 | clazz = null; |
| 222 | 0 | } |
| 223 | 0 | STEP_CLASS = clazz; |
| 224 | 0 | } |
| 225 | ||
| 226 | } | |
| 227 |