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
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 if (fieldControl != null && fieldControl.isFile()) {
184 if (Field.FILE.equals(field.getFieldType())) {
185 Object fileName = ObjectUtils.getNestedValue(bo, KRADConstants.BO_ATTACHMENT_FILE_NAME);
186 Object fileType = ObjectUtils.getNestedValue(bo, KRADConstants.BO_ATTACHMENT_FILE_CONTENT_TYPE);
187 field.setImageSrc(WebUtils.getAttachmentImageForUrl((String) fileType));
188 field.setPropertyValue(fileName);
189 }
190 }
191 FieldUtils.setInquiryURL(field, bo, propertyName);
192 } catch (InstantiationException e) {
193 LOG.error("Unable to get instance of KeyValuesFinder: " + e.getMessage());
194 throw new RuntimeException("Unable to get instance of KeyValuesFinder: " + e.getMessage());
195 } catch (ClassNotFoundException e) {
196 LOG.error("Unable to get instance of KeyValuesFinder: " + e.getMessage());
197 throw new RuntimeException("Unable to get instance of KeyValuesFinder: " + e.getMessage());
198 } catch (IllegalAccessException e) {
199 LOG.error("Unable to set columns: " + e.getMessage());
200 throw new RuntimeException("Unable to set columns: " + e.getMessage());
201 }
202
203 }
204
205
206
207
208
209
210
211
212 private static String lookupFinderValue(ControlDefinition fieldControl, Object prop, KeyValuesFinder finder) {
213 String propValue = null;
214
215
216 if (finder instanceof PersistableBusinessObjectValuesFinder) {
217 ((PersistableBusinessObjectValuesFinder) finder).setBusinessObjectClass(ClassLoaderUtils.getClass(fieldControl.getBusinessObjectClass()));
218 ((PersistableBusinessObjectValuesFinder) finder).setKeyAttributeName(fieldControl.getKeyAttribute());
219 ((PersistableBusinessObjectValuesFinder) finder).setLabelAttributeName(fieldControl.getLabelAttribute());
220 if (fieldControl.getIncludeBlankRow() != null) {
221 ((PersistableBusinessObjectValuesFinder) finder).setIncludeBlankRow(fieldControl.getIncludeBlankRow());
222 }
223 ((PersistableBusinessObjectValuesFinder) finder).setIncludeKeyInDescription(fieldControl.getIncludeKeyInLabel());
224 }
225 List<KeyValue> keyValues = finder.getKeyValues();
226 propValue = getPropertyValueFromList(prop, keyValues);
227 if(propValue==null) {
228 propValue = lookupInactiveFinderValue(prop, finder);
229 }
230 return propValue;
231 }
232
233 private static String lookupInactiveFinderValue(Object property, KeyValuesFinder finder){
234 List<KeyValue> keyValues = finder.getKeyValues(false);
235 return getPropertyValueFromList(property, keyValues);
236
237 }
238
239 private static String getPropertyValueFromList(Object property, List<KeyValue> keyValues){
240 String propertyValue = null;
241 if (property != null) {
242 for (Object element2 : keyValues) {
243 KeyValue element = (KeyValue) element2;
244 if (element.getKey().toString().equals(property.toString())) {
245 propertyValue = element.getValue();
246 break;
247 }
248 }
249 }
250 return propertyValue;
251 }
252
253
254
255
256
257
258
259
260
261
262
263
264
265 protected static boolean isMaintenanceFieldLevelHelpEnabled(Maintainable m, MaintainableFieldDefinition fieldDefinition) {
266 if ( fieldDefinition != null ) {
267 if ( fieldDefinition.isShowFieldLevelHelp() != null && fieldDefinition.isShowFieldLevelHelp() ) {
268 return true;
269 }
270 }
271 return false;
272 }
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287 protected static boolean isMaintenanceFieldLevelHelpDisabled(Maintainable m, MaintainableFieldDefinition fieldDefinition) {
288 if ( fieldDefinition != null ) {
289 if ( fieldDefinition.isShowFieldLevelHelp() != null && !fieldDefinition.isShowFieldLevelHelp() ) {
290 return true;
291 }
292 }
293 return false;
294 }
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313 public static final Field toField(MaintainableItemDefinition id, MaintainableSectionDefinition sd, BusinessObject o, Maintainable m, Section s, List<String> displayedFieldNames, Set<String> conditionallyRequiredMaintenanceFields) throws InstantiationException, IllegalAccessException {
314 Field field = new Field();
315
316
317 if (id instanceof MaintainableFieldDefinition) {
318 MaintainableFieldDefinition maintainableFieldDefinition = (MaintainableFieldDefinition) id;
319 field = FieldUtils.getPropertyField(o.getClass(), maintainableFieldDefinition.getName(), false);
320
321 boolean translateCodes = getMaintenanceDocumentDictionaryService().translateCodes(o.getClass());
322 if (translateCodes) {
323 FieldUtils.setAdditionalDisplayPropertyForCodes(o.getClass(), field.getPropertyName(), field);
324 }
325
326 setupField(field, maintainableFieldDefinition, conditionallyRequiredMaintenanceFields);
327
328 MaintenanceUtils.setFieldQuickfinder(o, field.getPropertyName(), maintainableFieldDefinition, field, displayedFieldNames, m);
329 MaintenanceUtils.setFieldDirectInquiry(o, field.getPropertyName(), maintainableFieldDefinition, field, displayedFieldNames);
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
361
362
363
364 field.setFieldLevelHelpEnabled(isMaintenanceFieldLevelHelpEnabled(m, maintainableFieldDefinition));
365 field.setFieldLevelHelpDisabled(isMaintenanceFieldLevelHelpDisabled(m, maintainableFieldDefinition));
366 field.setFieldLevelHelpUrl(maintainableFieldDefinition.getFieldLevelHelpUrl());
367 }
368
369 return field;
370
371 }
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389 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) {
390 LOG.debug( "getNewFormFields" );
391 String collName = collectionDefinition.getName();
392
393 List<Field> collFields = new ArrayList<Field>();
394 Collection<? extends FieldDefinitionI> collectionFields;
395
396 BusinessObject collBO = null;
397 try {
398 collectionFields = collectionDefinition.getFields();
399 collBO = m.getNewCollectionLine(parents + collName);
400
401 if ( LOG.isDebugEnabled() ) {
402 LOG.debug( "newBO for add line: " + collBO );
403 }
404
405 for ( FieldDefinitionI fieldDefinition : collectionFields ) {
406
407 Field collField = FieldUtils.getPropertyField(collectionDefinition.getBusinessObjectClass(), fieldDefinition.getName(), false);
408
409 if (fieldDefinition instanceof MaintainableFieldDefinition) {
410 setupField(collField, fieldDefinition, conditionallyRequiredMaintenanceFields);
411 }
412
413 String[] nameParts = StringUtils.split(collField.getPropertyName(), ".");
414 String fieldErrorKey = KRADConstants.MAINTENANCE_NEW_MAINTAINABLE + KRADConstants.ADD_PREFIX + ".";
415 fieldErrorKey += collName + ".";
416 for (int i = 0; i < nameParts.length; i++) {
417 fieldErrorKey += nameParts[i];
418 containerRowErrorKey.append(fieldErrorKey);
419 if (i < nameParts.length) {
420 fieldErrorKey += ".";
421 containerRowErrorKey.append(",");
422 }
423 }
424
425
426 BusinessObject collectionBoInstance = collectionDefinition.getBusinessObjectClass().newInstance();
427 FieldUtils.setInquiryURL(collField, collectionBoInstance, fieldDefinition.getName());
428 if (collectionDefinition instanceof MaintainableCollectionDefinition) {
429 MaintenanceUtils.setFieldQuickfinder(collectionBoInstance, parents+collectionDefinition.getName(), true, 0, fieldDefinition.getName(), collField, displayedFieldNames, m, (MaintainableFieldDefinition) fieldDefinition);
430 MaintenanceUtils
431 .setFieldDirectInquiry(collectionBoInstance, parents + collectionDefinition.getName(), true,
432 0, fieldDefinition.getName(), collField, displayedFieldNames, m,
433 (MaintainableFieldDefinition) fieldDefinition);
434 }
435 else {
436 LookupUtils.setFieldQuickfinder(collectionBoInstance, parents+collectionDefinition.getName(), true, 0, fieldDefinition.getName(), collField, displayedFieldNames, m);
437 LookupUtils.setFieldDirectInquiry(collectionBoInstance, fieldDefinition.getName(), collField);
438 }
439
440 collFields.add(collField);
441 }
442
443 } catch (InstantiationException e) {
444 LOG.error("Unable to create instance of object class" + e.getMessage());
445 throw new RuntimeException("Unable to create instance of object class" + e.getMessage());
446 } catch (IllegalAccessException e) {
447 LOG.error("Unable to create instance of object class" + e.getMessage());
448 throw new RuntimeException("Unable to create instance of object class" + e.getMessage());
449 }
450
451
452 collFields = FieldUtils.populateFieldsFromBusinessObject(collFields,collBO);
453
454
455
456 for ( Field field : collFields ) {
457
458 field.setPropertyName(KRADConstants.MAINTENANCE_ADD_PREFIX + parents + collectionDefinition.getName() + "." + field.getPropertyName());
459 }
460 LOG.debug("Error Key for section " + collectionDefinition.getName() + " : " + containerRowErrorKey.toString());
461
462
463 collFields = constructContainerField(collectionDefinition, parents, o, hideAdd, numberOfColumns, collName, collFields);
464
465 return collFields;
466 }
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481 public static List<Field> constructContainerField(CollectionDefinitionI collectionDefinition, String parents, BusinessObject o, boolean hideAdd, int numberOfColumns, String collName, List<Field> collFields) {
482
483 String collectionLabel = getDataDictionaryService().getCollectionLabel(o.getClass(), collectionDefinition.getName());
484
485
486 String collectionElementLabel = collectionDefinition.getSummaryTitle();
487 if(StringUtils.isEmpty(collectionElementLabel)){
488 collectionElementLabel = getDataDictionaryService().getCollectionElementLabel(o.getClass().getName(), collectionDefinition.getName(),collectionDefinition.getBusinessObjectClass());
489 }
490
491
492 Field containerField;
493 containerField = FieldUtils.constructContainerField(collName, collectionLabel, collFields, numberOfColumns);
494 if(StringUtils.isNotEmpty(collectionElementLabel)) {
495 containerField.setContainerElementName(collectionElementLabel);
496 }
497 collFields = new ArrayList();
498 collFields.add(containerField);
499
500
501 if(!hideAdd && collectionDefinition.getIncludeAddLine()) {
502 Field field = new Field();
503
504 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;
505 field.setPropertyName(addButtonName);
506 field.setFieldType(Field.IMAGE_SUBMIT);
507 field.setPropertyValue("images/tinybutton-add1.gif");
508
509 containerField.getContainerRows().add(new Row(field));
510 }
511
512 if (collectionDefinition instanceof MaintainableCollectionDefinition) {
513 if (FieldUtils.isCollectionMultipleLookupEnabled((MaintainableCollectionDefinition) collectionDefinition)) {
514 FieldUtils.modifyFieldToSupportMultipleValueLookups(containerField, parents, (MaintainableCollectionDefinition) collectionDefinition);
515 }
516 }
517
518 return collFields;
519 }
520
521
522
523
524
525
526 public static final List<Field> getNewFormFields(MaintainableCollectionDefinition collectionDefinition, BusinessObject o, Maintainable m, List<String> displayedFieldNames, Set<String> conditionallyRequiredMaintenanceFields, StringBuffer containerRowErrorKey, int numberOfColumns) {
527 String parent = "";
528 return getNewFormFields(collectionDefinition, o, m, displayedFieldNames, conditionallyRequiredMaintenanceFields, containerRowErrorKey, parent, false, numberOfColumns);
529 }
530
531
532
533
534
535
536
537
538
539
540 public static Field toField(FieldDefinition d, BusinessObject o, Section s) {
541 Field field = FieldUtils.getPropertyField(o.getClass(), d.getAttributeName(), false);
542
543 FieldUtils.setInquiryURL(field, o, field.getPropertyName());
544
545 String alternateDisplayPropertyName = getBusinessObjectDictionaryService()
546 .getInquiryFieldAlternateDisplayAttributeName(o.getClass(), d.getAttributeName());
547 if (StringUtils.isNotBlank(alternateDisplayPropertyName)) {
548 field.setAlternateDisplayPropertyName(alternateDisplayPropertyName);
549 }
550
551 String additionalDisplayPropertyName = getBusinessObjectDictionaryService()
552 .getInquiryFieldAdditionalDisplayAttributeName(o.getClass(), d.getAttributeName());
553 if (StringUtils.isNotBlank(additionalDisplayPropertyName)) {
554 field.setAdditionalDisplayPropertyName(additionalDisplayPropertyName);
555 }
556 else {
557 boolean translateCodes = getBusinessObjectDictionaryService().tranlateCodesInInquiry(o.getClass());
558 if (translateCodes) {
559 FieldUtils.setAdditionalDisplayPropertyForCodes(o.getClass(), d.getAttributeName(), field);
560 }
561 }
562
563 populateFieldFromBusinessObject(field, o);
564
565 return field;
566 }
567
568 public static DataDictionaryService getDataDictionaryService() {
569 if (dataDictionaryService == null) {
570 dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService();
571 }
572 return dataDictionaryService;
573 }
574
575 public static PersistenceStructureService getPersistenceStructureService() {
576 if (persistenceStructureService == null) {
577 persistenceStructureService = KRADServiceLocator.getPersistenceStructureService();
578 }
579 return persistenceStructureService;
580 }
581
582 public static BusinessObjectDictionaryService getBusinessObjectDictionaryService() {
583 if (businessObjectDictionaryService == null) {
584 businessObjectDictionaryService = KNSServiceLocator.getBusinessObjectDictionaryService();
585 }
586 return businessObjectDictionaryService;
587 }
588
589 public static MaintenanceDocumentDictionaryService getMaintenanceDocumentDictionaryService() {
590 if (maintenanceDocumentDictionaryService == null) {
591 maintenanceDocumentDictionaryService = KNSServiceLocator.getMaintenanceDocumentDictionaryService();
592 }
593 return maintenanceDocumentDictionaryService;
594 }
595
596 }
597