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