1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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;
24  
25  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.Time;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.util.ContentUtil;
31  import com.liferay.portal.util.PropsKeys;
32  import com.liferay.portal.util.PropsUtil;
33  import com.liferay.portlet.calendar.model.CalEvent;
34  
35  import java.util.Calendar;
36  import java.util.Date;
37  import java.util.Locale;
38  import java.util.TimeZone;
39  
40  import javax.portlet.PortletPreferences;
41  
42  /**
43   * <a href="CalUtil.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class CalUtil {
49  
50      public static String getEmailFromAddress(PortletPreferences prefs) {
51          String emailFromAddress = PropsUtil.get(
52              PropsKeys.CALENDAR_EMAIL_FROM_ADDRESS);
53  
54          return prefs.getValue("email-from-address", emailFromAddress);
55      }
56  
57      public static String getEmailFromName(PortletPreferences prefs) {
58          String emailFromName = PropsUtil.get(
59              PropsKeys.CALENDAR_EMAIL_FROM_NAME);
60  
61          return prefs.getValue("email-from-name", emailFromName);
62      }
63  
64      public static boolean getEmailEventReminderEnabled(
65          PortletPreferences prefs) {
66  
67          String emailEventReminderEnabled = prefs.getValue(
68              "email-event-reminder-enabled", StringPool.BLANK);
69  
70          if (Validator.isNotNull(emailEventReminderEnabled)) {
71              return GetterUtil.getBoolean(emailEventReminderEnabled);
72          }
73          else {
74              return GetterUtil.getBoolean(PropsUtil.get(
75                  PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_ENABLED));
76          }
77      }
78  
79      public static String getEmailEventReminderBody(PortletPreferences prefs) {
80          String emailEventReminderBody = prefs.getValue(
81              "email-event-reminder-body", StringPool.BLANK);
82  
83          if (Validator.isNotNull(emailEventReminderBody)) {
84              return emailEventReminderBody;
85          }
86          else {
87              return ContentUtil.get(PropsUtil.get(
88                  PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_BODY));
89          }
90      }
91  
92      public static String getEmailEventReminderSubject(
93          PortletPreferences prefs) {
94  
95          String emailEventReminderSubject = prefs.getValue(
96              "email-event-reminder-subject", StringPool.BLANK);
97  
98          if (Validator.isNotNull(emailEventReminderSubject)) {
99              return emailEventReminderSubject;
100         }
101         else {
102             return ContentUtil.get(PropsUtil.get(
103                 PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_SUBJECT));
104         }
105     }
106 
107     public static Date getEndTime(CalEvent event) {
108         long startTime = event.getStartDate().getTime();
109 
110         long endTime =
111             startTime + (Time.HOUR * event.getDurationHour()) +
112                 (Time.MINUTE * event.getDurationMinute());
113 
114         return new Date(endTime);
115     }
116 
117     public static boolean isAllDay(
118         CalEvent event, TimeZone timeZone, Locale locale) {
119 
120         Calendar cal = null;
121 
122         if (event.getTimeZoneSensitive()) {
123             cal = CalendarFactoryUtil.getCalendar(timeZone, locale);
124         }
125         else {
126             cal = CalendarFactoryUtil.getCalendar();
127         }
128 
129         cal.setTime(event.getStartDate());
130 
131         int hour = cal.get(Calendar.HOUR_OF_DAY);
132         int minute = cal.get(Calendar.MINUTE);
133         int second = cal.get(Calendar.SECOND);
134         int millisecond = cal.get(Calendar.MILLISECOND);
135 
136         int dHour = event.getDurationHour();
137         int dMinute = event.getDurationMinute();
138 
139         if ((hour == 0) && (minute == 0) && (second == 0) &&
140             (millisecond == 0) && (dHour == 24) && (dMinute == 0)) {
141 
142             return true;
143         }
144 
145         return false;
146     }
147 
148     public static String toString(Calendar cal) {
149         StringBuilder sb = new StringBuilder();
150 
151         sb.append(cal.get(Calendar.YEAR));
152         sb.append(StringPool.PERIOD);
153         sb.append(cal.get(Calendar.MONTH));
154         sb.append(StringPool.PERIOD);
155         sb.append(cal.get(Calendar.DATE));
156         sb.append(StringPool.PERIOD);
157         sb.append(cal.getTimeZone().getRawOffset());
158 
159         return sb.toString();
160     }
161 
162 }