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