001    /**
002     * Copyright (c) 2000-2012 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.portlet.messageboards.util.comparator;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
019    import com.liferay.portal.kernel.util.DateUtil;
020    import com.liferay.portal.kernel.util.OrderByComparator;
021    import com.liferay.portlet.messageboards.model.MBThread;
022    
023    import java.util.Date;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class ThreadLastPostDateComparator extends OrderByComparator {
029    
030            public static final String ORDER_BY_ASC =
031                    "MBThread.lastPostDate ASC, MBThread.threadId ASC";
032    
033            public static final String ORDER_BY_DESC =
034                    "MBThread.lastPostDate DESC, MBThread.threadId DESC";
035    
036            public static final String[] ORDER_BY_FIELDS = {"lastPostDate", "threadId"};
037    
038            public ThreadLastPostDateComparator() {
039                    this(false);
040            }
041    
042            public ThreadLastPostDateComparator(boolean ascending) {
043                    _ascending = ascending;
044            }
045    
046            @Override
047            public int compare(Object obj1, Object obj2) {
048                    MBThread thread1 = (MBThread)obj1;
049                    MBThread thread2 = (MBThread)obj2;
050    
051                    Date lastPostDate1 = thread1.getLastPostDate();
052                    Date lastPostDate2 = thread2.getLastPostDate();
053    
054                    boolean ignoreMilliseconds = false;
055    
056                    DB db = DBFactoryUtil.getDB();
057    
058                    if (!db.isSupportsDateMilliseconds()) {
059                            ignoreMilliseconds = true;
060                    }
061    
062                    int value = DateUtil.compareTo(
063                            lastPostDate1, lastPostDate2, ignoreMilliseconds);
064    
065                    if (value == 0) {
066                            if (thread1.getThreadId() < thread2.getThreadId()) {
067                                    value = -1;
068                            }
069                            else if (thread1.getThreadId() > thread2.getThreadId()) {
070                                    value = 1;
071                            }
072                    }
073    
074                    if (_ascending) {
075                            return value;
076                    }
077                    else {
078                            return -value;
079                    }
080            }
081    
082            @Override
083            public String getOrderBy() {
084                    if (_ascending) {
085                            return ORDER_BY_ASC;
086                    }
087                    else {
088                            return ORDER_BY_DESC;
089                    }
090            }
091    
092            @Override
093            public String[] getOrderByFields() {
094                    return ORDER_BY_FIELDS;
095            }
096    
097            @Override
098            public boolean isAscending() {
099                    return _ascending;
100            }
101    
102            private boolean _ascending;
103    
104    }