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    /**
021     * @author Brian Wing Shun Chan
022     */
023    public class WorkflowInstanceStateComparator extends OrderByComparator {
024    
025            public WorkflowInstanceStateComparator(
026                    boolean ascending, String orderByAsc, String orderByDesc,
027                    String[] orderByFields) {
028    
029                    _ascending = ascending;
030                    _orderByAsc = orderByAsc;
031                    _orderByDesc = orderByDesc;
032                    _orderByFields = orderByFields;
033            }
034    
035            @Override
036            public int compare(Object obj1, Object obj2) {
037                    WorkflowInstance workflowInstance1 = (WorkflowInstance)obj1;
038                    WorkflowInstance workflowInstance2 = (WorkflowInstance)obj2;
039    
040                    String state1 = workflowInstance1.getState();
041                    String state2 = workflowInstance2.getState();
042    
043                    int value = state1.compareTo(state2);
044    
045                    if (value == 0) {
046                            Long workflowInstanceId1 =
047                                    workflowInstance1.getWorkflowInstanceId();
048                            Long workflowInstanceId2 =
049                                    workflowInstance2.getWorkflowInstanceId();
050    
051                            value = workflowInstanceId1.compareTo(workflowInstanceId2);
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    }