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.calendar.util.comparator;
016    
017    import com.liferay.portal.kernel.util.Time;
018    import com.liferay.portlet.calendar.model.CalEvent;
019    import com.liferay.portlet.calendar.util.CalUtil;
020    
021    import java.util.Comparator;
022    import java.util.Date;
023    import java.util.Locale;
024    import java.util.TimeZone;
025    
026    /**
027     * @author Samuel Kong
028     */
029    public class EventTimeComparator implements Comparator<CalEvent> {
030    
031            public EventTimeComparator(TimeZone timeZone, Locale locale) {
032                    _timeZone = timeZone;
033                    _locale = locale;
034            }
035    
036            public int compare(CalEvent event1, CalEvent event2) {
037                    boolean allDay1 = CalUtil.isAllDay(event1, _timeZone, _locale);
038                    boolean allDay2 = CalUtil.isAllDay(event2, _timeZone, _locale);
039    
040                    if (allDay1 && allDay2) {
041                            return compareTitle(event1, event2);
042                    }
043                    else if (allDay1) {
044                            return -1;
045                    }
046                    else if (allDay2) {
047                            return 1;
048                    }
049    
050                    Date startDate1 = getStartDate(event1, _timeZone);
051                    Date startDate2 = getStartDate(event2, _timeZone);
052    
053                    int value = startDate1.compareTo(startDate2);
054    
055                    if (value != 0) {
056                            return value;
057                    }
058    
059                    Date endDate1 = getEndDate(event1, _timeZone);
060                    Date endDate2 = getEndDate(event2, _timeZone);
061    
062                    value = endDate1.compareTo(endDate2);
063    
064                    if (value != 0) {
065                            return value;
066                    }
067    
068                    return compareTitle(event1, event2);
069            }
070    
071            protected int compareTitle(CalEvent event1, CalEvent event2) {
072                    return event1.getTitle().toLowerCase().compareTo(
073                            event2.getTitle().toLowerCase());
074            }
075    
076            protected Date getEndDate(CalEvent event, TimeZone timeZone) {
077                    if (event.isTimeZoneSensitive()) {
078                            return Time.getDate(CalUtil.getEndTime(event), timeZone);
079                    }
080                    else {
081                            return CalUtil.getEndTime(event);
082                    }
083            }
084    
085            protected Date getStartDate(CalEvent event, TimeZone timeZone) {
086                    if (event.isTimeZoneSensitive()) {
087                            return Time.getDate(event.getStartDate(), timeZone);
088                    }
089                    else {
090                            return event.getStartDate();
091                    }
092            }
093    
094            private TimeZone _timeZone;
095            private Locale _locale;
096    
097    }