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    /**
021     * @author In??cio Nery
022     */
023    public class WorkflowInstanceCompletedComparator
024            extends OrderByComparator<WorkflowInstance> {
025    
026            public WorkflowInstanceCompletedComparator(
027                    boolean ascending, String orderByAsc, String orderByDesc,
028                    String[] orderByFields) {
029    
030                    _ascending = ascending;
031                    _orderByAsc = orderByAsc;
032                    _orderByDesc = orderByDesc;
033                    _orderByFields = orderByFields;
034            }
035    
036            @Override
037            public int compare(
038                    WorkflowInstance workflowInstance1,
039                    WorkflowInstance workflowInstance2) {
040    
041                    Boolean complete1 = workflowInstance1.isComplete();
042                    Boolean complete2 = workflowInstance2.isComplete();
043    
044                    int value = complete1.compareTo(complete2);
045    
046                    if (value == 0) {
047                            Long workflowInstanceId1 =
048                                    workflowInstance1.getWorkflowInstanceId();
049                            Long workflowInstanceId2 =
050                                    workflowInstance2.getWorkflowInstanceId();
051    
052                            value = workflowInstanceId1.compareTo(workflowInstanceId2);
053                    }
054    
055                    if (_ascending) {
056                            return value;
057                    }
058                    else {
059                            return -value;
060                    }
061            }
062    
063            @Override
064            public String getOrderBy() {
065                    if (isAscending()) {
066                            return _orderByAsc;
067                    }
068                    else {
069                            return _orderByDesc;
070                    }
071            }
072    
073            @Override
074            public String[] getOrderByFields() {
075                    return _orderByFields;
076            }
077    
078            @Override
079            public boolean isAscending() {
080                    return _ascending;
081            }
082    
083            private final boolean _ascending;
084            private final String _orderByAsc;
085            private final String _orderByDesc;
086            private final String[] _orderByFields;
087    
088    }