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