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 = {"displayDate"};
030    
031            public static final String ORDER_BY_DESC =
032                    "BlogsEntry.displayDate DESC, BlogsEntry.entryId DESC";
033    
034            public static final String[] ORDER_BY_FIELDS = {"displayDate", "entryId"};
035    
036            public EntryDisplayDateComparator() {
037                    this(false);
038            }
039    
040            public EntryDisplayDateComparator(boolean ascending) {
041                    _ascending = ascending;
042            }
043    
044            @Override
045            public int compare(Object obj1, Object obj2) {
046                    BlogsEntry entry1 = (BlogsEntry)obj1;
047                    BlogsEntry entry2 = (BlogsEntry)obj2;
048    
049                    int value = DateUtil.compareTo(
050                            entry1.getDisplayDate(), entry2.getDisplayDate());
051    
052                    if (value == 0) {
053                            if (entry1.getEntryId() < entry2.getEntryId()) {
054                                    value = -1;
055                            }
056                            else if (entry1.getEntryId() > entry2.getEntryId()) {
057                                    value = 1;
058                            }
059                    }
060    
061                    if (_ascending) {
062                            return value;
063                    }
064                    else {
065                            return -value;
066                    }
067            }
068    
069            @Override
070            public String getOrderBy() {
071                    if (_ascending) {
072                            return ORDER_BY_ASC;
073                    }
074                    else {
075                            return ORDER_BY_DESC;
076                    }
077            }
078    
079            @Override
080            public String[] getOrderByConditionFields() {
081                    return ORDER_BY_CONDITION_FIELDS;
082            }
083    
084            @Override
085            public String[] getOrderByFields() {
086                    return ORDER_BY_FIELDS;
087            }
088    
089            @Override
090            public boolean isAscending() {
091                    return _ascending;
092            }
093    
094            private boolean _ascending;
095    
096    }