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.DateUtil;
018    import com.liferay.portal.kernel.util.OrderByComparator;
019    import com.liferay.portal.model.User;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     */
024    public class UserLoginDateComparator extends OrderByComparator {
025    
026            public static final String ORDER_BY_ASC =
027                    "loginDate ASC, lastName ASC, firstName ASC, middleName ASC";
028    
029            public static final String ORDER_BY_DESC =
030                    "loginDate DESC, lastName DESC, firstName DESC, middleName DESC";
031    
032            public static final String[] ORDER_BY_FIELDS = {
033                    "loginDate", "lastName", "firstName", "middleName"
034            };
035    
036            public UserLoginDateComparator() {
037                    this(false);
038            }
039    
040            public UserLoginDateComparator(boolean ascending) {
041                    _ascending = ascending;
042            }
043    
044            @Override
045            public int compare(Object obj1, Object obj2) {
046                    User user1 = (User)obj1;
047                    User user2 = (User)obj2;
048    
049                    int value = DateUtil.compareTo(
050                            user1.getLoginDate(), user2.getLoginDate());
051    
052                    if (value == 0) {
053                            String lastName1 = user1.getLastName();
054                            String lastName2 = user2.getLastName();
055    
056                            value = lastName1.compareTo(lastName2);
057                    }
058    
059                    if (value == 0) {
060                            String firstName1 = user1.getFirstName();
061                            String firstName2 = user2.getFirstName();
062    
063                            value = firstName1.compareTo(firstName2);
064                    }
065    
066                    if (value == 0) {
067                            String middleName1 = user1.getMiddleName();
068                            String middleName2 = user2.getMiddleName();
069    
070                            value = middleName1.compareTo(middleName2);
071                    }
072    
073                    if (_ascending) {
074                            return value;
075                    }
076                    else {
077                            return -value;
078                    }
079            }
080    
081            @Override
082            public String getOrderBy() {
083                    if (_ascending) {
084                            return ORDER_BY_ASC;
085                    }
086                    else {
087                            return ORDER_BY_DESC;
088                    }
089            }
090    
091            @Override
092            public String[] getOrderByFields() {
093                    return ORDER_BY_FIELDS;
094            }
095    
096            @Override
097            public boolean isAscending() {
098                    return _ascending;
099            }
100    
101            private boolean _ascending;
102    
103    }