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.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     * @author Daniel Reuther
023     */
024    public class LayoutPriorityComparator extends OrderByComparator {
025    
026            public static final String ORDER_BY_ASC = "Layout.priority ASC";
027    
028            public static final String ORDER_BY_DESC = "Layout.priority DESC";
029    
030            public static final String[] ORDER_BY_FIELDS = {"priority"};
031    
032            public LayoutPriorityComparator() {
033                    this(true);
034            }
035    
036            public LayoutPriorityComparator(boolean ascending) {
037                    _ascending = ascending;
038            }
039    
040            public LayoutPriorityComparator(Layout layout, boolean lessThan) {
041                    _layout = layout;
042                    _lessThan = lessThan;
043    
044                    _ascending = true;
045            }
046    
047            @Override
048            public int compare(Object obj1, Object obj2) {
049                    Layout layout1 = (Layout)obj1;
050                    Layout layout2 = (Layout)obj2;
051    
052                    int value = 0;
053    
054                    int priority1 = layout1.getPriority();
055                    int priority2 = layout2.getPriority();
056    
057                    if (priority1 > priority2) {
058                            value = 1;
059                    }
060                    else if (priority1 < priority2) {
061                            value = -1;
062                    }
063                    else {
064                            if (_layout != null) {
065                                    if (_layout.equals(layout1)) {
066                                            if (_lessThan) {
067                                                    value = 1;
068                                            }
069                                            else {
070                                                    value = -1;
071                                            }
072                                    }
073                                    else if (_layout.equals(layout2)) {
074                                            if (_lessThan) {
075                                                    value = -1;
076                                            }
077                                            else {
078                                                    value = 1;
079                                            }
080                                    }
081                            }
082                    }
083    
084                    if (_ascending) {
085                            return value;
086                    }
087                    else {
088                            return -value;
089                    }
090            }
091    
092            @Override
093            public String getOrderBy() {
094                    if (_ascending) {
095                            return ORDER_BY_ASC;
096                    }
097                    else {
098                            return ORDER_BY_DESC;
099                    }
100            }
101    
102            @Override
103            public String[] getOrderByFields() {
104                    return ORDER_BY_FIELDS;
105            }
106    
107            @Override
108            public boolean isAscending() {
109                    return _ascending;
110            }
111    
112            private boolean _ascending;
113            private Layout _layout;
114            private boolean _lessThan;
115    
116    }