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.calendar.util.comparator;
016    
017    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
018    import com.liferay.portal.kernel.util.Time;
019    import com.liferay.portlet.calendar.model.CalEvent;
020    import com.liferay.portlet.calendar.util.CalUtil;
021    
022    import java.util.Calendar;
023    import java.util.Comparator;
024    import java.util.Date;
025    import java.util.Locale;
026    import java.util.TimeZone;
027    
028    /**
029     * @author Samuel Kong
030     * @author Jang Kim
031     */
032    public class EventTimeComparator implements Comparator<CalEvent> {
033    
034            public EventTimeComparator(TimeZone timeZone, Locale locale) {
035                    _timeZone = timeZone;
036                    _locale = locale;
037            }
038    
039            public int compare(CalEvent event1, CalEvent event2) {
040                    boolean allDay1 = CalUtil.isAllDay(event1, _timeZone, _locale);
041                    boolean allDay2 = CalUtil.isAllDay(event2, _timeZone, _locale);
042    
043                    if (allDay1 && allDay2) {
044                            return compareTitle(event1, event2);
045                    }
046                    else if (allDay1) {
047                            return -1;
048                    }
049                    else if (allDay2) {
050                            return 1;
051                    }
052    
053                    boolean repeating = event1.isRepeating() || event2.isRepeating();
054    
055                    Date startDate1 = getStartDate(event1, _timeZone, repeating);
056                    Date startDate2 = getStartDate(event2, _timeZone, repeating);
057    
058                    int value = startDate1.compareTo(startDate2);
059    
060                    if (value != 0) {
061                            return value;
062                    }
063    
064                    Long duration1 = getDuration(event1);
065                    Long duration2 = getDuration(event2);
066    
067                    value = duration1.compareTo(duration2);
068    
069                    if (value != 0) {
070                            return value;
071                    }
072    
073                    return compareTitle(event1, event2);
074            }
075    
076            protected int compareTitle(CalEvent event1, CalEvent event2) {
077                    return event1.getTitle().toLowerCase().compareTo(
078                            event2.getTitle().toLowerCase());
079            }
080    
081            protected Long getDuration(CalEvent event) {
082                    return (Time.HOUR * event.getDurationHour()) +
083                            (Time.MINUTE * event.getDurationMinute());
084            }
085    
086            protected Date getStartDate(
087                    CalEvent event, TimeZone timeZone, boolean repeating) {
088    
089                    if (repeating) {
090    
091                            // Normalize the start date of the event when at least one of the
092                            // events is a recurring event
093    
094                            Calendar calendar = null;
095    
096                            if (event.isTimeZoneSensitive()) {
097                                    calendar = CalendarFactoryUtil.getCalendar(_timeZone);
098                            }
099                            else {
100                                    calendar = CalendarFactoryUtil.getCalendar();
101                            }
102    
103                            calendar.setTime(event.getStartDate());
104    
105                            calendar.set(Calendar.MONTH, Calendar.JANUARY);
106                            calendar.set(Calendar.DATE, 1);
107                            calendar.set(Calendar.YEAR, 1);
108    
109                            return Time.getDate(calendar);
110                    }
111                    else {
112                            if (event.isTimeZoneSensitive()) {
113                                    return Time.getDate(event.getStartDate(), timeZone);
114                            }
115                            else {
116                                    return event.getStartDate();
117                            }
118                    }
119            }
120    
121            private Locale _locale;
122            private TimeZone _timeZone;
123    
124    }