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