| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| KualiModuleServiceImpl |
|
| 4.785714285714286;4.786 |
| 1 | /** | |
| 2 | * Copyright 2005-2011 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.framework.parameter.ParameterConstants; | |
| 22 | import org.kuali.rice.krad.bo.BusinessObject; | |
| 23 | import org.kuali.rice.krad.bo.ExternalizableBusinessObject; | |
| 24 | import org.kuali.rice.krad.document.TransactionalDocument; | |
| 25 | import org.kuali.rice.krad.service.KRADServiceLocatorWeb; | |
| 26 | import org.kuali.rice.krad.service.KualiModuleService; | |
| 27 | import org.kuali.rice.krad.service.ModuleService; | |
| 28 | import org.kuali.rice.krad.service.ModuleServiceNotFoundException; | |
| 29 | import org.kuali.rice.krad.util.KRADConstants; | |
| 30 | import org.springframework.beans.factory.InitializingBean; | |
| 31 | import org.springframework.beans.factory.NoSuchBeanDefinitionException; | |
| 32 | import org.springframework.context.ApplicationContext; | |
| 33 | import org.springframework.context.ApplicationContextAware; | |
| 34 | ||
| 35 | import java.util.ArrayList; | |
| 36 | import java.util.List; | |
| 37 | ||
| 38 | 0 | public class KualiModuleServiceImpl implements KualiModuleService, InitializingBean, ApplicationContextAware { |
| 39 | ||
| 40 | 0 | private List<ModuleService> installedModuleServices = new ArrayList<ModuleService>(); |
| 41 | private boolean loadRiceInstalledModuleServices; | |
| 42 | private ApplicationContext applicationContext; | |
| 43 | ||
| 44 | /** | |
| 45 | * @param applicationContext the applicationContext to set | |
| 46 | */ | |
| 47 | @Override | |
| 48 | public void setApplicationContext(ApplicationContext applicationContext) { | |
| 49 | 0 | this.applicationContext = applicationContext; |
| 50 | 0 | } |
| 51 | ||
| 52 | @Override | |
| 53 | public List<ModuleService> getInstalledModuleServices() { | |
| 54 | 0 | return installedModuleServices; |
| 55 | } | |
| 56 | ||
| 57 | @Override | |
| 58 | public ModuleService getModuleService(String moduleId) { | |
| 59 | 0 | for (ModuleService moduleService : installedModuleServices) { |
| 60 | 0 | if ( moduleService.getModuleConfiguration().getNamespaceCode().equals( moduleId ) ) { |
| 61 | 0 | return moduleService; |
| 62 | } | |
| 63 | } | |
| 64 | 0 | return null; |
| 65 | } | |
| 66 | ||
| 67 | @Override | |
| 68 | public ModuleService getModuleServiceByNamespaceCode(String namespaceCode) { | |
| 69 | 0 | for (ModuleService moduleService : installedModuleServices) { |
| 70 | 0 | if ( moduleService.getModuleConfiguration().getNamespaceCode().equals( namespaceCode ) ) { |
| 71 | 0 | return moduleService; |
| 72 | } | |
| 73 | } | |
| 74 | 0 | return null; |
| 75 | } | |
| 76 | ||
| 77 | @Override | |
| 78 | public boolean isModuleServiceInstalled(String namespaceCode) { | |
| 79 | 0 | for (ModuleService moduleService : installedModuleServices) { |
| 80 | 0 | if ( moduleService.getModuleConfiguration().getNamespaceCode().equals( namespaceCode ) ) { |
| 81 | 0 | return true; |
| 82 | } | |
| 83 | } | |
| 84 | 0 | return false; |
| 85 | } | |
| 86 | ||
| 87 | @Override | |
| 88 | public ModuleService getResponsibleModuleService(Class boClass) { | |
| 89 | 0 | if(boClass==null) { |
| 90 | 0 | return null; |
| 91 | } | |
| 92 | 0 | for (ModuleService moduleService : installedModuleServices) { |
| 93 | 0 | if ( moduleService.isResponsibleFor( boClass ) ) { |
| 94 | 0 | return moduleService; |
| 95 | } | |
| 96 | } | |
| 97 | //Throwing exception only for externalizable business objects | |
| 98 | 0 | if(ExternalizableBusinessObject.class.isAssignableFrom(boClass)){ |
| 99 | String message; | |
| 100 | 0 | if(!boClass.isInterface()) { |
| 101 | 0 | message = "There is no responsible module for the externalized business object class: "+boClass; |
| 102 | } else { | |
| 103 | 0 | message = "There is no responsible module for the externalized business object interface: "+boClass; |
| 104 | } | |
| 105 | 0 | throw new ModuleServiceNotFoundException(message); |
| 106 | } | |
| 107 | //Returning null for business objects other than externalizable to keep the framework backward compatible | |
| 108 | 0 | return null; |
| 109 | } | |
| 110 | ||
| 111 | @Override | |
| 112 | public ModuleService getResponsibleModuleServiceForJob(String jobName){ | |
| 113 | 0 | for(ModuleService moduleService : installedModuleServices){ |
| 114 | 0 | if(moduleService.isResponsibleForJob(jobName)){ |
| 115 | 0 | return moduleService; |
| 116 | } | |
| 117 | } | |
| 118 | 0 | return null; |
| 119 | } | |
| 120 | ||
| 121 | @Override | |
| 122 | public void setInstalledModuleServices(List<ModuleService> installedModuleServices) { | |
| 123 | 0 | this.installedModuleServices = installedModuleServices; |
| 124 | 0 | } |
| 125 | ||
| 126 | @Override | |
| 127 | public List<String> getDataDictionaryPackages() { | |
| 128 | 0 | List<String> packages = new ArrayList<String>(); |
| 129 | 0 | for ( ModuleService moduleService : installedModuleServices ) { |
| 130 | 0 | if ( moduleService.getModuleConfiguration().getDataDictionaryPackages() != null ) { |
| 131 | 0 | packages.addAll( moduleService.getModuleConfiguration().getDataDictionaryPackages() ); |
| 132 | } | |
| 133 | } | |
| 134 | 0 | return packages; |
| 135 | } | |
| 136 | ||
| 137 | @Override | |
| 138 | public String getNamespaceName(final String namespaceCode){ | |
| 139 | 0 | Namespace parameterNamespace = CoreApiServiceLocator.getNamespaceService().getNamespace(namespaceCode); |
| 140 | 0 | return parameterNamespace==null ? "" : parameterNamespace.getName(); |
| 141 | } | |
| 142 | ||
| 143 | /** | |
| 144 | * @param loadRiceInstalledModuleServices the loadRiceInstalledModuleServices to set | |
| 145 | */ | |
| 146 | public void setLoadRiceInstalledModuleServices( | |
| 147 | boolean loadRiceInstalledModuleServices) { | |
| 148 | 0 | this.loadRiceInstalledModuleServices = loadRiceInstalledModuleServices; |
| 149 | 0 | } |
| 150 | ||
| 151 | /*** | |
| 152 | * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() | |
| 153 | */ | |
| 154 | @Override | |
| 155 | public void afterPropertiesSet() throws Exception { | |
| 156 | 0 | if(loadRiceInstalledModuleServices){ |
| 157 | try { | |
| 158 | 0 | installedModuleServices.addAll( |
| 159 | GlobalResourceLoader.<KualiModuleService>getService(KualiModuleService.class.getSimpleName().substring(0, 1).toLowerCase() + KualiModuleService.class.getSimpleName().substring(1)).getInstalledModuleServices()); | |
| 160 | 0 | } catch ( NoSuchBeanDefinitionException ex ) { |
| 161 | 0 | installedModuleServices.addAll( ((KualiModuleService)applicationContext.getBean( KRADServiceLocatorWeb.KUALI_MODULE_SERVICE )).getInstalledModuleServices() ); |
| 162 | 0 | } |
| 163 | } | |
| 164 | 0 | } |
| 165 | ||
| 166 | @Override | |
| 167 | public String getNamespaceCode(Class<?> documentClass) { | |
| 168 | 0 | if (documentClass == null) { |
| 169 | 0 | throw new IllegalArgumentException("documentClass must not be null"); |
| 170 | } | |
| 171 | ||
| 172 | 0 | if (documentClass.isAnnotationPresent(ParameterConstants.NAMESPACE.class)) { |
| 173 | 0 | return (documentClass.getAnnotation(ParameterConstants.NAMESPACE.class)).namespace(); |
| 174 | } | |
| 175 | 0 | ModuleService moduleService = getResponsibleModuleService(documentClass); |
| 176 | 0 | if (moduleService != null) { |
| 177 | 0 | return moduleService.getModuleConfiguration().getNamespaceCode(); |
| 178 | } | |
| 179 | 0 | if (documentClass.getName().startsWith("org.kuali.rice.krad")) { |
| 180 | 0 | return KRADConstants.KRAD_NAMESPACE; |
| 181 | } | |
| 182 | 0 | if (documentClass.getName().startsWith("org.kuali.rice.edl")) { |
| 183 | 0 | return "KR-EDL"; |
| 184 | } | |
| 185 | 0 | if (documentClass.getName().startsWith("org.kuali.rice.kew")) { |
| 186 | 0 | return "KR-WKFLW"; |
| 187 | } | |
| 188 | 0 | if (documentClass.getName().startsWith("org.kuali.rice.edl")) { |
| 189 | 0 | return "KR-WKFLW"; |
| 190 | } | |
| 191 | 0 | if (documentClass.getName().startsWith("org.kuali.rice.kim")) { |
| 192 | 0 | return "KR-IDM"; |
| 193 | } | |
| 194 | 0 | if (documentClass.getName().startsWith("org.kuali.rice.core")) { |
| 195 | 0 | return "KR-CORE"; |
| 196 | } | |
| 197 | 0 | throw new IllegalArgumentException("Unable to determine the namespace code for documentClass " + documentClass.getName()); |
| 198 | } | |
| 199 | ||
| 200 | @Override | |
| 201 | public String getComponentCode(Class<?> documentClass) { | |
| 202 | 0 | if (documentClass == null) { |
| 203 | 0 | throw new IllegalArgumentException("documentClass must not be null"); |
| 204 | } | |
| 205 | ||
| 206 | 0 | if (documentClass.isAnnotationPresent(ParameterConstants.COMPONENT.class)) { |
| 207 | 0 | return documentClass.getAnnotation(ParameterConstants.COMPONENT.class).component(); |
| 208 | 0 | } else if (TransactionalDocument.class.isAssignableFrom(documentClass)) { |
| 209 | 0 | return documentClass.getSimpleName().replace("Document", ""); |
| 210 | 0 | } else if (BusinessObject.class.isAssignableFrom(documentClass)) { |
| 211 | 0 | return documentClass.getSimpleName(); |
| 212 | } | |
| 213 | 0 | throw new IllegalArgumentException("Unable to determine the component code for documentClass " + documentClass.getName()); |
| 214 | } | |
| 215 | ||
| 216 | } | |
| 217 |