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.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<Layout> {
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                    _layout = null;
040                    _lessThan = false;
041            }
042    
043            public LayoutPriorityComparator(Layout layout, boolean lessThan) {
044                    _layout = layout;
045                    _lessThan = lessThan;
046    
047                    _ascending = true;
048            }
049    
050            @Override
051            public int compare(Layout layout1, Layout layout2) {
052                    int value = 0;
053    
054                    int priority1 = -1;
055    
056                    if (layout1 != null) {
057                            priority1 = layout1.getPriority();
058                    }
059    
060                    int priority2 = -1;
061    
062                    if (layout2 != null) {
063                            priority2 = layout2.getPriority();
064                    }
065    
066                    if (priority1 > priority2) {
067                            value = 1;
068                    }
069                    else if (priority1 < priority2) {
070                            value = -1;
071                    }
072                    else {
073                            if (_layout != null) {
074                                    if (_layout.equals(layout1)) {
075                                            if (_lessThan) {
076                                                    value = 1;
077                                            }
078                                            else {
079                                                    value = -1;
080                                            }
081                                    }
082                                    else if (_layout.equals(layout2)) {
083                                            if (_lessThan) {
084                                                    value = -1;
085                                            }
086                                            else {
087                                                    value = 1;
088                                            }
089                                    }
090                            }
091                    }
092    
093                    if (_ascending) {
094                            return value;
095                    }
096                    else {
097                            return -value;
098                    }
099            }
100    
101            @Override
102            public String getOrderBy() {
103                    if (_ascending) {
104                            return ORDER_BY_ASC;
105                    }
106                    else {
107                            return ORDER_BY_DESC;
108                    }
109            }
110    
111            @Override
112            public String[] getOrderByFields() {
113                    return ORDER_BY_FIELDS;
114            }
115    
116            @Override
117            public boolean isAscending() {
118                    return _ascending;
119            }
120    
121            private final boolean _ascending;
122            private final Layout _layout;
123            private final boolean _lessThan;
124    
125    }