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