1   /**
2    * Copyright (c) 2000-2007 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.StringMaker;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.util.ContentUtil;
31  import com.liferay.portal.util.PropsUtil;
32  import com.liferay.portlet.calendar.model.CalEvent;
33  import com.liferay.util.Time;
34  
35  import java.io.IOException;
36  
37  import java.util.Calendar;
38  import java.util.Date;
39  import java.util.Locale;
40  import java.util.TimeZone;
41  
42  import javax.portlet.PortletPreferences;
43  
44  /**
45   * <a href="CalUtil.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class CalUtil {
51  
52      public static String getEmailFromAddress(PortletPreferences prefs) {
53          String emailFromAddress = PropsUtil.get(
54              PropsUtil.CALENDAR_EMAIL_FROM_ADDRESS);
55  
56          return prefs.getValue("email-from-address", emailFromAddress);
57      }
58  
59      public static String getEmailFromName(PortletPreferences prefs) {
60          String emailFromName = PropsUtil.get(
61              PropsUtil.CALENDAR_EMAIL_FROM_NAME);
62  
63          return prefs.getValue("email-from-name", emailFromName);
64      }
65  
66      public static boolean getEmailEventReminderEnabled(
67          PortletPreferences prefs) {
68  
69          String emailEventReminderEnabled = prefs.getValue(
70              "email-event-reminder-enabled", StringPool.BLANK);
71  
72          if (Validator.isNotNull(emailEventReminderEnabled)) {
73              return GetterUtil.getBoolean(emailEventReminderEnabled);
74          }
75          else {
76              return GetterUtil.getBoolean(PropsUtil.get(
77                  PropsUtil.CALENDAR_EMAIL_EVENT_REMINDER_ENABLED));
78          }
79      }
80  
81      public static String getEmailEventReminderBody(PortletPreferences prefs)
82          throws IOException {
83  
84          String emailEventReminderBody = prefs.getValue(
85              "email-event-reminder-body", StringPool.BLANK);
86  
87          if (Validator.isNotNull(emailEventReminderBody)) {
88              return emailEventReminderBody;
89          }
90          else {
91              return ContentUtil.get(PropsUtil.get(
92                  PropsUtil.CALENDAR_EMAIL_EVENT_REMINDER_BODY));
93          }
94      }
95  
96      public static String getEmailEventReminderSubject(PortletPreferences prefs)
97          throws IOException {
98  
99          String emailEventReminderSubject = prefs.getValue(
100             "email-event-reminder-subject", StringPool.BLANK);
101 
102         if (Validator.isNotNull(emailEventReminderSubject)) {
103             return emailEventReminderSubject;
104         }
105         else {
106             return ContentUtil.get(PropsUtil.get(
107                 PropsUtil.CALENDAR_EMAIL_EVENT_REMINDER_SUBJECT));
108         }
109     }
110 
111     public static Date getEndTime(CalEvent event) {
112         long startTime = event.getStartDate().getTime();
113 
114         long endTime =
115             startTime + (Time.HOUR * event.getDurationHour()) +
116                 (Time.MINUTE * event.getDurationMinute());
117 
118         return new Date(endTime);
119     }
120 
121     public static boolean isAllDay(
122         CalEvent event, TimeZone timeZone, Locale locale) {
123 
124         Calendar cal = null;
125 
126         if (event.getTimeZoneSensitive()) {
127             cal = CalendarFactoryUtil.getCalendar(timeZone, locale);
128         }
129         else {
130             cal = CalendarFactoryUtil.getCalendar();
131         }
132 
133         cal.setTime(event.getStartDate());
134 
135         int hour = cal.get(Calendar.HOUR_OF_DAY);
136         int minute = cal.get(Calendar.MINUTE);
137         int second = cal.get(Calendar.SECOND);
138         int millisecond = cal.get(Calendar.MILLISECOND);
139 
140         int dHour = event.getDurationHour();
141         int dMinute = event.getDurationMinute();
142 
143         if ((hour == 0) && (minute == 0) && (second == 0) &&
144             (millisecond == 0) && (dHour == 24) && (dMinute == 0)) {
145 
146             return true;
147         }
148 
149         return false;
150     }
151 
152     public static String toString(Calendar cal) {
153         StringMaker sm = new StringMaker();
154 
155         sm.append(cal.get(Calendar.YEAR));
156         sm.append(StringPool.PERIOD);
157         sm.append(cal.get(Calendar.MONTH));
158         sm.append(StringPool.PERIOD);
159         sm.append(cal.get(Calendar.DATE));
160         sm.append(StringPool.PERIOD);
161         sm.append(cal.getTimeZone().getRawOffset());
162 
163         return sm.toString();
164     }
165 
166 }