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 WorkflowTaskCompletionDateComparator extends OrderByComparator {
026    
027            public WorkflowTaskCompletionDateComparator(
028                    boolean ascending, String orderByAsc, String orderByDesc,
029                    String[] orderByFields) {
030    
031                    _ascending = ascending;
032                    _orderByAsc = orderByAsc;
033                    _orderByDesc = orderByDesc;
034                    _orderByFields = orderByFields;
035            }
036    
037            @Override
038            public int compare(Object obj1, Object obj2) {
039                    WorkflowTask workflowTask1 = (WorkflowTask)obj1;
040                    WorkflowTask workflowTask2 = (WorkflowTask)obj2;
041    
042                    Date completionDate1 = workflowTask1.getCompletionDate();
043                    Date completionDate2 = workflowTask2.getCompletionDate();
044    
045                    int value = completionDate1.compareTo(completionDate2);
046    
047                    if (value == 0) {
048                            Long workflowTaskId1 = workflowTask1.getWorkflowTaskId();
049                            Long workflowTaskId2 = workflowTask2.getWorkflowTaskId();
050    
051                            value = workflowTaskId1.compareTo(workflowTaskId2);
052                    }
053    
054                    if (_ascending) {
055                            return value;
056                    }
057                    else {
058                            return -value;
059                    }
060            }
061    
062            @Override
063            public String getOrderBy() {
064                    if (isAscending()) {
065                            return _orderByAsc;
066                    }
067                    else {
068                            return _orderByDesc;
069                    }
070            }
071    
072            @Override
073            public String[] getOrderByFields() {
074                    return _orderByFields;
075            }
076    
077            @Override
078            public boolean isAscending() {
079                    return _ascending;
080            }
081    
082            private boolean _ascending;
083            private String _orderByAsc;
084            private String _orderByDesc;
085            private String[] _orderByFields;
086    
087    }