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