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