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 LayoutPriorityComparator extends OrderByComparator {
024    
025            public static final String ORDER_BY_ASC = "Layout.priority ASC";
026    
027            public static final String[] ORDER_BY_FIELDS = {"priority"};
028    
029            public LayoutPriorityComparator(Layout layout, boolean lessThan) {
030                    _layout = layout;
031                    _lessThan = lessThan;
032            }
033    
034            @Override
035            public int compare(Object obj1, Object obj2) {
036                    Layout layout1 = (Layout)obj1;
037                    Layout layout2 = (Layout)obj2;
038    
039                    int priority1 = layout1.getPriority();
040                    int priority2 = layout2.getPriority();
041    
042                    if (priority1 > priority2) {
043                            return 1;
044                    }
045                    else if (priority1 < priority2) {
046                            return -1;
047                    }
048                    else {
049                            if (_layout.equals(layout1)) {
050                                    if (_lessThan) {
051                                            return 1;
052                                    }
053                                    else {
054                                            return -1;
055                                    }
056                            }
057                            else if (_layout.equals(layout2)) {
058                                    if (_lessThan) {
059                                            return -1;
060                                    }
061                                    else {
062                                            return 1;
063                                    }
064                            }
065    
066                            return 0;
067                    }
068            }
069    
070            @Override
071            public String getOrderBy() {
072                    return ORDER_BY_ASC;
073            }
074    
075            @Override
076            public String[] getOrderByFields() {
077                    return ORDER_BY_FIELDS;
078            }
079    
080            @Override
081            public boolean isAscending() {
082                    return true;
083            }
084    
085            private Layout _layout;
086            private boolean _lessThan;
087    
088    }