1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.service.impl;
17
18 import org.apache.commons.beanutils.PropertyUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.log4j.Logger;
21 import org.kuali.rice.core.api.config.property.ConfigurationService;
22 import org.kuali.rice.coreservice.framework.CoreFrameworkServiceLocator;
23 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
24 import org.kuali.rice.krad.bo.BusinessObject;
25 import org.kuali.rice.krad.bo.DataObjectRelationship;
26 import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
27 import org.kuali.rice.krad.bo.ModuleConfiguration;
28 import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
29 import org.kuali.rice.krad.datadictionary.PrimitiveAttributeDefinition;
30 import org.kuali.rice.krad.datadictionary.RelationshipDefinition;
31 import org.kuali.rice.krad.service.BusinessObjectNotLookupableException;
32 import org.kuali.rice.krad.service.KRADServiceLocator;
33 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
34 import org.kuali.rice.krad.service.KualiModuleService;
35 import org.kuali.rice.krad.service.LookupService;
36 import org.kuali.rice.krad.service.ModuleService;
37 import org.kuali.rice.krad.uif.UifParameters;
38 import org.kuali.rice.krad.util.ExternalizableBusinessObjectUtils;
39 import org.kuali.rice.krad.util.KRADConstants;
40 import org.kuali.rice.krad.util.ObjectUtils;
41 import org.kuali.rice.krad.util.UrlFactory;
42 import org.springframework.beans.BeansException;
43 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
44 import org.springframework.context.ApplicationContext;
45
46 import java.lang.reflect.Modifier;
47 import java.util.HashMap;
48 import java.util.List;
49 import java.util.Map;
50 import java.util.Properties;
51
52
53 public abstract class RemoteModuleServiceBase implements ModuleService {
54 protected static final Logger LOG = Logger.getLogger(RemoteModuleServiceBase.class);
55
56 protected ModuleConfiguration moduleConfiguration;
57 protected KualiModuleService kualiModuleService;
58 protected ApplicationContext applicationContext;
59 protected ConfigurationService kualiConfigurationService;
60 protected LookupService lookupService;
61
62
63
64
65 public boolean isResponsibleFor(Class businessObjectClass) {
66 if (getModuleConfiguration() == null) {
67 throw new IllegalStateException("Module configuration has not been initialized for the module service.");
68 }
69
70 if (getModuleConfiguration().getPackagePrefixes() == null || businessObjectClass == null) {
71 return false;
72 }
73 for (String prefix : getModuleConfiguration().getPackagePrefixes()) {
74 if (businessObjectClass.getPackage().getName().startsWith(prefix)) {
75 return true;
76 }
77 }
78 if (ExternalizableBusinessObject.class.isAssignableFrom(businessObjectClass)) {
79 Class externalizableBusinessObjectInterface =
80 ExternalizableBusinessObjectUtils.determineExternalizableBusinessObjectSubInterface(
81 businessObjectClass);
82 if (externalizableBusinessObjectInterface != null) {
83 for (String prefix : getModuleConfiguration().getPackagePrefixes()) {
84 if (externalizableBusinessObjectInterface.getPackage().getName().startsWith(prefix)) {
85 return true;
86 }
87 }
88 }
89 }
90 return false;
91 }
92
93
94
95
96 public boolean isResponsibleForJob(String jobName) {
97 if (getModuleConfiguration() == null) {
98 throw new IllegalStateException("Module configuration has not been initialized for the module service.");
99 }
100
101 if (getModuleConfiguration().getJobNames() == null || StringUtils.isEmpty(jobName)) {
102 return false;
103 }
104
105 return getModuleConfiguration().getJobNames().contains(jobName);
106 }
107
108
109 public List listPrimaryKeyFieldNames(Class businessObjectInterfaceClass) {
110 Class clazz = getExternalizableBusinessObjectImplementation(businessObjectInterfaceClass);
111 return KRADServiceLocator.getPersistenceStructureService().listPrimaryKeyFieldNames(clazz);
112 }
113
114
115
116
117 public BusinessObjectEntry getExternalizableBusinessObjectDictionaryEntry(Class businessObjectInterfaceClass) {
118 Class boClass = getExternalizableBusinessObjectImplementation(businessObjectInterfaceClass);
119
120 return boClass == null ? null : KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary()
121 .getBusinessObjectEntryForConcreteClass(boClass.getName());
122 }
123
124
125
126
127
128 public String getExternalizableDataObjectInquiryUrl(Class<?> inquiryDataObjectClass, Properties parameters) {
129 String baseUrl = getBaseInquiryUrl();
130
131
132 if (ExternalizableBusinessObject.class.isAssignableFrom(inquiryDataObjectClass)) {
133 Class implementationClass = getExternalizableBusinessObjectImplementation(inquiryDataObjectClass.asSubclass(
134 ExternalizableBusinessObject.class));
135 if (implementationClass == null) {
136 throw new RuntimeException("Can't find ExternalizableBusinessObject implementation class for "
137 + inquiryDataObjectClass.getName());
138 }
139
140 parameters.put(UifParameters.DATA_OBJECT_CLASS_NAME, implementationClass.getName());
141 }
142
143 return UrlFactory.parameterizeUrl(baseUrl, parameters);
144 }
145
146
147
148
149
150
151 protected String getBaseInquiryUrl() {
152 return getKualiConfigurationService().getPropertyValueAsString(KRADConstants.KRAD_INQUIRY_URL_KEY);
153 }
154
155
156
157
158
159 public String getExternalizableDataObjectLookupUrl(Class<?> lookupDataObjectClass, Properties parameters) {
160 String baseUrl = getBaseLookupUrl();
161
162
163 if (ExternalizableBusinessObject.class.isAssignableFrom(lookupDataObjectClass)) {
164 Class implementationClass = getExternalizableBusinessObjectImplementation(lookupDataObjectClass.asSubclass(
165 ExternalizableBusinessObject.class));
166 if (implementationClass == null) {
167 throw new RuntimeException("Can't find ExternalizableBusinessObject implementation class for "
168 + lookupDataObjectClass.getName());
169 }
170
171 parameters.put(UifParameters.DATA_OBJECT_CLASS_NAME, implementationClass.getName());
172 }
173
174 return UrlFactory.parameterizeUrl(baseUrl, parameters);
175 }
176
177
178
179
180
181
182 protected String getRiceBaseLookupUrl() {
183 return getKualiConfigurationService().getPropertyValueAsString(KRADConstants.KRAD_LOOKUP_URL_KEY);
184 }
185
186
187
188
189
190
191 protected String getBaseLookupUrl() {
192 return getKualiConfigurationService().getPropertyValueAsString(KRADConstants.KRAD_LOOKUP_URL_KEY);
193 }
194
195 @Deprecated
196 public String getExternalizableBusinessObjectInquiryUrl(Class inquiryBusinessObjectClass,
197 Map<String, String[]> parameters) {
198 if (!isExternalizable(inquiryBusinessObjectClass)) {
199 return KRADConstants.EMPTY_STRING;
200 }
201 String businessObjectClassAttribute;
202
203 Class implementationClass = getExternalizableBusinessObjectImplementation(inquiryBusinessObjectClass);
204 if (implementationClass == null) {
205 LOG.error("Can't find ExternalizableBusinessObject implementation class for " + inquiryBusinessObjectClass
206 .getName());
207 throw new RuntimeException("Can't find ExternalizableBusinessObject implementation class for interface "
208 + inquiryBusinessObjectClass.getName());
209 }
210 businessObjectClassAttribute = implementationClass.getName();
211 return UrlFactory.parameterizeUrl(getInquiryUrl(inquiryBusinessObjectClass), getUrlParameters(
212 businessObjectClassAttribute, parameters));
213 }
214
215
216
217
218
219
220
221 @Deprecated
222 @Override
223 public String getExternalizableBusinessObjectLookupUrl(Class inquiryBusinessObjectClass,
224 Map<String, String> parameters) {
225 Properties urlParameters = new Properties();
226
227 String riceBaseUrl = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
228 KRADConstants.KUALI_RICE_URL_KEY);
229 String lookupUrl = riceBaseUrl;
230 if (!lookupUrl.endsWith("/")) {
231 lookupUrl = lookupUrl + "/";
232 }
233 if (parameters.containsKey(KRADConstants.MULTIPLE_VALUE)) {
234 lookupUrl = lookupUrl + KRADConstants.MULTIPLE_VALUE_LOOKUP_ACTION;
235 } else {
236 lookupUrl = lookupUrl + KRADConstants.LOOKUP_ACTION;
237 }
238 for (String paramName : parameters.keySet()) {
239 urlParameters.put(paramName, parameters.get(paramName));
240 }
241
242 Class clazz = getExternalizableBusinessObjectImplementation(inquiryBusinessObjectClass);
243 urlParameters.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, clazz == null ? "" : clazz.getName());
244
245 return UrlFactory.parameterizeUrl(lookupUrl, urlParameters);
246 }
247
248
249
250
251
252 public <T extends ExternalizableBusinessObject> List<T> getExternalizableBusinessObjectsListForLookup(
253 Class<T> externalizableBusinessObjectClass, Map<String, Object> fieldValues, boolean unbounded) {
254 Class<? extends ExternalizableBusinessObject> implementationClass =
255 getExternalizableBusinessObjectImplementation(externalizableBusinessObjectClass);
256 if (isExternalizableBusinessObjectLookupable(implementationClass)) {
257 Map<String, String> searchCriteria = new HashMap<String, String>();
258 for (Map.Entry<String, Object> fieldValue : fieldValues.entrySet()) {
259 if (fieldValue.getValue() != null) {
260 searchCriteria.put(fieldValue.getKey(), fieldValue.getValue().toString());
261 } else {
262 searchCriteria.put(fieldValue.getKey(), null);
263 }
264 }
265 return (List<T>) getLookupService().findCollectionBySearchHelper(implementationClass, searchCriteria,
266 unbounded);
267 } else {
268 throw new BusinessObjectNotLookupableException(
269 "External business object is not a Lookupable: " + implementationClass);
270 }
271 }
272
273
274
275
276
277
278
279
280 public <T extends ExternalizableBusinessObject> T retrieveExternalizableBusinessObjectIfNecessary(
281 BusinessObject businessObject, T currentInstanceExternalizableBO, String externalizableRelationshipName) {
282
283 if (businessObject == null) {
284 return null;
285 }
286 Class clazz;
287 try {
288 clazz = getExternalizableBusinessObjectImplementation(PropertyUtils.getPropertyType(businessObject,
289 externalizableRelationshipName));
290 } catch (Exception iex) {
291 LOG.warn("Exception:"
292 + iex
293 + " thrown while trying to get property type for property:"
294 + externalizableRelationshipName
295 + " from business object:"
296 + businessObject);
297 return null;
298 }
299
300
301
302 BusinessObjectEntry entry =
303 KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getBusinessObjectEntries().get(
304 businessObject.getClass().getSimpleName());
305 RelationshipDefinition relationshipDefinition = entry.getRelationshipDefinition(externalizableRelationshipName);
306 List<PrimitiveAttributeDefinition> primitiveAttributeDefinitions =
307 relationshipDefinition.getPrimitiveAttributes();
308
309 Map<String, Object> fieldValuesInEBO = new HashMap<String, Object>();
310 Object sourcePropertyValue;
311 Object targetPropertyValue = null;
312 boolean sourceTargetPropertyValuesSame = true;
313 for (PrimitiveAttributeDefinition primitiveAttributeDefinition : primitiveAttributeDefinitions) {
314 sourcePropertyValue = ObjectUtils.getPropertyValue(businessObject,
315 primitiveAttributeDefinition.getSourceName());
316 if (currentInstanceExternalizableBO != null) {
317 targetPropertyValue = ObjectUtils.getPropertyValue(currentInstanceExternalizableBO,
318 primitiveAttributeDefinition.getTargetName());
319 }
320 if (sourcePropertyValue == null) {
321 return null;
322 } else if (targetPropertyValue == null || (targetPropertyValue != null && !targetPropertyValue.equals(
323 sourcePropertyValue))) {
324 sourceTargetPropertyValuesSame = false;
325 }
326 fieldValuesInEBO.put(primitiveAttributeDefinition.getTargetName(), sourcePropertyValue);
327 }
328
329 if (!sourceTargetPropertyValuesSame) {
330 return (T) getExternalizableBusinessObject(clazz, fieldValuesInEBO);
331 }
332 return currentInstanceExternalizableBO;
333 }
334
335
336
337
338
339
340
341
342 @Override
343 public List<? extends ExternalizableBusinessObject> retrieveExternalizableBusinessObjectsList(
344 BusinessObject businessObject, String externalizableRelationshipName, Class externalizableClazz) {
345
346 if (businessObject == null) {
347 return null;
348 }
349
350
351 String className = businessObject.getClass().getName();
352 String key = className.substring(className.lastIndexOf(".") + 1);
353 BusinessObjectEntry entry =
354 KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getBusinessObjectEntries().get(
355 key);
356 RelationshipDefinition relationshipDefinition = entry.getRelationshipDefinition(externalizableRelationshipName);
357 List<PrimitiveAttributeDefinition> primitiveAttributeDefinitions =
358 relationshipDefinition.getPrimitiveAttributes();
359 Map<String, Object> fieldValuesInEBO = new HashMap<String, Object>();
360 Object sourcePropertyValue;
361 for (PrimitiveAttributeDefinition primitiveAttributeDefinition : primitiveAttributeDefinitions) {
362 sourcePropertyValue = ObjectUtils.getPropertyValue(businessObject,
363 primitiveAttributeDefinition.getSourceName());
364 if (sourcePropertyValue == null) {
365 return null;
366 }
367 fieldValuesInEBO.put(primitiveAttributeDefinition.getTargetName(), sourcePropertyValue);
368 }
369 return getExternalizableBusinessObjectsList(getExternalizableBusinessObjectImplementation(externalizableClazz),
370 fieldValuesInEBO);
371 }
372
373
374
375
376 @Override
377 public <E extends ExternalizableBusinessObject> Class<E> getExternalizableBusinessObjectImplementation(
378 Class<E> externalizableBusinessObjectInterface) {
379 if (getModuleConfiguration() == null) {
380 throw new IllegalStateException("Module configuration has not been initialized for the module service.");
381 }
382 Map<Class, Class> ebos = getModuleConfiguration().getExternalizableBusinessObjectImplementations();
383 if (ebos == null) {
384 return null;
385 }
386 if (ebos.containsValue(externalizableBusinessObjectInterface)) {
387 return externalizableBusinessObjectInterface;
388 } else {
389 Class<E> implementationClass = ebos.get(externalizableBusinessObjectInterface);
390 int implClassModifiers = implementationClass.getModifiers();
391 if (Modifier.isInterface(implClassModifiers) || Modifier.isAbstract(implClassModifiers)) {
392 throw new RuntimeException("Implementation class must be non-abstract class: ebo interface: "
393 + externalizableBusinessObjectInterface.getName()
394 + " impl class: "
395 + implementationClass.getName()
396 + " module: "
397 + getModuleConfiguration().getNamespaceCode());
398 }
399 return implementationClass;
400 }
401
402 }
403
404 @Deprecated
405 protected Properties getUrlParameters(String businessObjectClassAttribute, Map<String, String[]> parameters) {
406 Properties urlParameters = new Properties();
407 for (String paramName : parameters.keySet()) {
408 String[] parameterValues = parameters.get(paramName);
409 if (parameterValues.length > 0) {
410 urlParameters.put(paramName, parameterValues[0]);
411 }
412 }
413 urlParameters.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, businessObjectClassAttribute);
414 urlParameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.CONTINUE_WITH_INQUIRY_METHOD_TO_CALL);
415 return urlParameters;
416 }
417
418 @Deprecated
419 protected String getInquiryUrl(Class inquiryBusinessObjectClass) {
420 String riceBaseUrl = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
421 KRADConstants.KUALI_RICE_URL_KEY);
422 String inquiryUrl = riceBaseUrl;
423 if (!inquiryUrl.endsWith("/")) {
424 inquiryUrl = inquiryUrl + "/";
425 }
426 return inquiryUrl + KRADConstants.INQUIRY_ACTION;
427 }
428
429
430
431
432 public void afterPropertiesSet() throws Exception {
433 KualiModuleService kualiModuleService = null;
434 try {
435 kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
436 if (kualiModuleService == null) {
437 kualiModuleService = ((KualiModuleService) applicationContext.getBean(
438 KRADServiceLocatorWeb.KUALI_MODULE_SERVICE));
439 }
440 } catch (NoSuchBeanDefinitionException ex) {
441 kualiModuleService = ((KualiModuleService) applicationContext.getBean(
442 KRADServiceLocatorWeb.KUALI_MODULE_SERVICE));
443 }
444 kualiModuleService.getInstalledModuleServices().add(this);
445 }
446
447
448
449
450 public ModuleConfiguration getModuleConfiguration() {
451 return this.moduleConfiguration;
452 }
453
454
455
456
457 public void setModuleConfiguration(ModuleConfiguration moduleConfiguration) {
458 this.moduleConfiguration = moduleConfiguration;
459 }
460
461
462
463
464 @Override
465 public boolean isExternalizable(Class boClazz) {
466 if (boClazz == null) {
467 return false;
468 }
469 return ExternalizableBusinessObject.class.isAssignableFrom(boClazz);
470 }
471
472 public <T extends ExternalizableBusinessObject> T createNewObjectFromExternalizableClass(Class<T> boClass) {
473 try {
474 return (T) getExternalizableBusinessObjectImplementation(boClass).newInstance();
475 } catch (Exception e) {
476 throw new RuntimeException("Unable to create externalizable business object class", e);
477 }
478 }
479
480 public DataObjectRelationship getBusinessObjectRelationship(Class boClass, String attributeName,
481 String attributePrefix) {
482 return null;
483 }
484
485
486
487
488
489 public KualiModuleService getKualiModuleService() {
490 return this.kualiModuleService;
491 }
492
493
494
495
496 public void setKualiModuleService(KualiModuleService kualiModuleService) {
497 this.kualiModuleService = kualiModuleService;
498 }
499
500 protected ConfigurationService getKualiConfigurationService() {
501 if (this.kualiConfigurationService == null) {
502 this.kualiConfigurationService = KRADServiceLocator.getKualiConfigurationService();
503 }
504
505 return this.kualiConfigurationService;
506 }
507
508 public void setKualiConfigurationService(ConfigurationService kualiConfigurationService) {
509 this.kualiConfigurationService = kualiConfigurationService;
510 }
511
512
513
514
515 @Override
516 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
517 this.applicationContext = applicationContext;
518 }
519
520
521
522
523
524
525 @Override
526 public List<List<String>> listAlternatePrimaryKeyFieldNames(Class businessObjectInterfaceClass) {
527 return null;
528 }
529
530
531
532
533
534
535 @Override
536 public boolean isLocked() {
537 ModuleConfiguration configuration = this.getModuleConfiguration();
538 if (configuration != null) {
539 String namespaceCode = configuration.getNamespaceCode();
540 String componentCode = KRADConstants.DetailTypes.ALL_DETAIL_TYPE;
541 String parameterName = KRADConstants.SystemGroupParameterNames.OLTP_LOCKOUT_ACTIVE_IND;
542 ParameterService parameterService = CoreFrameworkServiceLocator.getParameterService();
543 String shouldLockout = parameterService.getParameterValueAsString(namespaceCode, componentCode,
544 parameterName);
545 if (StringUtils.isNotBlank(shouldLockout)) {
546 return parameterService.getParameterValueAsBoolean(namespaceCode, componentCode, parameterName);
547 }
548 }
549 return false;
550 }
551
552
553
554
555
556
557 protected LookupService getLookupService() {
558 return lookupService != null ? lookupService : KRADServiceLocatorWeb.getLookupService();
559 }
560
561 @Override
562 public boolean goToCentralRiceForInquiry() {
563 return false;
564 }
565 }