001/** 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krad.uif.util; 017 018import java.util.ArrayList; 019import java.util.Collections; 020import java.util.HashMap; 021import java.util.LinkedHashMap; 022import java.util.List; 023import java.util.Map; 024import java.util.UUID; 025 026import org.kuali.rice.krad.bo.ExternalizableBusinessObject; 027import org.kuali.rice.krad.bo.ModuleConfiguration; 028import org.kuali.rice.krad.service.KRADServiceLocatorWeb; 029import org.kuali.rice.krad.service.KualiModuleService; 030import org.kuali.rice.krad.service.ModuleService; 031import org.springframework.beans.BeansException; 032import org.springframework.beans.factory.InitializingBean; 033import org.springframework.context.ApplicationContext; 034import org.springframework.context.ApplicationContextAware; 035 036/** 037 * Mock module service for supporting UIF calls. 038 * 039 * @author Kuali Rice Team (rice.collab@kuali.org) 040 */ 041public class MockKualiModuleService implements KualiModuleService, ApplicationContextAware, InitializingBean { 042 043 private static MockKualiModuleService bootstrap; 044 private static ApplicationContext applicationContext; 045 private static Map<String, ModuleService> installedModuleServices = new LinkedHashMap<String, ModuleService>(); 046 047 private Map<String, List<String>> resourceBundleNames; 048 049 private static ModuleConfiguration createMockModuleConfiguration(String namespaceCode, 050 Class<? extends ExternalizableBusinessObject> boClass) { 051 ModuleConfiguration rv = new ModuleConfiguration(); 052 rv.setApplicationContext(applicationContext); 053 rv.setDataDictionaryService(KRADServiceLocatorWeb.getDataDictionaryService()); 054 rv.setInitializeDataDictionary(false); 055 rv.setNamespaceCode(namespaceCode); 056 @SuppressWarnings("rawtypes") 057 Map<Class, Class> externalizableBusinessObjectImplementations = new HashMap<Class, Class>(); 058 externalizableBusinessObjectImplementations.put(boClass, boClass); 059 rv.setExternalizableBusinessObjectImplementations(externalizableBusinessObjectImplementations); 060 List<String> packagePrefixes = new ArrayList<String>(); 061 packagePrefixes.add(boClass.getPackage().getName()); 062 rv.setPackagePrefixes(packagePrefixes); 063 rv.setResourceBundleNames(bootstrap.resourceBundleNames.get(namespaceCode)); 064 return rv; 065 } 066 067 public static <T extends ExternalizableBusinessObject> void registerModuleService(String moduleId, 068 String namespaceCode, Class<T> boClass, boolean lookupable, boolean inquirable, List<T> instances) { 069 MockModuleService mockService = new MockModuleService(instances, lookupable ? Collections 070 .<Class<?>> singletonList(boClass) : Collections.<Class<?>> emptyList(), inquirable ? Collections 071 .<Class<?>> singletonList(boClass) : Collections.<Class<?>> emptyList()); 072 mockService.setKualiModuleService(bootstrap); 073 mockService.setModuleConfiguration(createMockModuleConfiguration(namespaceCode, boClass)); 074 installedModuleServices.put(moduleId, mockService); 075 } 076 077 public static class MockBusinessObject implements ExternalizableBusinessObject { 078 079 @Override 080 public void refresh() { 081 } 082 } 083 084 /** 085 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() 086 */ 087 @Override 088 public void afterPropertiesSet() throws Exception { 089 bootstrap = this; 090 if (resourceBundleNames == null) { 091 resourceBundleNames = Collections.emptyMap(); 092 } else { 093 for (Map.Entry<String, List<String>> resourceBundleEntry : resourceBundleNames.entrySet()) { 094 registerModuleService(UUID.randomUUID().toString(), resourceBundleEntry.getKey(), 095 MockBusinessObject.class, false, false, Collections.<MockBusinessObject> emptyList()); 096 } 097 } 098 } 099 100 /** 101 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext) 102 */ 103 @Override 104 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 105 MockKualiModuleService.applicationContext = applicationContext; 106 } 107 108 /** 109 * @return the resourceBundleNames 110 */ 111 public Map<String, List<String>> getResourceBundleNames() { 112 return this.resourceBundleNames; 113 } 114 115 /** 116 * @param resourceBundleNames the resourceBundleNames to set 117 */ 118 public void setResourceBundleNames(Map<String, List<String>> resourceBundleNames) { 119 this.resourceBundleNames = resourceBundleNames; 120 } 121 122 /** 123 * @see org.kuali.rice.krad.service.KualiModuleService#getInstalledModuleServices() 124 */ 125 @Override 126 public List<ModuleService> getInstalledModuleServices() { 127 return new ArrayList<ModuleService>(installedModuleServices.values()); 128 } 129 130 /** 131 * @see org.kuali.rice.krad.service.KualiModuleService#getModuleService(java.lang.String) 132 */ 133 @Override 134 public ModuleService getModuleService(String moduleId) { 135 return installedModuleServices.get(moduleId); 136 } 137 138 /** 139 * @see org.kuali.rice.krad.service.KualiModuleService#getModuleServiceByNamespaceCode(java.lang.String) 140 */ 141 @Override 142 public ModuleService getModuleServiceByNamespaceCode(String namespaceCode) { 143 for (ModuleService moduleService : installedModuleServices.values()) { 144 if (moduleService.getModuleConfiguration().getNamespaceCode().equals(namespaceCode)) { 145 return moduleService; 146 } 147 } 148 return null; 149 } 150 151 /** 152 * @see org.kuali.rice.krad.service.KualiModuleService#isModuleServiceInstalled(java.lang.String) 153 */ 154 @Override 155 public boolean isModuleServiceInstalled(String namespaceCode) { 156 for (ModuleService moduleService : installedModuleServices.values()) { 157 if (moduleService.getModuleConfiguration().getNamespaceCode().equals(namespaceCode)) { 158 return true; 159 } 160 } 161 return false; 162 } 163 164 /** 165 * @see org.kuali.rice.krad.service.KualiModuleService#getResponsibleModuleService(java.lang.Class) 166 */ 167 @Override 168 public ModuleService getResponsibleModuleService(Class boClass) { 169 for (ModuleService moduleService : installedModuleServices.values()) { 170 if (moduleService.isResponsibleFor(boClass)) { 171 return moduleService; 172 } 173 } 174 return null; 175 } 176 177 /** 178 * @see org.kuali.rice.krad.service.KualiModuleService#setInstalledModuleServices(java.util.List) 179 */ 180 @Override 181 public void setInstalledModuleServices(List<ModuleService> moduleServices) { 182 throw new UnsupportedOperationException(); 183 } 184 185 /** 186 * @see org.kuali.rice.krad.service.KualiModuleService#getDataDictionaryPackages() 187 */ 188 @Override 189 public List<String> getDataDictionaryPackages() { 190 throw new UnsupportedOperationException(); 191 } 192 193 /** 194 * @see org.kuali.rice.krad.service.KualiModuleService#getNamespaceName(java.lang.String) 195 */ 196 @Override 197 public String getNamespaceName(String namespaceCode) { 198 throw new UnsupportedOperationException(); 199 } 200 201 /** 202 * @see org.kuali.rice.krad.service.KualiModuleService#getNamespaceCode(java.lang.Class) 203 */ 204 @Override 205 public String getNamespaceCode(Class<?> documentOrStepClass) { 206 throw new UnsupportedOperationException(); 207 } 208 209 /** 210 * @see org.kuali.rice.krad.service.KualiModuleService#getComponentCode(java.lang.Class) 211 */ 212 @Override 213 public String getComponentCode(Class<?> documentOrStepClass) { 214 throw new UnsupportedOperationException(); 215 } 216 217}