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            @Override
040            public int compare(CalEvent event1, CalEvent event2) {
041                    boolean allDay1 = CalUtil.isAllDay(event1, _timeZone, _locale);
042                    boolean allDay2 = CalUtil.isAllDay(event2, _timeZone, _locale);
043    
044                    if (allDay1 && allDay2) {
045                            return compareTitle(event1, event2);
046                    }
047                    else if (allDay1) {
048                            return -1;
049                    }
050                    else if (allDay2) {
051                            return 1;
052                    }
053    
054                    boolean repeating = event1.isRepeating() || event2.isRepeating();
055    
056                    Date startDate1 = getStartDate(event1, _timeZone, repeating);
057                    Date startDate2 = getStartDate(event2, _timeZone, repeating);
058    
059                    int value = startDate1.compareTo(startDate2);
060    
061                    if (value != 0) {
062                            return value;
063                    }
064    
065                    Long duration1 = getDuration(event1);
066                    Long duration2 = getDuration(event2);
067    
068                    value = duration1.compareTo(duration2);
069    
070                    if (value != 0) {
071                            return value;
072                    }
073    
074                    return compareTitle(event1, event2);
075            }
076    
077            protected int compareTitle(CalEvent event1, CalEvent event2) {
078                    return event1.getTitle().toLowerCase().compareTo(
079                            event2.getTitle().toLowerCase());
080            }
081    
082            protected Long getDuration(CalEvent event) {
083                    return (Time.HOUR * event.getDurationHour()) +
084                            (Time.MINUTE * event.getDurationMinute());
085            }
086    
087            protected Date getStartDate(
088                    CalEvent event, TimeZone timeZone, boolean repeating) {
089    
090                    if (repeating) {
091    
092                            // Normalize the start date of the event when at least one of the
093                            // events is a recurring event
094    
095                            Calendar calendar = null;
096    
097                            if (event.isTimeZoneSensitive()) {
098                                    calendar = CalendarFactoryUtil.getCalendar(_timeZone);
099                            }
100                            else {
101                                    calendar = CalendarFactoryUtil.getCalendar();
102                            }
103    
104                            calendar.setTime(event.getStartDate());
105    
106                            calendar.set(Calendar.MONTH, Calendar.JANUARY);
107                            calendar.set(Calendar.DATE, 1);
108                            calendar.set(Calendar.YEAR, 1);
109    
110                            return Time.getDate(calendar);
111                    }
112                    else {
113                            if (event.isTimeZoneSensitive()) {
114                                    return Time.getDate(event.getStartDate(), timeZone);
115                            }
116                            else {
117                                    return event.getStartDate();
118                            }
119                    }
120            }
121    
122            private Locale _locale;
123            private TimeZone _timeZone;
124    
125    }