1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.calendar.util.comparator;
16  
17  import com.liferay.portal.kernel.util.Time;
18  import com.liferay.portlet.calendar.model.CalEvent;
19  import com.liferay.portlet.calendar.util.CalUtil;
20  
21  import java.util.Comparator;
22  import java.util.Date;
23  import java.util.Locale;
24  import java.util.TimeZone;
25  
26  /**
27   * <a href="EventTimeComparator.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Samuel Kong
30   */
31  public class EventTimeComparator implements Comparator<CalEvent> {
32  
33      public EventTimeComparator(TimeZone timeZone, Locale locale) {
34          _timeZone = timeZone;
35          _locale = locale;
36      }
37  
38      public int compare(CalEvent event1, CalEvent event2) {
39          boolean allDay1 = CalUtil.isAllDay(event1, _timeZone, _locale);
40          boolean allDay2 = CalUtil.isAllDay(event2, _timeZone, _locale);
41  
42          if (allDay1 && allDay2) {
43              return compareTitle(event1, event2);
44          }
45          else if (allDay1) {
46              return -1;
47          }
48          else if (allDay2) {
49              return 1;
50          }
51  
52          Date startDate1 = getStartDate(event1, _timeZone);
53          Date startDate2 = getStartDate(event2, _timeZone);
54  
55          int value = startDate1.compareTo(startDate2);
56  
57          if (value != 0) {
58              return value;
59          }
60  
61          Date endDate1 = getEndDate(event1, _timeZone);
62          Date endDate2 = getEndDate(event2, _timeZone);
63  
64          value = endDate1.compareTo(endDate2);
65  
66          if (value != 0) {
67              return value;
68          }
69  
70          return compareTitle(event1, event2);
71      }
72  
73      protected int compareTitle(CalEvent event1, CalEvent event2) {
74          return event1.getTitle().toLowerCase().compareTo(
75              event2.getTitle().toLowerCase());
76      }
77  
78      protected Date getEndDate(CalEvent event, TimeZone timeZone) {
79          if (event.isTimeZoneSensitive()) {
80              return Time.getDate(CalUtil.getEndTime(event), timeZone);
81          }
82          else {
83              return CalUtil.getEndTime(event);
84          }
85      }
86  
87      protected Date getStartDate(CalEvent event, TimeZone timeZone) {
88          if (event.isTimeZoneSensitive()) {
89              return Time.getDate(event.getStartDate(), timeZone);
90          }
91          else {
92              return event.getStartDate();
93          }
94      }
95  
96      private TimeZone _timeZone;
97      private Locale _locale;
98  
99  }