View Javadoc
1   /**
2    * Copyright 2005-2015 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.uif.util;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.LinkedHashMap;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.UUID;
25  
26  import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
27  import org.kuali.rice.krad.bo.ModuleConfiguration;
28  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
29  import org.kuali.rice.krad.service.KualiModuleService;
30  import org.kuali.rice.krad.service.ModuleService;
31  import org.springframework.beans.BeansException;
32  import org.springframework.beans.factory.InitializingBean;
33  import org.springframework.context.ApplicationContext;
34  import org.springframework.context.ApplicationContextAware;
35  
36  /**
37   * Mock module service for supporting UIF calls.
38   * 
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   */
41  public class MockKualiModuleService implements KualiModuleService, ApplicationContextAware, InitializingBean {
42  
43      private static MockKualiModuleService bootstrap;
44      private static ApplicationContext applicationContext;
45      private static Map<String, ModuleService> installedModuleServices = new LinkedHashMap<String, ModuleService>();
46      
47      private Map<String, List<String>> resourceBundleNames;
48  
49      private static ModuleConfiguration createMockModuleConfiguration(String namespaceCode,
50              Class<? extends ExternalizableBusinessObject> boClass) {
51          ModuleConfiguration rv = new ModuleConfiguration();
52          rv.setApplicationContext(applicationContext);
53          rv.setDataDictionaryService(KRADServiceLocatorWeb.getDataDictionaryService());
54          rv.setInitializeDataDictionary(false);
55          rv.setNamespaceCode(namespaceCode);
56          @SuppressWarnings("rawtypes")
57          Map<Class, Class> externalizableBusinessObjectImplementations = new HashMap<Class, Class>();
58          externalizableBusinessObjectImplementations.put(boClass, boClass);
59          rv.setExternalizableBusinessObjectImplementations(externalizableBusinessObjectImplementations);
60          List<String> packagePrefixes = new ArrayList<String>();
61          packagePrefixes.add(boClass.getPackage().getName());
62          rv.setPackagePrefixes(packagePrefixes);
63          rv.setResourceBundleNames(bootstrap.resourceBundleNames.get(namespaceCode));
64          return rv;
65      }
66  
67      public static <T extends ExternalizableBusinessObject> void registerModuleService(String moduleId,
68              String namespaceCode, Class<T> boClass, boolean lookupable, boolean inquirable, List<T> instances) {
69          MockModuleService mockService = new MockModuleService(instances, lookupable ? Collections
70                  .<Class<?>> singletonList(boClass) : Collections.<Class<?>> emptyList(), inquirable ? Collections
71                  .<Class<?>> singletonList(boClass) : Collections.<Class<?>> emptyList());
72          mockService.setKualiModuleService(bootstrap);
73          mockService.setModuleConfiguration(createMockModuleConfiguration(namespaceCode, boClass));
74          installedModuleServices.put(moduleId, mockService);
75      }
76  
77      public static class MockBusinessObject implements ExternalizableBusinessObject {
78  
79          @Override
80          public void refresh() {
81          }
82      }
83      
84      /**
85       * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
86       */
87      @Override
88      public void afterPropertiesSet() throws Exception {
89          bootstrap = this;
90          if (resourceBundleNames == null) {
91              resourceBundleNames = Collections.emptyMap();
92          } else {
93              for (Map.Entry<String, List<String>> resourceBundleEntry : resourceBundleNames.entrySet()) {
94                  registerModuleService(UUID.randomUUID().toString(), resourceBundleEntry.getKey(),
95                          MockBusinessObject.class, false, false, Collections.<MockBusinessObject> emptyList());
96              }
97          }
98      }
99  
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 }