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