1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kns.service.impl; |
17 | |
|
18 | |
import java.lang.reflect.Modifier; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.List; |
21 | |
import java.util.Map; |
22 | |
import java.util.Properties; |
23 | |
|
24 | |
import org.apache.commons.beanutils.PropertyUtils; |
25 | |
import org.apache.commons.lang.StringUtils; |
26 | |
import org.apache.log4j.Logger; |
27 | |
import org.kuali.rice.kns.bo.BusinessObject; |
28 | |
import org.kuali.rice.kns.bo.BusinessObjectRelationship; |
29 | |
import org.kuali.rice.kns.bo.ExternalizableBusinessObject; |
30 | |
import org.kuali.rice.kns.bo.ModuleConfiguration; |
31 | |
import org.kuali.rice.kns.datadictionary.BusinessObjectEntry; |
32 | |
import org.kuali.rice.kns.datadictionary.PrimitiveAttributeDefinition; |
33 | |
import org.kuali.rice.kns.datadictionary.RelationshipDefinition; |
34 | |
import org.kuali.rice.kns.exception.KualiException; |
35 | |
import org.kuali.rice.kns.service.BusinessObjectDictionaryService; |
36 | |
import org.kuali.rice.kns.service.BusinessObjectService; |
37 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
38 | |
import org.kuali.rice.kns.service.KualiModuleService; |
39 | |
import org.kuali.rice.kns.service.LookupService; |
40 | |
import org.kuali.rice.kns.service.ModuleService; |
41 | |
import org.kuali.rice.kns.util.ExternalizableBusinessObjectUtils; |
42 | |
import org.kuali.rice.kns.util.KNSConstants; |
43 | |
import org.kuali.rice.kns.util.ObjectUtils; |
44 | |
import org.kuali.rice.kns.util.UrlFactory; |
45 | |
import org.springframework.beans.BeansException; |
46 | |
import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
47 | |
import org.springframework.context.ApplicationContext; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 0 | public class ModuleServiceBase implements ModuleService { |
56 | |
|
57 | 0 | protected static final Logger LOG = Logger.getLogger(ModuleServiceBase.class); |
58 | |
|
59 | |
protected ModuleConfiguration moduleConfiguration; |
60 | |
protected BusinessObjectService businessObjectService; |
61 | |
protected LookupService lookupService; |
62 | |
protected BusinessObjectDictionaryService businessObjectDictionaryService; |
63 | |
protected KualiModuleService kualiModuleService; |
64 | |
protected ApplicationContext applicationContext; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public boolean isResponsibleFor(Class businessObjectClass) { |
70 | 0 | if(getModuleConfiguration() == null) |
71 | 0 | throw new IllegalStateException("Module configuration has not been initialized for the module service."); |
72 | |
|
73 | 0 | if (getModuleConfiguration().getPackagePrefixes() == null || businessObjectClass == null) { |
74 | 0 | return false; |
75 | |
} |
76 | 0 | for (String prefix : getModuleConfiguration().getPackagePrefixes()) { |
77 | 0 | if (businessObjectClass.getPackage().getName().startsWith(prefix)) { |
78 | 0 | return true; |
79 | |
} |
80 | |
} |
81 | 0 | if (ExternalizableBusinessObject.class.isAssignableFrom(businessObjectClass)) { |
82 | 0 | Class externalizableBusinessObjectInterface = ExternalizableBusinessObjectUtils.determineExternalizableBusinessObjectSubInterface(businessObjectClass); |
83 | 0 | if (externalizableBusinessObjectInterface != null) { |
84 | 0 | for (String prefix : getModuleConfiguration().getPackagePrefixes()) { |
85 | 0 | if (externalizableBusinessObjectInterface.getPackage().getName().startsWith(prefix)) { |
86 | 0 | return true; |
87 | |
} |
88 | |
} |
89 | |
} |
90 | |
} |
91 | 0 | return false; |
92 | |
} |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public boolean isResponsibleForJob(String jobName) { |
100 | 0 | if(getModuleConfiguration() == null) |
101 | 0 | throw new IllegalStateException("Module configuration has not been initialized for the module service."); |
102 | |
|
103 | 0 | if (getModuleConfiguration().getJobNames() == null || StringUtils.isEmpty(jobName)) |
104 | 0 | return false; |
105 | |
|
106 | 0 | return getModuleConfiguration().getJobNames().contains(jobName); |
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public <T extends ExternalizableBusinessObject> T getExternalizableBusinessObject(Class<T> businessObjectClass, Map<String, Object> fieldValues) { |
113 | 0 | Class<? extends ExternalizableBusinessObject> implementationClass = getExternalizableBusinessObjectImplementation(businessObjectClass); |
114 | 0 | ExternalizableBusinessObject businessObject = (ExternalizableBusinessObject) |
115 | |
getBusinessObjectService().findByPrimaryKey(implementationClass, fieldValues); |
116 | 0 | return (T) businessObject; |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public <T extends ExternalizableBusinessObject> List<T> getExternalizableBusinessObjectsList( |
123 | |
Class<T> externalizableBusinessObjectClass, Map<String, Object> fieldValues) { |
124 | 0 | Class<? extends ExternalizableBusinessObject> implementationClass = getExternalizableBusinessObjectImplementation(externalizableBusinessObjectClass); |
125 | 0 | return (List<T>) getBusinessObjectService().findMatching(implementationClass, fieldValues); |
126 | |
} |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
public <T extends ExternalizableBusinessObject> List<T> getExternalizableBusinessObjectsListForLookup( |
132 | |
Class<T> externalizableBusinessObjectClass, Map<String, Object> fieldValues, boolean unbounded) { |
133 | 0 | Class<? extends ExternalizableBusinessObject> implementationClass = getExternalizableBusinessObjectImplementation(externalizableBusinessObjectClass); |
134 | 0 | if (isExternalizableBusinessObjectLookupable(implementationClass)) { |
135 | 0 | return (List<T>) getLookupService().findCollectionBySearchHelper(implementationClass, fieldValues, unbounded); |
136 | |
} else { |
137 | 0 | throw new KualiException("External business object is not lookupable: "+implementationClass); |
138 | |
} |
139 | |
} |
140 | |
|
141 | |
public List listPrimaryKeyFieldNames(Class businessObjectInterfaceClass){ |
142 | 0 | Class clazz = getExternalizableBusinessObjectImplementation(businessObjectInterfaceClass); |
143 | 0 | return KNSServiceLocator.getPersistenceStructureService().listPrimaryKeyFieldNames(clazz); |
144 | |
} |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
public BusinessObjectEntry getExternalizableBusinessObjectDictionaryEntry( |
150 | |
Class businessObjectInterfaceClass) { |
151 | 0 | Class boClass = businessObjectInterfaceClass; |
152 | 0 | if(businessObjectInterfaceClass.isInterface()) |
153 | 0 | boClass = getExternalizableBusinessObjectImplementation(businessObjectInterfaceClass); |
154 | 0 | return boClass==null?null: |
155 | |
KNSServiceLocator.getDataDictionaryService().getDataDictionary().getBusinessObjectEntryForConcreteClass(boClass.getName()); |
156 | |
} |
157 | |
|
158 | |
public String getExternalizableBusinessObjectInquiryUrl(Class inquiryBusinessObjectClass, Map<String, String[]> parameters) { |
159 | 0 | if(!ExternalizableBusinessObject.class.isAssignableFrom(inquiryBusinessObjectClass)) { |
160 | 0 | return KNSConstants.EMPTY_STRING; |
161 | |
} |
162 | |
String businessObjectClassAttribute; |
163 | 0 | if(inquiryBusinessObjectClass.isInterface()){ |
164 | 0 | Class implementationClass = getExternalizableBusinessObjectImplementation(inquiryBusinessObjectClass); |
165 | 0 | if (implementationClass == null) { |
166 | 0 | LOG.error("Can't find ExternalizableBusinessObject implementation class for interface " + inquiryBusinessObjectClass.getName()); |
167 | 0 | throw new RuntimeException("Can't find ExternalizableBusinessObject implementation class for interface " + inquiryBusinessObjectClass.getName()); |
168 | |
} |
169 | 0 | businessObjectClassAttribute = implementationClass.getName(); |
170 | 0 | }else{ |
171 | 0 | LOG.warn("Inquiry was invoked with a non-interface class object " + inquiryBusinessObjectClass.getName()); |
172 | 0 | businessObjectClassAttribute = inquiryBusinessObjectClass.getName(); |
173 | |
} |
174 | 0 | return UrlFactory.parameterizeUrl( |
175 | |
getInquiryUrl(inquiryBusinessObjectClass), |
176 | |
getUrlParameters(businessObjectClassAttribute, parameters)); |
177 | |
} |
178 | |
|
179 | |
protected Properties getUrlParameters(String businessObjectClassAttribute, Map<String, String[]> parameters){ |
180 | 0 | Properties urlParameters = new Properties(); |
181 | 0 | for (String paramName : parameters.keySet()) { |
182 | 0 | String[] parameterValues = parameters.get(paramName); |
183 | 0 | if (parameterValues.length > 0) { |
184 | 0 | urlParameters.put(paramName, parameterValues[0]); |
185 | |
} |
186 | 0 | } |
187 | 0 | urlParameters.put(KNSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, businessObjectClassAttribute); |
188 | 0 | urlParameters.put(KNSConstants.DISPATCH_REQUEST_PARAMETER, KNSConstants.CONTINUE_WITH_INQUIRY_METHOD_TO_CALL); |
189 | 0 | return urlParameters; |
190 | |
} |
191 | |
|
192 | |
protected String getInquiryUrl(Class inquiryBusinessObjectClass){ |
193 | 0 | String riceBaseUrl = KNSServiceLocator.getKualiConfigurationService().getPropertyString(KNSConstants.APPLICATION_URL_KEY); |
194 | 0 | String inquiryUrl = riceBaseUrl; |
195 | 0 | if (!inquiryUrl.endsWith("/")) { |
196 | 0 | inquiryUrl = inquiryUrl + "/"; |
197 | |
} |
198 | 0 | return inquiryUrl + "kr/" + KNSConstants.INQUIRY_ACTION; |
199 | |
} |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
public String getExternalizableBusinessObjectLookupUrl(Class inquiryBusinessObjectClass, Map<String, String> parameters) { |
207 | 0 | Properties urlParameters = new Properties(); |
208 | |
|
209 | 0 | String riceBaseUrl = KNSServiceLocator.getKualiConfigurationService().getPropertyString(KNSConstants.APPLICATION_URL_KEY); |
210 | 0 | String lookupUrl = riceBaseUrl; |
211 | 0 | if (!lookupUrl.endsWith("/")) { |
212 | 0 | lookupUrl = lookupUrl + "/"; |
213 | |
} |
214 | 0 | if (parameters.containsKey(KNSConstants.MULTIPLE_VALUE)) { |
215 | 0 | lookupUrl = lookupUrl + "kr/" + KNSConstants.MULTIPLE_VALUE_LOOKUP_ACTION; |
216 | |
} |
217 | |
else { |
218 | 0 | lookupUrl = lookupUrl + "kr/" + KNSConstants.LOOKUP_ACTION; |
219 | |
} |
220 | 0 | for (String paramName : parameters.keySet()) { |
221 | 0 | urlParameters.put(paramName, parameters.get(paramName)); |
222 | |
} |
223 | |
|
224 | 0 | Class clazz = getExternalizableBusinessObjectImplementation(inquiryBusinessObjectClass); |
225 | 0 | urlParameters.put(KNSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, clazz==null?"":clazz.getName()); |
226 | |
|
227 | 0 | return UrlFactory.parameterizeUrl(lookupUrl, urlParameters); |
228 | |
} |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
public <T extends ExternalizableBusinessObject> T retrieveExternalizableBusinessObjectIfNecessary( |
238 | |
BusinessObject businessObject, T currentInstanceExternalizableBO, String externalizableRelationshipName) { |
239 | |
|
240 | 0 | if(businessObject==null) return null; |
241 | |
Class clazz; |
242 | |
try{ |
243 | 0 | clazz = getExternalizableBusinessObjectImplementation( |
244 | |
PropertyUtils.getPropertyType(businessObject, externalizableRelationshipName)); |
245 | 0 | } catch(Exception iex){ |
246 | 0 | LOG.warn("Exception:"+iex+" thrown while trying to get property type for property:"+externalizableRelationshipName+ |
247 | |
" from business object:"+businessObject); |
248 | 0 | return null; |
249 | 0 | } |
250 | |
|
251 | |
|
252 | |
|
253 | 0 | BusinessObjectEntry entry = |
254 | |
KNSServiceLocator.getDataDictionaryService().getDataDictionary().getBusinessObjectEntries().get( |
255 | |
businessObject.getClass().getSimpleName()); |
256 | 0 | RelationshipDefinition relationshipDefinition = entry.getRelationshipDefinition(externalizableRelationshipName); |
257 | 0 | List<PrimitiveAttributeDefinition> primitiveAttributeDefinitions = relationshipDefinition.getPrimitiveAttributes(); |
258 | |
|
259 | 0 | Map<String, Object> fieldValuesInEBO = new HashMap<String, Object>(); |
260 | |
Object sourcePropertyValue; |
261 | 0 | Object targetPropertyValue = null; |
262 | 0 | boolean sourceTargetPropertyValuesSame = true; |
263 | 0 | for(PrimitiveAttributeDefinition primitiveAttributeDefinition: primitiveAttributeDefinitions){ |
264 | 0 | sourcePropertyValue = ObjectUtils.getPropertyValue( |
265 | |
businessObject, primitiveAttributeDefinition.getSourceName()); |
266 | 0 | if(currentInstanceExternalizableBO!=null) |
267 | 0 | targetPropertyValue = ObjectUtils.getPropertyValue(currentInstanceExternalizableBO, primitiveAttributeDefinition.getTargetName()); |
268 | 0 | if(sourcePropertyValue==null){ |
269 | 0 | return null; |
270 | 0 | } else if(targetPropertyValue==null || (targetPropertyValue!=null && !targetPropertyValue.equals(sourcePropertyValue))){ |
271 | 0 | sourceTargetPropertyValuesSame = false; |
272 | |
} |
273 | 0 | fieldValuesInEBO.put(primitiveAttributeDefinition.getTargetName(), sourcePropertyValue); |
274 | |
} |
275 | |
|
276 | 0 | if(!sourceTargetPropertyValuesSame) |
277 | 0 | return (T) getExternalizableBusinessObject(clazz, fieldValuesInEBO); |
278 | 0 | return currentInstanceExternalizableBO; |
279 | |
} |
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
public List<? extends ExternalizableBusinessObject> retrieveExternalizableBusinessObjectsList( |
289 | |
BusinessObject businessObject, String externalizableRelationshipName, Class externalizableClazz) { |
290 | |
|
291 | 0 | if(businessObject==null) return null; |
292 | |
|
293 | |
|
294 | 0 | String className = businessObject.getClass().getName(); |
295 | 0 | String key = className.substring(className.lastIndexOf(".")+1); |
296 | 0 | BusinessObjectEntry entry = |
297 | |
KNSServiceLocator.getDataDictionaryService().getDataDictionary().getBusinessObjectEntries().get(key); |
298 | 0 | RelationshipDefinition relationshipDefinition = entry.getRelationshipDefinition(externalizableRelationshipName); |
299 | 0 | List<PrimitiveAttributeDefinition> primitiveAttributeDefinitions = relationshipDefinition.getPrimitiveAttributes(); |
300 | 0 | Map<String, Object> fieldValuesInEBO = new HashMap<String, Object>(); |
301 | |
Object sourcePropertyValue; |
302 | 0 | for(PrimitiveAttributeDefinition primitiveAttributeDefinition: primitiveAttributeDefinitions){ |
303 | 0 | sourcePropertyValue = ObjectUtils.getPropertyValue( |
304 | |
businessObject, primitiveAttributeDefinition.getSourceName()); |
305 | 0 | if(sourcePropertyValue==null){ |
306 | 0 | return null; |
307 | |
} |
308 | 0 | fieldValuesInEBO.put(primitiveAttributeDefinition.getTargetName(), sourcePropertyValue); |
309 | |
} |
310 | 0 | return getExternalizableBusinessObjectsList( |
311 | |
getExternalizableBusinessObjectImplementation(externalizableClazz), fieldValuesInEBO); |
312 | |
} |
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
public <E extends ExternalizableBusinessObject> Class<E> getExternalizableBusinessObjectImplementation(Class<E> externalizableBusinessObjectInterface) { |
318 | 0 | if (getModuleConfiguration() == null) { |
319 | 0 | throw new IllegalStateException("Module configuration has not been initialized for the module service."); |
320 | |
} |
321 | 0 | int classModifiers = externalizableBusinessObjectInterface.getModifiers(); |
322 | 0 | if (!Modifier.isInterface(classModifiers) && !Modifier.isAbstract(classModifiers)) { |
323 | |
|
324 | 0 | return externalizableBusinessObjectInterface; |
325 | |
} |
326 | 0 | if (getModuleConfiguration().getExternalizableBusinessObjectImplementations() == null) { |
327 | 0 | return null; |
328 | |
} |
329 | |
else { |
330 | 0 | Class<E> implementationClass = getModuleConfiguration().getExternalizableBusinessObjectImplementations().get(externalizableBusinessObjectInterface); |
331 | 0 | int implClassModifiers = implementationClass.getModifiers(); |
332 | 0 | if (Modifier.isInterface(implClassModifiers) || Modifier.isAbstract(implClassModifiers)) { |
333 | 0 | throw new RuntimeException("Implementation class must be non-abstract class: ebo interface: " + externalizableBusinessObjectInterface.getName() + " impl class: " |
334 | |
+ implementationClass.getName() + " module: " + getModuleConfiguration().getNamespaceCode()); |
335 | |
} |
336 | 0 | return implementationClass; |
337 | |
} |
338 | |
|
339 | |
} |
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
public void afterPropertiesSet() throws Exception { |
345 | 0 | KualiModuleService kualiModuleService = null; |
346 | |
try { |
347 | 0 | kualiModuleService = KNSServiceLocator.getKualiModuleService(); |
348 | 0 | if ( kualiModuleService == null ) { |
349 | 0 | kualiModuleService = ((KualiModuleService)applicationContext.getBean( KNSServiceLocator.KUALI_MODULE_SERVICE )); |
350 | |
} |
351 | 0 | } catch ( NoSuchBeanDefinitionException ex ) { |
352 | 0 | kualiModuleService = ((KualiModuleService)applicationContext.getBean( KNSServiceLocator.KUALI_MODULE_SERVICE )); |
353 | 0 | } |
354 | 0 | kualiModuleService.getInstalledModuleServices().add( this ); |
355 | 0 | } |
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
public ModuleConfiguration getModuleConfiguration() { |
361 | 0 | return this.moduleConfiguration; |
362 | |
} |
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
public void setModuleConfiguration(ModuleConfiguration moduleConfiguration) { |
368 | 0 | this.moduleConfiguration = moduleConfiguration; |
369 | 0 | } |
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
public boolean isExternalizable(Class boClazz){ |
375 | 0 | if(boClazz==null) return false; |
376 | 0 | return ExternalizableBusinessObject.class.isAssignableFrom(boClazz); |
377 | |
} |
378 | |
|
379 | |
public boolean isExternalizableBusinessObjectLookupable(Class boClass) { |
380 | 0 | return getBusinessObjectDictionaryService().isLookupable(boClass); |
381 | |
} |
382 | |
|
383 | |
public boolean isExternalizableBusinessObjectInquirable(Class boClass) { |
384 | 0 | return getBusinessObjectDictionaryService().isInquirable(boClass); |
385 | |
} |
386 | |
|
387 | |
public <T extends ExternalizableBusinessObject> T createNewObjectFromExternalizableClass(Class<T> boClass) { |
388 | |
try { |
389 | 0 | return (T) getExternalizableBusinessObjectImplementation(boClass).newInstance(); |
390 | 0 | } catch (Exception e) { |
391 | 0 | throw new RuntimeException("Unable to create externalizable business object class", e); |
392 | |
} |
393 | |
} |
394 | |
|
395 | |
public BusinessObjectRelationship getBusinessObjectRelationship(Class boClass, String attributeName, String attributePrefix){ |
396 | 0 | return null; |
397 | |
} |
398 | |
|
399 | |
|
400 | |
|
401 | |
public BusinessObjectDictionaryService getBusinessObjectDictionaryService () { |
402 | 0 | if ( businessObjectDictionaryService == null ) { |
403 | 0 | businessObjectDictionaryService = KNSServiceLocator.getBusinessObjectDictionaryService(); |
404 | |
} |
405 | 0 | return businessObjectDictionaryService; |
406 | |
} |
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
public BusinessObjectService getBusinessObjectService() { |
412 | 0 | if ( businessObjectService == null ) { |
413 | 0 | businessObjectService = KNSServiceLocator.getBusinessObjectService(); |
414 | |
} |
415 | 0 | return businessObjectService; |
416 | |
} |
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | |
|
422 | |
protected LookupService getLookupService() { |
423 | 0 | return lookupService != null ? lookupService : KNSServiceLocator.getLookupService(); |
424 | |
} |
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
public KualiModuleService getKualiModuleService() { |
430 | 0 | return this.kualiModuleService; |
431 | |
} |
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
public void setKualiModuleService(KualiModuleService kualiModuleService) { |
437 | 0 | this.kualiModuleService = kualiModuleService; |
438 | 0 | } |
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
444 | 0 | this.applicationContext = applicationContext; |
445 | 0 | } |
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
public List<List<String>> listAlternatePrimaryKeyFieldNames( |
455 | |
Class businessObjectInterfaceClass) { |
456 | 0 | return null; |
457 | |
} |
458 | |
|
459 | |
} |
460 | |
|