View Javadoc
1   /**
2    * Copyright 2005-2014 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 java.util.ArrayList;
19  import java.util.List;
20  
21  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
22  import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
23  import org.kuali.rice.coreservice.api.namespace.Namespace;
24  import org.kuali.rice.coreservice.framework.parameter.ParameterConstants;
25  import org.kuali.rice.krad.bo.BusinessObject;
26  import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
27  import org.kuali.rice.krad.document.TransactionalDocument;
28  import org.kuali.rice.krad.exception.ModuleServiceNotFoundException;
29  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
30  import org.kuali.rice.krad.service.KualiModuleService;
31  import org.kuali.rice.krad.service.ModuleService;
32  import org.kuali.rice.krad.util.KRADConstants;
33  import org.springframework.beans.factory.InitializingBean;
34  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
35  import org.springframework.context.ApplicationContext;
36  import org.springframework.context.ApplicationContextAware;
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 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