001    /**
002     * Copyright (c) 2000-2010 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 String ORDER_BY_ASC = "createDate ASC";
027    
028            public static String ORDER_BY_DESC = "createDate DESC";
029    
030            public static String[] ORDER_BY_FIELDS = {"createDate"};
031    
032            public MessageCreateDateComparator(boolean ascending) {
033                    _ascending = ascending;
034            }
035    
036            public int compare(Object obj1, Object obj2) {
037                    MBMessage message1 = (MBMessage)obj1;
038                    MBMessage message2 = (MBMessage)obj2;
039    
040                    int value = DateUtil.compareTo(
041                            message1.getCreateDate(), message2.getCreateDate());
042    
043                    if (_ascending) {
044                            return value;
045                    }
046                    else {
047                            return -value;
048                    }
049            }
050    
051            public String getOrderBy() {
052                    if (_ascending) {
053                            return ORDER_BY_ASC;
054                    }
055                    else {
056                            return ORDER_BY_DESC;
057                    }
058            }
059    
060            public String[] getOrderByFields() {
061                    return ORDER_BY_FIELDS;
062            }
063    
064            public boolean isAscending() {
065                    return _ascending;
066            }
067    
068            private boolean _ascending;
069    
070    }