1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
38
39
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
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
102
103 @Override
104 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
105 MockKualiModuleService.applicationContext = applicationContext;
106 }
107
108
109
110
111 public Map<String, List<String>> getResourceBundleNames() {
112 return this.resourceBundleNames;
113 }
114
115
116
117
118 public void setResourceBundleNames(Map<String, List<String>> resourceBundleNames) {
119 this.resourceBundleNames = resourceBundleNames;
120 }
121
122
123
124
125 @Override
126 public List<ModuleService> getInstalledModuleServices() {
127 return new ArrayList<ModuleService>(installedModuleServices.values());
128 }
129
130
131
132
133 @Override
134 public ModuleService getModuleService(String moduleId) {
135 return installedModuleServices.get(moduleId);
136 }
137
138
139
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
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
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
179
180 @Override
181 public void setInstalledModuleServices(List<ModuleService> moduleServices) {
182 throw new UnsupportedOperationException();
183 }
184
185
186
187
188 @Override
189 public List<String> getDataDictionaryPackages() {
190 throw new UnsupportedOperationException();
191 }
192
193
194
195
196 @Override
197 public String getNamespaceName(String namespaceCode) {
198 throw new UnsupportedOperationException();
199 }
200
201
202
203
204 @Override
205 public String getNamespaceCode(Class<?> documentOrStepClass) {
206 throw new UnsupportedOperationException();
207 }
208
209
210
211
212 @Override
213 public String getComponentCode(Class<?> documentOrStepClass) {
214 throw new UnsupportedOperationException();
215 }
216
217 }