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