001    /**
002     * Copyright (c) 2000-2011 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            @Override
042            public int compare(Object obj1, Object obj2) {
043                    WorkflowTask workflowTask1 = (WorkflowTask)obj1;
044                    WorkflowTask workflowTask2 = (WorkflowTask)obj2;
045    
046                    Date dueDate1 = workflowTask1.getDueDate();
047                    Date dueDate2 = workflowTask2.getDueDate();
048    
049                    int value = dueDate1.compareTo(dueDate2);
050    
051                    if (value == 0) {
052                            Long workflowTaskId1 = workflowTask1.getWorkflowTaskId();
053                            Long workflowTaskId2 = workflowTask2.getWorkflowTaskId();
054    
055                            value = workflowTaskId1.compareTo(workflowTaskId2);
056                    }
057    
058                    if (_ascending) {
059                            return value;
060                    }
061                    else {
062                            return -value;
063                    }
064            }
065    
066            @Override
067            public String getOrderBy() {
068                    if (_ascending) {
069                            return ORDER_BY_ASC;
070                    }
071                    else {
072                            return ORDER_BY_DESC;
073                    }
074            }
075    
076            @Override
077            public String[] getOrderByFields() {
078                    return ORDER_BY_FIELDS;
079            }
080    
081            @Override
082            public boolean isAscending() {
083                    return _ascending;
084            }
085    
086            private boolean _ascending;
087    
088    }