001    /**
002     * Copyright (c) 2000-2013 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 {
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(Object obj1, Object obj2) {
045                    User user1 = (User)obj1;
046                    User user2 = (User)obj2;
047    
048                    String lastName1 = user1.getLastName();
049                    String lastName2 = user2.getLastName();
050    
051                    int value = lastName1.compareTo(lastName2);
052    
053                    if (value == 0) {
054                            String firstName1 = user1.getFirstName();
055                            String firstName2 = user2.getFirstName();
056    
057                            value = firstName1.compareTo(firstName2);
058                    }
059    
060                    if (value == 0) {
061                            String middleName1 = user1.getMiddleName();
062                            String middleName2 = user2.getMiddleName();
063    
064                            value = middleName1.compareTo(middleName2);
065                    }
066    
067                    if (_ascending) {
068                            return value;
069                    }
070                    else {
071                            return -value;
072                    }
073            }
074    
075            @Override
076            public String getOrderBy() {
077                    if (_ascending) {
078                            return ORDER_BY_ASC;
079                    }
080                    else {
081                            return ORDER_BY_DESC;
082                    }
083            }
084    
085            @Override
086            public String[] getOrderByFields() {
087                    return ORDER_BY_FIELDS;
088            }
089    
090            @Override
091            public boolean isAscending() {
092                    return _ascending;
093            }
094    
095            private boolean _ascending;
096    
097    }