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