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.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 {
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(Object obj1, Object obj2) {
038                    MBMessage message1 = (MBMessage)obj1;
039                    MBMessage message2 = (MBMessage)obj2;
040    
041                    int value = DateUtil.compareTo(
042                            message1.getCreateDate(), message2.getCreateDate());
043    
044                    if (_ascending) {
045                            return value;
046                    }
047                    else {
048                            return -value;
049                    }
050            }
051    
052            @Override
053            public String getOrderBy() {
054                    if (_ascending) {
055                            return ORDER_BY_ASC;
056                    }
057                    else {
058                            return ORDER_BY_DESC;
059                    }
060            }
061    
062            @Override
063            public String[] getOrderByFields() {
064                    return ORDER_BY_FIELDS;
065            }
066    
067            @Override
068            public boolean isAscending() {
069                    return _ascending;
070            }
071    
072            private boolean _ascending;
073    
074    }