1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.calendar.util.comparator;
24  
25  import com.liferay.portal.kernel.util.Time;
26  import com.liferay.portlet.calendar.model.CalEvent;
27  import com.liferay.portlet.calendar.util.CalUtil;
28  
29  import java.util.Calendar;
30  import java.util.Comparator;
31  import java.util.Date;
32  import java.util.Locale;
33  import java.util.TimeZone;
34  
35  /**
36   * <a href="EventTimeComparator.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Samuel Kong
39   *
40   */
41  public class EventTimeComparator implements Comparator<CalEvent> {
42  
43      public EventTimeComparator(TimeZone timeZone, Locale locale) {
44          _timeZone = timeZone;
45          _locale = locale;
46      }
47  
48      public int compare(CalEvent event1, CalEvent event2) {
49          boolean allDay1 = CalUtil.isAllDay(event1, _timeZone, _locale);
50          boolean allDay2 = CalUtil.isAllDay(event2, _timeZone, _locale);
51  
52          if (allDay1 && allDay2) {
53              return compareTitle(event1, event2);
54          }
55          else if (allDay1) {
56              return -1;
57          }
58          else if (allDay2) {
59              return 1;
60          }
61  
62          int value = compareTime(
63              getStartDate(event1, _timeZone), getStartDate(event2, _timeZone));
64  
65          if (value != 0) {
66              return value;
67          }
68  
69          value = compareTime(
70              getEndDate(event1, _timeZone), getEndDate(event2, _timeZone));
71  
72          if (value != 0) {
73              return value;
74          }
75  
76          return compareTitle(event1, event2);
77      }
78  
79      protected int compareTitle(CalEvent event1, CalEvent event2) {
80          return event1.getTitle().toLowerCase().compareTo(
81              event2.getTitle().toLowerCase());
82      }
83  
84      protected int compareTime(Date date1, Date date2) {
85          Calendar cal1 = Calendar.getInstance(_timeZone, _locale);
86  
87          cal1.setTime(date1);
88  
89          cal1.set(Calendar.YEAR, 1970);
90          cal1.set(Calendar.MONTH, 1);
91          cal1.set(Calendar.DAY_OF_YEAR, 1);
92  
93          Calendar cal2 = Calendar.getInstance(_timeZone, _locale);
94  
95          cal2.setTime(date2);
96  
97          cal2.set(Calendar.YEAR, 1970);
98          cal2.set(Calendar.MONTH, 1);
99          cal2.set(Calendar.DAY_OF_YEAR, 1);
100 
101         return cal1.compareTo(cal2);
102     }
103 
104     protected Date getEndDate(CalEvent event, TimeZone timeZone) {
105         if (event.isTimeZoneSensitive()) {
106             return Time.getDate(CalUtil.getEndTime(event), timeZone);
107         }
108         else {
109             return CalUtil.getEndTime(event);
110         }
111     }
112 
113     protected Date getStartDate(CalEvent event, TimeZone timeZone) {
114         if (event.isTimeZoneSensitive()) {
115             return Time.getDate(event.getStartDate(), timeZone);
116         }
117         else {
118             return event.getStartDate();
119         }
120     }
121 
122     private TimeZone _timeZone;
123     private Locale _locale;
124 
125 }