1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.web.ui;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Set;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.kuali.rice.core.util.ClassLoaderUtils;
26 import org.kuali.rice.core.util.KeyLabelPair;
27 import org.kuali.rice.kns.bo.BusinessObject;
28 import org.kuali.rice.kns.datadictionary.CollectionDefinitionI;
29 import org.kuali.rice.kns.datadictionary.FieldDefinition;
30 import org.kuali.rice.kns.datadictionary.FieldDefinitionI;
31 import org.kuali.rice.kns.datadictionary.MaintainableCollectionDefinition;
32 import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition;
33 import org.kuali.rice.kns.datadictionary.MaintainableItemDefinition;
34 import org.kuali.rice.kns.datadictionary.MaintainableSectionDefinition;
35 import org.kuali.rice.kns.datadictionary.control.ApcSelectControlDefinition;
36 import org.kuali.rice.kns.datadictionary.control.ControlDefinition;
37 import org.kuali.rice.kns.lookup.LookupUtils;
38 import org.kuali.rice.kns.lookup.keyvalues.ApcValuesFinder;
39 import org.kuali.rice.kns.lookup.keyvalues.KeyValuesFinder;
40 import org.kuali.rice.kns.lookup.keyvalues.PersistableBusinessObjectValuesFinder;
41 import org.kuali.rice.kns.maintenance.Maintainable;
42 import org.kuali.rice.kns.service.BusinessObjectDictionaryService;
43 import org.kuali.rice.kns.service.DataDictionaryService;
44 import org.kuali.rice.kns.service.KNSServiceLocator;
45 import org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService;
46 import org.kuali.rice.kns.service.PersistenceStructureService;
47 import org.kuali.rice.kns.util.FieldUtils;
48 import org.kuali.rice.kns.util.KNSConstants;
49 import org.kuali.rice.kns.util.MaintenanceUtils;
50 import org.kuali.rice.kns.util.ObjectUtils;
51 import org.kuali.rice.kns.web.format.Formatter;
52
53 public class FieldBridge {
54 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FieldBridge.class);
55 private static DataDictionaryService dataDictionaryService;
56 private static PersistenceStructureService persistenceStructureService;
57 private static BusinessObjectDictionaryService businessObjectDictionaryService;
58 private static MaintenanceDocumentDictionaryService maintenanceDocumentDictionaryService;
59
60
61
62
63
64
65
66 public static final void setupField(Field field, FieldDefinitionI definition, Set<String> conditionallyRequiredMaintenanceFields) {
67 if (definition instanceof MaintainableFieldDefinition) {
68 MaintainableFieldDefinition maintainableFieldDefinition = ((MaintainableFieldDefinition) definition);
69
70 field.setFieldRequired(maintainableFieldDefinition.isRequired());
71 field.setReadOnly(maintainableFieldDefinition.isUnconditionallyReadOnly());
72 if (maintainableFieldDefinition.isLookupReadOnly()) {
73 field.setFieldType(Field.LOOKUP_READONLY);
74 }
75
76
77 if (StringUtils.isNotBlank(maintainableFieldDefinition.getWebUILeaveFieldFunction())) {
78 field.setWebOnBlurHandler(maintainableFieldDefinition.getWebUILeaveFieldFunction());
79 }
80
81 if (StringUtils.isNotBlank(maintainableFieldDefinition.getWebUILeaveFieldCallbackFunction())) {
82 field.setWebOnBlurHandlerCallback(maintainableFieldDefinition.getWebUILeaveFieldCallbackFunction());
83 }
84
85 if (maintainableFieldDefinition.getWebUILeaveFieldFunctionParameters()!=null) {
86 field.setWebUILeaveFieldFunctionParameters(maintainableFieldDefinition.getWebUILeaveFieldFunctionParameters());
87 }
88
89 if (StringUtils.isNotBlank(maintainableFieldDefinition.getAlternateDisplayAttributeName())) {
90 field.setAlternateDisplayPropertyName(maintainableFieldDefinition.getAlternateDisplayAttributeName());
91 }
92
93 if (StringUtils.isNotBlank(maintainableFieldDefinition.getAdditionalDisplayAttributeName())) {
94 field.setAdditionalDisplayPropertyName(maintainableFieldDefinition.getAdditionalDisplayAttributeName());
95 }
96
97 if (conditionallyRequiredMaintenanceFields != null && conditionallyRequiredMaintenanceFields.contains(field.getPropertyName())) {
98 field.setFieldRequired(true);
99 }
100
101 if (((MaintainableFieldDefinition) definition).isTriggerOnChange()) {
102 field.setTriggerOnChange(true);
103 }
104 }
105 }
106
107
108
109
110
111
112
113
114
115
116 public static final void populateFieldFromBusinessObject(Field field, BusinessObject bo) {
117 if (bo == null) {
118 throw new RuntimeException("Inquiry Business object is null.");
119 }
120
121 field.setReadOnly(true);
122
123 Formatter formatter = field.getFormatter();
124 String propertyName = field.getPropertyName();
125
126
127 ControlDefinition fieldControl = getDataDictionaryService().getAttributeControlDefinition(bo.getClass(),
128 propertyName);
129 try {
130 Object prop = ObjectUtils.getPropertyValue(bo, field.getPropertyName());
131
132
133
134 String propValue = KNSConstants.EMPTY_STRING;
135 if (fieldControl != null && fieldControl.isSelect()
136 && StringUtils.isBlank(field.getAdditionalDisplayPropertyName())
137 && StringUtils.isBlank(field.getAlternateDisplayPropertyName())) {
138 Class<KeyValuesFinder> keyValuesFinderName = ClassLoaderUtils.getClass(fieldControl
139 .getValuesFinderClass());
140 KeyValuesFinder finder = keyValuesFinderName.newInstance();
141
142 propValue = lookupFinderValue(fieldControl, prop, finder);
143 } else {
144 propValue = ObjectUtils.getFormattedPropertyValue(bo, field.getPropertyName(), formatter);
145 }
146 field.setPropertyValue(propValue);
147
148
149
150 if (StringUtils.isNotBlank(field.getAlternateDisplayPropertyName())) {
151 String alternatePropertyValue = ObjectUtils.getFormattedPropertyValueUsingDataDictionary(bo, field
152 .getAlternateDisplayPropertyName());
153 field.setAlternateDisplayPropertyValue(alternatePropertyValue);
154 }
155
156 if (StringUtils.isNotBlank(field.getAdditionalDisplayPropertyName())) {
157 String additionalPropertyValue = ObjectUtils.getFormattedPropertyValueUsingDataDictionary(bo, field
158 .getAdditionalDisplayPropertyName());
159 field.setAdditionalDisplayPropertyValue(additionalPropertyValue);
160 }
161
162
163
164 if ( fieldControl != null && fieldControl.isKualiUser() ) {
165
166 try {
167 if ( StringUtils.isNotBlank(field.getUniversalIdAttributeName()) ) {
168 Object principalId = ObjectUtils.getNestedValue(bo, field.getUniversalIdAttributeName());
169 if ( principalId != null ) {
170 field.setUniversalIdValue(principalId.toString());
171 }
172 }
173 if ( StringUtils.isNotBlank(field.getPersonNameAttributeName()) ) {
174 Object personName = ObjectUtils.getNestedValue(bo, field.getPersonNameAttributeName());
175 if ( personName != null ) {
176 field.setPersonNameValue( personName.toString() );
177 }
178 }
179 } catch ( Exception ex ) {
180 LOG.warn( "Unable to get principal ID or person name property in FieldBridge.", ex );
181 }
182 }
183 FieldUtils.setInquiryURL(field, bo, propertyName);
184 } catch (InstantiationException e) {
185 LOG.error("Unable to get instance of KeyValuesFinder: " + e.getMessage());
186 throw new RuntimeException("Unable to get instance of KeyValuesFinder: " + e.getMessage());
187 } catch (IllegalAccessException e) {
188 LOG.error("Unable to set columns: " + e.getMessage());
189 throw new RuntimeException("Unable to set columns: " + e.getMessage());
190 }
191
192 }
193
194
195
196
197
198
199
200
201 private static String lookupFinderValue(ControlDefinition fieldControl, Object prop, KeyValuesFinder finder) {
202 String propValue = null;
203
204 if (finder != null) {
205 if (finder instanceof ApcValuesFinder && fieldControl instanceof ApcSelectControlDefinition) {
206 ((ApcValuesFinder) finder).setParameterNamespace(((ApcSelectControlDefinition) fieldControl).getParameterNamespace());
207 ((ApcValuesFinder) finder).setParameterDetailType(((ApcSelectControlDefinition) fieldControl).getParameterDetailType());
208 ((ApcValuesFinder) finder).setParameterName(((ApcSelectControlDefinition) fieldControl).getParameterName());
209 }
210 }
211
212
213 if (finder instanceof PersistableBusinessObjectValuesFinder) {
214 ((PersistableBusinessObjectValuesFinder) finder).setBusinessObjectClass(ClassLoaderUtils.getClass(fieldControl.getBusinessObjectClass()));
215 ((PersistableBusinessObjectValuesFinder) finder).setKeyAttributeName(fieldControl.getKeyAttribute());
216 ((PersistableBusinessObjectValuesFinder) finder).setLabelAttributeName(fieldControl.getLabelAttribute());
217 if (fieldControl.getIncludeBlankRow() != null) {
218 ((PersistableBusinessObjectValuesFinder) finder).setIncludeBlankRow(fieldControl.getIncludeBlankRow());
219 }
220 ((PersistableBusinessObjectValuesFinder) finder).setIncludeKeyInDescription(fieldControl.getIncludeKeyInLabel());
221 }
222 List keyValues = finder.getKeyValues();
223 propValue = getPropertyValueFromList(prop, keyValues);
224 if(propValue==null)
225 propValue = lookupInactiveFinderValue(prop, finder);
226 return propValue;
227 }
228
229 private static String lookupInactiveFinderValue(Object property, KeyValuesFinder finder){
230 List keyValues = finder.getKeyValues(false);
231 return getPropertyValueFromList(property, keyValues);
232
233 }
234
235 private static String getPropertyValueFromList(Object property, List keyValues){
236 String propertyValue = null;
237 if (property != null) {
238 for (Iterator iter = keyValues.iterator(); iter.hasNext();) {
239 KeyLabelPair element = (KeyLabelPair) iter.next();
240 if (element.getKey().toString().equals(property.toString())) {
241 propertyValue = element.getLabel();
242 break;
243 }
244 }
245 }
246 return propertyValue;
247 }
248
249
250
251
252
253
254
255
256
257
258
259
260
261 protected static boolean isMaintenanceFieldLevelHelpEnabled(Maintainable m, MaintainableFieldDefinition fieldDefinition) {
262 if ( fieldDefinition != null ) {
263 if ( fieldDefinition.isShowFieldLevelHelp() != null && fieldDefinition.isShowFieldLevelHelp() ) {
264 return true;
265 }
266 }
267 return false;
268 }
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 protected static boolean isMaintenanceFieldLevelHelpDisabled(Maintainable m, MaintainableFieldDefinition fieldDefinition) {
284 if ( fieldDefinition != null ) {
285 if ( fieldDefinition.isShowFieldLevelHelp() != null && !fieldDefinition.isShowFieldLevelHelp() ) {
286 return true;
287 }
288 }
289 return false;
290 }
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309 public static final Field toField(MaintainableItemDefinition id, MaintainableSectionDefinition sd, BusinessObject o, Maintainable m, Section s, List<String> displayedFieldNames, Set<String> conditionallyRequiredMaintenanceFields) throws InstantiationException, IllegalAccessException {
310 Field field = new Field();
311
312
313 if (id instanceof MaintainableFieldDefinition) {
314 MaintainableFieldDefinition maintainableFieldDefinition = (MaintainableFieldDefinition) id;
315 field = FieldUtils.getPropertyField(o.getClass(), maintainableFieldDefinition.getName(), false);
316
317 boolean translateCodes = getMaintenanceDocumentDictionaryService().translateCodes(o.getClass());
318 if (translateCodes) {
319 FieldUtils.setAdditionalDisplayPropertyForCodes(o.getClass(), field.getPropertyName(), field);
320 }
321
322 setupField(field, maintainableFieldDefinition, conditionallyRequiredMaintenanceFields);
323
324 MaintenanceUtils.setFieldQuickfinder(o, field.getPropertyName(), maintainableFieldDefinition, field, displayedFieldNames, m);
325 MaintenanceUtils.setFieldDirectInquiry(o, field.getPropertyName(), maintainableFieldDefinition, field, displayedFieldNames);
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360 field.setFieldLevelHelpEnabled(isMaintenanceFieldLevelHelpEnabled(m, maintainableFieldDefinition));
361 field.setFieldLevelHelpDisabled(isMaintenanceFieldLevelHelpDisabled(m, maintainableFieldDefinition));
362 field.setFieldLevelHelpUrl(maintainableFieldDefinition.getFieldLevelHelpUrl());
363 }
364
365 return field;
366
367 }
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385 public static final List<Field> getNewFormFields(CollectionDefinitionI collectionDefinition, BusinessObject o, Maintainable m, List<String> displayedFieldNames, Set<String> conditionallyRequiredMaintenanceFields, StringBuffer containerRowErrorKey, String parents, boolean hideAdd, int numberOfColumns) {
386 LOG.debug( "getNewFormFields" );
387 String collName = collectionDefinition.getName();
388
389 List<Field> collFields = new ArrayList<Field>();
390 Collection<? extends FieldDefinitionI> collectionFields;
391
392 BusinessObject collBO = null;
393 try {
394 collectionFields = collectionDefinition.getFields();
395 collBO = m.getNewCollectionLine(parents + collName);
396
397 if ( LOG.isDebugEnabled() ) {
398 LOG.debug( "newBO for add line: " + collBO );
399 }
400
401 for ( FieldDefinitionI fieldDefinition : collectionFields ) {
402
403 Field collField = FieldUtils.getPropertyField(collectionDefinition.getBusinessObjectClass(), fieldDefinition.getName(), false);
404
405 if (fieldDefinition instanceof MaintainableFieldDefinition) {
406 setupField(collField, (MaintainableFieldDefinition)fieldDefinition, conditionallyRequiredMaintenanceFields);
407 }
408
409 String[] nameParts = StringUtils.split(collField.getPropertyName(), ".");
410 String fieldErrorKey = KNSConstants.MAINTENANCE_NEW_MAINTAINABLE + KNSConstants.ADD_PREFIX + ".";
411 fieldErrorKey += collName + ".";
412 for (int i = 0; i < nameParts.length; i++) {
413 fieldErrorKey += nameParts[i];
414 containerRowErrorKey.append(fieldErrorKey);
415 if (i < nameParts.length) {
416 fieldErrorKey += ".";
417 containerRowErrorKey.append(",");
418 }
419 }
420
421
422 BusinessObject collectionBoInstance = (BusinessObject) collectionDefinition.getBusinessObjectClass().newInstance();
423 FieldUtils.setInquiryURL(collField, collectionBoInstance, fieldDefinition.getName());
424 if (collectionDefinition instanceof MaintainableCollectionDefinition) {
425 MaintenanceUtils.setFieldQuickfinder(collectionBoInstance, parents+collectionDefinition.getName(), true, 0, fieldDefinition.getName(), collField, displayedFieldNames, m, (MaintainableFieldDefinition) fieldDefinition);
426 MaintenanceUtils.setFieldDirectInquiry(collectionBoInstance, parents+collectionDefinition.getName(), true, 0, fieldDefinition.getName(), collField, displayedFieldNames, m, (MaintainableFieldDefinition) fieldDefinition);
427 }
428 else {
429 LookupUtils.setFieldQuickfinder(collectionBoInstance, parents+collectionDefinition.getName(), true, 0, fieldDefinition.getName(), collField, displayedFieldNames, m);
430 LookupUtils.setFieldDirectInquiry(collectionBoInstance, fieldDefinition.getName(), collField);
431 }
432
433 collFields.add(collField);
434 }
435
436 } catch (InstantiationException e) {
437 LOG.error("Unable to create instance of object class" + e.getMessage());
438 throw new RuntimeException("Unable to create instance of object class" + e.getMessage());
439 } catch (IllegalAccessException e) {
440 LOG.error("Unable to create instance of object class" + e.getMessage());
441 throw new RuntimeException("Unable to create instance of object class" + e.getMessage());
442 }
443
444
445 collFields = FieldUtils.populateFieldsFromBusinessObject(collFields,collBO);
446
447
448
449 for ( Field field : collFields ) {
450
451 field.setPropertyName(KNSConstants.MAINTENANCE_ADD_PREFIX + parents + collectionDefinition.getName() + "." + field.getPropertyName());
452 }
453 LOG.debug("Error Key for section " + collectionDefinition.getName() + " : " + containerRowErrorKey.toString());
454
455
456 collFields = constructContainerField(collectionDefinition, parents, o, hideAdd, numberOfColumns, collName, collFields);
457
458 return collFields;
459 }
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474 public static List<Field> constructContainerField(CollectionDefinitionI collectionDefinition, String parents, BusinessObject o, boolean hideAdd, int numberOfColumns, String collName, List<Field> collFields) {
475
476 String collectionLabel = getDataDictionaryService().getCollectionLabel(o.getClass(), collectionDefinition.getName());
477
478
479 String collectionElementLabel = collectionDefinition.getSummaryTitle();
480 if(StringUtils.isEmpty(collectionElementLabel)){
481 collectionElementLabel = getDataDictionaryService().getCollectionElementLabel(o.getClass().getName(), collectionDefinition.getName(),collectionDefinition.getBusinessObjectClass());
482 }
483
484
485 Field containerField;
486 containerField = FieldUtils.constructContainerField(collName, collectionLabel, collFields, numberOfColumns);
487 if(StringUtils.isNotEmpty(collectionElementLabel)) {
488 containerField.setContainerElementName(collectionElementLabel);
489 }
490 collFields = new ArrayList();
491 collFields.add(containerField);
492
493
494 if(!hideAdd && collectionDefinition.getIncludeAddLine()) {
495 Field field = new Field();
496
497 String addButtonName = KNSConstants.DISPATCH_REQUEST_PARAMETER + "." + KNSConstants.ADD_LINE_METHOD + "." + parents + collectionDefinition.getName() + "." + KNSConstants.METHOD_TO_CALL_BOPARM_LEFT_DEL + collectionDefinition.getBusinessObjectClass().getName() + KNSConstants.METHOD_TO_CALL_BOPARM_RIGHT_DEL;
498 field.setPropertyName(addButtonName);
499 field.setFieldType(Field.IMAGE_SUBMIT);
500 field.setPropertyValue("images/tinybutton-add1.gif");
501
502 containerField.getContainerRows().add(new Row(field));
503 }
504
505 if (collectionDefinition instanceof MaintainableCollectionDefinition) {
506 if (FieldUtils.isCollectionMultipleLookupEnabled((MaintainableCollectionDefinition) collectionDefinition)) {
507 FieldUtils.modifyFieldToSupportMultipleValueLookups(containerField, parents, (MaintainableCollectionDefinition) collectionDefinition);
508 }
509 }
510
511 return collFields;
512 }
513
514
515
516
517
518
519 public static final List<Field> getNewFormFields(MaintainableCollectionDefinition collectionDefinition, BusinessObject o, Maintainable m, List<String> displayedFieldNames, Set<String> conditionallyRequiredMaintenanceFields, StringBuffer containerRowErrorKey, int numberOfColumns) {
520 String parent = "";
521 return getNewFormFields(collectionDefinition, o, m, displayedFieldNames, conditionallyRequiredMaintenanceFields, containerRowErrorKey, parent, false, numberOfColumns);
522 }
523
524
525
526
527
528
529
530
531
532
533 public static Field toField(FieldDefinition d, BusinessObject o, Section s) {
534 Field field = FieldUtils.getPropertyField(o.getClass(), d.getAttributeName(), false);
535
536 FieldUtils.setInquiryURL(field, o, field.getPropertyName());
537
538 String alternateDisplayPropertyName = getBusinessObjectDictionaryService()
539 .getInquiryFieldAlternateDisplayAttributeName(o.getClass(), d.getAttributeName());
540 if (StringUtils.isNotBlank(alternateDisplayPropertyName)) {
541 field.setAlternateDisplayPropertyName(alternateDisplayPropertyName);
542 }
543
544 String additionalDisplayPropertyName = getBusinessObjectDictionaryService()
545 .getInquiryFieldAdditionalDisplayAttributeName(o.getClass(), d.getAttributeName());
546 if (StringUtils.isNotBlank(additionalDisplayPropertyName)) {
547 field.setAdditionalDisplayPropertyName(additionalDisplayPropertyName);
548 }
549 else {
550 boolean translateCodes = getBusinessObjectDictionaryService().tranlateCodesInInquiry(o.getClass());
551 if (translateCodes) {
552 FieldUtils.setAdditionalDisplayPropertyForCodes(o.getClass(), d.getAttributeName(), field);
553 }
554 }
555
556 populateFieldFromBusinessObject(field, o);
557
558 return field;
559 }
560
561 public static DataDictionaryService getDataDictionaryService() {
562 if (dataDictionaryService == null) {
563 dataDictionaryService = KNSServiceLocator.getDataDictionaryService();
564 }
565 return dataDictionaryService;
566 }
567
568 public static PersistenceStructureService getPersistenceStructureService() {
569 if (persistenceStructureService == null) {
570 persistenceStructureService = KNSServiceLocator.getPersistenceStructureService();
571 }
572 return persistenceStructureService;
573 }
574
575 public static BusinessObjectDictionaryService getBusinessObjectDictionaryService() {
576 if (businessObjectDictionaryService == null) {
577 businessObjectDictionaryService = KNSServiceLocator.getBusinessObjectDictionaryService();
578 }
579 return businessObjectDictionaryService;
580 }
581
582 public static MaintenanceDocumentDictionaryService getMaintenanceDocumentDictionaryService() {
583 if (maintenanceDocumentDictionaryService == null) {
584 maintenanceDocumentDictionaryService = KNSServiceLocator.getMaintenanceDocumentDictionaryService();
585 }
586 return maintenanceDocumentDictionaryService;
587 }
588
589 }
590