001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.workflow.comparator;
016    
017    import com.liferay.portal.kernel.util.OrderByComparator;
018    import com.liferay.portal.kernel.workflow.WorkflowTask;
019    
020    import java.util.Date;
021    
022    /**
023     * @author Shuyang Zhou
024     */
025    public class BaseWorkflowTaskDueDateComparator extends OrderByComparator {
026    
027            public static String ORDER_BY_ASC = "dueDate ASC, workflowTaskId ASC";
028    
029            public static String ORDER_BY_DESC = "dueDate DESC, workflowTaskId DESC";
030    
031            public static String[] ORDER_BY_FIELDS = {"dueDate", "workflowTaskId"};
032    
033            public BaseWorkflowTaskDueDateComparator() {
034                    this(false);
035            }
036    
037            public BaseWorkflowTaskDueDateComparator(boolean ascending) {
038                    _ascending = ascending;
039            }
040    
041            public int compare(Object obj1, Object obj2) {
042                    WorkflowTask workflowTask1 = (WorkflowTask)obj1;
043                    WorkflowTask workflowTask2 = (WorkflowTask)obj2;
044    
045                    Date dueDate1 = workflowTask1.getDueDate();
046                    Date dueDate2 = workflowTask2.getDueDate();
047    
048                    int value = dueDate1.compareTo(dueDate2);
049    
050                    if (value == 0) {
051                            Long workflowTaskId1 = workflowTask1.getWorkflowTaskId();
052                            Long workflowTaskId2 = workflowTask2.getWorkflowTaskId();
053    
054                            value = workflowTaskId1.compareTo(workflowTaskId2);
055                    }
056    
057                    if (_ascending) {
058                            return value;
059                    }
060                    else {
061                            return -value;
062                    }
063            }
064    
065            public String getOrderBy() {
066                    if (_ascending) {
067                            return ORDER_BY_ASC;
068                    }
069                    else {
070                            return ORDER_BY_DESC;
071                    }
072            }
073    
074            public String[] getOrderByFields() {
075                    return ORDER_BY_FIELDS;
076            }
077    
078            public boolean isAscending() {
079                    return _ascending;
080            }
081    
082            private boolean _ascending;
083    
084    }