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.kew.actionlist.dao.impl;
017
018import java.util.Comparator;
019
020import org.kuali.rice.kew.actionitem.ActionItem;
021import org.kuali.rice.kew.actionitem.ActionItemComparator;
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 {
039
040        private ActionItemComparator itemComparator = new ActionItemComparator();
041        
042        public int compare(Object object1, Object object2) throws ClassCastException {
043                ActionItem actionItem1 = (ActionItem)object1;
044                ActionItem actionItem2 = (ActionItem)object2;
045                if (requiresComparison(actionItem1, actionItem2)) {
046                        return itemComparator.compare(object1, object2);
047                }
048                return 0;
049        }
050
051        /**
052         * Returns whether or not the two action items require comparison.  The Action List only operates 
053         * on Action Items for a single user and we only care about comparing the action items if they are
054         * on the same document and for the same user.
055         */
056        protected boolean requiresComparison(ActionItem actionItem1, ActionItem actionItem2) {
057                return actionItem1.getDocumentId().equals(actionItem2.getDocumentId()) &&
058                        actionItem1.getPrincipalId().equals(actionItem2.getPrincipalId());
059        }
060
061}