001    /**
002     * Copyright (c) 2000-present 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<User> {
024    
025            public static final String ORDER_BY_ASC =
026                    "jobTitle ASC, lastName ASC, firstName ASC, middleName ASC";
027    
028            public static final String ORDER_BY_DESC =
029                    "jobTitle DESC, lastName DESC, firstName DESC, middleName DESC";
030    
031            public static final String[] ORDER_BY_FIELDS = {
032                    "jobTitle", "lastName", "firstName", "middleName"
033            };
034    
035            public UserJobTitleComparator() {
036                    this(false);
037            }
038    
039            public UserJobTitleComparator(boolean ascending) {
040                    _ascending = ascending;
041            }
042    
043            @Override
044            public int compare(User user1, User user2) {
045                    String jobTitle1 = user1.getJobTitle();
046                    String jobTitle2 = user2.getJobTitle();
047    
048                    int value = jobTitle1.compareTo(jobTitle2);
049    
050                    if (value == 0) {
051                            String lastName1 = user1.getLastName();
052                            String lastName2 = user2.getLastName();
053    
054                            value = lastName1.compareTo(lastName2);
055                    }
056    
057                    if (value == 0) {
058                            String firstName1 = user1.getFirstName();
059                            String firstName2 = user2.getFirstName();
060    
061                            value = firstName1.compareTo(firstName2);
062                    }
063    
064                    if (value == 0) {
065                            String middleName1 = user1.getMiddleName();
066                            String middleName2 = user2.getMiddleName();
067    
068                            value = middleName1.compareTo(middleName2);
069                    }
070    
071                    if (_ascending) {
072                            return value;
073                    }
074                    else {
075                            return -value;
076                    }
077            }
078    
079            @Override
080            public String getOrderBy() {
081                    if (_ascending) {
082                            return ORDER_BY_ASC;
083                    }
084                    else {
085                            return ORDER_BY_DESC;
086                    }
087            }
088    
089            @Override
090            public String[] getOrderByFields() {
091                    return ORDER_BY_FIELDS;
092            }
093    
094            @Override
095            public boolean isAscending() {
096                    return _ascending;
097            }
098    
099            private final boolean _ascending;
100    
101    }