View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.uif.lifecycle.model;
17  
18  import java.util.List;
19  import java.util.UUID;
20  
21  import org.kuali.rice.krad.uif.component.BindingInfo;
22  import org.kuali.rice.krad.uif.component.Component;
23  import org.kuali.rice.krad.uif.component.ComponentSecurity;
24  import org.kuali.rice.krad.uif.component.DataBinding;
25  import org.kuali.rice.krad.uif.component.PropertyReplacer;
26  import org.kuali.rice.krad.uif.container.Container;
27  import org.kuali.rice.krad.uif.layout.LayoutManager;
28  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleTaskBase;
29  import org.kuali.rice.krad.uif.lifecycle.ApplyModelComponentPhase;
30  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
31  import org.kuali.rice.krad.uif.lifecycle.ViewLifecyclePhase;
32  import org.kuali.rice.krad.uif.modifier.ComponentModifier;
33  import org.kuali.rice.krad.uif.util.LifecycleElement;
34  import org.kuali.rice.krad.uif.view.ExpressionEvaluator;
35  import org.kuali.rice.krad.uif.view.View;
36  
37  /**
38   * Evaluate expressions for the component.
39   * 
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  public class EvaluateExpressionsTask extends ViewLifecycleTaskBase<Component> {
43  
44      /**
45       * Constructor.
46       * 
47       * @param phase The apply model phase for the component.
48       */
49      public EvaluateExpressionsTask(ViewLifecyclePhase phase) {
50          super(phase, Component.class);
51      }
52  
53      /**
54       * Checks against the visited ids to see if the id is duplicate, if so it is adjusted to make an
55       * unique id by appending an unique identifier
56       * 
57       * @param element The component or layout manager to adjust the ID on.
58       * @return original or adjusted id
59       */
60      public String adjustIdIfNecessary(LifecycleElement element) {
61          ApplyModelComponentPhase phase = (ApplyModelComponentPhase) getElementState();
62          String id = element.getId();
63          
64          if (phase.visit(element)) {
65              element.setId(id + '-' + UUID.randomUUID());
66              boolean visitedAfter = phase.visit(element);
67              assert !visitedAfter;
68          }
69  
70          return id;
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      protected void performLifecycleTask() {
78          ExpressionEvaluator expressionEvaluator = ViewLifecycle.getExpressionEvaluator();
79          View view = ViewLifecycle.getView();
80          Component component = (Component) getElementState().getElement();
81  
82          List<PropertyReplacer> componentPropertyReplacers = component.getPropertyReplacers();
83          if (componentPropertyReplacers != null) {
84              for (PropertyReplacer replacer : componentPropertyReplacers) {
85                  expressionEvaluator.evaluateExpressionsOnConfigurable(view, replacer, component.getContext());
86              }
87          }
88  
89          List<ComponentModifier> componentModifiers = component.getComponentModifiers();
90          if (componentModifiers != null) {
91              for (ComponentModifier modifier : componentModifiers) {
92                  expressionEvaluator.evaluateExpressionsOnConfigurable(view, modifier, component.getContext());
93              }
94          }
95  
96          expressionEvaluator.evaluateExpressionsOnConfigurable(view, component, component.getContext());
97          
98          // evaluate expressions on component security
99          ComponentSecurity componentSecurity = component.getComponentSecurity();
100         expressionEvaluator.evaluateExpressionsOnConfigurable(view, componentSecurity, component.getContext());
101 
102         // evaluate expressions on the binding info object
103         if (component instanceof DataBinding) {
104             BindingInfo bindingInfo = ((DataBinding) component).getBindingInfo();
105             expressionEvaluator.evaluateExpressionsOnConfigurable(view, bindingInfo, component.getContext());
106         }
107         
108         // set context evaluate expressions on the layout manager
109         if (component instanceof Container) {
110             LayoutManager layoutManager = ((Container) component).getLayoutManager();
111 
112             if (layoutManager != null) {
113                 expressionEvaluator.evaluateExpressionsOnConfigurable(view, layoutManager,
114                         layoutManager.getContext());
115 
116                 adjustIdIfNecessary(layoutManager);
117             }
118         }
119     }
120 
121 }