View Javadoc

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