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