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.User;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     */
023    public class UserJobTitleComparator extends OrderByComparator {
024    
025            public static String ORDER_BY_ASC =
026                    "User_.jobTitle ASC, User_.lastName ASC, User_.firstName ASC, " +
027                            "User_.middleName ASC";
028    
029            public static String ORDER_BY_DESC =
030                    "User_.jobTitle DESC, User_.lastName DESC, User_.firstName DESC, " +
031                            "User_.middleName DESC";
032    
033            public static String[] ORDER_BY_FIELDS = {
034                    "jobTitle", "lastName", "firstName", "middleName"
035            };
036    
037            public UserJobTitleComparator() {
038                    this(false);
039            }
040    
041            public UserJobTitleComparator(boolean ascending) {
042                    _ascending = ascending;
043            }
044    
045            public int compare(Object obj1, Object obj2) {
046                    User user1 = (User)obj1;
047                    User user2 = (User)obj2;
048    
049                    int value = user1.getJobTitle().compareTo(user2.getJobTitle());
050    
051                    if (value == 0) {
052                            value = user1.getLastName().compareTo(user2.getLastName());
053                    }
054    
055                    if (value == 0) {
056                            value = user1.getFirstName().compareTo(user2.getFirstName());
057                    }
058    
059                    if (value == 0) {
060                            value = user1.getMiddleName().compareTo(user2.getMiddleName());
061                    }
062    
063                    if (_ascending) {
064                            return value;
065                    }
066                    else {
067                            return -value;
068                    }
069            }
070    
071            public String getOrderBy() {
072                    if (_ascending) {
073                            return ORDER_BY_ASC;
074                    }
075                    else {
076                            return ORDER_BY_DESC;
077                    }
078            }
079    
080            public String[] getOrderByFields() {
081                    return ORDER_BY_FIELDS;
082            }
083    
084            public boolean isAscending() {
085                    return _ascending;
086            }
087    
088            private boolean _ascending;
089    
090    }