001    /**
002     * Copyright (c) 2000-present 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.util.DateUtil;
018    import com.liferay.portal.kernel.util.OrderByComparator;
019    import com.liferay.portlet.messageboards.model.MBMessage;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     */
024    public class MessageCreateDateComparator extends OrderByComparator<MBMessage> {
025    
026            public static final String ORDER_BY_ASC = "MBMessage.createDate ASC";
027    
028            public static final String ORDER_BY_DESC = "MBMessage.createDate DESC";
029    
030            public static final String[] ORDER_BY_FIELDS = {"createDate"};
031    
032            public MessageCreateDateComparator(boolean ascending) {
033                    _ascending = ascending;
034            }
035    
036            @Override
037            public int compare(MBMessage message1, MBMessage message2) {
038                    int value = DateUtil.compareTo(
039                            message1.getCreateDate(), message2.getCreateDate());
040    
041                    if (_ascending) {
042                            return value;
043                    }
044                    else {
045                            return -value;
046                    }
047            }
048    
049            @Override
050            public String getOrderBy() {
051                    if (_ascending) {
052                            return ORDER_BY_ASC;
053                    }
054                    else {
055                            return ORDER_BY_DESC;
056                    }
057            }
058    
059            @Override
060            public String[] getOrderByFields() {
061                    return ORDER_BY_FIELDS;
062            }
063    
064            @Override
065            public boolean isAscending() {
066                    return _ascending;
067            }
068    
069            private final boolean _ascending;
070    
071    }