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