001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krad.uif.lifecycle; 017 018import java.util.HashMap; 019import java.util.List; 020import java.util.Map; 021 022import org.apache.commons.lang.StringUtils; 023import org.kuali.rice.krad.uif.UifConstants; 024import org.kuali.rice.krad.uif.component.Component; 025import org.kuali.rice.krad.uif.modifier.ComponentModifier; 026 027/** 028 * View lifecycle task to run component modifiers based on the lifecycle phase. 029 * 030 * @author Kuali Rice Team (rice.collab@kuali.org) 031 * @see org.kuali.rice.krad.uif.modifier.ComponentModifier 032 */ 033public class RunComponentModifiersTask extends ViewLifecycleTaskBase<Component> { 034 035 /** 036 * Default constructor. 037 */ 038 public RunComponentModifiersTask() { 039 super(Component.class); 040 } 041 042 /** 043 * Runs any configured Component Modifiers for the given component that match the 044 * given run phase and who run condition evaluation succeeds. 045 * 046 * {@inheritDoc} 047 */ 048 @Override 049 protected void performLifecycleTask() { 050 Component component = (Component) getElementState().getElement(); 051 052 List<ComponentModifier> componentModifiers = component.getComponentModifiers(); 053 if (componentModifiers == null) { 054 return; 055 } 056 057 Object model = ViewLifecycle.getModel(); 058 String runPhase = getElementState().getViewPhase(); 059 for (ComponentModifier modifier : component.getComponentModifiers()) { 060 if (!StringUtils.equals(modifier.getRunPhase(), runPhase)) { 061 continue; 062 } 063 064 boolean runCondition = true; 065 if (StringUtils.isNotBlank(modifier.getRunCondition())) { 066 Map<String, Object> context = new HashMap<String, Object>(); 067 context.putAll(component.getContext()); 068 069 if (context.isEmpty()) { 070 context.putAll(ViewLifecycle.getView().getContext()); 071 context.put(UifConstants.ContextVariableNames.COMPONENT, component); 072 } 073 074 String conditionEvaluation = ViewLifecycle.getExpressionEvaluator().evaluateExpressionTemplate(context, 075 modifier.getRunCondition()); 076 runCondition = Boolean.parseBoolean(conditionEvaluation); 077 } 078 079 if (runCondition) { 080 modifier.performModification(model, component); 081 } 082 } 083 } 084}