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