001    /**
002     * Copyright (c) 2000-2012 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.exception.SystemException;
018    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Time;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portal.util.PropsUtil;
028    import com.liferay.portal.util.PropsValues;
029    import com.liferay.portlet.calendar.model.CalEvent;
030    import com.liferay.util.ContentUtil;
031    
032    import java.util.Calendar;
033    import java.util.Date;
034    import java.util.Locale;
035    import java.util.TimeZone;
036    
037    import javax.portlet.PortletPreferences;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class CalUtil {
043    
044            public static String getEmailEventReminderBody(
045                    PortletPreferences preferences) {
046    
047                    String emailEventReminderBody = preferences.getValue(
048                            "emailEventReminderBody", StringPool.BLANK);
049    
050                    if (Validator.isNotNull(emailEventReminderBody)) {
051                            return emailEventReminderBody;
052                    }
053                    else {
054                            return ContentUtil.get(
055                                    PropsUtil.get(PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_BODY));
056                    }
057            }
058    
059            public static boolean getEmailEventReminderEnabled(
060                    PortletPreferences preferences) {
061    
062                    String emailEventReminderEnabled = preferences.getValue(
063                            "emailEventReminderEnabled", StringPool.BLANK);
064    
065                    if (Validator.isNotNull(emailEventReminderEnabled)) {
066                            return GetterUtil.getBoolean(emailEventReminderEnabled);
067                    }
068                    else {
069                            return GetterUtil.getBoolean(
070                                    PropsUtil.get(PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_ENABLED));
071                    }
072            }
073    
074            public static String getEmailEventReminderSubject(
075                    PortletPreferences preferences) {
076    
077                    String emailEventReminderSubject = preferences.getValue(
078                            "emailEventReminderSubject", StringPool.BLANK);
079    
080                    if (Validator.isNotNull(emailEventReminderSubject)) {
081                            return emailEventReminderSubject;
082                    }
083                    else {
084                            return ContentUtil.get(
085                                    PropsUtil.get(PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_SUBJECT));
086                    }
087            }
088    
089            public static String getEmailFromAddress(
090                            PortletPreferences preferences, long companyId)
091                    throws SystemException {
092    
093                    return PortalUtil.getEmailFromAddress(
094                            preferences, companyId, PropsValues.CALENDAR_EMAIL_FROM_ADDRESS);
095            }
096    
097            public static String getEmailFromName(
098                            PortletPreferences preferences, long companyId)
099                    throws SystemException {
100    
101                    return PortalUtil.getEmailFromName(
102                            preferences, companyId, PropsValues.CALENDAR_EMAIL_FROM_NAME);
103            }
104    
105            public static Date getEndTime(CalEvent event) {
106                    long startTime = event.getStartDate().getTime();
107    
108                    long endTime =
109                            startTime + (Time.HOUR * event.getDurationHour()) +
110                                    (Time.MINUTE * event.getDurationMinute());
111    
112                    return new Date(endTime);
113            }
114    
115            public static boolean isAllDay(
116                    CalEvent event, TimeZone timeZone, Locale locale) {
117    
118                    if (event.isAllDay()) {
119                            return true;
120                    }
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, String[] types) {
151                    StringBundler sb = new StringBundler(9);
152    
153                    if (cal != null) {
154                            sb.append(cal.get(Calendar.YEAR));
155                            sb.append(StringPool.PERIOD);
156                            sb.append(cal.get(Calendar.MONTH));
157                            sb.append(StringPool.PERIOD);
158                            sb.append(cal.get(Calendar.DATE));
159                            sb.append(StringPool.PERIOD);
160                            sb.append(cal.getTimeZone().getRawOffset());
161                    }
162    
163                    if ((types != null) && (types.length > 0) &&
164                            ((types.length > 1) || Validator.isNotNull(types[0]))) {
165    
166                            sb.append(StringPool.PERIOD);
167                            sb.append(StringUtil.merge(types, StringPool.PERIOD));
168                    }
169    
170                    return sb.toString();
171            }
172    
173    }