1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.field;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.util.ConcreteKeyValue;
20 import org.kuali.rice.core.api.util.KeyValue;
21 import org.kuali.rice.core.api.util.type.TypeUtils;
22 import org.kuali.rice.core.web.format.Formatter;
23 import org.kuali.rice.krad.bo.DataObjectRelationship;
24 import org.kuali.rice.krad.bo.KualiCode;
25 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
26 import org.kuali.rice.krad.datadictionary.AttributeSecurity;
27 import org.kuali.rice.krad.datadictionary.validation.constraint.CaseConstraint;
28 import org.kuali.rice.krad.datadictionary.validation.constraint.MustOccurConstraint;
29 import org.kuali.rice.krad.datadictionary.validation.constraint.PrerequisiteConstraint;
30 import org.kuali.rice.krad.datadictionary.validation.constraint.SimpleConstraint;
31 import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
32 import org.kuali.rice.krad.keyvalues.KeyValuesFinder;
33 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
34 import org.kuali.rice.krad.uif.UifConstants;
35 import org.kuali.rice.krad.uif.control.MultiValueControl;
36 import org.kuali.rice.krad.uif.control.UifKeyValuesFinder;
37 import org.kuali.rice.krad.uif.util.ComponentFactory;
38 import org.kuali.rice.krad.uif.view.FormView;
39 import org.kuali.rice.krad.uif.view.View;
40 import org.kuali.rice.krad.uif.control.Control;
41 import org.kuali.rice.krad.uif.control.MultiValueControlBase;
42 import org.kuali.rice.krad.uif.component.BindingInfo;
43 import org.kuali.rice.krad.uif.component.Component;
44 import org.kuali.rice.krad.uif.component.DataBinding;
45 import org.kuali.rice.krad.uif.util.ClientValidationUtils;
46 import org.kuali.rice.krad.uif.util.ComponentUtils;
47 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
48 import org.kuali.rice.krad.uif.util.ViewModelUtils;
49 import org.kuali.rice.krad.uif.view.ViewModel;
50 import org.kuali.rice.krad.uif.widget.DirectInquiry;
51 import org.kuali.rice.krad.uif.widget.Inquiry;
52 import org.kuali.rice.krad.uif.widget.QuickFinder;
53 import org.kuali.rice.krad.uif.widget.Suggest;
54 import org.kuali.rice.krad.util.KRADPropertyConstants;
55 import org.kuali.rice.krad.util.ObjectUtils;
56 import org.kuali.rice.krad.valuefinder.ValueFinder;
57
58 import java.util.ArrayList;
59 import java.util.List;
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 public class InputField extends DataField {
80 private static final long serialVersionUID = -3703656713706343840L;
81
82
83 private String customValidatorClass;
84 private ValidCharactersConstraint validCharactersConstraint;
85 private CaseConstraint caseConstraint;
86 private List<PrerequisiteConstraint> dependencyConstraints;
87 private List<MustOccurConstraint> mustOccurConstraints;
88 private SimpleConstraint simpleConstraint;
89
90
91 private Control control;
92 private KeyValuesFinder optionsFinder;
93 private boolean performUppercase;
94
95 private String errorMessagePlacement;
96 private ErrorsField errorsField;
97
98
99 private String constraintText;
100 private String instructionalText;
101
102 private MessageField instructionalMessageField;
103 private MessageField constraintMessageField;
104
105 private AttributeQuery fieldAttributeQuery;
106
107
108 private QuickFinder fieldLookup;
109 private DirectInquiry fieldDirectInquiry;
110 private Suggest fieldSuggest;
111
112 public InputField() {
113 super();
114
115 simpleConstraint = new SimpleConstraint();
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129
130 @Override
131 public void performFinalize(View view, Object model, Component parent) {
132 super.performFinalize(view, model, parent);
133
134 setupIds();
135
136
137 List<KeyValue> fieldOptions = new ArrayList<KeyValue>();
138
139
140 if ((control != null) && control instanceof MultiValueControlBase) {
141 MultiValueControlBase multiValueControl = (MultiValueControlBase) control;
142 if ((multiValueControl.getOptions() != null) && !multiValueControl.getOptions().isEmpty()) {
143 fieldOptions = multiValueControl.getOptions();
144 }
145 }
146
147
148 if (fieldOptions.isEmpty() && (optionsFinder != null)) {
149 if (optionsFinder instanceof UifKeyValuesFinder) {
150 fieldOptions = ((UifKeyValuesFinder) optionsFinder).getKeyValues((ViewModel) model);
151
152
153 if (((UifKeyValuesFinder) optionsFinder).isAddBlankOption()) {
154 fieldOptions.add(0, new ConcreteKeyValue("", ""));
155 }
156 } else {
157 fieldOptions = optionsFinder.getKeyValues();
158 }
159
160 if ((control != null) && control instanceof MultiValueControlBase) {
161 ((MultiValueControlBase) control).setOptions(fieldOptions);
162 }
163 }
164
165
166 if (isReadOnly()
167 && !fieldOptions.isEmpty()
168 && StringUtils.isBlank(getAlternateDisplayValue())
169 && StringUtils.isBlank(getAdditionalDisplayValue())
170 && StringUtils.isBlank(getAlternateDisplayPropertyName())
171 && StringUtils.isBlank(getAdditionalDisplayPropertyName())) {
172
173 Object fieldValue = ObjectPropertyUtils.getPropertyValue(model, getBindingInfo().getBindingPath());
174
175
176 if ((fieldValue != null) && (TypeUtils.isSimpleType(fieldValue.getClass()))) {
177 for (KeyValue keyValue : fieldOptions) {
178 if (StringUtils.equals((String) fieldValue, keyValue.getKey())) {
179 setAlternateDisplayValue(keyValue.getValue());
180 break;
181 }
182 }
183 }
184 }
185
186
187 if (isReadOnly() || getControl() == null) {
188 return;
189 }
190
191
192 if (StringUtils.isNotBlank(instructionalText)) {
193 instructionalMessageField.setMessageText(instructionalText);
194 }
195
196
197 if (StringUtils.isNotBlank(constraintText)) {
198 constraintMessageField.setMessageText(constraintText);
199 }
200
201
202 adjustPrerequisiteConstraintBinding(dependencyConstraints);
203
204
205 adjustMustOccurConstraintBinding(mustOccurConstraints);
206
207
208 if (caseConstraint != null) {
209 String propertyName = getBindingInfo().getPropertyAdjustedBindingPath(caseConstraint.getPropertyName());
210 caseConstraint.setPropertyName(propertyName);
211 }
212
213 setupFieldQuery();
214
215 if (view instanceof FormView && ((FormView) view).isValidateClientSide()) {
216 ClientValidationUtils.processAndApplyConstraints(this, view);
217 }
218 }
219
220 protected void adjustMustOccurConstraintBinding(List<MustOccurConstraint> mustOccurConstraints) {
221 if (mustOccurConstraints != null) {
222 for (MustOccurConstraint mustOccurConstraint : mustOccurConstraints) {
223 adjustPrerequisiteConstraintBinding(mustOccurConstraint.getPrerequisiteConstraints());
224 adjustMustOccurConstraintBinding(mustOccurConstraint.getMustOccurConstraints());
225 }
226 }
227 }
228
229 protected void adjustPrerequisiteConstraintBinding(List<PrerequisiteConstraint> prerequisiteConstraints) {
230 if (prerequisiteConstraints != null) {
231 for (PrerequisiteConstraint prerequisiteConstraint : prerequisiteConstraints) {
232 String propertyName = getBindingInfo().getPropertyAdjustedBindingPath(prerequisiteConstraint.getPropertyName());
233 prerequisiteConstraint.setPropertyName(propertyName);
234 }
235 }
236 }
237
238
239
240
241
242
243 protected void setupFieldQuery() {
244 if (getFieldAttributeQuery() != null) {
245
246 getFieldAttributeQuery().updateQueryFieldMapping(getBindingInfo());
247 getFieldAttributeQuery().updateReturnFieldMapping(getBindingInfo());
248 getFieldAttributeQuery().updateQueryMethodArgumentFieldList(getBindingInfo());
249
250
251 String script = "executeFieldQuery('" + getControl().getId() + "',";
252 script += "'" + getId() + "'," + getFieldAttributeQuery().getQueryFieldMappingJsString() + ",";
253 script += getFieldAttributeQuery().getQueryMethodArgumentFieldsJsString() + ",";
254 script += getFieldAttributeQuery().getReturnFieldMappingJsString() + ");";
255
256 if (StringUtils.isNotBlank(getControl().getOnBlurScript())) {
257 script = getControl().getOnBlurScript() + script;
258 }
259 getControl().setOnBlurScript(script);
260 }
261 }
262
263
264
265
266
267
268 protected void setupIds() {
269
270 if (getControl() != null) {
271 getControl().setId(getId());
272 }
273
274 setNestedComponentIdAndSuffix(getErrorsField(), UifConstants.IdSuffixes.ERRORS);
275 setNestedComponentIdAndSuffix(getLabelField(), UifConstants.IdSuffixes.LABEL);
276 setNestedComponentIdAndSuffix(getInstructionalMessageField(), UifConstants.IdSuffixes.INSTRUCTIONAL);
277 setNestedComponentIdAndSuffix(getConstraintMessageField(), UifConstants.IdSuffixes.CONSTRAINT);
278 setNestedComponentIdAndSuffix(getFieldLookup(), UifConstants.IdSuffixes.QUICK_FINDER);
279 setNestedComponentIdAndSuffix(getFieldDirectInquiry(), UifConstants.IdSuffixes.DIRECT_INQUIRY);
280 setNestedComponentIdAndSuffix(getFieldSuggest(), UifConstants.IdSuffixes.SUGGEST);
281
282 setId(getId() + UifConstants.IdSuffixes.ATTRIBUTE);
283 }
284
285
286
287
288
289
290
291 private void setNestedComponentIdAndSuffix(Component component, String suffix) {
292 if (component != null) {
293 String fieldId = getId();
294 fieldId += suffix;
295
296 component.setId(fieldId);
297 }
298 }
299
300
301
302
303
304
305
306
307
308
309
310
311 public void copyFromAttributeDefinition(View view, AttributeDefinition attributeDefinition) {
312 super.copyFromAttributeDefinition(view, attributeDefinition);
313
314
315 if (getMaxLength() == null) {
316 setMaxLength(attributeDefinition.getMaxLength());
317 }
318
319
320 if (getMinLength() == null) {
321 setMinLength(attributeDefinition.getMinLength());
322 }
323
324
325 if (getValidCharactersConstraint() == null) {
326 setValidCharactersConstraint(attributeDefinition.getValidCharactersConstraint());
327 }
328
329 if (getCaseConstraint() == null) {
330 setCaseConstraint(attributeDefinition.getCaseConstraint());
331 }
332
333 if (getDependencyConstraints() == null) {
334 setDependencyConstraints(attributeDefinition.getPrerequisiteConstraints());
335 }
336
337 if (getMustOccurConstraints() == null) {
338 setMustOccurConstraints(attributeDefinition.getMustOccurConstraints());
339 }
340
341
342 if (getRequired() == null) {
343 setRequired(attributeDefinition.isRequired());
344
345
346 if (getRequired() == null) {
347 setRequired(false);
348 }
349 }
350
351
352 if ((getControl() == null) && (attributeDefinition.getControlField() != null)) {
353 Control control = attributeDefinition.getControlField();
354 view.assignComponentIds(control);
355
356 setControl(ComponentUtils.copy(control));
357 }
358
359
360 if (StringUtils.isEmpty(getConstraintText())) {
361 setConstraintText(attributeDefinition.getConstraintText());
362 getConstraintMessageField().setMessageText(attributeDefinition.getConstraintText());
363 }
364
365
366 if (getOptionsFinder() == null) {
367 setOptionsFinder(attributeDefinition.getOptionsFinder());
368 }
369 }
370
371
372
373
374 @Override
375 public List<Component> getComponentsForLifecycle() {
376 List<Component> components = super.getComponentsForLifecycle();
377
378 components.add(control);
379 components.add(errorsField);
380 components.add(fieldLookup);
381 components.add(fieldDirectInquiry);
382 components.add(fieldSuggest);
383
384 return components;
385 }
386
387
388
389
390 @Override
391 public boolean isInputAllowed() {
392 return true;
393 }
394
395
396
397
398
399
400
401
402
403
404
405
406
407 public Control getControl() {
408 return this.control;
409 }
410
411
412
413
414
415
416 public void setControl(Control control) {
417 this.control = control;
418 }
419
420 public String getErrorMessagePlacement() {
421 return this.errorMessagePlacement;
422 }
423
424 public void setErrorMessagePlacement(String errorMessagePlacement) {
425 this.errorMessagePlacement = errorMessagePlacement;
426 }
427
428
429
430
431
432
433
434
435 public ErrorsField getErrorsField() {
436 return this.errorsField;
437 }
438
439
440
441
442
443
444 public void setErrorsField(ErrorsField errorsField) {
445 this.errorsField = errorsField;
446 }
447
448
449
450
451
452
453
454
455
456 public KeyValuesFinder getOptionsFinder() {
457 return this.optionsFinder;
458 }
459
460
461
462
463
464
465 public void setOptionsFinder(KeyValuesFinder optionsFinder) {
466 this.optionsFinder = optionsFinder;
467 }
468
469
470
471
472
473
474
475 public void setOptionsFinderClass(Class<? extends KeyValuesFinder> optionsFinderClass) {
476 this.optionsFinder = ObjectUtils.newInstance(optionsFinderClass);
477 }
478
479
480
481
482
483
484
485 public String getInstructionalText() {
486 return this.instructionalText;
487 }
488
489
490
491
492
493
494 public void setInstructionalText(String instructionalText) {
495 this.instructionalText = instructionalText;
496 }
497
498
499
500
501 @Override
502 public boolean getSupportsOnLoad() {
503 return true;
504 }
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520 public QuickFinder getFieldLookup() {
521 return this.fieldLookup;
522 }
523
524
525
526
527
528
529 public void setFieldLookup(QuickFinder fieldLookup) {
530 this.fieldLookup = fieldLookup;
531 }
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548 public Suggest getFieldSuggest() {
549 return fieldSuggest;
550 }
551
552
553
554
555
556
557 public void setFieldSuggest(Suggest fieldSuggest) {
558 this.fieldSuggest = fieldSuggest;
559 }
560
561
562
563
564
565
566
567
568
569
570
571 public MessageField getInstructionalMessageField() {
572 return this.instructionalMessageField;
573 }
574
575
576
577
578
579
580
581
582
583
584
585 public void setInstructionalMessageField(MessageField instructionalMessageField) {
586 this.instructionalMessageField = instructionalMessageField;
587 }
588
589
590
591
592
593
594
595
596
597
598
599
600 public String getConstraintText() {
601 return this.constraintText;
602 }
603
604
605
606
607
608
609 public void setConstraintText(String constraintText) {
610 this.constraintText = constraintText;
611 }
612
613
614
615
616
617
618
619
620
621
622
623 public MessageField getConstraintMessageField() {
624 return this.constraintMessageField;
625 }
626
627
628
629
630
631
632
633
634
635
636
637 public void setConstraintMessageField(MessageField constraintMessageField) {
638 this.constraintMessageField = constraintMessageField;
639 }
640
641
642
643
644
645
646
647 public ValidCharactersConstraint getValidCharactersConstraint() {
648 return this.validCharactersConstraint;
649 }
650
651
652
653
654 public void setValidCharactersConstraint(ValidCharactersConstraint validCharactersConstraint) {
655 this.validCharactersConstraint = validCharactersConstraint;
656 }
657
658
659
660
661 public CaseConstraint getCaseConstraint() {
662 return this.caseConstraint;
663 }
664
665
666
667
668 public void setCaseConstraint(CaseConstraint caseConstraint) {
669 this.caseConstraint = caseConstraint;
670 }
671
672
673
674
675 public List<PrerequisiteConstraint> getDependencyConstraints() {
676 return this.dependencyConstraints;
677 }
678
679
680
681
682 public void setDependencyConstraints(List<PrerequisiteConstraint> dependencyConstraints) {
683 this.dependencyConstraints = dependencyConstraints;
684 }
685
686
687
688
689 public List<MustOccurConstraint> getMustOccurConstraints() {
690 return this.mustOccurConstraints;
691 }
692
693
694
695
696 public void setMustOccurConstraints(List<MustOccurConstraint> mustOccurConstraints) {
697 this.mustOccurConstraints = mustOccurConstraints;
698 }
699
700
701
702
703
704
705
706 public SimpleConstraint getSimpleConstraint() {
707 return this.simpleConstraint;
708 }
709
710
711
712
713
714
715
716
717 public void setSimpleConstraint(SimpleConstraint simpleConstraint) {
718 this.simpleConstraint = simpleConstraint;
719 }
720
721
722
723
724
725
726
727
728 public Integer getMaxLength() {
729 return simpleConstraint.getMaxLength();
730 }
731
732
733
734
735
736
737 public void setMaxLength(Integer maxLength) {
738 simpleConstraint.setMaxLength(maxLength);
739 }
740
741
742
743
744 public Integer getMinLength() {
745 return simpleConstraint.getMinLength();
746 }
747
748
749
750
751 public void setMinLength(Integer minLength) {
752 simpleConstraint.setMinLength(minLength);
753 }
754
755
756
757
758 @Override
759 public Boolean getRequired() {
760 return this.simpleConstraint.getRequired();
761 }
762
763
764
765
766 @Override
767 public void setRequired(Boolean required) {
768 this.simpleConstraint.setRequired(required);
769 }
770
771
772
773
774
775
776 public String getExclusiveMin() {
777 return simpleConstraint.getExclusiveMin();
778 }
779
780
781
782
783 public void setExclusiveMin(String exclusiveMin) {
784 simpleConstraint.setExclusiveMin(exclusiveMin);
785 }
786
787
788
789
790
791
792 public String getInclusiveMax() {
793 return simpleConstraint.getInclusiveMax();
794 }
795
796
797
798
799 public void setInclusiveMax(String inclusiveMax) {
800 simpleConstraint.setInclusiveMax(inclusiveMax);
801 }
802
803
804
805
806
807
808 public void setFieldDirectInquiry(DirectInquiry fieldDirectInquiry) {
809 this.fieldDirectInquiry = fieldDirectInquiry;
810 }
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826 public DirectInquiry getFieldDirectInquiry() {
827 return fieldDirectInquiry;
828 }
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844 public AttributeQuery getFieldAttributeQuery() {
845 return fieldAttributeQuery;
846 }
847
848
849
850
851
852
853 public void setFieldAttributeQuery(AttributeQuery fieldAttributeQuery) {
854 this.fieldAttributeQuery = fieldAttributeQuery;
855 }
856
857
858
859
860
861
862
863
864
865
866
867 public boolean isPerformUppercase() {
868 return performUppercase;
869 }
870
871
872
873
874
875
876 public void setPerformUppercase(boolean performUppercase) {
877 this.performUppercase = performUppercase;
878 }
879 }