001    /**
002     * Copyright (c) 2000-2010 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.UserGroup;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     */
023    public class UserGroupDescriptionComparator extends OrderByComparator {
024    
025            public static String ORDER_BY_ASC =
026                    "UserGroup.description ASC, UserGroup.name ASC";
027    
028            public static String ORDER_BY_DESC =
029                    "UserGroup.description DESC, UserGroup.name DESC";
030    
031            public static String[] ORDER_BY_FIELDS = {"description", "name"};
032    
033            public UserGroupDescriptionComparator() {
034                    this(false);
035            }
036    
037            public UserGroupDescriptionComparator(boolean ascending) {
038                    _ascending = ascending;
039            }
040    
041            public int compare(Object obj1, Object obj2) {
042                    UserGroup userGroup1 = (UserGroup)obj1;
043                    UserGroup userGroup2 = (UserGroup)obj2;
044    
045                    int value = userGroup1.getDescription().compareTo(
046                            userGroup2.getDescription());
047    
048                    if (value == 0) {
049                            value = userGroup1.getName().compareTo(userGroup2.getName());
050                    }
051    
052                    if (_ascending) {
053                            return value;
054                    }
055                    else {
056                            return -value;
057                    }
058            }
059    
060            public String getOrderBy() {
061                    if (_ascending) {
062                            return ORDER_BY_ASC;
063                    }
064                    else {
065                            return ORDER_BY_DESC;
066                    }
067            }
068    
069            public String[] getOrderByFields() {
070                    return ORDER_BY_FIELDS;
071            }
072    
073            public boolean isAscending() {
074                    return _ascending;
075            }
076    
077            private boolean _ascending;
078    
079    }