001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 getEmailFromAddress(
045                            PortletPreferences preferences, long companyId)
046                    throws SystemException {
047    
048                    return PortalUtil.getEmailFromAddress(
049                            preferences, companyId, PropsValues.CALENDAR_EMAIL_FROM_ADDRESS);
050            }
051    
052            public static String getEmailFromName(
053                            PortletPreferences preferences, long companyId)
054                    throws SystemException {
055    
056                    return PortalUtil.getEmailFromName(
057                            preferences, companyId, PropsValues.CALENDAR_EMAIL_FROM_NAME);
058            }
059    
060            public static boolean getEmailEventReminderEnabled(
061                    PortletPreferences preferences) {
062    
063                    String emailEventReminderEnabled = preferences.getValue(
064                            "emailEventReminderEnabled", StringPool.BLANK);
065    
066                    if (Validator.isNotNull(emailEventReminderEnabled)) {
067                            return GetterUtil.getBoolean(emailEventReminderEnabled);
068                    }
069                    else {
070                            return GetterUtil.getBoolean(PropsUtil.get(
071                                    PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_ENABLED));
072                    }
073            }
074    
075            public static String getEmailEventReminderBody(
076                    PortletPreferences preferences) {
077    
078                    String emailEventReminderBody = preferences.getValue(
079                            "emailEventReminderBody", StringPool.BLANK);
080    
081                    if (Validator.isNotNull(emailEventReminderBody)) {
082                            return emailEventReminderBody;
083                    }
084                    else {
085                            return ContentUtil.get(PropsUtil.get(
086                                    PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_BODY));
087                    }
088            }
089    
090            public static String getEmailEventReminderSubject(
091                    PortletPreferences preferences) {
092    
093                    String emailEventReminderSubject = preferences.getValue(
094                            "emailEventReminderSubject", StringPool.BLANK);
095    
096                    if (Validator.isNotNull(emailEventReminderSubject)) {
097                            return emailEventReminderSubject;
098                    }
099                    else {
100                            return ContentUtil.get(PropsUtil.get(
101                                    PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_SUBJECT));
102                    }
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    }