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 UserLastNameComparator extends OrderByComparator<User> {
024    
025            public static final String ORDER_BY_ASC =
026                    "lastName ASC, firstName ASC, middleName ASC";
027    
028            public static final String ORDER_BY_DESC =
029                    "lastName DESC, firstName DESC, middleName DESC";
030    
031            public static final String[] ORDER_BY_FIELDS = {
032                    "lastName", "firstName", "middleName"
033            };
034    
035            public UserLastNameComparator() {
036                    this(false);
037            }
038    
039            public UserLastNameComparator(boolean ascending) {
040                    _ascending = ascending;
041            }
042    
043            @Override
044            public int compare(User user1, User user2) {
045                    String lastName1 = user1.getLastName();
046                    String lastName2 = user2.getLastName();
047    
048                    int value = lastName1.compareTo(lastName2);
049    
050                    if (value == 0) {
051                            String firstName1 = user1.getFirstName();
052                            String firstName2 = user2.getFirstName();
053    
054                            value = firstName1.compareTo(firstName2);
055                    }
056    
057                    if (value == 0) {
058                            String middleName1 = user1.getMiddleName();
059                            String middleName2 = user2.getMiddleName();
060    
061                            value = middleName1.compareTo(middleName2);
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 final boolean _ascending;
093    
094    }