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.core.api.mo.common.active.Inactivatable;
019import org.kuali.rice.krad.uif.view.View;
020import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
021
022import java.util.ArrayList;
023import java.util.List;
024
025/**
026 * Collection filter that removes inactive lines from a collection whose line types
027 * implement the <code>Inactivatable</code> interface
028 *
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031public class ActiveCollectionFilter implements CollectionFilter {
032    private static final long serialVersionUID = 3273495753269940272L;
033
034    /**
035     * Iterates through the collection and if the collection line type implements <code>Inactivatable</code>,
036     * active indexes are added to the show indexes list
037     *
038     * @see CollectionFilter#filter(org.kuali.rice.krad.uif.view.View, Object, org.kuali.rice.krad.uif.container.CollectionGroup)
039     */
040    @Override
041    public List<Integer> filter(View view, Object model, CollectionGroup collectionGroup) {
042        // get the collection for this group from the model
043        List<Object> modelCollection =
044                ObjectPropertyUtils.getPropertyValue(model, collectionGroup.getBindingInfo().getBindingPath());
045
046        // iterate through and add only active indexes
047        List<Integer> showIndexes = new ArrayList<Integer>();
048        if (modelCollection != null) {
049            int lineIndex = 0;
050            for (Object line : modelCollection) {
051                if (line instanceof Inactivatable) {
052                    boolean active = ((Inactivatable) line).isActive();
053                    if (active) {
054                        showIndexes.add(lineIndex);
055                    }
056                }
057                lineIndex++;
058            }
059        }
060
061        return showIndexes;
062    }
063}