001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }