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.WorkflowInstance;
019    
020    import java.util.Date;
021    
022    /**
023     * @author Shuyang Zhou
024     */
025    public class WorkflowInstanceStartDateComparator
026            extends OrderByComparator<WorkflowInstance> {
027    
028            public WorkflowInstanceStartDateComparator(
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(
040                    WorkflowInstance workflowInstance1,
041                    WorkflowInstance workflowInstance2) {
042    
043                    Date startDate1 = workflowInstance1.getStartDate();
044                    Date startDate2 = workflowInstance2.getStartDate();
045    
046                    int value = startDate1.compareTo(startDate2);
047    
048                    if (value == 0) {
049                            Long workflowInstanceId1 =
050                                    workflowInstance1.getWorkflowInstanceId();
051                            Long workflowInstanceId2 =
052                                    workflowInstance2.getWorkflowInstanceId();
053    
054                            value = workflowInstanceId1.compareTo(workflowInstanceId2);
055                    }
056    
057                    if (_ascending) {
058                            return value;
059                    }
060                    else {
061                            return -value;
062                    }
063            }
064    
065            @Override
066            public String getOrderBy() {
067                    if (isAscending()) {
068                            return _orderByAsc;
069                    }
070                    else {
071                            return _orderByDesc;
072                    }
073            }
074    
075            @Override
076            public String[] getOrderByFields() {
077                    return _orderByFields;
078            }
079    
080            @Override
081            public boolean isAscending() {
082                    return _ascending;
083            }
084    
085            private final boolean _ascending;
086            private final String _orderByAsc;
087            private final String _orderByDesc;
088            private final String[] _orderByFields;
089    
090    }