001/**
002 * Copyright 2005-2017 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.kew.actionlist.dao.impl;
017
018import java.util.Comparator;
019
020import org.kuali.rice.kew.actionitem.ActionItemComparator;
021import org.kuali.rice.kew.api.action.ActionItemContract;
022
023
024/**
025 * Compares an action item to another action item and determines if one
026 * item has a higher priority than the other.
027 * Therefore, calling code needs to ensure that the document and user on the item
028 * are the same.  If action items for different documents are passed in, then the
029 * compare method should always return 0.
030 *
031 * If the response returned from compare is less than 0, it means the first argument is
032 * lower priority than the second.  If a value greater than 0 is returned it means the
033 * first argument has a higher priority then the second.  If the result returned is 0, then
034 * the two action items have the same priority.
035 *
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 */
038public class ActionListPriorityComparator implements Comparator<ActionItemContract> {
039
040        private ActionItemComparator itemComparator = new ActionItemComparator();
041
042        @Override
043    public int compare(ActionItemContract actionItem1, ActionItemContract actionItem2) throws ClassCastException {
044                if (requiresComparison(actionItem1, actionItem2)) {
045                        return itemComparator.compare(actionItem1, actionItem2);
046                }
047                return 0;
048        }
049
050        /**
051         * Returns whether or not the two action items require comparison.  The Action List only operates
052         * on Action Items for a single user and we only care about comparing the action items if they are
053         * on the same document and for the same user.
054         */
055        protected boolean requiresComparison(ActionItemContract actionItem1, ActionItemContract actionItem2) {
056                return actionItem1.getDocumentId().equals(actionItem2.getDocumentId()) &&
057                        actionItem1.getPrincipalId().equals(actionItem2.getPrincipalId());
058        }
059
060}