001/**
002 * Copyright 2005-2018 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.container;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.kuali.rice.krad.datadictionary.Copyable;
024import org.kuali.rice.krad.datadictionary.parse.BeanTag;
025import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
026import org.kuali.rice.krad.uif.UifConstants;
027import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
028import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
029import org.kuali.rice.krad.uif.view.ExpressionEvaluator;
030import org.kuali.rice.krad.uif.view.View;
031
032/**
033 * Collection filter that evaluates a configured el expression against each line
034 *
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 */
037@BeanTag(name = "conditionalFilter", parent = "Uif-ConditionalCollectionFilter")
038public class ELCollectionFilter implements CollectionFilter, Copyable {
039    private static final long serialVersionUID = 3273495753269940272L;
040
041    private String expression = "";
042
043    /**
044     * Iterates through the collection and evaluates the el expression in context of the line. If the expression
045     * evaluates to true, the line will remain, else be filtered out
046     *
047     * @see org.kuali.rice.krad.uif.container.CollectionFilter#filter(org.kuali.rice.krad.uif.view.View, Object,
048     *      CollectionGroup)
049     */
050    @Override
051    public List<Integer> filter(View view, Object model, CollectionGroup collectionGroup) {
052        // get the collection for this group from the model
053        List<Object> modelCollection = ObjectPropertyUtils.getPropertyValue(model,
054                collectionGroup.getBindingInfo().getBindingPath());
055
056        ExpressionEvaluator expressionEvaluator = ViewLifecycle.getExpressionEvaluator();
057
058        // iterate through and add index that pass the expression
059        List<Integer> showIndexes = new ArrayList<Integer>();
060
061        int lineIndex = 0;
062        for (Object line : modelCollection) {
063            Map<String, Object> context = new HashMap<String, Object>(collectionGroup.getContext());
064            context.put(UifConstants.ContextVariableNames.LINE, line);
065            context.put(UifConstants.ContextVariableNames.INDEX, lineIndex);
066
067            Boolean conditionPasses = (Boolean) expressionEvaluator.evaluateExpression(context, expression);
068            if (conditionPasses) {
069                showIndexes.add(lineIndex);
070            }
071
072            lineIndex++;
073        }
074
075        return showIndexes;
076    }
077
078    /**
079     * Expression that will be evaluated for each line to determine whether the line should be filtered
080     *
081     * <p>
082     * If expression passes, the line will remain in the collection, otherwise be filtered out. The expression given
083     * should evaluate to a boolean
084     * </p>
085     *
086     * @return valid el expression that evaluates to a boolean
087     */
088    @BeanTagAttribute
089    public String getExpression() {
090        return expression;
091    }
092
093    /**
094     * Setter for the expression to use for filtering
095     *
096     * @param expression
097     */
098    public void setExpression(String expression) {
099        this.expression = expression;
100    }
101
102    /**
103     * @see Copyable#clone()
104     */
105    @Override
106    public ELCollectionFilter clone() throws CloneNotSupportedException {
107        return (ELCollectionFilter) super.clone();
108    }
109
110}