1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.calendar.util.comparator;
21  
22  import com.liferay.portal.kernel.util.Time;
23  import com.liferay.portlet.calendar.model.CalEvent;
24  import com.liferay.portlet.calendar.util.CalUtil;
25  
26  import java.util.Calendar;
27  import java.util.Comparator;
28  import java.util.Date;
29  import java.util.Locale;
30  import java.util.TimeZone;
31  
32  /**
33   * <a href="EventTimeComparator.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Samuel Kong
36   *
37   */
38  public class EventTimeComparator implements Comparator<CalEvent> {
39  
40      public EventTimeComparator(TimeZone timeZone, Locale locale) {
41          _timeZone = timeZone;
42          _locale = locale;
43      }
44  
45      public int compare(CalEvent event1, CalEvent event2) {
46          boolean allDay1 = CalUtil.isAllDay(event1, _timeZone, _locale);
47          boolean allDay2 = CalUtil.isAllDay(event2, _timeZone, _locale);
48  
49          if (allDay1 && allDay2) {
50              return compareTitle(event1, event2);
51          }
52          else if (allDay1) {
53              return -1;
54          }
55          else if (allDay2) {
56              return 1;
57          }
58  
59          int value = compareTime(
60              getStartDate(event1, _timeZone), getStartDate(event2, _timeZone));
61  
62          if (value != 0) {
63              return value;
64          }
65  
66          value = compareTime(
67              getEndDate(event1, _timeZone), getEndDate(event2, _timeZone));
68  
69          if (value != 0) {
70              return value;
71          }
72  
73          return compareTitle(event1, event2);
74      }
75  
76      protected int compareTitle(CalEvent event1, CalEvent event2) {
77          return event1.getTitle().toLowerCase().compareTo(
78              event2.getTitle().toLowerCase());
79      }
80  
81      protected int compareTime(Date date1, Date date2) {
82          Calendar cal1 = Calendar.getInstance(_timeZone, _locale);
83  
84          cal1.setTime(date1);
85  
86          cal1.set(Calendar.YEAR, 1970);
87          cal1.set(Calendar.MONTH, 1);
88          cal1.set(Calendar.DAY_OF_YEAR, 1);
89  
90          Calendar cal2 = Calendar.getInstance(_timeZone, _locale);
91  
92          cal2.setTime(date2);
93  
94          cal2.set(Calendar.YEAR, 1970);
95          cal2.set(Calendar.MONTH, 1);
96          cal2.set(Calendar.DAY_OF_YEAR, 1);
97  
98          return cal1.compareTo(cal2);
99      }
100 
101     protected Date getEndDate(CalEvent event, TimeZone timeZone) {
102         if (event.isTimeZoneSensitive()) {
103             return Time.getDate(CalUtil.getEndTime(event), timeZone);
104         }
105         else {
106             return CalUtil.getEndTime(event);
107         }
108     }
109 
110     protected Date getStartDate(CalEvent event, TimeZone timeZone) {
111         if (event.isTimeZoneSensitive()) {
112             return Time.getDate(event.getStartDate(), timeZone);
113         }
114         else {
115             return event.getStartDate();
116         }
117     }
118 
119     private TimeZone _timeZone;
120     private Locale _locale;
121 
122 }