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