001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.calendar.util;
016    
017    import com.liferay.portal.kernel.cal.TZSRecurrence;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Time;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.PropsUtil;
029    import com.liferay.portal.util.PropsValues;
030    import com.liferay.portlet.calendar.model.CalEvent;
031    import com.liferay.util.ContentUtil;
032    
033    import java.util.Calendar;
034    import java.util.Date;
035    import java.util.Locale;
036    import java.util.TimeZone;
037    
038    import javax.portlet.PortletPreferences;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     */
043    public class CalUtil {
044    
045            public static Date getDaylightSavingTimeOffsetDate(
046                    CalEvent event, TimeZone userTimeZone, Calendar cal, Date date) {
047    
048                    TZSRecurrence recurrence = event.getRecurrenceObj();
049    
050                    TimeZone eventTimeZone = recurrence.getTimeZone();
051    
052                    if (eventTimeZone.inDaylightTime(cal.getTime()) ==
053                                    userTimeZone.inDaylightTime(cal.getTime())) {
054    
055                            return date;
056                    }
057    
058                    Calendar calendar = Calendar.getInstance();
059    
060                    calendar.setTime(date);
061    
062                    if (eventTimeZone.inDaylightTime(cal.getTime())) {
063                            calendar.add(Calendar.HOUR_OF_DAY, -1);
064                    }
065                    else {
066                            calendar.add(Calendar.HOUR_OF_DAY, 1);
067                    }
068    
069                    return calendar.getTime();
070            }
071    
072            public static String getEmailEventReminderBody(
073                    PortletPreferences preferences) {
074    
075                    String emailEventReminderBody = preferences.getValue(
076                            "emailEventReminderBody", StringPool.BLANK);
077    
078                    if (Validator.isNotNull(emailEventReminderBody)) {
079                            return emailEventReminderBody;
080                    }
081                    else {
082                            return ContentUtil.get(
083                                    PropsUtil.get(PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_BODY));
084                    }
085            }
086    
087            public static boolean getEmailEventReminderEnabled(
088                    PortletPreferences preferences) {
089    
090                    String emailEventReminderEnabled = preferences.getValue(
091                            "emailEventReminderEnabled", StringPool.BLANK);
092    
093                    if (Validator.isNotNull(emailEventReminderEnabled)) {
094                            return GetterUtil.getBoolean(emailEventReminderEnabled);
095                    }
096                    else {
097                            return GetterUtil.getBoolean(
098                                    PropsUtil.get(PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_ENABLED));
099                    }
100            }
101    
102            public static String getEmailEventReminderSubject(
103                    PortletPreferences preferences) {
104    
105                    String emailEventReminderSubject = preferences.getValue(
106                            "emailEventReminderSubject", StringPool.BLANK);
107    
108                    if (Validator.isNotNull(emailEventReminderSubject)) {
109                            return emailEventReminderSubject;
110                    }
111                    else {
112                            return ContentUtil.get(
113                                    PropsUtil.get(PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_SUBJECT));
114                    }
115            }
116    
117            public static String getEmailFromAddress(
118                            PortletPreferences preferences, long companyId)
119                    throws SystemException {
120    
121                    return PortalUtil.getEmailFromAddress(
122                            preferences, companyId, PropsValues.CALENDAR_EMAIL_FROM_ADDRESS);
123            }
124    
125            public static String getEmailFromName(
126                            PortletPreferences preferences, long companyId)
127                    throws SystemException {
128    
129                    return PortalUtil.getEmailFromName(
130                            preferences, companyId, PropsValues.CALENDAR_EMAIL_FROM_NAME);
131            }
132    
133            public static Date getEndTime(CalEvent event) {
134                    long startTime = event.getStartDate().getTime();
135    
136                    long endTime =
137                            startTime + (Time.HOUR * event.getDurationHour()) +
138                                    (Time.MINUTE * event.getDurationMinute());
139    
140                    return new Date(endTime);
141            }
142    
143            public static boolean isAllDay(
144                    CalEvent event, TimeZone timeZone, Locale locale) {
145    
146                    if (event.isAllDay()) {
147                            return true;
148                    }
149    
150                    Calendar cal = null;
151    
152                    if (event.getTimeZoneSensitive()) {
153                            cal = CalendarFactoryUtil.getCalendar(timeZone, locale);
154                    }
155                    else {
156                            cal = CalendarFactoryUtil.getCalendar();
157                    }
158    
159                    cal.setTime(event.getStartDate());
160    
161                    int hour = cal.get(Calendar.HOUR_OF_DAY);
162                    int minute = cal.get(Calendar.MINUTE);
163                    int second = cal.get(Calendar.SECOND);
164                    int millisecond = cal.get(Calendar.MILLISECOND);
165    
166                    int dHour = event.getDurationHour();
167                    int dMinute = event.getDurationMinute();
168    
169                    if ((hour == 0) && (minute == 0) && (second == 0) &&
170                            (millisecond == 0) && (dHour == 24) && (dMinute == 0)) {
171    
172                            return true;
173                    }
174    
175                    return false;
176            }
177    
178            public static String toString(Calendar cal, String[] types) {
179                    StringBundler sb = new StringBundler(9);
180    
181                    if (cal != null) {
182                            sb.append(cal.get(Calendar.YEAR));
183                            sb.append(StringPool.PERIOD);
184                            sb.append(cal.get(Calendar.MONTH));
185                            sb.append(StringPool.PERIOD);
186                            sb.append(cal.get(Calendar.DATE));
187                            sb.append(StringPool.PERIOD);
188                            sb.append(cal.getTimeZone().getRawOffset());
189                    }
190    
191                    if ((types != null) && (types.length > 0) &&
192                            ((types.length > 1) || Validator.isNotNull(types[0]))) {
193    
194                            sb.append(StringPool.PERIOD);
195                            sb.append(StringUtil.merge(types, StringPool.PERIOD));
196                    }
197    
198                    return sb.toString();
199            }
200    
201    }