1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.calendar.util;
21  
22  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
23  import com.liferay.portal.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.Time;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.util.ContentUtil;
28  import com.liferay.portal.util.PropsKeys;
29  import com.liferay.portal.util.PropsUtil;
30  import com.liferay.portlet.calendar.model.CalEvent;
31  
32  import java.util.Calendar;
33  import java.util.Date;
34  import java.util.Locale;
35  import java.util.TimeZone;
36  
37  import javax.portlet.PortletPreferences;
38  
39  /**
40   * <a href="CalUtil.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class CalUtil {
46  
47      public static String getEmailFromAddress(PortletPreferences prefs) {
48          String emailFromAddress = PropsUtil.get(
49              PropsKeys.CALENDAR_EMAIL_FROM_ADDRESS);
50  
51          return prefs.getValue("email-from-address", emailFromAddress);
52      }
53  
54      public static String getEmailFromName(PortletPreferences prefs) {
55          String emailFromName = PropsUtil.get(
56              PropsKeys.CALENDAR_EMAIL_FROM_NAME);
57  
58          return prefs.getValue("email-from-name", emailFromName);
59      }
60  
61      public static boolean getEmailEventReminderEnabled(
62          PortletPreferences prefs) {
63  
64          String emailEventReminderEnabled = prefs.getValue(
65              "email-event-reminder-enabled", StringPool.BLANK);
66  
67          if (Validator.isNotNull(emailEventReminderEnabled)) {
68              return GetterUtil.getBoolean(emailEventReminderEnabled);
69          }
70          else {
71              return GetterUtil.getBoolean(PropsUtil.get(
72                  PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_ENABLED));
73          }
74      }
75  
76      public static String getEmailEventReminderBody(PortletPreferences prefs) {
77          String emailEventReminderBody = prefs.getValue(
78              "email-event-reminder-body", StringPool.BLANK);
79  
80          if (Validator.isNotNull(emailEventReminderBody)) {
81              return emailEventReminderBody;
82          }
83          else {
84              return ContentUtil.get(PropsUtil.get(
85                  PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_BODY));
86          }
87      }
88  
89      public static String getEmailEventReminderSubject(
90          PortletPreferences prefs) {
91  
92          String emailEventReminderSubject = prefs.getValue(
93              "email-event-reminder-subject", StringPool.BLANK);
94  
95          if (Validator.isNotNull(emailEventReminderSubject)) {
96              return emailEventReminderSubject;
97          }
98          else {
99              return ContentUtil.get(PropsUtil.get(
100                 PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_SUBJECT));
101         }
102     }
103 
104     public static Date getEndTime(CalEvent event) {
105         long startTime = event.getStartDate().getTime();
106 
107         long endTime =
108             startTime + (Time.HOUR * event.getDurationHour()) +
109                 (Time.MINUTE * event.getDurationMinute());
110 
111         return new Date(endTime);
112     }
113 
114     public static boolean isAllDay(
115         CalEvent event, TimeZone timeZone, Locale locale) {
116 
117         Calendar cal = null;
118 
119         if (event.getTimeZoneSensitive()) {
120             cal = CalendarFactoryUtil.getCalendar(timeZone, locale);
121         }
122         else {
123             cal = CalendarFactoryUtil.getCalendar();
124         }
125 
126         cal.setTime(event.getStartDate());
127 
128         int hour = cal.get(Calendar.HOUR_OF_DAY);
129         int minute = cal.get(Calendar.MINUTE);
130         int second = cal.get(Calendar.SECOND);
131         int millisecond = cal.get(Calendar.MILLISECOND);
132 
133         int dHour = event.getDurationHour();
134         int dMinute = event.getDurationMinute();
135 
136         if ((hour == 0) && (minute == 0) && (second == 0) &&
137             (millisecond == 0) && (dHour == 24) && (dMinute == 0)) {
138 
139             return true;
140         }
141 
142         return false;
143     }
144 
145     public static String toString(Calendar cal) {
146         StringBuilder sb = new StringBuilder();
147 
148         sb.append(cal.get(Calendar.YEAR));
149         sb.append(StringPool.PERIOD);
150         sb.append(cal.get(Calendar.MONTH));
151         sb.append(StringPool.PERIOD);
152         sb.append(cal.get(Calendar.DATE));
153         sb.append(StringPool.PERIOD);
154         sb.append(cal.getTimeZone().getRawOffset());
155 
156         return sb.toString();
157     }
158 
159 }