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;
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 prefs) {
43          String emailFromAddress = PropsUtil.get(
44              PropsKeys.CALENDAR_EMAIL_FROM_ADDRESS);
45  
46          return prefs.getValue("email-from-address", emailFromAddress);
47      }
48  
49      public static String getEmailFromName(PortletPreferences prefs) {
50          String emailFromName = PropsUtil.get(
51              PropsKeys.CALENDAR_EMAIL_FROM_NAME);
52  
53          return prefs.getValue("email-from-name", emailFromName);
54      }
55  
56      public static boolean getEmailEventReminderEnabled(
57          PortletPreferences prefs) {
58  
59          String emailEventReminderEnabled = prefs.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(PortletPreferences prefs) {
72          String emailEventReminderBody = prefs.getValue(
73              "email-event-reminder-body", StringPool.BLANK);
74  
75          if (Validator.isNotNull(emailEventReminderBody)) {
76              return emailEventReminderBody;
77          }
78          else {
79              return ContentUtil.get(PropsUtil.get(
80                  PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_BODY));
81          }
82      }
83  
84      public static String getEmailEventReminderSubject(
85          PortletPreferences prefs) {
86  
87          String emailEventReminderSubject = prefs.getValue(
88              "email-event-reminder-subject", StringPool.BLANK);
89  
90          if (Validator.isNotNull(emailEventReminderSubject)) {
91              return emailEventReminderSubject;
92          }
93          else {
94              return ContentUtil.get(PropsUtil.get(
95                  PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_SUBJECT));
96          }
97      }
98  
99      public static Date getEndTime(CalEvent event) {
100         long startTime = event.getStartDate().getTime();
101 
102         long endTime =
103             startTime + (Time.HOUR * event.getDurationHour()) +
104                 (Time.MINUTE * event.getDurationMinute());
105 
106         return new Date(endTime);
107     }
108 
109     public static boolean isAllDay(
110         CalEvent event, TimeZone timeZone, Locale locale) {
111 
112         Calendar cal = null;
113 
114         if (event.getTimeZoneSensitive()) {
115             cal = CalendarFactoryUtil.getCalendar(timeZone, locale);
116         }
117         else {
118             cal = CalendarFactoryUtil.getCalendar();
119         }
120 
121         cal.setTime(event.getStartDate());
122 
123         int hour = cal.get(Calendar.HOUR_OF_DAY);
124         int minute = cal.get(Calendar.MINUTE);
125         int second = cal.get(Calendar.SECOND);
126         int millisecond = cal.get(Calendar.MILLISECOND);
127 
128         int dHour = event.getDurationHour();
129         int dMinute = event.getDurationMinute();
130 
131         if ((hour == 0) && (minute == 0) && (second == 0) &&
132             (millisecond == 0) && (dHour == 24) && (dMinute == 0)) {
133 
134             return true;
135         }
136 
137         return false;
138     }
139 
140     public static String toString(Calendar cal) {
141         StringBundler sb = new StringBundler(7);
142 
143         sb.append(cal.get(Calendar.YEAR));
144         sb.append(StringPool.PERIOD);
145         sb.append(cal.get(Calendar.MONTH));
146         sb.append(StringPool.PERIOD);
147         sb.append(cal.get(Calendar.DATE));
148         sb.append(StringPool.PERIOD);
149         sb.append(cal.getTimeZone().getRawOffset());
150 
151         return sb.toString();
152     }
153 
154 }