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.portlet.dynamicdatamapping.util.comparator;
016    
017    import com.liferay.portal.kernel.util.OrderByComparator;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
020    
021    /**
022     * @author Miguel Pastor
023     */
024    public class StructureStructureKeyComparator
025            extends OrderByComparator<DDMStructure> {
026    
027            public static final String ORDER_BY_ASC = "DDMStructure.structureKey ASC";
028    
029            public static final String ORDER_BY_DESC = "DDMStructure.structureKey DESC";
030    
031            public static final String[] ORDER_BY_FIELDS = {"structureKey"};
032    
033            public StructureStructureKeyComparator() {
034                    this(false);
035            }
036    
037            public StructureStructureKeyComparator(boolean ascending) {
038                    _ascending = ascending;
039            }
040    
041            @Override
042            public int compare(DDMStructure structure1, DDMStructure structure2) {
043                    String structureKey1 = structure1.getStructureKey();
044    
045                    structureKey1 = StringUtil.toLowerCase(structureKey1);
046    
047                    String structureKey2 = structure2.getStructureKey();
048    
049                    structureKey2 = StringUtil.toLowerCase(structureKey2);
050    
051                    int value = structureKey1.compareTo(structureKey2);
052    
053                    if (_ascending) {
054                            return value;
055                    }
056                    else {
057                            return -value;
058                    }
059            }
060    
061            @Override
062            public String getOrderBy() {
063                    if (_ascending) {
064                            return ORDER_BY_ASC;
065                    }
066                    else {
067                            return ORDER_BY_DESC;
068                    }
069            }
070    
071            @Override
072            public String[] getOrderByFields() {
073                    return ORDER_BY_FIELDS;
074            }
075    
076            @Override
077            public boolean isAscending() {
078                    return _ascending;
079            }
080    
081            private final boolean _ascending;
082    
083    }