1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.service.impl;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.config.property.ConfigurationService;
20 import org.kuali.rice.core.api.exception.RiceRuntimeException;
21 import org.kuali.rice.kim.api.identity.Person;
22 import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
23 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
24 import org.kuali.rice.krad.inquiry.Inquirable;
25 import org.kuali.rice.krad.service.DataDictionaryService;
26 import org.kuali.rice.krad.service.KRADServiceLocator;
27 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
28 import org.kuali.rice.krad.service.ModuleService;
29 import org.kuali.rice.krad.uif.UifConstants;
30 import org.kuali.rice.krad.uif.component.ComponentSecurity;
31 import org.kuali.rice.krad.uif.container.Group;
32 import org.kuali.rice.krad.uif.element.Action;
33 import org.kuali.rice.krad.uif.field.FieldGroup;
34 import org.kuali.rice.krad.uif.layout.TableLayoutManager;
35 import org.kuali.rice.krad.uif.util.ViewCleaner;
36 import org.kuali.rice.krad.uif.view.ViewAuthorizer;
37 import org.kuali.rice.krad.uif.view.ViewPresentationController;
38 import org.kuali.rice.krad.uif.component.BindingInfo;
39 import org.kuali.rice.krad.uif.component.ClientSideState;
40 import org.kuali.rice.krad.uif.component.Component;
41 import org.kuali.rice.krad.uif.component.DataBinding;
42 import org.kuali.rice.krad.uif.component.PropertyReplacer;
43 import org.kuali.rice.krad.uif.component.RequestParameter;
44 import org.kuali.rice.krad.uif.container.CollectionGroup;
45 import org.kuali.rice.krad.uif.container.Container;
46 import org.kuali.rice.krad.uif.control.Control;
47 import org.kuali.rice.krad.uif.field.DataField;
48 import org.kuali.rice.krad.uif.field.InputField;
49 import org.kuali.rice.krad.uif.field.Field;
50 import org.kuali.rice.krad.uif.field.RemoteFieldsHolder;
51 import org.kuali.rice.krad.uif.layout.LayoutManager;
52 import org.kuali.rice.krad.uif.modifier.ComponentModifier;
53 import org.kuali.rice.krad.uif.service.ExpressionEvaluatorService;
54 import org.kuali.rice.krad.uif.service.ViewDictionaryService;
55 import org.kuali.rice.krad.uif.service.ViewHelperService;
56 import org.kuali.rice.krad.uif.util.BooleanMap;
57 import org.kuali.rice.krad.uif.util.CloneUtils;
58 import org.kuali.rice.krad.uif.util.ComponentFactory;
59 import org.kuali.rice.krad.uif.util.ComponentUtils;
60 import org.kuali.rice.krad.uif.util.ExpressionUtils;
61 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
62 import org.kuali.rice.krad.uif.util.ScriptUtils;
63 import org.kuali.rice.krad.uif.util.ViewModelUtils;
64 import org.kuali.rice.krad.uif.view.View;
65 import org.kuali.rice.krad.uif.view.ViewModel;
66 import org.kuali.rice.krad.uif.widget.Inquiry;
67 import org.kuali.rice.krad.uif.widget.Widget;
68 import org.kuali.rice.krad.util.GlobalVariables;
69 import org.kuali.rice.krad.util.GrowlMessage;
70 import org.kuali.rice.krad.util.KRADConstants;
71 import org.kuali.rice.krad.util.MessageMap;
72 import org.kuali.rice.krad.util.ObjectUtils;
73 import org.kuali.rice.krad.valuefinder.ValueFinder;
74 import org.kuali.rice.krad.web.form.UifFormBase;
75 import org.springframework.util.MethodInvoker;
76
77 import java.io.Serializable;
78 import java.lang.annotation.Annotation;
79 import java.text.MessageFormat;
80 import java.util.ArrayList;
81 import java.util.Collection;
82 import java.util.Collections;
83 import java.util.HashMap;
84 import java.util.HashSet;
85 import java.util.List;
86 import java.util.Map;
87 import java.util.Map.Entry;
88 import java.util.Set;
89
90
91
92
93
94
95 public class ViewHelperServiceImpl implements ViewHelperService, Serializable {
96 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ViewHelperServiceImpl.class);
97
98 private transient DataDictionaryService dataDictionaryService;
99 private transient ExpressionEvaluatorService expressionEvaluatorService;
100 private transient ViewDictionaryService viewDictionaryService;
101 private transient ConfigurationService configurationService;
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 @Override
118 public void populateViewFromRequestParameters(View view, Map<String, String> parameters) {
119
120
121 Map<String, Set<PropertyReplacer>> viewPropertyReplacers = new HashMap<String, Set<PropertyReplacer>>();
122 for (PropertyReplacer replacer : view.getPropertyReplacers()) {
123 Set<PropertyReplacer> propertyReplacers = new HashSet<PropertyReplacer>();
124 if (viewPropertyReplacers.containsKey(replacer.getPropertyName())) {
125 propertyReplacers = viewPropertyReplacers.get(replacer.getPropertyName());
126 }
127 propertyReplacers.add(replacer);
128
129 viewPropertyReplacers.put(replacer.getPropertyName(), propertyReplacers);
130 }
131
132 Map<String, Annotation> annotatedFields = CloneUtils.getFieldsWithAnnotation(view.getClass(),
133 RequestParameter.class);
134
135
136
137 Map<String, String> viewRequestParameters = new HashMap<String, String>();
138 for (String fieldToPopulate : annotatedFields.keySet()) {
139 RequestParameter requestParameter = (RequestParameter) annotatedFields.get(fieldToPopulate);
140
141
142 String requestParameterName = requestParameter.parameterName();
143 if (StringUtils.isBlank(requestParameterName)) {
144 requestParameterName = fieldToPopulate;
145 }
146
147 if (!parameters.containsKey(requestParameterName)) {
148 continue;
149 }
150
151 String fieldValue = parameters.get(requestParameterName);
152 if (StringUtils.isNotBlank(fieldValue)) {
153 viewRequestParameters.put(requestParameterName, fieldValue);
154 ObjectPropertyUtils.setPropertyValue(view, fieldToPopulate, fieldValue);
155
156
157
158 if (view.getPropertyExpressions().containsKey(fieldToPopulate)) {
159 view.getPropertyExpressions().remove(fieldToPopulate);
160 }
161
162 if (viewPropertyReplacers.containsKey(fieldToPopulate)) {
163 Set<PropertyReplacer> propertyReplacers = viewPropertyReplacers.get(fieldToPopulate);
164 for (PropertyReplacer replacer : propertyReplacers) {
165 view.getPropertyReplacers().remove(replacer);
166 }
167 }
168 }
169 }
170
171 view.setViewRequestParameters(viewRequestParameters);
172 }
173
174
175
176
177
178 @Override
179 public void performInitialization(View view, Object model) {
180 view.assignComponentIds(view);
181
182
183
184 view.setIdSequence(100000);
185 performComponentInitialization(view, model, view);
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199
200 public void performComponentLifecycle(View view, Object model, Component component, String origId) {
201 Component origComponent = view.getViewIndex().getComponentById(origId);
202
203
204
205 Integer currentSequenceVal = view.getIdSequence();
206 Integer startingSequenceVal = view.getViewIndex().getIdSequenceSnapshot().get(component.getId());
207
208 if (startingSequenceVal != null) {
209 view.setIdSequence(startingSequenceVal);
210 }
211
212 view.assignComponentIds(component);
213
214
215
216 view.setIdSequence(currentSequenceVal);
217
218
219 String suffix = StringUtils.replaceOnce(origComponent.getId(), origComponent.getBaseId(), "");
220 ComponentUtils.updateIdWithSuffix(component, suffix);
221
222 Component parent = (Component) origComponent.getContext().get(UifConstants.ContextVariableNames.PARENT);
223
224
225 component.pushAllToContext(origComponent.getContext());
226 List<Component> nestedComponents = ComponentUtils.getAllNestedComponents(component);
227 for (Component nestedComponent : nestedComponents) {
228 nestedComponent.pushAllToContext(origComponent.getContext());
229 }
230
231
232
233
234 Map<String, String> expressionGraph = view.getViewIndex().getComponentExpressionGraphs().get(
235 component.getBaseId());
236 component.setExpressionGraph(expressionGraph);
237 ExpressionUtils.populatePropertyExpressionsFromGraph(component, false);
238
239
240 if (component instanceof DataBinding) {
241 ((DataBinding) component).setBindingInfo(((DataBinding) origComponent).getBindingInfo());
242 ((DataBinding) component).getBindingInfo().setBindingPath(
243 ((DataBinding) origComponent).getBindingInfo().getBindingPath());
244 }
245
246
247 if (component instanceof Field) {
248 ((Field) component).setLabelRendered(((Field) origComponent).isLabelRendered());
249 } else if (component instanceof CollectionGroup) {
250 ((CollectionGroup) component).setSubCollectionSuffix(
251 ((CollectionGroup) origComponent).getSubCollectionSuffix());
252 }
253
254 if (origComponent.isRefreshedByAction()) {
255 component.setRefreshedByAction(true);
256 }
257
258
259 if (component.isResetDataOnRefresh()) {
260
261 if (component instanceof DataField) {
262
263
264
265 ObjectPropertyUtils.initializeProperty(model,
266 ((DataField) component).getBindingInfo().getBindingPath());
267 }
268 }
269
270 performComponentInitialization(view, model, component);
271 view.getViewIndex().indexComponent(component);
272
273 performComponentApplyModel(view, component, model);
274 view.getViewIndex().indexComponent(component);
275
276
277 if (component instanceof Group || component instanceof FieldGroup) {
278 List<Component> nestedGroupComponents = ComponentUtils.getAllNestedComponents(component);
279 for (Component nestedComponent : nestedGroupComponents) {
280 Component origNestedComponent = view.getViewIndex().getComponentById(nestedComponent.getId() + suffix);
281
282 if (origNestedComponent != null) {
283
284 if (nestedComponent instanceof DataBinding) {
285 ((DataBinding) nestedComponent).setBindingInfo(
286 ((DataBinding) origNestedComponent).getBindingInfo());
287 ((DataBinding) nestedComponent).getBindingInfo().setBindingPath(
288 ((DataBinding) origNestedComponent).getBindingInfo().getBindingPath());
289 }
290
291
292 if (nestedComponent instanceof Field) {
293 ((Field) nestedComponent).setLabelRendered(((Field) origNestedComponent).isLabelRendered());
294 }
295
296 if (origNestedComponent.isRefreshedByAction()) {
297 nestedComponent.setRefreshedByAction(true);
298 }
299
300
301 ComponentUtils.updateIdWithSuffix(nestedComponent, suffix);
302 }
303 }
304 }
305
306
307 Map<String, Object> clientState = new HashMap<String, Object>();
308 performComponentFinalize(view, component, model, parent, clientState);
309
310
311 String clientStateScript = buildClientSideStateScript(view, clientState, true);
312 String onLoadScript = component.getOnLoadScript();
313 if (StringUtils.isNotBlank(onLoadScript)) {
314 clientStateScript = onLoadScript + clientStateScript;
315 }
316 component.setOnLoadScript(clientStateScript);
317
318 view.getViewIndex().indexComponent(component);
319 }
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346 public void performComponentInitialization(View view, Object model, Component component) {
347 if (component == null) {
348 return;
349 }
350
351 if (StringUtils.isBlank(component.getId())) {
352 throw new RiceRuntimeException("Id is not set, this should not happen unless a component is misconfigured");
353 }
354
355
356
357 LOG.debug("Initializing component: " + component.getId() + " with type: " + component.getClass());
358
359
360 if (!(component instanceof View)) {
361 view.getViewIndex().addInitialComponentStateIfNeeded(component);
362 }
363
364
365
366 ExpressionUtils.populatePropertyExpressionsFromGraph(component, true);
367
368
369 component.performInitialization(view, model);
370
371
372 if (component instanceof DataField) {
373 initializeDataFieldFromDataDictionary(view, (DataField) component);
374 }
375
376 if (component instanceof Container) {
377 LayoutManager layoutManager = ((Container) component).getLayoutManager();
378
379
380 addCustomContainerComponents(view, model, (Container) component);
381
382
383
384 if (!(component instanceof CollectionGroup)) {
385 processAnyRemoteFieldsHolder(view, model, (Container) component);
386 }
387 }
388
389
390 if (component instanceof CollectionGroup) {
391
392 }
393
394
395 performCustomInitialization(view, component);
396
397
398 runComponentModifiers(view, component, null, UifConstants.ViewPhases.INITIALIZE);
399
400
401 for (Component nestedComponent : component.getComponentsForLifecycle()) {
402 performComponentInitialization(view, model, nestedComponent);
403 }
404
405
406 for (PropertyReplacer replacer : component.getPropertyReplacers()) {
407 for (Component replacerComponent : replacer.getNestedComponents()) {
408 performComponentInitialization(view, model, replacerComponent);
409 }
410 }
411 }
412
413
414
415
416
417
418
419
420
421
422 protected void processAnyRemoteFieldsHolder(View view, Object model, Container container) {
423 List<Component> processedItems = new ArrayList<Component>();
424
425
426
427 for (Component item : container.getItems()) {
428 if (item instanceof RemoteFieldsHolder) {
429 List<InputField> translatedFields = ((RemoteFieldsHolder) item).fetchAndTranslateRemoteFields(view,
430 model, container);
431 processedItems.addAll(translatedFields);
432 } else {
433 processedItems.add(item);
434 }
435 }
436
437
438 container.setItems(processedItems);
439 }
440
441
442
443
444
445
446
447
448 protected void initializeDataFieldFromDataDictionary(View view, DataField field) {
449 AttributeDefinition attributeDefinition = null;
450
451 String dictionaryAttributeName = field.getDictionaryAttributeName();
452 String dictionaryObjectEntry = field.getDictionaryObjectEntry();
453
454
455
456 if (StringUtils.isNotBlank(dictionaryObjectEntry) && StringUtils.isBlank(dictionaryAttributeName)) {
457 dictionaryAttributeName = field.getPropertyName();
458 }
459
460
461 if (StringUtils.isNotBlank(dictionaryAttributeName) && StringUtils.isNotBlank(dictionaryObjectEntry)) {
462 attributeDefinition = getDataDictionaryService().getAttributeDefinition(dictionaryObjectEntry,
463 dictionaryAttributeName);
464 }
465
466
467 if (attributeDefinition == null) {
468 String propertyPath = field.getBindingInfo().getBindingPath();
469 if (StringUtils.isNotBlank(field.getBindingInfo().getCollectionPath())) {
470 propertyPath = field.getBindingInfo().getCollectionPath();
471 if (StringUtils.isNotBlank(field.getBindingInfo().getBindByNamePrefix())) {
472 propertyPath += "." + field.getBindingInfo().getBindByNamePrefix();
473 }
474 propertyPath += "." + field.getBindingInfo().getBindingName();
475 }
476
477 attributeDefinition = findNestedDictionaryAttribute(view, field, null, propertyPath);
478 }
479
480
481 if (attributeDefinition != null) {
482 field.copyFromAttributeDefinition(view, attributeDefinition);
483 }
484
485
486 if (field instanceof InputField) {
487 InputField inputField = (InputField) field;
488 if (inputField.getControl() == null) {
489 Control control = ComponentFactory.getTextControl();
490 control.setId(view.getNextId());
491 control.setBaseId(control.getId());
492
493 inputField.setControl(control);
494 }
495 }
496 }
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523 protected AttributeDefinition findNestedDictionaryAttribute(View view, DataField field, String parentPath,
524 String propertyPath) {
525 AttributeDefinition attributeDefinition = null;
526
527
528 String dictionaryAttributeName = propertyPath;
529 String dictionaryObjectEntry = null;
530
531 if (field.getBindingInfo().isBindToMap()) {
532 parentPath = "";
533 if (!field.getBindingInfo().isBindToForm() && StringUtils.isNotBlank(
534 field.getBindingInfo().getBindingObjectPath())) {
535 parentPath = field.getBindingInfo().getBindingObjectPath();
536 }
537 if (StringUtils.isNotBlank(field.getBindingInfo().getBindByNamePrefix())) {
538 if (StringUtils.isNotBlank(parentPath)) {
539 parentPath += "." + field.getBindingInfo().getBindByNamePrefix();
540 } else {
541 parentPath = field.getBindingInfo().getBindByNamePrefix();
542 }
543 }
544
545 dictionaryAttributeName = field.getBindingInfo().getBindingName();
546 }
547
548 if (StringUtils.isNotBlank(parentPath)) {
549 Class<?> dictionaryModelClass = ViewModelUtils.getPropertyTypeByClassAndView(view, parentPath);
550 if (dictionaryModelClass != null) {
551 dictionaryObjectEntry = dictionaryModelClass.getName();
552
553 attributeDefinition = getDataDictionaryService().getAttributeDefinition(dictionaryObjectEntry,
554 dictionaryAttributeName);
555 }
556 }
557
558
559
560 if ((attributeDefinition == null) && StringUtils.contains(propertyPath, ".")) {
561 String nextParentPath = StringUtils.substringBefore(propertyPath, ".");
562 if (StringUtils.isNotBlank(parentPath)) {
563 nextParentPath = parentPath + "." + nextParentPath;
564 }
565 String nextPropertyPath = StringUtils.substringAfter(propertyPath, ".");
566
567 return findNestedDictionaryAttribute(view, field, nextParentPath, nextPropertyPath);
568 }
569
570
571 if (attributeDefinition != null) {
572 field.setDictionaryAttributeName(dictionaryAttributeName);
573 field.setDictionaryObjectEntry(dictionaryObjectEntry);
574 }
575
576 return attributeDefinition;
577 }
578
579
580
581
582
583 @Override
584 public void performApplyModel(View view, Object model) {
585
586 retrieveEditModesAndActionFlags(view, (UifFormBase) model);
587
588
589 setViewContext(view, model);
590
591 performComponentApplyModel(view, view, model);
592 }
593
594
595
596
597
598
599
600
601
602 protected void retrieveEditModesAndActionFlags(View view, UifFormBase model) {
603 ViewPresentationController presentationController = view.getPresentationController();
604 ViewAuthorizer authorizer = view.getAuthorizer();
605
606 Person user = GlobalVariables.getUserSession().getPerson();
607
608 Set<String> actionFlags = presentationController.getActionFlags(view, model);
609 actionFlags = authorizer.getActionFlags(view, model, user, actionFlags);
610
611 view.setActionFlags(new BooleanMap(actionFlags));
612
613 Set<String> editModes = presentationController.getEditModes(view, model);
614 editModes = authorizer.getEditModes(view, model, user, editModes);
615
616 view.setEditModes(new BooleanMap(editModes));
617 }
618
619
620
621
622
623
624
625
626 protected void setViewContext(View view, Object model) {
627 view.pushAllToContext(getPreModelContext(view));
628
629
630 for (Entry<String, String> variableExpression : view.getExpressionVariables().entrySet()) {
631 String variableName = variableExpression.getKey();
632 Object value = getExpressionEvaluatorService().evaluateExpression(model, view.getContext(),
633 variableExpression.getValue());
634 view.pushObjectToContext(variableName, value);
635 }
636 }
637
638
639
640
641
642
643
644
645 protected Map<String, Object> getPreModelContext(View view) {
646 Map<String, Object> context = new HashMap<String, Object>();
647
648 context.put(UifConstants.ContextVariableNames.VIEW, view);
649 context.put(UifConstants.ContextVariableNames.VIEW_HELPER, this);
650
651 Map<String, String> properties = KRADServiceLocator.getKualiConfigurationService().getAllProperties();
652 context.put(UifConstants.ContextVariableNames.CONFIG_PROPERTIES, properties);
653 context.put(UifConstants.ContextVariableNames.CONSTANTS, KRADConstants.class);
654 context.put(UifConstants.ContextVariableNames.UIF_CONSTANTS, UifConstants.class);
655
656 return context;
657 }
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674 protected void performComponentApplyModel(View view, Component component, Object model) {
675 if (component == null) {
676 return;
677 }
678
679
680 component.pushAllToContext(getCommonContext(view, component));
681
682 getExpressionEvaluatorService().evaluateExpressionsOnConfigurable(view, component, model,
683 component.getContext());
684
685
686 ComponentSecurity componentSecurity = component.getComponentSecurity();
687 getExpressionEvaluatorService().evaluateExpressionsOnConfigurable(view, componentSecurity, model,
688 component.getContext());
689
690
691 if (component instanceof DataBinding) {
692 BindingInfo bindingInfo = ((DataBinding) component).getBindingInfo();
693 getExpressionEvaluatorService().evaluateExpressionsOnConfigurable(view, bindingInfo, model,
694 component.getContext());
695 }
696
697
698 if (component instanceof Container) {
699 LayoutManager layoutManager = ((Container) component).getLayoutManager();
700
701 if (layoutManager != null) {
702 layoutManager.getContext().putAll(getCommonContext(view, component));
703 layoutManager.pushObjectToContext(UifConstants.ContextVariableNames.PARENT, component);
704 layoutManager.pushObjectToContext(UifConstants.ContextVariableNames.MANAGER, layoutManager);
705
706 getExpressionEvaluatorService().evaluateExpressionsOnConfigurable(view, layoutManager, model,
707 layoutManager.getContext());
708 }
709 }
710
711
712 syncClientSideStateForComponent(component, ((ViewModel) model).getClientStateForSyncing());
713
714
715 applyAuthorizationAndPresentationLogic(view, component, (ViewModel) model);
716
717
718 Component parent = (Component) component.getContext().get(UifConstants.ContextVariableNames.PARENT);
719 component.performApplyModel(view, model, parent);
720
721
722 performCustomApplyModel(view, component, model);
723
724
725 runComponentModifiers(view, component, model, UifConstants.ViewPhases.APPLY_MODEL);
726
727
728 for (Component nestedComponent : component.getComponentsForLifecycle()) {
729 if (nestedComponent != null) {
730 nestedComponent.pushObjectToContext(UifConstants.ContextVariableNames.PARENT, component);
731 performComponentApplyModel(view, nestedComponent, model);
732 }
733 }
734 }
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757 protected void applyAuthorizationAndPresentationLogic(View view, Component component, ViewModel model) {
758 ViewPresentationController presentationController = view.getPresentationController();
759 ViewAuthorizer authorizer = view.getAuthorizer();
760
761 Person user = GlobalVariables.getUserSession().getPerson();
762
763
764 if (!component.isRender()) {
765 return;
766 }
767
768
769 if (component instanceof View) {
770 if (!view.isReadOnly()) {
771 boolean canEditView = authorizer.canEditView(view, model, user);
772 if (canEditView) {
773 canEditView = presentationController.canEditView(view, model);
774 }
775 view.setReadOnly(!canEditView);
776 }
777 }
778
779
780 else if (component instanceof Group) {
781 Group group = (Group) component;
782
783
784 if (!group.isHidden()) {
785 boolean canViewGroup = authorizer.canViewGroup(view, model, group, group.getId(), user);
786 if (canViewGroup) {
787 canViewGroup = presentationController.canViewGroup(view, model, group, group.getId());
788 }
789 group.setHidden(!canViewGroup);
790 group.setRender(canViewGroup);
791 }
792
793
794 if (!group.isReadOnly()) {
795 boolean canEditGroup = authorizer.canEditGroup(view, model, group, group.getId(), user);
796 if (canEditGroup) {
797 canEditGroup = presentationController.canEditGroup(view, model, group, group.getId());
798 }
799 group.setReadOnly(!canEditGroup);
800 }
801 }
802
803
804 else if (component instanceof Field) {
805 Field field = (Field) component;
806
807 String propertyName = null;
808 if (field instanceof DataBinding) {
809 propertyName = ((DataBinding) field).getPropertyName();
810 }
811
812
813 if (!field.isHidden()) {
814 boolean canViewField = authorizer.canViewField(view, model, field, propertyName, user);
815 if (canViewField) {
816 canViewField = presentationController.canViewField(view, model, field, propertyName);
817 }
818 field.setHidden(!canViewField);
819 field.setRender(canViewField);
820 }
821
822
823 if (!field.isReadOnly()) {
824
825 boolean canEditField = authorizer.canEditField(view, model, field, propertyName, user);
826 if (canEditField) {
827 canEditField = presentationController.canEditField(view, model, field, propertyName);
828 }
829 field.setReadOnly(!canEditField);
830 }
831
832
833 if ((field.getRequired() == null) || !field.getRequired().booleanValue()) {
834 boolean fieldIsRequired = presentationController.fieldIsRequired(view, model, field, propertyName);
835 }
836
837 if (field instanceof DataField) {
838 DataField dataField = (DataField) field;
839
840
841 boolean canUnmaskValue = authorizer.canUnmaskField(view, model, dataField, dataField.getPropertyName(),
842 user);
843 if (!canUnmaskValue) {
844 dataField.setApplyMask(true);
845 dataField.setMaskFormatter(dataField.getComponentSecurity().getAttributeSecurity().
846 getMaskFormatter());
847 } else {
848
849 boolean canPartiallyUnmaskValue = authorizer.canPartialUnmaskField(view, model, dataField,
850 dataField.getPropertyName(), user);
851 if (!canPartiallyUnmaskValue) {
852 dataField.setApplyMask(true);
853 dataField.setMaskFormatter(
854 dataField.getComponentSecurity().getAttributeSecurity().getPartialMaskFormatter());
855 }
856 }
857 }
858
859
860 if (field instanceof Action) {
861 Action action = (Action) field;
862
863 boolean canTakeAction = authorizer.canPerformAction(view, model, action, action.getActionEvent(),
864 action.getId(), user);
865 if (canTakeAction) {
866 canTakeAction = presentationController.canPerformAction(view, model, action,
867 action.getActionEvent(), action.getId());
868 }
869 action.setRender(canTakeAction);
870 }
871 }
872
873
874 else if (component instanceof Widget) {
875 Widget widget = (Widget) component;
876
877
878 if (!widget.isHidden()) {
879 boolean canViewWidget = authorizer.canViewWidget(view, model, widget, widget.getId(), user);
880 if (canViewWidget) {
881 canViewWidget = presentationController.canViewWidget(view, model, widget, widget.getId());
882 }
883 widget.setHidden(!canViewWidget);
884 widget.setRender(canViewWidget);
885 }
886
887
888 if (!widget.isReadOnly()) {
889 boolean canEditWidget = authorizer.canEditWidget(view, model, widget, widget.getId(), user);
890 if (canEditWidget) {
891 canEditWidget = presentationController.canEditWidget(view, model, widget, widget.getId());
892 }
893 widget.setReadOnly(!canEditWidget);
894 }
895 }
896 }
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913 protected void runComponentModifiers(View view, Component component, Object model, String runPhase) {
914 for (ComponentModifier modifier : component.getComponentModifiers()) {
915
916 if (StringUtils.equals(runPhase, UifConstants.ViewPhases.INITIALIZE)) {
917 modifier.performInitialization(view, model, component);
918 }
919
920
921 if (StringUtils.equals(modifier.getRunPhase(), runPhase)) {
922
923 boolean runModifier = true;
924 if (StringUtils.isNotBlank(modifier.getRunCondition())) {
925 Map<String, Object> context = new HashMap<String, Object>();
926 context.put(UifConstants.ContextVariableNames.COMPONENT, component);
927 context.put(UifConstants.ContextVariableNames.VIEW, view);
928
929 String conditionEvaluation = getExpressionEvaluatorService().evaluateExpressionTemplate(model,
930 context, modifier.getRunCondition());
931 runModifier = Boolean.parseBoolean(conditionEvaluation);
932 }
933
934 if (runModifier) {
935 modifier.performModification(view, model, component);
936 }
937 }
938 }
939 }
940
941
942
943
944
945
946
947
948 protected Map<String, Object> getCommonContext(View view, Component component) {
949 Map<String, Object> context = new HashMap<String, Object>();
950
951 context.putAll(view.getContext());
952 context.put(UifConstants.ContextVariableNames.COMPONENT, component);
953
954 return context;
955 }
956
957
958
959
960
961 @Override
962 public void performFinalize(View view, Object model) {
963
964 String growlScript = buildGrowlScript(view);
965 ((ViewModel) model).setGrowlScript(growlScript);
966
967 Map<String, Object> clientState = new HashMap<String, Object>();
968 performComponentFinalize(view, view, model, null, clientState);
969
970 String clientStateScript = buildClientSideStateScript(view, clientState, false);
971 String viewPreLoadScript = view.getPreLoadScript();
972 if (StringUtils.isNotBlank(viewPreLoadScript)) {
973 clientStateScript = viewPreLoadScript + clientStateScript;
974 }
975 view.setPreLoadScript(clientStateScript);
976
977
978 if (!((ViewModel) model).isDefaultsApplied()) {
979 applyDefaultValues(view, view, model);
980 ((ViewModel) model).setDefaultsApplied(true);
981 }
982 }
983
984
985
986
987
988
989
990
991
992
993
994
995 protected String buildClientSideStateScript(View view, Map<String, Object> clientSideState, boolean updateOnly) {
996
997
998
999 for (Entry<String, Object> additionalState : view.getClientSideState().entrySet()) {
1000 if (!clientSideState.containsKey(additionalState.getKey())) {
1001 clientSideState.put(additionalState.getKey(), additionalState.getValue());
1002 } else {
1003 Object state = clientSideState.get(additionalState.getKey());
1004 Object mergeState = additionalState.getValue();
1005 if ((state instanceof Map) && (mergeState instanceof Map)) {
1006 ((Map) state).putAll((Map) mergeState);
1007 } else {
1008 clientSideState.put(additionalState.getKey(), additionalState.getValue());
1009 }
1010 }
1011 }
1012
1013
1014 String clientStateScript = "";
1015 if (!clientSideState.isEmpty()) {
1016 if (updateOnly) {
1017 clientStateScript = "updateViewState({";
1018 } else {
1019 clientStateScript = "initializeViewState({";
1020 }
1021
1022 for (Entry<String, Object> stateEntry : clientSideState.entrySet()) {
1023 clientStateScript += "'" + stateEntry.getKey() + "':";
1024 clientStateScript += ScriptUtils.translateValue(stateEntry.getValue());
1025 clientStateScript += ",";
1026 }
1027 clientStateScript = StringUtils.removeEnd(clientStateScript, ",");
1028 clientStateScript += "});";
1029 }
1030
1031
1032 if (!updateOnly) {
1033 String kradImageLocation = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
1034 "krad.externalizable.images.url");
1035 clientStateScript += "setConfigParam('"
1036 + UifConstants.ClientSideVariables.KRAD_IMAGE_LOCATION
1037 + "','"
1038 + kradImageLocation
1039 + "');";
1040
1041 String kradURL = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString("krad.url");
1042 clientStateScript +=
1043 "setConfigParam('" + UifConstants.ClientSideVariables.KRAD_URL + "','" + kradURL + "');";
1044 }
1045
1046 return clientStateScript;
1047 }
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065 protected String buildGrowlScript(View view) {
1066 String growlScript = "";
1067
1068 ConfigurationService configService = getConfigurationService();
1069
1070 MessageMap messageMap = GlobalVariables.getMessageMap();
1071 for (GrowlMessage growl : messageMap.getGrowlMessages()) {
1072 if (view.isGrowlMessagingEnabled()) {
1073 String message = configService.getPropertyValueAsString(growl.getMessageKey());
1074
1075 if (StringUtils.isNotBlank(message)) {
1076 if (growl.getMessageParameters() != null) {
1077 message = message.replace("'", "''");
1078 message = MessageFormat.format(message, (Object[]) growl.getMessageParameters());
1079 }
1080
1081
1082 message = message.replace("'", "\\'");
1083
1084 String title = growl.getTitle();
1085 title = title.replace("'", "\\'");
1086
1087 growlScript =
1088 growlScript + "showGrowl('" + message + "', '" + title + "', '" + growl.getTheme() + "');";
1089 }
1090 } else {
1091 messageMap.putInfoForSectionId(KRADConstants.GLOBAL_INFO, growl.getMessageKey(),
1092 growl.getMessageParameters());
1093 }
1094 }
1095
1096 return growlScript;
1097 }
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109 protected void performComponentFinalize(View view, Component component, Object model, Component parent,
1110 Map<String, Object> clientSideState) {
1111 if (component == null) {
1112 return;
1113 }
1114
1115
1116 ViewModel viewModel = (ViewModel) model;
1117 if ((component instanceof DataBinding) && view.isSupportsRequestOverrideOfReadOnlyFields() && !viewModel
1118 .getReadOnlyFieldsList().isEmpty()) {
1119 String propertyName = ((DataBinding) component).getPropertyName();
1120 if (viewModel.getReadOnlyFieldsList().contains(propertyName)) {
1121 component.setReadOnly(true);
1122 }
1123 }
1124
1125
1126 invokeMethodFinalizer(view, component, model);
1127
1128
1129 component.performFinalize(view, model, parent);
1130
1131
1132 addClientSideStateForComponent(component, clientSideState);
1133
1134
1135 performCustomFinalize(view, component, model, parent);
1136
1137
1138 runComponentModifiers(view, component, model, UifConstants.ViewPhases.FINALIZE);
1139
1140
1141 for (Component nestedComponent : component.getComponentsForLifecycle()) {
1142 performComponentFinalize(view, nestedComponent, model, component, clientSideState);
1143 }
1144 }
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160 protected void addClientSideStateForComponent(Component component, Map<String, Object> clientSideState) {
1161 Map<String, Annotation> annotatedFields = CloneUtils.getFieldsWithAnnotation(component.getClass(),
1162 ClientSideState.class);
1163
1164 if (!annotatedFields.isEmpty()) {
1165 Map<String, Object> componentClientState = null;
1166 if (component instanceof View) {
1167 componentClientState = clientSideState;
1168 } else {
1169 if (clientSideState.containsKey(component.getId())) {
1170 componentClientState = (Map<String, Object>) clientSideState.get(component.getId());
1171 } else {
1172 componentClientState = new HashMap<String, Object>();
1173 clientSideState.put(component.getId(), componentClientState);
1174 }
1175 }
1176
1177 for (Entry<String, Annotation> annotatedField : annotatedFields.entrySet()) {
1178 ClientSideState clientSideStateAnnot = (ClientSideState) annotatedField.getValue();
1179
1180 String variableName = clientSideStateAnnot.variableName();
1181 if (StringUtils.isBlank(variableName)) {
1182 variableName = annotatedField.getKey();
1183 }
1184
1185 Object value = ObjectPropertyUtils.getPropertyValue(component, annotatedField.getKey());
1186 componentClientState.put(variableName, value);
1187 }
1188 }
1189 }
1190
1191
1192
1193
1194
1195
1196
1197
1198 protected void syncClientSideStateForComponent(Component component, Map<String, Object> clientSideState) {
1199
1200 Map<String, Object> componentState = null;
1201 if (component instanceof View) {
1202 componentState = clientSideState;
1203 } else {
1204 if (clientSideState.containsKey(component.getId())) {
1205 componentState = (Map<String, Object>) clientSideState.get(component.getId());
1206 }
1207 }
1208
1209
1210 if ((componentState != null) && (!componentState.isEmpty())) {
1211 Map<String, Annotation> annotatedFields = CloneUtils.getFieldsWithAnnotation(component.getClass(),
1212 ClientSideState.class);
1213
1214 for (Entry<String, Annotation> annotatedField : annotatedFields.entrySet()) {
1215 ClientSideState clientSideStateAnnot = (ClientSideState) annotatedField.getValue();
1216
1217 String variableName = clientSideStateAnnot.variableName();
1218 if (StringUtils.isBlank(variableName)) {
1219 variableName = annotatedField.getKey();
1220 }
1221
1222 if (componentState.containsKey(variableName)) {
1223 Object value = componentState.get(variableName);
1224 ObjectPropertyUtils.setPropertyValue(component, annotatedField.getKey(), value);
1225 }
1226 }
1227 }
1228 }
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239 protected void invokeMethodFinalizer(View view, Component component, Object model) {
1240 String finalizeMethodToCall = component.getFinalizeMethodToCall();
1241 MethodInvoker finalizeMethodInvoker = component.getFinalizeMethodInvoker();
1242
1243 if (StringUtils.isBlank(finalizeMethodToCall) && (finalizeMethodInvoker == null)) {
1244 return;
1245 }
1246
1247 if (finalizeMethodInvoker == null) {
1248 finalizeMethodInvoker = new MethodInvoker();
1249 }
1250
1251
1252
1253 if (StringUtils.isBlank(finalizeMethodInvoker.getTargetMethod())) {
1254 finalizeMethodInvoker.setTargetMethod(finalizeMethodToCall);
1255 }
1256
1257
1258 if ((finalizeMethodInvoker.getTargetClass() == null) && (finalizeMethodInvoker.getTargetObject() == null)) {
1259 finalizeMethodInvoker.setTargetObject(view.getViewHelperService());
1260 }
1261
1262
1263 List<Object> additionalArguments = component.getFinalizeMethodAdditionalArguments();
1264 if (additionalArguments == null) {
1265 additionalArguments = new ArrayList<Object>();
1266 }
1267
1268 Object[] arguments = new Object[2 + additionalArguments.size()];
1269 arguments[0] = component;
1270 arguments[1] = model;
1271
1272 int argumentIndex = 1;
1273 for (Object argument : additionalArguments) {
1274 argumentIndex++;
1275 arguments[argumentIndex] = argument;
1276 }
1277 finalizeMethodInvoker.setArguments(arguments);
1278
1279
1280 try {
1281 LOG.debug("Invoking finalize method: "
1282 + finalizeMethodInvoker.getTargetMethod()
1283 + " for component: "
1284 + component.getId());
1285 finalizeMethodInvoker.prepare();
1286
1287 Class<?> methodReturnType = finalizeMethodInvoker.getPreparedMethod().getReturnType();
1288 if (StringUtils.equals("void", methodReturnType.getName())) {
1289 finalizeMethodInvoker.invoke();
1290 } else {
1291 String renderOutput = (String) finalizeMethodInvoker.invoke();
1292
1293 component.setSelfRendered(true);
1294 component.setRenderedHtmlOutput(renderOutput);
1295 }
1296 } catch (Exception e) {
1297 LOG.error("Error invoking finalize method for component: " + component.getId(), e);
1298 throw new RuntimeException("Error invoking finalize method for component: " + component.getId(), e);
1299 }
1300 }
1301
1302
1303
1304
1305 @Override
1306 public void cleanViewAfterRender(View view) {
1307 ViewCleaner.cleanView(view);
1308 }
1309
1310
1311
1312
1313
1314 @Override
1315 public void processCollectionAddLine(View view, Object model, String collectionPath) {
1316
1317 CollectionGroup collectionGroup = view.getViewIndex().getCollectionGroupByPath(collectionPath);
1318 if (collectionGroup == null) {
1319 logAndThrowRuntime("Unable to get collection group component for path: " + collectionPath);
1320 }
1321
1322
1323 Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(model, collectionPath);
1324 if (collection == null) {
1325 logAndThrowRuntime("Unable to get collection property from model for path: " + collectionPath);
1326 }
1327
1328
1329 String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath();
1330 Object addLine = ObjectPropertyUtils.getPropertyValue(model, addLinePath);
1331 if (addLine == null) {
1332 logAndThrowRuntime("Add line instance not found for path: " + addLinePath);
1333 }
1334
1335 processBeforeAddLine(view, collectionGroup, model, addLine);
1336
1337
1338 boolean isValidLine = performAddLineValidation(view, collectionGroup, model, addLine);
1339 if (isValidLine) {
1340
1341
1342
1343 addLine(collection, addLine, collectionGroup.getAddLinePlacement().equals("TOP"));
1344
1345
1346 collectionGroup.initializeNewCollectionLine(view, model, collectionGroup, true);
1347 }
1348
1349 ((UifFormBase) model).getAddedCollectionItems().add(addLine);
1350
1351 processAfterAddLine(view, collectionGroup, model, addLine);
1352 }
1353
1354
1355
1356
1357
1358 @Override
1359 public void processCollectionSaveLine(View view, Object model, String collectionPath, int selectedLineIndex) {
1360
1361 CollectionGroup collectionGroup = view.getViewIndex().getCollectionGroupByPath(collectionPath);
1362 if (collectionGroup == null) {
1363 logAndThrowRuntime("Unable to get collection group component for path: " + collectionPath);
1364 }
1365
1366
1367 Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(model, collectionPath);
1368 if (collection == null) {
1369 logAndThrowRuntime("Unable to get collection property from model for path: " + collectionPath);
1370 }
1371
1372
1373
1374 if (collection instanceof List) {
1375 Object saveLine = ((List<Object>) collection).get(selectedLineIndex);
1376
1377 processBeforeSaveLine(view, collectionGroup, model, saveLine);
1378
1379 ((UifFormBase) model).getAddedCollectionItems().remove(saveLine);
1380
1381 processAfterSaveLine(view, collectionGroup, model, saveLine);
1382
1383 } else {
1384 logAndThrowRuntime("Only List collection implementations are supported for the delete by index method");
1385 }
1386
1387 }
1388
1389
1390
1391
1392
1393 @Override
1394 public void processCollectionAddBlankLine(View view, Object model, String collectionPath) {
1395
1396 CollectionGroup collectionGroup = view.getViewIndex().getCollectionGroupByPath(collectionPath);
1397 if (collectionGroup == null) {
1398 logAndThrowRuntime("Unable to get collection group component for path: " + collectionPath);
1399 }
1400
1401
1402 Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(model, collectionPath);
1403 if (collection == null) {
1404 logAndThrowRuntime("Unable to get collection property from model for path: " + collectionPath);
1405 }
1406
1407 Object newLine = ObjectUtils.newInstance(collectionGroup.getCollectionObjectClass());
1408 applyDefaultValuesForCollectionLine(view, model, collectionGroup, newLine);
1409 addLine(collection, newLine, collectionGroup.getAddLinePlacement().equals("TOP"));
1410
1411 ((UifFormBase) model).getAddedCollectionItems().add(newLine);
1412
1413 }
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423 protected void addLine(Collection<Object> collection, Object addLine, boolean insertFirst) {
1424 if (insertFirst && (collection instanceof List)) {
1425 ((List) collection).add(0, addLine);
1426 } else {
1427 collection.add(addLine);
1428 }
1429 }
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443 protected boolean performAddLineValidation(View view, CollectionGroup collectionGroup, Object model,
1444 Object addLine) {
1445 boolean isValid = true;
1446
1447
1448
1449
1450 return isValid;
1451 }
1452
1453
1454
1455
1456
1457 public void processCollectionDeleteLine(View view, Object model, String collectionPath, int lineIndex) {
1458
1459 CollectionGroup collectionGroup = view.getViewIndex().getCollectionGroupByPath(collectionPath);
1460 if (collectionGroup == null) {
1461 logAndThrowRuntime("Unable to get collection group component for path: " + collectionPath);
1462 }
1463
1464
1465 Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(model, collectionPath);
1466 if (collection == null) {
1467 logAndThrowRuntime("Unable to get collection property from model for path: " + collectionPath);
1468 }
1469
1470
1471
1472 if (collection instanceof List) {
1473 Object deleteLine = ((List<Object>) collection).get(lineIndex);
1474
1475
1476 boolean isValid = performDeleteLineValidation(view, collectionGroup, deleteLine);
1477 if (isValid) {
1478 ((List<Object>) collection).remove(lineIndex);
1479 processAfterDeleteLine(view, collectionGroup, model, lineIndex);
1480 }
1481 } else {
1482 logAndThrowRuntime("Only List collection implementations are supported for the delete by index method");
1483 }
1484 }
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496 protected boolean performDeleteLineValidation(View view, CollectionGroup collectionGroup, Object deleteLine) {
1497 boolean isValid = true;
1498
1499
1500
1501
1502 return isValid;
1503 }
1504
1505
1506
1507
1508 public void processMultipleValueLookupResults(View view, Object model, String collectionPath,
1509 String lookupResultValues) {
1510
1511 if (StringUtils.isBlank(lookupResultValues)) {
1512 return;
1513 }
1514
1515
1516 CollectionGroup collectionGroup = view.getViewIndex().getCollectionGroupByPath(collectionPath);
1517 if (collectionGroup == null) {
1518 throw new RuntimeException("Unable to find collection group for path: " + collectionPath);
1519 }
1520
1521 Class<?> collectionObjectClass = collectionGroup.getCollectionObjectClass();
1522 Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(model,
1523 collectionGroup.getBindingInfo().getBindingPath());
1524 if (collection == null) {
1525 Class<?> collectionClass = ObjectPropertyUtils.getPropertyType(model,
1526 collectionGroup.getBindingInfo().getBindingPath());
1527 collection = (Collection<Object>) ObjectUtils.newInstance(collectionClass);
1528 ObjectPropertyUtils.setPropertyValue(model, collectionGroup.getBindingInfo().getBindingPath(), collection);
1529 }
1530
1531 Map<String, String> fieldConversions = collectionGroup.getCollectionLookup().getFieldConversions();
1532 List<String> toFieldNamesColl = new ArrayList(fieldConversions.values());
1533 Collections.sort(toFieldNamesColl);
1534 String[] toFieldNames = new String[toFieldNamesColl.size()];
1535 toFieldNamesColl.toArray(toFieldNames);
1536
1537
1538 String[] lineValues = StringUtils.split(lookupResultValues, ",");
1539
1540
1541 for (String lineValue : lineValues) {
1542 Object lineDataObject = null;
1543
1544
1545 ModuleService moduleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(
1546 collectionObjectClass);
1547 if (moduleService != null && moduleService.isExternalizable(collectionObjectClass)) {
1548 lineDataObject = moduleService.createNewObjectFromExternalizableClass(collectionObjectClass.asSubclass(
1549 ExternalizableBusinessObject.class));
1550 } else {
1551 lineDataObject = ObjectUtils.newInstance(collectionObjectClass);
1552 }
1553
1554
1555 applyDefaultValuesForCollectionLine(view, model, collectionGroup, lineDataObject);
1556
1557 String[] fieldValues = StringUtils.split(lineValue, ":");
1558 if (fieldValues.length != toFieldNames.length) {
1559 throw new RuntimeException(
1560 "Value count passed back from multi-value lookup does not match field conversion count");
1561 }
1562
1563
1564 for (int i = 0; i < fieldValues.length; i++) {
1565 String fieldName = toFieldNames[i];
1566 ObjectPropertyUtils.setPropertyValue(lineDataObject, fieldName, fieldValues[i]);
1567 }
1568
1569
1570
1571 collection.add(lineDataObject);
1572 }
1573 }
1574
1575
1576
1577
1578
1579
1580
1581
1582 public void buildInquiryLink(Object dataObject, String propertyName, Inquiry inquiry) {
1583 Inquirable inquirable = getViewDictionaryService().getInquirable(dataObject.getClass(), inquiry.getViewName());
1584 if (inquirable != null) {
1585 inquirable.buildInquirableLink(dataObject, propertyName, inquiry);
1586 } else {
1587
1588
1589
1590 inquiry.setRender(false);
1591 }
1592 }
1593
1594
1595
1596
1597
1598
1599 public void applyDefaultValuesForCollectionLine(View view, Object model, CollectionGroup collectionGroup,
1600 Object line) {
1601
1602 List<DataField> dataFields = ComponentUtils.getComponentsOfTypeDeep(collectionGroup.getAddLineItems(),
1603 DataField.class);
1604 for (DataField dataField : dataFields) {
1605 String bindingPath = "";
1606 if (StringUtils.isNotBlank(dataField.getBindingInfo().getBindByNamePrefix())) {
1607 bindingPath = dataField.getBindingInfo().getBindByNamePrefix() + ".";
1608 }
1609 bindingPath += dataField.getBindingInfo().getBindingName();
1610
1611 populateDefaultValueForField(view, line, dataField, bindingPath);
1612 }
1613 }
1614
1615
1616
1617
1618
1619
1620
1621
1622 protected void applyDefaultValues(View view, Component component, Object model) {
1623 if (component == null) {
1624 return;
1625 }
1626
1627
1628 if (component instanceof DataField) {
1629 DataField dataField = ((DataField) component);
1630
1631
1632 dataField.getBindingInfo().setDefaults(view, dataField.getPropertyName());
1633
1634 populateDefaultValueForField(view, model, dataField, dataField.getBindingInfo().getBindingPath());
1635 }
1636
1637 List<Component> nestedComponents = component.getComponentsForLifecycle();
1638
1639
1640 if (component instanceof View) {
1641 nestedComponents.addAll(((View) component).getItems());
1642 }
1643
1644 for (Component nested : nestedComponents) {
1645 applyDefaultValues(view, nested, model);
1646 }
1647 }
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665 protected void populateDefaultValueForField(View view, Object object, DataField dataField, String bindingPath) {
1666
1667 String defaultValue = dataField.getDefaultValue();
1668 Object[] defaultValues = dataField.getDefaultValues();
1669
1670 if (StringUtils.isBlank(defaultValue) && defaultValues != null && defaultValues.length > 0) {
1671 ObjectPropertyUtils.setPropertyValue(object, bindingPath, defaultValues);
1672 }
1673 else {
1674 if (StringUtils.isBlank(defaultValue) && (dataField.getDefaultValueFinderClass() != null)) {
1675 ValueFinder defaultValueFinder = ObjectUtils.newInstance(dataField.getDefaultValueFinderClass());
1676 defaultValue = defaultValueFinder.getValue();
1677 }
1678
1679
1680 if (StringUtils.isNotBlank(defaultValue) && ObjectPropertyUtils.isWritableProperty(object, bindingPath)) {
1681 if (getExpressionEvaluatorService().containsElPlaceholder(defaultValue)) {
1682 Map<String, Object> context = getPreModelContext(view);
1683 defaultValue = getExpressionEvaluatorService().evaluateExpressionTemplate(null, context, defaultValue);
1684 }
1685
1686
1687 ObjectPropertyUtils.setPropertyValue(object, bindingPath, defaultValue);
1688 }
1689 }
1690 }
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709 protected void addCustomContainerComponents(View view, Object model, Container container) {
1710
1711 }
1712
1713
1714
1715
1716
1717
1718
1719
1720 protected void performCustomInitialization(View view, Component component) {
1721
1722 }
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733 protected void performCustomApplyModel(View view, Component component, Object model) {
1734
1735 }
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745 protected void performCustomFinalize(View view, Component component, Object model, Component parent) {
1746
1747 }
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760 protected void processBeforeAddLine(View view, CollectionGroup collectionGroup, Object model, Object addLine) {
1761
1762 }
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775 protected void processAfterAddLine(View view, CollectionGroup collectionGroup, Object model, Object addLine) {
1776
1777 }
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789 protected void processBeforeSaveLine(View view, CollectionGroup collectionGroup, Object model, Object addLine) {
1790
1791 }
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803 protected void processAfterSaveLine(View view, CollectionGroup collectionGroup, Object model, Object addLine) {
1804
1805 }
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816 protected void processAfterDeleteLine(View view, CollectionGroup collectionGroup, Object model, int lineIndex) {
1817
1818 }
1819
1820
1821
1822
1823
1824
1825 protected void logAndThrowRuntime(String message) {
1826 LOG.error(message);
1827 throw new RuntimeException(message);
1828 }
1829
1830
1831
1832
1833
1834
1835 protected DataDictionaryService getDataDictionaryService() {
1836 if (this.dataDictionaryService == null) {
1837 this.dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService();
1838 }
1839
1840 return this.dataDictionaryService;
1841 }
1842
1843
1844
1845
1846
1847
1848 public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
1849 this.dataDictionaryService = dataDictionaryService;
1850 }
1851
1852
1853
1854
1855
1856
1857 protected ExpressionEvaluatorService getExpressionEvaluatorService() {
1858 if (this.expressionEvaluatorService == null) {
1859 this.expressionEvaluatorService = KRADServiceLocatorWeb.getExpressionEvaluatorService();
1860 }
1861
1862 return this.expressionEvaluatorService;
1863 }
1864
1865
1866
1867
1868
1869
1870 public void setExpressionEvaluatorService(ExpressionEvaluatorService expressionEvaluatorService) {
1871 this.expressionEvaluatorService = expressionEvaluatorService;
1872 }
1873
1874
1875
1876
1877
1878
1879 public ViewDictionaryService getViewDictionaryService() {
1880 if (this.viewDictionaryService == null) {
1881 this.viewDictionaryService = KRADServiceLocatorWeb.getViewDictionaryService();
1882 }
1883 return this.viewDictionaryService;
1884 }
1885
1886
1887
1888
1889
1890
1891 public void setViewDictionaryService(ViewDictionaryService viewDictionaryService) {
1892 this.viewDictionaryService = viewDictionaryService;
1893 }
1894
1895
1896
1897
1898
1899
1900 public ConfigurationService getConfigurationService() {
1901 if (this.configurationService == null) {
1902 this.configurationService = KRADServiceLocator.getKualiConfigurationService();
1903 }
1904 return this.configurationService;
1905 }
1906
1907
1908
1909
1910
1911
1912 public void setConfigurationService(ConfigurationService configurationService) {
1913 this.configurationService = configurationService;
1914 }
1915 }