1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class CalUtil {
48  
49      public static String getEmailFromAddress(PortletPreferences prefs) {
50          String emailFromAddress = PropsUtil.get(
51              PropsKeys.CALENDAR_EMAIL_FROM_ADDRESS);
52  
53          return prefs.getValue("email-from-address", emailFromAddress);
54      }
55  
56      public static String getEmailFromName(PortletPreferences prefs) {
57          String emailFromName = PropsUtil.get(
58              PropsKeys.CALENDAR_EMAIL_FROM_NAME);
59  
60          return prefs.getValue("email-from-name", emailFromName);
61      }
62  
63      public static boolean getEmailEventReminderEnabled(
64          PortletPreferences prefs) {
65  
66          String emailEventReminderEnabled = prefs.getValue(
67              "email-event-reminder-enabled", StringPool.BLANK);
68  
69          if (Validator.isNotNull(emailEventReminderEnabled)) {
70              return GetterUtil.getBoolean(emailEventReminderEnabled);
71          }
72          else {
73              return GetterUtil.getBoolean(PropsUtil.get(
74                  PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_ENABLED));
75          }
76      }
77  
78      public static String getEmailEventReminderBody(PortletPreferences prefs) {
79          String emailEventReminderBody = prefs.getValue(
80              "email-event-reminder-body", StringPool.BLANK);
81  
82          if (Validator.isNotNull(emailEventReminderBody)) {
83              return emailEventReminderBody;
84          }
85          else {
86              return ContentUtil.get(PropsUtil.get(
87                  PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_BODY));
88          }
89      }
90  
91      public static String getEmailEventReminderSubject(
92          PortletPreferences prefs) {
93  
94          String emailEventReminderSubject = prefs.getValue(
95              "email-event-reminder-subject", StringPool.BLANK);
96  
97          if (Validator.isNotNull(emailEventReminderSubject)) {
98              return emailEventReminderSubject;
99          }
100         else {
101             return ContentUtil.get(PropsUtil.get(
102                 PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_SUBJECT));
103         }
104     }
105 
106     public static Date getEndTime(CalEvent event) {
107         long startTime = event.getStartDate().getTime();
108 
109         long endTime =
110             startTime + (Time.HOUR * event.getDurationHour()) +
111                 (Time.MINUTE * event.getDurationMinute());
112 
113         return new Date(endTime);
114     }
115 
116     public static boolean isAllDay(
117         CalEvent event, TimeZone timeZone, Locale locale) {
118 
119         Calendar cal = null;
120 
121         if (event.getTimeZoneSensitive()) {
122             cal = CalendarFactoryUtil.getCalendar(timeZone, locale);
123         }
124         else {
125             cal = CalendarFactoryUtil.getCalendar();
126         }
127 
128         cal.setTime(event.getStartDate());
129 
130         int hour = cal.get(Calendar.HOUR_OF_DAY);
131         int minute = cal.get(Calendar.MINUTE);
132         int second = cal.get(Calendar.SECOND);
133         int millisecond = cal.get(Calendar.MILLISECOND);
134 
135         int dHour = event.getDurationHour();
136         int dMinute = event.getDurationMinute();
137 
138         if ((hour == 0) && (minute == 0) && (second == 0) &&
139             (millisecond == 0) && (dHour == 24) && (dMinute == 0)) {
140 
141             return true;
142         }
143 
144         return false;
145     }
146 
147     public static String toString(Calendar cal) {
148         StringBuilder sb = new StringBuilder();
149 
150         sb.append(cal.get(Calendar.YEAR));
151         sb.append(StringPool.PERIOD);
152         sb.append(cal.get(Calendar.MONTH));
153         sb.append(StringPool.PERIOD);
154         sb.append(cal.get(Calendar.DATE));
155         sb.append(StringPool.PERIOD);
156         sb.append(cal.getTimeZone().getRawOffset());
157 
158         return sb.toString();
159     }
160 
161 }