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.model;
017
018import java.util.List;
019import java.util.UUID;
020
021import org.kuali.rice.krad.uif.component.BindingInfo;
022import org.kuali.rice.krad.uif.component.Component;
023import org.kuali.rice.krad.uif.component.ComponentSecurity;
024import org.kuali.rice.krad.uif.component.DataBinding;
025import org.kuali.rice.krad.uif.component.PropertyReplacer;
026import org.kuali.rice.krad.uif.container.Container;
027import org.kuali.rice.krad.uif.layout.LayoutManager;
028import org.kuali.rice.krad.uif.lifecycle.ApplyModelComponentPhase;
029import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
030import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleTaskBase;
031import org.kuali.rice.krad.uif.modifier.ComponentModifier;
032import org.kuali.rice.krad.uif.util.LifecycleElement;
033import org.kuali.rice.krad.uif.view.ExpressionEvaluator;
034import org.kuali.rice.krad.uif.view.View;
035
036/**
037 * Evaluate expressions for the component.
038 * 
039 * @author Kuali Rice Team (rice.collab@kuali.org)
040 */
041public class EvaluateExpressionsTask extends ViewLifecycleTaskBase<Component> {
042
043    /**
044     * Default constructor.
045     */
046    public EvaluateExpressionsTask() {
047        super(Component.class);
048    }
049
050    /**
051     * Checks against the visited ids to see if the id is duplicate, if so it is adjusted to make an
052     * unique id by appending an unique identifier
053     * 
054     * @param element The component or layout manager to adjust the ID on.
055     * @return original or adjusted id
056     */
057    public String adjustIdIfNecessary(LifecycleElement element) {
058        ApplyModelComponentPhase phase = (ApplyModelComponentPhase) getElementState();
059        String id = element.getId();
060        
061        if (phase.visit(element)) {
062            element.setId(id + '-' + UUID.randomUUID());
063            boolean visitedAfter = phase.visit(element);
064            assert !visitedAfter;
065        }
066
067        return id;
068    }
069
070    /**
071     * {@inheritDoc}
072     */
073    @Override
074    protected void performLifecycleTask() {
075        ExpressionEvaluator expressionEvaluator = ViewLifecycle.getExpressionEvaluator();
076        View view = ViewLifecycle.getView();
077        Component component = (Component) getElementState().getElement();
078
079        List<PropertyReplacer> componentPropertyReplacers = component.getPropertyReplacers();
080        if (componentPropertyReplacers != null) {
081            for (PropertyReplacer replacer : componentPropertyReplacers) {
082                expressionEvaluator.evaluateExpressionsOnConfigurable(view, replacer, component.getContext());
083            }
084        }
085
086        List<ComponentModifier> componentModifiers = component.getComponentModifiers();
087        if (componentModifiers != null) {
088            for (ComponentModifier modifier : componentModifiers) {
089                expressionEvaluator.evaluateExpressionsOnConfigurable(view, modifier, component.getContext());
090            }
091        }
092
093        expressionEvaluator.evaluateExpressionsOnConfigurable(view, component, component.getContext());
094        
095        // evaluate expressions on component security
096        ComponentSecurity componentSecurity = component.getComponentSecurity();
097        expressionEvaluator.evaluateExpressionsOnConfigurable(view, componentSecurity, component.getContext());
098
099        // evaluate expressions on the binding info object
100        if (component instanceof DataBinding) {
101            BindingInfo bindingInfo = ((DataBinding) component).getBindingInfo();
102            expressionEvaluator.evaluateExpressionsOnConfigurable(view, bindingInfo, component.getContext());
103        }
104        
105        // set context evaluate expressions on the layout manager
106        if (component instanceof Container) {
107            LayoutManager layoutManager = ((Container) component).getLayoutManager();
108
109            if (layoutManager != null) {
110                expressionEvaluator.evaluateExpressionsOnConfigurable(view, layoutManager,
111                        layoutManager.getContext());
112
113                adjustIdIfNecessary(layoutManager);
114            }
115        }
116    }
117
118}