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 */
32 public class RunComponentModifiersTask extends ViewLifecycleTaskBase<Component> {
33
34 /**
35 * Constructor.
36 *
37 * @param phase The lifecycle phase to run component modifiers for.
38 */
39 public RunComponentModifiersTask(ViewLifecyclePhase phase) {
40 super(phase, Component.class);
41 }
42
43 /**
44 * Runs any configured <code>ComponentModifiers</code> for the given component that match the
45 * given run phase and who run condition evaluation succeeds
46 *
47 * <p>
48 * If called during the initialize phase, the performInitialization method will be invoked on
49 * the <code>ComponentModifier</code> before running
50 * </p>
51 *
52 * {@inheritDoc}
53 */
54 @Override
55 protected void performLifecycleTask() {
56 Component component = (Component) getElementState().getElement();
57
58 List<ComponentModifier> componentModifiers = component.getComponentModifiers();
59 if (componentModifiers == null) {
60 return;
61 }
62
63 Object model = ViewLifecycle.getModel();
64 String runPhase = getElementState().getViewPhase();
65 for (ComponentModifier modifier : component.getComponentModifiers()) {
66 // if run phase is initialize, invoke initialize method on modifier first
67 if (StringUtils.equals(runPhase, UifConstants.ViewPhases.INITIALIZE)) {
68 modifier.performInitialization(model, component);
69 }
70
71 // check run phase matches
72 if (StringUtils.equals(modifier.getRunPhase(), runPhase)) {
73 // check condition (if set) evaluates to true
74 boolean runModifier = true;
75 if (StringUtils.isNotBlank(modifier.getRunCondition())) {
76 Map<String, Object> context = new HashMap<String, Object>();
77 context.put(UifConstants.ContextVariableNames.COMPONENT, component);
78 context.put(UifConstants.ContextVariableNames.VIEW, ViewLifecycle.getView());
79
80 String conditionEvaluation = ViewLifecycle.getExpressionEvaluator()
81 .evaluateExpressionTemplate(context, modifier.getRunCondition());
82 runModifier = Boolean.parseBoolean(conditionEvaluation);
83 }
84
85 if (runModifier) {
86 modifier.performModification(model, component);
87 }
88 }
89 }
90 }
91
92 }