| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ELCollectionFilter |
|
| 1.5;1.5 |
| 1 | /** | |
| 2 | * Copyright 2005-2011 The Kuali Foundation | |
| 3 | * | |
| 4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.opensource.org/licenses/ecl2.php | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | package org.kuali.rice.krad.uif.container; | |
| 17 | ||
| 18 | import org.kuali.rice.krad.service.KRADServiceLocatorWeb; | |
| 19 | import org.kuali.rice.krad.uif.UifConstants; | |
| 20 | import org.kuali.rice.krad.uif.service.ExpressionEvaluatorService; | |
| 21 | import org.kuali.rice.krad.uif.util.ObjectPropertyUtils; | |
| 22 | import org.kuali.rice.krad.uif.view.View; | |
| 23 | ||
| 24 | import java.util.ArrayList; | |
| 25 | import java.util.HashMap; | |
| 26 | import java.util.List; | |
| 27 | import java.util.Map; | |
| 28 | ||
| 29 | /** | |
| 30 | * Collection filter that evaluates a configured el expression against each line | |
| 31 | * | |
| 32 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
| 33 | */ | |
| 34 | 0 | public class ELCollectionFilter implements CollectionFilter { |
| 35 | private static final long serialVersionUID = 3273495753269940272L; | |
| 36 | ||
| 37 | 0 | private String expression = ""; |
| 38 | ||
| 39 | /** | |
| 40 | * Iterates through the collection and evaluates the el expression in context of the line. If the expression | |
| 41 | * evaluates to true, the line will remain, else be filtered out | |
| 42 | * | |
| 43 | * @see org.kuali.rice.krad.uif.container.CollectionFilter#filter(org.kuali.rice.krad.uif.view.View, Object, | |
| 44 | * CollectionGroup) | |
| 45 | */ | |
| 46 | @Override | |
| 47 | public List<Integer> filter(View view, Object model, CollectionGroup collectionGroup) { | |
| 48 | // get the collection for this group from the model | |
| 49 | 0 | List<Object> modelCollection = ObjectPropertyUtils.getPropertyValue(model, |
| 50 | collectionGroup.getBindingInfo().getBindingPath()); | |
| 51 | ||
| 52 | // iterate through and add index that pass the expression | |
| 53 | 0 | List<Integer> showIndexes = new ArrayList<Integer>(); |
| 54 | ||
| 55 | 0 | int lineIndex = 0; |
| 56 | 0 | for (Object line : modelCollection) { |
| 57 | 0 | Map<String, Object> context = new HashMap<String, Object>(collectionGroup.getContext()); |
| 58 | 0 | context.put(UifConstants.ContextVariableNames.LINE, line); |
| 59 | ||
| 60 | 0 | Boolean conditionPasses = (Boolean) getExpressionEvaluatorService().evaluateExpression(model, context, |
| 61 | expression); | |
| 62 | 0 | if (conditionPasses) { |
| 63 | 0 | showIndexes.add(lineIndex); |
| 64 | } | |
| 65 | ||
| 66 | 0 | lineIndex++; |
| 67 | 0 | } |
| 68 | ||
| 69 | 0 | return showIndexes; |
| 70 | } | |
| 71 | ||
| 72 | /** | |
| 73 | * Expression that will be evaluated for each line to determine whether the line should be filtered | |
| 74 | * | |
| 75 | * <p> | |
| 76 | * If expression passes, the line will remain in the collection, otherwise be filtered out. The expression given | |
| 77 | * should evaluate to a boolean | |
| 78 | * </p> | |
| 79 | * | |
| 80 | * @return String valid el expression that evaluates to a boolean. | |
| 81 | */ | |
| 82 | public String getExpression() { | |
| 83 | 0 | return expression; |
| 84 | } | |
| 85 | ||
| 86 | /** | |
| 87 | * Setter for the expression to use for filtering | |
| 88 | * | |
| 89 | * @param expression | |
| 90 | */ | |
| 91 | public void setExpression(String expression) { | |
| 92 | 0 | this.expression = expression; |
| 93 | 0 | } |
| 94 | ||
| 95 | public ExpressionEvaluatorService getExpressionEvaluatorService() { | |
| 96 | 0 | return KRADServiceLocatorWeb.getExpressionEvaluatorService(); |
| 97 | } | |
| 98 | } |