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.portlet.journal.util.comparator;
016    
017    import com.liferay.portal.kernel.util.DateUtil;
018    import com.liferay.portal.kernel.util.OrderByComparator;
019    import com.liferay.portlet.journal.model.JournalArticle;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     */
024    public class ArticleDisplayDateComparator extends OrderByComparator {
025    
026            public static final String ORDER_BY_ASC = "displayDate ASC, version ASC";
027    
028            public static final String ORDER_BY_DESC = "displayDate DESC, version DESC";
029    
030            public static final String[] ORDER_BY_FIELDS = {"displayDate", "version"};
031    
032            public ArticleDisplayDateComparator() {
033                    this(false);
034            }
035    
036            public ArticleDisplayDateComparator(boolean ascending) {
037                    _ascending = ascending;
038            }
039    
040            @Override
041            public int compare(Object obj1, Object obj2) {
042                    JournalArticle article1 = (JournalArticle)obj1;
043                    JournalArticle article2 = (JournalArticle)obj2;
044    
045                    int value = DateUtil.compareTo(
046                            article1.getDisplayDate(), article2.getDisplayDate());
047    
048                    if (value == 0) {
049                            if (article1.getVersion() < article2.getVersion()) {
050                                    value = -1;
051                            }
052                            else if (article1.getVersion() > article2.getVersion()) {
053                                    value = 1;
054                            }
055                    }
056    
057                    if (_ascending) {
058                            return value;
059                    }
060                    else {
061                            return -value;
062                    }
063            }
064    
065            @Override
066            public String getOrderBy() {
067                    if (_ascending) {
068                            return ORDER_BY_ASC;
069                    }
070                    else {
071                            return ORDER_BY_DESC;
072                    }
073            }
074    
075            @Override
076            public String[] getOrderByFields() {
077                    return ORDER_BY_FIELDS;
078            }
079    
080            @Override
081            public boolean isAscending() {
082                    return _ascending;
083            }
084    
085            private boolean _ascending;
086    
087    }