001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }