1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.calendar.util;
16  
17  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
18  import com.liferay.portal.kernel.util.GetterUtil;
19  import com.liferay.portal.kernel.util.PropsKeys;
20  import com.liferay.portal.kernel.util.StringBundler;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Time;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.util.ContentUtil;
25  import com.liferay.portal.util.PropsUtil;
26  import com.liferay.portlet.calendar.model.CalEvent;
27  
28  import java.util.Calendar;
29  import java.util.Date;
30  import java.util.Locale;
31  import java.util.TimeZone;
32  
33  import javax.portlet.PortletPreferences;
34  
35  /**
36   * <a href="CalUtil.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class CalUtil {
41  
42      public static String getEmailFromAddress(PortletPreferences preferences) {
43          String emailFromAddress = PropsUtil.get(
44              PropsKeys.CALENDAR_EMAIL_FROM_ADDRESS);
45  
46          return preferences.getValue("email-from-address", emailFromAddress);
47      }
48  
49      public static String getEmailFromName(PortletPreferences preferences) {
50          String emailFromName = PropsUtil.get(
51              PropsKeys.CALENDAR_EMAIL_FROM_NAME);
52  
53          return preferences.getValue("email-from-name", emailFromName);
54      }
55  
56      public static boolean getEmailEventReminderEnabled(
57          PortletPreferences preferences) {
58  
59          String emailEventReminderEnabled = preferences.getValue(
60              "email-event-reminder-enabled", StringPool.BLANK);
61  
62          if (Validator.isNotNull(emailEventReminderEnabled)) {
63              return GetterUtil.getBoolean(emailEventReminderEnabled);
64          }
65          else {
66              return GetterUtil.getBoolean(PropsUtil.get(
67                  PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_ENABLED));
68          }
69      }
70  
71      public static String getEmailEventReminderBody(
72          PortletPreferences preferences) {
73  
74          String emailEventReminderBody = preferences.getValue(
75              "email-event-reminder-body", StringPool.BLANK);
76  
77          if (Validator.isNotNull(emailEventReminderBody)) {
78              return emailEventReminderBody;
79          }
80          else {
81              return ContentUtil.get(PropsUtil.get(
82                  PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_BODY));
83          }
84      }
85  
86      public static String getEmailEventReminderSubject(
87          PortletPreferences preferences) {
88  
89          String emailEventReminderSubject = preferences.getValue(
90              "email-event-reminder-subject", StringPool.BLANK);
91  
92          if (Validator.isNotNull(emailEventReminderSubject)) {
93              return emailEventReminderSubject;
94          }
95          else {
96              return ContentUtil.get(PropsUtil.get(
97                  PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_SUBJECT));
98          }
99      }
100 
101     public static Date getEndTime(CalEvent event) {
102         long startTime = event.getStartDate().getTime();
103 
104         long endTime =
105             startTime + (Time.HOUR * event.getDurationHour()) +
106                 (Time.MINUTE * event.getDurationMinute());
107 
108         return new Date(endTime);
109     }
110 
111     public static boolean isAllDay(
112         CalEvent event, TimeZone timeZone, Locale locale) {
113 
114         Calendar cal = null;
115 
116         if (event.getTimeZoneSensitive()) {
117             cal = CalendarFactoryUtil.getCalendar(timeZone, locale);
118         }
119         else {
120             cal = CalendarFactoryUtil.getCalendar();
121         }
122 
123         cal.setTime(event.getStartDate());
124 
125         int hour = cal.get(Calendar.HOUR_OF_DAY);
126         int minute = cal.get(Calendar.MINUTE);
127         int second = cal.get(Calendar.SECOND);
128         int millisecond = cal.get(Calendar.MILLISECOND);
129 
130         int dHour = event.getDurationHour();
131         int dMinute = event.getDurationMinute();
132 
133         if ((hour == 0) && (minute == 0) && (second == 0) &&
134             (millisecond == 0) && (dHour == 24) && (dMinute == 0)) {
135 
136             return true;
137         }
138 
139         return false;
140     }
141 
142     public static String toString(Calendar cal) {
143         StringBundler sb = new StringBundler(7);
144 
145         sb.append(cal.get(Calendar.YEAR));
146         sb.append(StringPool.PERIOD);
147         sb.append(cal.get(Calendar.MONTH));
148         sb.append(StringPool.PERIOD);
149         sb.append(cal.get(Calendar.DATE));
150         sb.append(StringPool.PERIOD);
151         sb.append(cal.getTimeZone().getRawOffset());
152 
153         return sb.toString();
154     }
155 
156 }