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