001    /**
002     * Copyright (c) 2000-2012 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.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());
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());
113                            }
114                            else if (e instanceof AssetCategoryException ||
115                                             e instanceof AssetTagException) {
116    
117                                    SessionErrors.add(actionRequest, e.getClass(), 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());
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(
307                                            dayPos.toArray(new DayAndPosition[dayPos.size()]));
308                            }
309                            else if (recurrenceType == Recurrence.MONTHLY) {
310                                    int monthlyType = ParamUtil.getInteger(
311                                            actionRequest, "monthlyType");
312    
313                                    if (monthlyType == 0) {
314                                            int monthlyDay = ParamUtil.getInteger(
315                                                    actionRequest, "monthlyDay0");
316    
317                                            recurrence.setByMonthDay(new int[] {monthlyDay});
318    
319                                            int monthlyInterval = ParamUtil.getInteger(
320                                                    actionRequest, "monthlyInterval0", 1);
321    
322                                            recurrence.setInterval(monthlyInterval);
323                                    }
324                                    else {
325                                            int monthlyPos = ParamUtil.getInteger(
326                                                    actionRequest, "monthlyPos");
327                                            int monthlyDay = ParamUtil.getInteger(
328                                                    actionRequest, "monthlyDay1");
329    
330                                            DayAndPosition[] dayPos = {
331                                                    new DayAndPosition(monthlyDay, monthlyPos)};
332    
333                                            recurrence.setByDay(dayPos);
334    
335                                            int monthlyInterval = ParamUtil.getInteger(
336                                                    actionRequest, "monthlyInterval1", 1);
337    
338                                            recurrence.setInterval(monthlyInterval);
339                                    }
340                            }
341                            else if (recurrenceType == Recurrence.YEARLY) {
342                                    int yearlyType = ParamUtil.getInteger(
343                                            actionRequest, "yearlyType");
344    
345                                    if (yearlyType == 0) {
346                                            int yearlyMonth = ParamUtil.getInteger(
347                                                    actionRequest, "yearlyMonth0");
348                                            int yearlyDay = ParamUtil.getInteger(
349                                                    actionRequest, "yearlyDay0");
350    
351                                            recurrence.setByMonth(new int[] {yearlyMonth});
352                                            recurrence.setByMonthDay(new int[] {yearlyDay});
353    
354                                            int yearlyInterval = ParamUtil.getInteger(
355                                                    actionRequest, "yearlyInterval0", 1);
356    
357                                            recurrence.setInterval(yearlyInterval);
358                                    }
359                                    else {
360                                            int yearlyPos = ParamUtil.getInteger(
361                                                    actionRequest, "yearlyPos");
362                                            int yearlyDay = ParamUtil.getInteger(
363                                                    actionRequest, "yearlyDay1");
364                                            int yearlyMonth = ParamUtil.getInteger(
365                                                    actionRequest, "yearlyMonth1");
366    
367                                            DayAndPosition[] dayPos = {
368                                                    new DayAndPosition(yearlyDay, yearlyPos)};
369    
370                                            recurrence.setByDay(dayPos);
371    
372                                            recurrence.setByMonth(new int[] {yearlyMonth});
373    
374                                            int yearlyInterval = ParamUtil.getInteger(
375                                                    actionRequest, "yearlyInterval1", 1);
376    
377                                            recurrence.setInterval(yearlyInterval);
378                                    }
379                            }
380    
381                            int endDateType = ParamUtil.getInteger(
382                                    actionRequest, "endDateType");
383    
384                            if (endDateType == 1) {
385                                    int endDateOccurrence = ParamUtil.getInteger(
386                                            actionRequest, "endDateOccurrence");
387    
388                                    recurrence.setOccurrence(endDateOccurrence);
389                            }
390                            else if (endDateType == 2) {
391                                    Calendar endDate = CalendarFactoryUtil.getCalendar(timeZone);
392    
393                                    endDate.set(Calendar.MONTH, endDateMonth);
394                                    endDate.set(Calendar.DATE, endDateDay);
395                                    endDate.set(Calendar.YEAR, endDateYear);
396                                    endDate.set(Calendar.HOUR_OF_DAY, startDateHour);
397                                    endDate.set(Calendar.MINUTE, startDateMinute);
398                                    endDate.set(Calendar.SECOND, 0);
399                                    endDate.set(Calendar.MILLISECOND, 0);
400    
401                                    Calendar recEndCal = null;
402    
403                                    if (timeZoneSensitive) {
404                                            recEndCal = CalendarFactoryUtil.getCalendar(
405                                                    TimeZoneUtil.getTimeZone(StringPool.UTC));
406    
407                                            recEndCal.setTime(endDate.getTime());
408                                    }
409                                    else {
410                                            recEndCal = (Calendar)endDate.clone();
411                                    }
412    
413                                    recurrence.setUntil(recEndCal);
414                            }
415                    }
416    
417                    int remindBy = ParamUtil.getInteger(actionRequest, "remindBy");
418                    int firstReminder = ParamUtil.getInteger(
419                            actionRequest, "firstReminder");
420                    int secondReminder = ParamUtil.getInteger(
421                            actionRequest, "secondReminder");
422    
423                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
424                            CalEvent.class.getName(), actionRequest);
425    
426                    if (eventId <= 0) {
427    
428                            // Add event
429    
430                            CalEvent event = CalEventServiceUtil.addEvent(
431                                    title, description, location, startDateMonth, startDateDay,
432                                    startDateYear, startDateHour, startDateMinute, endDateMonth,
433                                    endDateDay, endDateYear, durationHour, durationMinute, allDay,
434                                    timeZoneSensitive, type, repeating, recurrence, remindBy,
435                                    firstReminder, secondReminder, serviceContext);
436    
437                            AssetPublisherUtil.addAndStoreSelection(
438                                    actionRequest, CalEvent.class.getName(), event.getEventId(),
439                                    -1);
440                    }
441                    else {
442    
443                            // Update event
444    
445                            CalEventServiceUtil.updateEvent(
446                                    eventId, title, description, location, startDateMonth,
447                                    startDateDay, startDateYear, startDateHour, startDateMinute,
448                                    endDateMonth, endDateDay, endDateYear, durationHour,
449                                    durationMinute, allDay, timeZoneSensitive, type, repeating,
450                                    recurrence, remindBy, firstReminder, secondReminder,
451                                    serviceContext);
452                    }
453            }
454    
455    }