001    /**
002     * Copyright 2005-2012 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     */
016    package org.kuali.rice.krad.uif.container;
017    
018    import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
019    import org.kuali.rice.krad.uif.UifConstants;
020    import org.kuali.rice.krad.uif.service.ExpressionEvaluatorService;
021    import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
022    import org.kuali.rice.krad.uif.view.View;
023    
024    import java.util.ArrayList;
025    import java.util.HashMap;
026    import java.util.List;
027    import 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     */
034    public 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                context.put(UifConstants.ContextVariableNames.INDEX, lineIndex);
060    
061                Boolean conditionPasses = (Boolean) getExpressionEvaluatorService().evaluateExpression(model, context,
062                        expression);
063                if (conditionPasses) {
064                    showIndexes.add(lineIndex);
065                }
066    
067                lineIndex++;
068            }
069    
070            return showIndexes;
071        }
072    
073        /**
074         * Expression that will be evaluated for each line to determine whether the line should be filtered
075         *
076         * <p>
077         * If expression passes, the line will remain in the collection, otherwise be filtered out. The expression given
078         * should evaluate to a boolean
079         * </p>
080         *
081         * @return String valid el expression that evaluates to a boolean.
082         */
083        public String getExpression() {
084            return expression;
085        }
086    
087        /**
088         * Setter for the expression to use for filtering
089         *
090         * @param expression
091         */
092        public void setExpression(String expression) {
093            this.expression = expression;
094        }
095    
096        public ExpressionEvaluatorService getExpressionEvaluatorService() {
097            return KRADServiceLocatorWeb.getExpressionEvaluatorService();
098        }
099    }