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