001    /**
002     * Copyright (c) 2000-2013 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 final String ORDER_BY_ASC = "dueDate ASC, workflowTaskId ASC";
028    
029            public static final String ORDER_BY_DESC =
030                    "dueDate DESC, workflowTaskId DESC";
031    
032            public static final String[] ORDER_BY_FIELDS =
033                    {"dueDate", "workflowTaskId"};
034    
035            public BaseWorkflowTaskDueDateComparator() {
036                    this(false);
037            }
038    
039            public BaseWorkflowTaskDueDateComparator(boolean ascending) {
040                    _ascending = ascending;
041            }
042    
043            @Override
044            public int compare(Object obj1, Object obj2) {
045                    WorkflowTask workflowTask1 = (WorkflowTask)obj1;
046                    WorkflowTask workflowTask2 = (WorkflowTask)obj2;
047    
048                    Date dueDate1 = workflowTask1.getDueDate();
049                    Date dueDate2 = workflowTask2.getDueDate();
050    
051                    int value = dueDate1.compareTo(dueDate2);
052    
053                    if (value == 0) {
054                            Long workflowTaskId1 = workflowTask1.getWorkflowTaskId();
055                            Long workflowTaskId2 = workflowTask2.getWorkflowTaskId();
056    
057                            value = workflowTaskId1.compareTo(workflowTaskId2);
058                    }
059    
060                    if (_ascending) {
061                            return value;
062                    }
063                    else {
064                            return -value;
065                    }
066            }
067    
068            @Override
069            public String getOrderBy() {
070                    if (_ascending) {
071                            return ORDER_BY_ASC;
072                    }
073                    else {
074                            return ORDER_BY_DESC;
075                    }
076            }
077    
078            @Override
079            public String[] getOrderByFields() {
080                    return ORDER_BY_FIELDS;
081            }
082    
083            @Override
084            public boolean isAscending() {
085                    return _ascending;
086            }
087    
088            private boolean _ascending;
089    
090    }