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;
17  
18  import java.util.HashMap;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.rice.krad.uif.UifConstants;
24  import org.kuali.rice.krad.uif.component.Component;
25  import org.kuali.rice.krad.uif.modifier.ComponentModifier;
26  
27  /**
28   * View lifecycle task to run component modifiers based on the lifecycle phase.
29   *
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   * @see org.kuali.rice.krad.uif.modifier.ComponentModifier
32   */
33  public class RunComponentModifiersTask extends ViewLifecycleTaskBase<Component> {
34  
35      /**
36       * Default constructor.
37       */
38      public RunComponentModifiersTask() {
39          super(Component.class);
40      }
41  
42      /**
43       * Runs any configured Component Modifiers for the given component that match the
44       * given run phase and who run condition evaluation succeeds.
45       *
46       * {@inheritDoc}
47       */
48      @Override
49      protected void performLifecycleTask() {
50          Component component = (Component) getElementState().getElement();
51  
52          List<ComponentModifier> componentModifiers = component.getComponentModifiers();
53          if (componentModifiers == null) {
54              return;
55          }
56  
57          Object model = ViewLifecycle.getModel();
58          String runPhase = getElementState().getViewPhase();
59          for (ComponentModifier modifier : component.getComponentModifiers()) {
60              if (!StringUtils.equals(modifier.getRunPhase(), runPhase)) {
61                  continue;
62              }
63  
64              boolean runCondition = true;
65              if (StringUtils.isNotBlank(modifier.getRunCondition())) {
66                  Map<String, Object> context = new HashMap<String, Object>();
67                  context.putAll(component.getContext());
68  
69                  if (context.isEmpty()) {
70                      context.putAll(ViewLifecycle.getView().getContext());
71                      context.put(UifConstants.ContextVariableNames.COMPONENT, component);
72                  }
73  
74                  String conditionEvaluation = ViewLifecycle.getExpressionEvaluator().evaluateExpressionTemplate(context,
75                          modifier.getRunCondition());
76                  runCondition = Boolean.parseBoolean(conditionEvaluation);
77              }
78  
79              if (runCondition) {
80                  modifier.performModification(model, component);
81              }
82          }
83      }
84  }