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.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.mo.common.active.Inactivatable;
020import org.kuali.rice.krad.uif.view.View;
021import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
022
023import java.util.ArrayList;
024import java.util.List;
025
026/**
027 * Collection filter for maintenance groups that removes inactive lines if certain
028 * conditions are met
029 *
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 */
032public class MaintenanceActiveCollectionFilter implements CollectionFilter {
033    private static final long serialVersionUID = -6045332235106531456L;
034
035    private String oldBindingObjectPath;
036
037    /**
038     * Iterates through the collection and if the collection line type implements <code>Inactivatable</code>
039     * active indexes are added to the show indexes list
040     *
041     * <p>
042     * In the case of a new line being added, the user is not allowed to hide the record (even if it is inactive).
043     * Likewise in the case of an edit where the active flag has changed between the old and new side, the user
044     * is not allowed to hide
045     * </p>
046     *
047     * @see CollectionFilter#filter(org.kuali.rice.krad.uif.view.View, Object, org.kuali.rice.krad.uif.container.CollectionGroup)
048     */
049    @Override
050    public List<Integer> filter(View view, Object model, CollectionGroup collectionGroup) {
051
052        // get the collection for this group from the model
053        List<Object> newCollection =
054                ObjectPropertyUtils.getPropertyValue(model, collectionGroup.getBindingInfo().getBindingPath());
055
056        // Get collection from old data object
057        List<Object> oldCollection = null;
058        String oldCollectionBindingPath = null;
059        oldCollectionBindingPath = StringUtils.replaceOnce(collectionGroup.getBindingInfo().getBindingPath(),
060                    collectionGroup.getBindingInfo().getBindingObjectPath(), oldBindingObjectPath);
061        oldCollection = ObjectPropertyUtils.getPropertyValue(model, oldCollectionBindingPath);
062
063        // iterate through and add only active indexes
064        List<Integer> showIndexes = new ArrayList<Integer>();
065        for (int i = 0; i < newCollection.size(); i++) {
066            Object line = newCollection.get(i);
067            if (line instanceof Inactivatable) {
068                boolean active = ((Inactivatable) line).isActive();
069                if ((oldCollection != null) && (oldCollection.size() > i)) {
070                    // if active status has changed, show record
071                    Inactivatable oldLine = (Inactivatable) oldCollection.get(i);
072                    if (oldLine.isActive()) {
073                        showIndexes.add(i);
074                    }
075                } else {
076                    // TODO: if newly added line, show record
077                    // If only new and no old add the newline
078                    if (active) {
079                        showIndexes.add(i);
080                    }
081                }
082            }
083        }
084
085        return showIndexes;
086    }
087
088    /**
089     * Gives the binding path to the old data object for comparison, used to
090     * get the active status of the old object
091     *
092     * @return String binding path
093     */
094    public String getOldBindingObjectPath() {
095        return oldBindingObjectPath;
096    }
097
098    /**
099     * Setter for the path to the old data object
100     *
101     * @param oldBindingObjectPath
102     */
103    public void setOldBindingObjectPath(String oldBindingObjectPath) {
104        this.oldBindingObjectPath = oldBindingObjectPath;
105    }
106}