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