001    /**
002     * Copyright (c) 2000-present 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.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     * @deprecated As of 7.0.0, with no direct replacement
042     */
043    @Deprecated
044    public class CalUtil {
045    
046            public static Date getDaylightSavingTimeOffsetDate(
047                    CalEvent event, TimeZone userTimeZone, Calendar cal, Date date) {
048    
049                    TZSRecurrence recurrence = event.getRecurrenceObj();
050    
051                    TimeZone eventTimeZone = recurrence.getTimeZone();
052    
053                    if (eventTimeZone.inDaylightTime(cal.getTime()) ==
054                                    userTimeZone.inDaylightTime(cal.getTime())) {
055    
056                            return date;
057                    }
058    
059                    Calendar calendar = Calendar.getInstance();
060    
061                    calendar.setTime(date);
062    
063                    if (eventTimeZone.inDaylightTime(cal.getTime())) {
064                            calendar.add(Calendar.HOUR_OF_DAY, -1);
065                    }
066                    else {
067                            calendar.add(Calendar.HOUR_OF_DAY, 1);
068                    }
069    
070                    return calendar.getTime();
071            }
072    
073            public static String getEmailEventReminderBody(
074                    PortletPreferences preferences) {
075    
076                    String emailEventReminderBody = preferences.getValue(
077                            "emailEventReminderBody", StringPool.BLANK);
078    
079                    if (Validator.isNotNull(emailEventReminderBody)) {
080                            return emailEventReminderBody;
081                    }
082                    else {
083                            return ContentUtil.get(
084                                    PropsUtil.get(PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_BODY));
085                    }
086            }
087    
088            public static boolean getEmailEventReminderEnabled(
089                    PortletPreferences preferences) {
090    
091                    String emailEventReminderEnabled = preferences.getValue(
092                            "emailEventReminderEnabled", StringPool.BLANK);
093    
094                    if (Validator.isNotNull(emailEventReminderEnabled)) {
095                            return GetterUtil.getBoolean(emailEventReminderEnabled);
096                    }
097                    else {
098                            return GetterUtil.getBoolean(
099                                    PropsUtil.get(PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_ENABLED));
100                    }
101            }
102    
103            public static String getEmailEventReminderSubject(
104                    PortletPreferences preferences) {
105    
106                    String emailEventReminderSubject = preferences.getValue(
107                            "emailEventReminderSubject", StringPool.BLANK);
108    
109                    if (Validator.isNotNull(emailEventReminderSubject)) {
110                            return emailEventReminderSubject;
111                    }
112                    else {
113                            return ContentUtil.get(
114                                    PropsUtil.get(PropsKeys.CALENDAR_EMAIL_EVENT_REMINDER_SUBJECT));
115                    }
116            }
117    
118            public static String getEmailFromAddress(
119                    PortletPreferences preferences, long companyId) {
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    
128                    return PortalUtil.getEmailFromName(
129                            preferences, companyId, PropsValues.CALENDAR_EMAIL_FROM_NAME);
130            }
131    
132            public static Date getEndTime(CalEvent event) {
133                    long startTime = event.getStartDate().getTime();
134    
135                    long endTime =
136                            startTime + (Time.HOUR * event.getDurationHour()) +
137                                    (Time.MINUTE * event.getDurationMinute());
138    
139                    return new Date(endTime);
140            }
141    
142            public static boolean isAllDay(
143                    CalEvent event, TimeZone timeZone, Locale locale) {
144    
145                    if (event.isAllDay()) {
146                            return true;
147                    }
148    
149                    Calendar cal = null;
150    
151                    if (event.getTimeZoneSensitive()) {
152                            cal = CalendarFactoryUtil.getCalendar(timeZone, locale);
153                    }
154                    else {
155                            cal = CalendarFactoryUtil.getCalendar();
156                    }
157    
158                    cal.setTime(event.getStartDate());
159    
160                    int hour = cal.get(Calendar.HOUR_OF_DAY);
161                    int minute = cal.get(Calendar.MINUTE);
162                    int second = cal.get(Calendar.SECOND);
163                    int millisecond = cal.get(Calendar.MILLISECOND);
164    
165                    int dHour = event.getDurationHour();
166                    int dMinute = event.getDurationMinute();
167    
168                    if ((hour == 0) && (minute == 0) && (second == 0) &&
169                            (millisecond == 0) && (dHour == 24) && (dMinute == 0)) {
170    
171                            return true;
172                    }
173    
174                    return false;
175            }
176    
177            public static String toString(Calendar cal, String[] types) {
178                    StringBundler sb = new StringBundler(9);
179    
180                    if (cal != null) {
181                            sb.append(cal.get(Calendar.YEAR));
182                            sb.append(StringPool.PERIOD);
183                            sb.append(cal.get(Calendar.MONTH));
184                            sb.append(StringPool.PERIOD);
185                            sb.append(cal.get(Calendar.DATE));
186                            sb.append(StringPool.PERIOD);
187                            sb.append(cal.getTimeZone().getRawOffset());
188                    }
189    
190                    if ((types != null) && (types.length > 0) &&
191                            ((types.length > 1) || Validator.isNotNull(types[0]))) {
192    
193                            sb.append(StringPool.PERIOD);
194                            sb.append(StringUtil.merge(types, StringPool.PERIOD));
195                    }
196    
197                    return sb.toString();
198            }
199    
200    }