1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class EventTimeComparator implements Comparator<CalEvent> {
41  
42      public EventTimeComparator(TimeZone timeZone, Locale locale) {
43          _timeZone = timeZone;
44          _locale = locale;
45      }
46  
47      public int compare(CalEvent event1, CalEvent event2) {
48          boolean allDay1 = CalUtil.isAllDay(event1, _timeZone, _locale);
49          boolean allDay2 = CalUtil.isAllDay(event2, _timeZone, _locale);
50  
51          if (allDay1 && allDay2) {
52              return compareTitle(event1, event2);
53          }
54          else if (allDay1) {
55              return -1;
56          }
57          else if (allDay2) {
58              return 1;
59          }
60  
61          int value = compareTime(
62              getStartDate(event1, _timeZone), getStartDate(event2, _timeZone));
63  
64          if (value != 0) {
65              return value;
66          }
67  
68          value = compareTime(
69              getEndDate(event1, _timeZone), getEndDate(event2, _timeZone));
70  
71          if (value != 0) {
72              return value;
73          }
74  
75          return compareTitle(event1, event2);
76      }
77  
78      protected int compareTitle(CalEvent event1, CalEvent event2) {
79          return event1.getTitle().toLowerCase().compareTo(
80              event2.getTitle().toLowerCase());
81      }
82  
83      protected int compareTime(Date date1, Date date2) {
84          Calendar cal1 = Calendar.getInstance(_timeZone, _locale);
85  
86          cal1.setTime(date1);
87  
88          cal1.set(Calendar.YEAR, 1970);
89          cal1.set(Calendar.MONTH, 1);
90          cal1.set(Calendar.DAY_OF_YEAR, 1);
91  
92          Calendar cal2 = Calendar.getInstance(_timeZone, _locale);
93  
94          cal2.setTime(date2);
95  
96          cal2.set(Calendar.YEAR, 1970);
97          cal2.set(Calendar.MONTH, 1);
98          cal2.set(Calendar.DAY_OF_YEAR, 1);
99  
100         return cal1.compareTo(cal2);
101     }
102 
103     protected Date getEndDate(CalEvent event, TimeZone timeZone) {
104         if (event.isTimeZoneSensitive()) {
105             return Time.getDate(CalUtil.getEndTime(event), timeZone);
106         }
107         else {
108             return CalUtil.getEndTime(event);
109         }
110     }
111 
112     protected Date getStartDate(CalEvent event, TimeZone timeZone) {
113         if (event.isTimeZoneSensitive()) {
114             return Time.getDate(event.getStartDate(), timeZone);
115         }
116         else {
117             return event.getStartDate();
118         }
119     }
120 
121     private TimeZone _timeZone;
122     private Locale _locale;
123 
124 }