001    /**
002     * Copyright (c) 2000-2010 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.util.CalendarFactoryUtil;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.PropsKeys;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Time;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.util.ContentUtil;
025    import com.liferay.portal.util.PropsUtil;
026    import com.liferay.portlet.calendar.model.CalEvent;
027    
028    import java.util.Calendar;
029    import java.util.Date;
030    import java.util.Locale;
031    import java.util.TimeZone;
032    
033    import javax.portlet.PortletPreferences;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class CalUtil {
039    
040            public static String getEmailFromAddress(PortletPreferences preferences) {
041                    String emailFromAddress = PropsUtil.get(
042                            PropsKeys.CALENDAR_EMAIL_FROM_ADDRESS);
043    
044                    return preferences.getValue("email-from-address", emailFromAddress);
045            }
046    
047            public static String getEmailFromName(PortletPreferences preferences) {
048                    String emailFromName = PropsUtil.get(
049                            PropsKeys.CALENDAR_EMAIL_FROM_NAME);
050    
051                    return preferences.getValue("email-from-name", emailFromName);
052            }
053    
054            public static boolean getEmailEventReminderEnabled(
055                    PortletPreferences preferences) {
056    
057                    String emailEventReminderEnabled = preferences.getValue(
058                            "email-event-reminder-enabled", StringPool.BLANK);
059    
060                    if (Validator.isNotNull(emailEventReminderEnabled)) {
061                            return GetterUtil.getBoolean(emailEventReminderEnabled);
062                    }
063                    else {
064                            return GetterUtil.getBoolean(PropsUtil.get(
065                                    PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_ENABLED));
066                    }
067            }
068    
069            public static String getEmailEventReminderBody(
070                    PortletPreferences preferences) {
071    
072                    String emailEventReminderBody = preferences.getValue(
073                            "email-event-reminder-body", StringPool.BLANK);
074    
075                    if (Validator.isNotNull(emailEventReminderBody)) {
076                            return emailEventReminderBody;
077                    }
078                    else {
079                            return ContentUtil.get(PropsUtil.get(
080                                    PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_BODY));
081                    }
082            }
083    
084            public static String getEmailEventReminderSubject(
085                    PortletPreferences preferences) {
086    
087                    String emailEventReminderSubject = preferences.getValue(
088                            "email-event-reminder-subject", StringPool.BLANK);
089    
090                    if (Validator.isNotNull(emailEventReminderSubject)) {
091                            return emailEventReminderSubject;
092                    }
093                    else {
094                            return ContentUtil.get(PropsUtil.get(
095                                    PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_SUBJECT));
096                    }
097            }
098    
099            public static Date getEndTime(CalEvent event) {
100                    long startTime = event.getStartDate().getTime();
101    
102                    long endTime =
103                            startTime + (Time.HOUR * event.getDurationHour()) +
104                                    (Time.MINUTE * event.getDurationMinute());
105    
106                    return new Date(endTime);
107            }
108    
109            public static boolean isAllDay(
110                    CalEvent event, TimeZone timeZone, Locale locale) {
111    
112                    Calendar cal = null;
113    
114                    if (event.getTimeZoneSensitive()) {
115                            cal = CalendarFactoryUtil.getCalendar(timeZone, locale);
116                    }
117                    else {
118                            cal = CalendarFactoryUtil.getCalendar();
119                    }
120    
121                    cal.setTime(event.getStartDate());
122    
123                    int hour = cal.get(Calendar.HOUR_OF_DAY);
124                    int minute = cal.get(Calendar.MINUTE);
125                    int second = cal.get(Calendar.SECOND);
126                    int millisecond = cal.get(Calendar.MILLISECOND);
127    
128                    int dHour = event.getDurationHour();
129                    int dMinute = event.getDurationMinute();
130    
131                    if ((hour == 0) && (minute == 0) && (second == 0) &&
132                            (millisecond == 0) && (dHour == 24) && (dMinute == 0)) {
133    
134                            return true;
135                    }
136    
137                    return false;
138            }
139    
140            public static String toString(Calendar cal) {
141                    StringBundler sb = new StringBundler(7);
142    
143                    sb.append(cal.get(Calendar.YEAR));
144                    sb.append(StringPool.PERIOD);
145                    sb.append(cal.get(Calendar.MONTH));
146                    sb.append(StringPool.PERIOD);
147                    sb.append(cal.get(Calendar.DATE));
148                    sb.append(StringPool.PERIOD);
149                    sb.append(cal.getTimeZone().getRawOffset());
150    
151                    return sb.toString();
152            }
153    
154    }