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