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(PropsUtil.get(
055                                    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(PropsUtil.get(
070                                    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(PropsUtil.get(
085                                    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                    Calendar cal = null;
119    
120                    if (event.getTimeZoneSensitive()) {
121                            cal = CalendarFactoryUtil.getCalendar(timeZone, locale);
122                    }
123                    else {
124                            cal = CalendarFactoryUtil.getCalendar();
125                    }
126    
127                    cal.setTime(event.getStartDate());
128    
129                    int hour = cal.get(Calendar.HOUR_OF_DAY);
130                    int minute = cal.get(Calendar.MINUTE);
131                    int second = cal.get(Calendar.SECOND);
132                    int millisecond = cal.get(Calendar.MILLISECOND);
133    
134                    int dHour = event.getDurationHour();
135                    int dMinute = event.getDurationMinute();
136    
137                    if ((hour == 0) && (minute == 0) && (second == 0) &&
138                            (millisecond == 0) && (dHour == 24) && (dMinute == 0)) {
139    
140                            return true;
141                    }
142    
143                    return false;
144            }
145    
146            public static String toString(Calendar cal, String[] types) {
147    
148                    StringBundler sb = new StringBundler(9);
149    
150                    if (cal != null) {
151                            sb.append(cal.get(Calendar.YEAR));
152                            sb.append(StringPool.PERIOD);
153                            sb.append(cal.get(Calendar.MONTH));
154                            sb.append(StringPool.PERIOD);
155                            sb.append(cal.get(Calendar.DATE));
156                            sb.append(StringPool.PERIOD);
157                            sb.append(cal.getTimeZone().getRawOffset());
158                    }
159    
160                    if ((types != null) && (types.length > 0) &&
161                            ((types.length > 1) || Validator.isNotNull(types[0]))) {
162    
163                            sb.append(StringPool.PERIOD);
164                            sb.append(StringUtil.merge(types, StringPool.PERIOD));
165                    }
166    
167                    return sb.toString();
168            }
169    
170    }