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.action;
016    
017    import com.liferay.portal.kernel.cal.DayAndPosition;
018    import com.liferay.portal.kernel.cal.Duration;
019    import com.liferay.portal.kernel.cal.Recurrence;
020    import com.liferay.portal.kernel.cal.TZSRecurrence;
021    import com.liferay.portal.kernel.portlet.LiferayWindowState;
022    import com.liferay.portal.kernel.servlet.SessionErrors;
023    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
024    import com.liferay.portal.kernel.util.Constants;
025    import com.liferay.portal.kernel.util.LocaleUtil;
026    import com.liferay.portal.kernel.util.ParamUtil;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.TimeZoneUtil;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.model.User;
031    import com.liferay.portal.security.auth.PrincipalException;
032    import com.liferay.portal.service.ServiceContext;
033    import com.liferay.portal.service.ServiceContextFactory;
034    import com.liferay.portal.struts.PortletAction;
035    import com.liferay.portal.util.PortalUtil;
036    import com.liferay.portlet.asset.AssetCategoryException;
037    import com.liferay.portlet.asset.AssetTagException;
038    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
039    import com.liferay.portlet.calendar.EventDurationException;
040    import com.liferay.portlet.calendar.EventEndDateException;
041    import com.liferay.portlet.calendar.EventStartDateException;
042    import com.liferay.portlet.calendar.EventTitleException;
043    import com.liferay.portlet.calendar.NoSuchEventException;
044    import com.liferay.portlet.calendar.model.CalEvent;
045    import com.liferay.portlet.calendar.model.CalEventConstants;
046    import com.liferay.portlet.calendar.service.CalEventServiceUtil;
047    
048    import java.util.ArrayList;
049    import java.util.Calendar;
050    import java.util.List;
051    import java.util.Locale;
052    import java.util.TimeZone;
053    
054    import javax.portlet.ActionRequest;
055    import javax.portlet.ActionResponse;
056    import javax.portlet.PortletConfig;
057    import javax.portlet.RenderRequest;
058    import javax.portlet.RenderResponse;
059    import javax.portlet.WindowState;
060    
061    import org.apache.struts.action.ActionForm;
062    import org.apache.struts.action.ActionForward;
063    import org.apache.struts.action.ActionMapping;
064    
065    /**
066     * @author Brian Wing Shun Chan
067     */
068    public class EditEventAction extends PortletAction {
069    
070            @Override
071            public void processAction(
072                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
073                            ActionRequest actionRequest, ActionResponse actionResponse)
074                    throws Exception {
075    
076                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
077    
078                    try {
079                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
080                                    updateEvent(actionRequest);
081                            }
082                            else if (cmd.equals(Constants.DELETE)) {
083                                    deleteEvent(actionRequest);
084                            }
085    
086                            WindowState windowState = actionRequest.getWindowState();
087    
088                            if (!windowState.equals(LiferayWindowState.POP_UP)) {
089                                    sendRedirect(actionRequest, actionResponse);
090                            }
091                            else {
092                                    String redirect = PortalUtil.escapeRedirect(
093                                            ParamUtil.getString(actionRequest, "redirect"));
094    
095                                    if (Validator.isNotNull(redirect)) {
096                                            actionResponse.sendRedirect(redirect);
097                                    }
098                            }
099                    }
100                    catch (Exception e) {
101                            if (e instanceof NoSuchEventException ||
102                                    e instanceof PrincipalException) {
103    
104                                    SessionErrors.add(actionRequest, e.getClass());
105    
106                                    setForward(actionRequest, "portlet.calendar.error");
107                            }
108                            else if (e instanceof EventDurationException ||
109                                             e instanceof EventEndDateException ||
110                                             e instanceof EventStartDateException ||
111                                             e instanceof EventTitleException) {
112    
113                                    SessionErrors.add(actionRequest, e.getClass());
114                            }
115                            else if (e instanceof AssetCategoryException ||
116                                             e instanceof AssetTagException) {
117    
118                                    SessionErrors.add(actionRequest, e.getClass(), e);
119                            }
120                            else {
121                                    throw e;
122                            }
123                    }
124            }
125    
126            @Override
127            public ActionForward render(
128                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
129                            RenderRequest renderRequest, RenderResponse renderResponse)
130                    throws Exception {
131    
132                    try {
133                            ActionUtil.getEvent(renderRequest);
134                    }
135                    catch (Exception e) {
136                            if (e instanceof NoSuchEventException ||
137                                    e instanceof PrincipalException) {
138    
139                                    SessionErrors.add(renderRequest, e.getClass());
140    
141                                    return mapping.findForward("portlet.calendar.error");
142                            }
143                            else {
144                                    throw e;
145                            }
146                    }
147    
148                    return mapping.findForward(
149                            getForward(renderRequest, "portlet.calendar.edit_event"));
150            }
151    
152            protected void addWeeklyDayPos(
153                    ActionRequest actionRequest, List<DayAndPosition> list, int day) {
154    
155                    if (ParamUtil.getBoolean(actionRequest, "weeklyDayPos" + day)) {
156                            list.add(new DayAndPosition(day, 0));
157                    }
158            }
159    
160            protected void deleteEvent(ActionRequest actionRequest) throws Exception {
161                    long eventId = ParamUtil.getLong(actionRequest, "eventId");
162    
163                    CalEventServiceUtil.deleteEvent(eventId);
164            }
165    
166            protected void updateEvent(ActionRequest actionRequest) throws Exception {
167                    long eventId = ParamUtil.getLong(actionRequest, "eventId");
168    
169                    String title = ParamUtil.getString(actionRequest, "title");
170                    String description = ParamUtil.getString(actionRequest, "description");
171                    String location = ParamUtil.getString(actionRequest, "location");
172    
173                    int startDateMonth = ParamUtil.getInteger(
174                            actionRequest, "startDateMonth");
175                    int startDateDay = ParamUtil.getInteger(actionRequest, "startDateDay");
176                    int startDateYear = ParamUtil.getInteger(
177                            actionRequest, "startDateYear");
178                    int startDateHour = ParamUtil.getInteger(
179                            actionRequest, "startDateHour");
180                    int startDateMinute = ParamUtil.getInteger(
181                            actionRequest, "startDateMinute");
182                    int startDateAmPm = ParamUtil.getInteger(
183                            actionRequest, "startDateAmPm");
184    
185                    if (startDateAmPm == Calendar.PM) {
186                            startDateHour += 12;
187                    }
188    
189                    int durationHour = ParamUtil.getInteger(actionRequest, "durationHour");
190                    int durationMinute = ParamUtil.getInteger(
191                            actionRequest, "durationMinute");
192                    boolean allDay = ParamUtil.getBoolean(actionRequest, "allDay");
193    
194                    boolean timeZoneSensitive = false;
195    
196                    if (allDay) {
197                            timeZoneSensitive = false;
198                    }
199                    else {
200                            timeZoneSensitive = ParamUtil.getBoolean(
201                                    actionRequest, "timeZoneSensitive");
202                    }
203    
204                    String type = ParamUtil.getString(actionRequest, "type");
205    
206                    int endDateMonth = ParamUtil.getInteger(actionRequest, "endDateMonth");
207                    int endDateDay = ParamUtil.getInteger(actionRequest, "endDateDay");
208                    int endDateYear = ParamUtil.getInteger(actionRequest, "endDateYear");
209    
210                    boolean repeating = false;
211    
212                    int recurrenceType = ParamUtil.getInteger(
213                            actionRequest, "recurrenceType");
214    
215                    if (recurrenceType != Recurrence.NO_RECURRENCE) {
216                            repeating = true;
217                    }
218    
219                    Locale locale = null;
220                    TimeZone timeZone = null;
221    
222                    if (timeZoneSensitive) {
223                            User user = PortalUtil.getUser(actionRequest);
224    
225                            locale = user.getLocale();
226                            timeZone = user.getTimeZone();
227                    }
228                    else {
229                            locale = LocaleUtil.getDefault();
230                            timeZone = TimeZoneUtil.getDefault();
231                    }
232    
233                    Calendar startDate = CalendarFactoryUtil.getCalendar(timeZone, locale);
234    
235                    startDate.set(Calendar.MONTH, startDateMonth);
236                    startDate.set(Calendar.DATE, startDateDay);
237                    startDate.set(Calendar.YEAR, startDateYear);
238                    startDate.set(Calendar.HOUR_OF_DAY, startDateHour);
239                    startDate.set(Calendar.MINUTE, startDateMinute);
240                    startDate.set(Calendar.SECOND, 0);
241                    startDate.set(Calendar.MILLISECOND, 0);
242    
243                    if (allDay) {
244                            startDate.set(Calendar.HOUR_OF_DAY, 0);
245                            startDate.set(Calendar.MINUTE, 0);
246                            startDate.set(Calendar.SECOND, 0);
247                            startDate.set(Calendar.MILLISECOND, 0);
248    
249                            durationHour = 24;
250                            durationMinute = 0;
251                    }
252    
253                    TZSRecurrence recurrence = null;
254    
255                    if (repeating) {
256                            Calendar recStartCal = null;
257    
258                            if (timeZoneSensitive) {
259                                    recStartCal = CalendarFactoryUtil.getCalendar(
260                                            TimeZoneUtil.getTimeZone(StringPool.UTC));
261    
262                                    recStartCal.setTime(startDate.getTime());
263                            }
264                            else {
265                                    recStartCal = (Calendar)startDate.clone();
266                            }
267    
268                            recurrence = new TZSRecurrence(
269                                    recStartCal, new Duration(1, 0, 0, 0), recurrenceType);
270    
271                            recurrence.setTimeZone(timeZone);
272    
273                            recurrence.setWeekStart(Calendar.SUNDAY);
274    
275                            if (recurrenceType == Recurrence.DAILY) {
276                                    int dailyType = ParamUtil.getInteger(
277                                            actionRequest, "dailyType");
278    
279                                    if (dailyType == 0) {
280                                            int dailyInterval = ParamUtil.getInteger(
281                                                    actionRequest, "dailyInterval", 1);
282    
283                                            recurrence.setInterval(dailyInterval);
284                                    }
285                                    else {
286                                            DayAndPosition[] dayPos = {
287                                                    new DayAndPosition(Calendar.MONDAY, 0),
288                                                    new DayAndPosition(Calendar.TUESDAY, 0),
289                                                    new DayAndPosition(Calendar.WEDNESDAY, 0),
290                                                    new DayAndPosition(Calendar.THURSDAY, 0),
291                                                    new DayAndPosition(Calendar.FRIDAY, 0)};
292    
293                                            recurrence.setByDay(dayPos);
294                                    }
295                            }
296                            else if (recurrenceType == Recurrence.WEEKLY) {
297                                    int weeklyInterval = ParamUtil.getInteger(
298                                            actionRequest, "weeklyInterval", 1);
299    
300                                    recurrence.setInterval(weeklyInterval);
301    
302                                    List<DayAndPosition> dayPos = new ArrayList<DayAndPosition>();
303    
304                                    addWeeklyDayPos(actionRequest, dayPos, Calendar.SUNDAY);
305                                    addWeeklyDayPos(actionRequest, dayPos, Calendar.MONDAY);
306                                    addWeeklyDayPos(actionRequest, dayPos, Calendar.TUESDAY);
307                                    addWeeklyDayPos(actionRequest, dayPos, Calendar.WEDNESDAY);
308                                    addWeeklyDayPos(actionRequest, dayPos, Calendar.THURSDAY);
309                                    addWeeklyDayPos(actionRequest, dayPos, Calendar.FRIDAY);
310                                    addWeeklyDayPos(actionRequest, dayPos, Calendar.SATURDAY);
311    
312                                    if (dayPos.size() == 0) {
313                                            dayPos.add(new DayAndPosition(Calendar.MONDAY, 0));
314                                    }
315    
316                                    recurrence.setByDay(
317                                            dayPos.toArray(new DayAndPosition[dayPos.size()]));
318                            }
319                            else if (recurrenceType == Recurrence.MONTHLY) {
320                                    int monthlyType = ParamUtil.getInteger(
321                                            actionRequest, "monthlyType");
322    
323                                    if (monthlyType == 0) {
324                                            int monthlyDay = ParamUtil.getInteger(
325                                                    actionRequest, "monthlyDay0");
326    
327                                            recurrence.setByMonthDay(new int[] {monthlyDay});
328    
329                                            int monthlyInterval = ParamUtil.getInteger(
330                                                    actionRequest, "monthlyInterval0", 1);
331    
332                                            recurrence.setInterval(monthlyInterval);
333                                    }
334                                    else {
335                                            int monthlyPos = ParamUtil.getInteger(
336                                                    actionRequest, "monthlyPos");
337                                            int monthlyDay = ParamUtil.getInteger(
338                                                    actionRequest, "monthlyDay1");
339    
340                                            DayAndPosition[] dayPos = {
341                                                    new DayAndPosition(monthlyDay, monthlyPos)};
342    
343                                            recurrence.setByDay(dayPos);
344    
345                                            int monthlyInterval = ParamUtil.getInteger(
346                                                    actionRequest, "monthlyInterval1", 1);
347    
348                                            recurrence.setInterval(monthlyInterval);
349                                    }
350                            }
351                            else if (recurrenceType == Recurrence.YEARLY) {
352                                    int yearlyType = ParamUtil.getInteger(
353                                            actionRequest, "yearlyType");
354    
355                                    if (yearlyType == 0) {
356                                            int yearlyMonth = ParamUtil.getInteger(
357                                                    actionRequest, "yearlyMonth0");
358                                            int yearlyDay = ParamUtil.getInteger(
359                                                    actionRequest, "yearlyDay0");
360    
361                                            recurrence.setByMonth(new int[] {yearlyMonth});
362                                            recurrence.setByMonthDay(new int[] {yearlyDay});
363    
364                                            int yearlyInterval = ParamUtil.getInteger(
365                                                    actionRequest, "yearlyInterval0", 1);
366    
367                                            recurrence.setInterval(yearlyInterval);
368                                    }
369                                    else {
370                                            int yearlyPos = ParamUtil.getInteger(
371                                                    actionRequest, "yearlyPos");
372                                            int yearlyDay = ParamUtil.getInteger(
373                                                    actionRequest, "yearlyDay1");
374                                            int yearlyMonth = ParamUtil.getInteger(
375                                                    actionRequest, "yearlyMonth1");
376    
377                                            DayAndPosition[] dayPos = {
378                                                    new DayAndPosition(yearlyDay, yearlyPos)};
379    
380                                            recurrence.setByDay(dayPos);
381    
382                                            recurrence.setByMonth(new int[] {yearlyMonth});
383    
384                                            int yearlyInterval = ParamUtil.getInteger(
385                                                    actionRequest, "yearlyInterval1", 1);
386    
387                                            recurrence.setInterval(yearlyInterval);
388                                    }
389                            }
390    
391                            int endDateType = ParamUtil.getInteger(
392                                    actionRequest, "endDateType");
393    
394                            if (endDateType == CalEventConstants.END_DATE_TYPE_END_AFTER) {
395                                    int endDateOccurrence = ParamUtil.getInteger(
396                                            actionRequest, "endDateOccurrence");
397    
398                                    recurrence.setOccurrence(endDateOccurrence);
399                            }
400                            else if (endDateType == CalEventConstants.END_DATE_TYPE_END_BY) {
401                                    Calendar endDate = CalendarFactoryUtil.getCalendar(timeZone);
402    
403                                    endDate.set(Calendar.MONTH, endDateMonth);
404                                    endDate.set(Calendar.DATE, endDateDay);
405                                    endDate.set(Calendar.YEAR, endDateYear);
406                                    endDate.set(Calendar.HOUR_OF_DAY, startDateHour);
407                                    endDate.set(Calendar.MINUTE, startDateMinute);
408                                    endDate.set(Calendar.SECOND, 0);
409                                    endDate.set(Calendar.MILLISECOND, 0);
410    
411                                    Calendar recEndCal = null;
412    
413                                    if (timeZoneSensitive) {
414                                            recEndCal = CalendarFactoryUtil.getCalendar(
415                                                    TimeZoneUtil.getTimeZone(StringPool.UTC));
416    
417                                            recEndCal.setTime(endDate.getTime());
418                                    }
419                                    else {
420                                            recEndCal = (Calendar)endDate.clone();
421                                    }
422    
423                                    recurrence.setUntil(recEndCal);
424                            }
425                    }
426    
427                    int remindBy = ParamUtil.getInteger(actionRequest, "remindBy");
428                    int firstReminder = ParamUtil.getInteger(
429                            actionRequest, "firstReminder");
430                    int secondReminder = ParamUtil.getInteger(
431                            actionRequest, "secondReminder");
432    
433                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
434                            CalEvent.class.getName(), actionRequest);
435    
436                    if (eventId <= 0) {
437    
438                            // Add event
439    
440                            CalEvent event = CalEventServiceUtil.addEvent(
441                                    title, description, location, startDateMonth, startDateDay,
442                                    startDateYear, startDateHour, startDateMinute, durationHour,
443                                    durationMinute, allDay, timeZoneSensitive, type, repeating,
444                                    recurrence, remindBy, firstReminder, secondReminder,
445                                    serviceContext);
446    
447                            AssetPublisherUtil.addAndStoreSelection(
448                                    actionRequest, CalEvent.class.getName(), event.getEventId(),
449                                    -1);
450                    }
451                    else {
452    
453                            // Update event
454    
455                            CalEventServiceUtil.updateEvent(
456                                    eventId, title, description, location, startDateMonth,
457                                    startDateDay, startDateYear, startDateHour, startDateMinute,
458                                    durationHour, durationMinute, allDay, timeZoneSensitive, type,
459                                    repeating, recurrence, remindBy, firstReminder, secondReminder,
460                                    serviceContext);
461                    }
462            }
463    
464    }