001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }