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