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.WorkflowDefinition;
019    
020    /**
021     * @author Shuyang Zhou
022     */
023    public class WorkflowDefinitionNameComparator
024            extends OrderByComparator<WorkflowDefinition> {
025    
026            public WorkflowDefinitionNameComparator(
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                    WorkflowDefinition workflowDefinition1,
039                    WorkflowDefinition workflowDefinition2) {
040    
041                    String name1 = workflowDefinition1.getName();
042                    String name2 = workflowDefinition2.getName();
043    
044                    int value = name1.compareTo(name2);
045    
046                    if (value == 0) {
047                            Integer version1 = workflowDefinition1.getVersion();
048                            Integer version2 = workflowDefinition2.getVersion();
049    
050                            value = version1.compareTo(version2);
051                    }
052    
053                    if (_ascending) {
054                            return value;
055                    }
056                    else {
057                            return -value;
058                    }
059            }
060    
061            @Override
062            public String getOrderBy() {
063                    if (isAscending()) {
064                            return _orderByAsc;
065                    }
066                    else {
067                            return _orderByDesc;
068                    }
069            }
070    
071            @Override
072            public String[] getOrderByFields() {
073                    return _orderByFields;
074            }
075    
076            @Override
077            public boolean isAscending() {
078                    return _ascending;
079            }
080    
081            private final boolean _ascending;
082            private final String _orderByAsc;
083            private final String _orderByDesc;
084            private final String[] _orderByFields;
085    
086    }