001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.util.comparator;
016    
017    import com.liferay.portal.kernel.util.OrderByComparator;
018    import com.liferay.portal.model.Layout;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     */
023    public class LayoutComparator extends OrderByComparator {
024    
025            public static String ORDER_BY_ASC = "groupId ASC, layoutId ASC";
026    
027            public static String ORDER_BY_DESC = "groupId DESC, layoutId DESC";
028    
029            public static String[] ORDER_BY_FIELDS = {"groupId", "layoutId"};
030    
031            public LayoutComparator() {
032                    this(false);
033            }
034    
035            public LayoutComparator(boolean ascending) {
036                    _ascending = ascending;
037            }
038    
039            @Override
040            public int compare(Object obj1, Object obj2) {
041                    Layout layout1 = (Layout)obj1;
042                    Layout layout2 = (Layout)obj2;
043    
044                    Long groupId1 = new Long(layout1.getGroupId());
045                    Long groupId2 = new Long(layout2.getGroupId());
046    
047                    int value = groupId1.compareTo(groupId2);
048    
049                    if (value != 0) {
050                            if (_ascending) {
051                                    return value;
052                            }
053                            else {
054                                    return -value;
055                            }
056                    }
057    
058                    Long layoutId1 = new Long(layout1.getLayoutId());
059                    Long layoutId2 = new Long(layout2.getLayoutId());
060    
061                    value = layoutId1.compareTo(layoutId2);
062    
063                    if (_ascending) {
064                            return value;
065                    }
066                    else {
067                            return -value;
068                    }
069            }
070    
071            @Override
072            public String getOrderBy() {
073                    if (_ascending) {
074                            return ORDER_BY_ASC;
075                    }
076                    else {
077                            return ORDER_BY_DESC;
078                    }
079            }
080    
081            @Override
082            public String[] getOrderByFields() {
083                    return ORDER_BY_FIELDS;
084            }
085    
086            @Override
087            public boolean isAscending() {
088                    return _ascending;
089            }
090    
091            private boolean _ascending;
092    
093    }