1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.calendar.action;
24  
25  import com.liferay.portal.kernel.cal.DayAndPosition;
26  import com.liferay.portal.kernel.cal.Duration;
27  import com.liferay.portal.kernel.cal.Recurrence;
28  import com.liferay.portal.kernel.cal.TZSRecurrence;
29  import com.liferay.portal.kernel.servlet.SessionErrors;
30  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
31  import com.liferay.portal.kernel.util.Constants;
32  import com.liferay.portal.kernel.util.LocaleUtil;
33  import com.liferay.portal.kernel.util.ParamUtil;
34  import com.liferay.portal.kernel.util.TimeZoneUtil;
35  import com.liferay.portal.model.Layout;
36  import com.liferay.portal.model.User;
37  import com.liferay.portal.security.auth.PrincipalException;
38  import com.liferay.portal.struts.PortletAction;
39  import com.liferay.portal.util.PortalUtil;
40  import com.liferay.portal.util.WebKeys;
41  import com.liferay.portlet.calendar.EventDurationException;
42  import com.liferay.portlet.calendar.EventEndDateException;
43  import com.liferay.portlet.calendar.EventStartDateException;
44  import com.liferay.portlet.calendar.EventTitleException;
45  import com.liferay.portlet.calendar.NoSuchEventException;
46  import com.liferay.portlet.calendar.service.CalEventServiceUtil;
47  
48  import java.util.ArrayList;
49  import java.util.Calendar;
50  import java.util.List;
51  import java.util.Locale;
52  import java.util.TimeZone;
53  
54  import javax.portlet.ActionRequest;
55  import javax.portlet.ActionResponse;
56  import javax.portlet.PortletConfig;
57  import javax.portlet.RenderRequest;
58  import javax.portlet.RenderResponse;
59  
60  import org.apache.struts.action.ActionForm;
61  import org.apache.struts.action.ActionForward;
62  import org.apache.struts.action.ActionMapping;
63  
64  /**
65   * <a href="EditEventAction.java.html"><b><i>View Source</i></b></a>
66   *
67   * @author Brian Wing Shun Chan
68   *
69   */
70  public class EditEventAction extends PortletAction {
71  
72      public void processAction(
73              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
74              ActionRequest actionRequest, ActionResponse actionResponse)
75          throws Exception {
76  
77          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
78  
79          try {
80              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
81                  updateEvent(actionRequest);
82              }
83              else if (cmd.equals(Constants.DELETE)) {
84                  deleteEvent(actionRequest);
85              }
86  
87              sendRedirect(actionRequest, actionResponse);
88          }
89          catch (Exception e) {
90              if (e instanceof NoSuchEventException ||
91                  e instanceof PrincipalException) {
92  
93                  SessionErrors.add(actionRequest, e.getClass().getName());
94  
95                  setForward(actionRequest, "portlet.calendar.error");
96              }
97              else if (e instanceof EventDurationException ||
98                       e instanceof EventEndDateException ||
99                       e instanceof EventStartDateException ||
100                      e instanceof EventTitleException) {
101 
102                 SessionErrors.add(actionRequest, e.getClass().getName());
103             }
104             else {
105                 throw e;
106             }
107         }
108     }
109 
110     public ActionForward render(
111             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
112             RenderRequest renderRequest, RenderResponse renderResponse)
113         throws Exception {
114 
115         try {
116             ActionUtil.getEvent(renderRequest);
117         }
118         catch (Exception e) {
119             if (e instanceof NoSuchEventException ||
120                 e instanceof PrincipalException) {
121 
122                 SessionErrors.add(renderRequest, e.getClass().getName());
123 
124                 return mapping.findForward("portlet.calendar.error");
125             }
126             else {
127                 throw e;
128             }
129         }
130 
131         return mapping.findForward(
132             getForward(renderRequest, "portlet.calendar.edit_event"));
133     }
134 
135     protected void addWeeklyDayPos(
136         ActionRequest actionRequest, List<DayAndPosition> list, int day) {
137 
138         if (ParamUtil.getBoolean(actionRequest, "weeklyDayPos" + day)) {
139             list.add(new DayAndPosition(day, 0));
140         }
141     }
142 
143     protected void deleteEvent(ActionRequest actionRequest) throws Exception {
144         long eventId = ParamUtil.getLong(actionRequest, "eventId");
145 
146         CalEventServiceUtil.deleteEvent(eventId);
147     }
148 
149     protected void updateEvent(ActionRequest actionRequest) throws Exception {
150         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
151 
152         long eventId = ParamUtil.getLong(actionRequest, "eventId");
153 
154         String title = ParamUtil.getString(actionRequest, "title");
155         String description = ParamUtil.getString(actionRequest, "description");
156 
157         int startDateMonth = ParamUtil.getInteger(
158             actionRequest, "startDateMonth");
159         int startDateDay = ParamUtil.getInteger(actionRequest, "startDateDay");
160         int startDateYear = ParamUtil.getInteger(
161             actionRequest, "startDateYear");
162         int startDateHour = ParamUtil.getInteger(
163             actionRequest, "startDateHour");
164         int startDateMinute = ParamUtil.getInteger(
165             actionRequest, "startDateMinute");
166         int startDateAmPm = ParamUtil.getInteger(
167             actionRequest, "startDateAmPm");
168 
169         if (startDateAmPm == Calendar.PM) {
170             startDateHour += 12;
171         }
172 
173         int durationHour = ParamUtil.getInteger(actionRequest, "durationHour");
174         int durationMinute = ParamUtil.getInteger(
175             actionRequest, "durationMinute");
176         boolean allDay = ParamUtil.getBoolean(actionRequest, "allDay");
177         boolean timeZoneSensitive = ParamUtil.getBoolean(
178             actionRequest, "timeZoneSensitive");
179         String type = ParamUtil.getString(actionRequest, "type");
180 
181         int endDateMonth = ParamUtil.getInteger(actionRequest, "endDateMonth");
182         int endDateDay = ParamUtil.getInteger(actionRequest, "endDateDay");
183         int endDateYear = ParamUtil.getInteger(actionRequest, "endDateYear");
184 
185         boolean repeating = false;
186 
187         int recurrenceType = ParamUtil.getInteger(
188             actionRequest, "recurrenceType");
189 
190         if (recurrenceType != Recurrence.NO_RECURRENCE) {
191             repeating = true;
192         }
193 
194         Locale locale = null;
195         TimeZone timeZone = null;
196 
197         if (timeZoneSensitive) {
198             User user = PortalUtil.getUser(actionRequest);
199 
200             locale = user.getLocale();
201             timeZone = user.getTimeZone();
202         }
203         else {
204             locale = LocaleUtil.getDefault();
205             timeZone = TimeZoneUtil.getDefault();
206         }
207 
208         Calendar startDate = CalendarFactoryUtil.getCalendar(timeZone, locale);
209 
210         startDate.set(Calendar.MONTH, startDateMonth);
211         startDate.set(Calendar.DATE, startDateDay);
212         startDate.set(Calendar.YEAR, startDateYear);
213         startDate.set(Calendar.HOUR_OF_DAY, startDateHour);
214         startDate.set(Calendar.MINUTE, startDateMinute);
215         startDate.set(Calendar.SECOND, 0);
216         startDate.set(Calendar.MILLISECOND, 0);
217 
218         if (allDay) {
219             startDate.set(Calendar.HOUR_OF_DAY, 0);
220             startDate.set(Calendar.MINUTE, 0);
221             startDate.set(Calendar.SECOND, 0);
222             startDate.set(Calendar.MILLISECOND, 0);
223 
224             durationHour = 24;
225             durationMinute = 0;
226         }
227 
228         TZSRecurrence recurrence = null;
229 
230         if (repeating) {
231             Calendar recStartCal = null;
232 
233             if (timeZoneSensitive) {
234                 recStartCal = CalendarFactoryUtil.getCalendar();
235 
236                 recStartCal.setTime(startDate.getTime());
237             }
238             else {
239                 recStartCal = (Calendar)startDate.clone();
240             }
241 
242             recurrence = new TZSRecurrence(
243                 recStartCal, new Duration(1, 0, 0, 0), recurrenceType);
244 
245             recurrence.setTimeZone(timeZone);
246 
247             recurrence.setWeekStart(Calendar.SUNDAY);
248 
249             if (recurrenceType == Recurrence.DAILY) {
250                 int dailyType = ParamUtil.getInteger(
251                     actionRequest, "dailyType");
252 
253                 if (dailyType == 0) {
254                     int dailyInterval = ParamUtil.getInteger(
255                         actionRequest, "dailyInterval", 1);
256 
257                     recurrence.setInterval(dailyInterval);
258                 }
259                 else {
260                     DayAndPosition[] dayPos = {
261                         new DayAndPosition(Calendar.MONDAY, 0),
262                         new DayAndPosition(Calendar.TUESDAY, 0),
263                         new DayAndPosition(Calendar.WEDNESDAY, 0),
264                         new DayAndPosition(Calendar.THURSDAY, 0),
265                         new DayAndPosition(Calendar.FRIDAY, 0)};
266 
267                     recurrence.setByDay(dayPos);
268                 }
269             }
270             else if (recurrenceType == Recurrence.WEEKLY) {
271                 int weeklyInterval = ParamUtil.getInteger(
272                     actionRequest, "weeklyInterval", 1);
273 
274                 recurrence.setInterval(weeklyInterval);
275 
276                 List<DayAndPosition> dayPos = new ArrayList<DayAndPosition>();
277 
278                 addWeeklyDayPos(actionRequest, dayPos, Calendar.SUNDAY);
279                 addWeeklyDayPos(actionRequest, dayPos, Calendar.MONDAY);
280                 addWeeklyDayPos(actionRequest, dayPos, Calendar.TUESDAY);
281                 addWeeklyDayPos(actionRequest, dayPos, Calendar.WEDNESDAY);
282                 addWeeklyDayPos(actionRequest, dayPos, Calendar.THURSDAY);
283                 addWeeklyDayPos(actionRequest, dayPos, Calendar.FRIDAY);
284                 addWeeklyDayPos(actionRequest, dayPos, Calendar.SATURDAY);
285 
286                 if (dayPos.size() == 0) {
287                     dayPos.add(new DayAndPosition(Calendar.MONDAY, 0));
288                 }
289 
290                 recurrence.setByDay(dayPos.toArray(new DayAndPosition[0]));
291             }
292             else if (recurrenceType == Recurrence.MONTHLY) {
293                 int monthlyType = ParamUtil.getInteger(
294                     actionRequest, "monthlyType");
295 
296                 if (monthlyType == 0) {
297                     int monthlyDay = ParamUtil.getInteger(
298                         actionRequest, "monthlyDay0");
299 
300                     recurrence.setByMonthDay(new int[] {monthlyDay});
301 
302                     int monthlyInterval = ParamUtil.getInteger(
303                         actionRequest, "monthlyInterval0", 1);
304 
305                     recurrence.setInterval(monthlyInterval);
306                 }
307                 else {
308                     int monthlyPos = ParamUtil.getInteger(
309                         actionRequest, "monthlyPos");
310                     int monthlyDay = ParamUtil.getInteger(
311                         actionRequest, "monthlyDay1");
312 
313                     DayAndPosition[] dayPos = {
314                         new DayAndPosition(monthlyDay, monthlyPos)};
315 
316                     recurrence.setByDay(dayPos);
317 
318                     int monthlyInterval = ParamUtil.getInteger(
319                         actionRequest, "monthlyInterval1", 1);
320 
321                     recurrence.setInterval(monthlyInterval);
322                 }
323             }
324             else if (recurrenceType == Recurrence.YEARLY) {
325                 int yearlyType = ParamUtil.getInteger(
326                     actionRequest, "yearlyType");
327 
328                 if (yearlyType == 0) {
329                     int yearlyMonth = ParamUtil.getInteger(
330                         actionRequest, "yearlyMonth0");
331                     int yearlyDay = ParamUtil.getInteger(
332                         actionRequest, "yearlyDay0");
333 
334                     recurrence.setByMonth(new int[] {yearlyMonth});
335                     recurrence.setByMonthDay(new int[] {yearlyDay});
336 
337                     int yearlyInterval = ParamUtil.getInteger(
338                         actionRequest, "yearlyInterval0", 1);
339 
340                     recurrence.setInterval(yearlyInterval);
341                 }
342                 else {
343                     int yearlyPos = ParamUtil.getInteger(
344                         actionRequest, "yearlyPos");
345                     int yearlyDay = ParamUtil.getInteger(
346                         actionRequest, "yearlyDay1");
347                     int yearlyMonth = ParamUtil.getInteger(
348                         actionRequest, "yearlyMonth1");
349 
350                     DayAndPosition[] dayPos = {
351                         new DayAndPosition(yearlyDay, yearlyPos)};
352 
353                     recurrence.setByDay(dayPos);
354 
355                     recurrence.setByMonth(new int[] {yearlyMonth});
356 
357                     int yearlyInterval = ParamUtil.getInteger(
358                         actionRequest, "yearlyInterval1", 1);
359 
360                     recurrence.setInterval(yearlyInterval);
361                 }
362             }
363 
364             int endDateType = ParamUtil.getInteger(
365                 actionRequest, "endDateType");
366 
367             if (endDateType == 1) {
368                 int endDateOccurrence = ParamUtil.getInteger(
369                     actionRequest, "endDateOccurrence");
370 
371                 recurrence.setOccurrence(endDateOccurrence);
372             }
373             else if (endDateType == 2) {
374                 Calendar recEndCal = null;
375 
376                 if (timeZoneSensitive) {
377                     recEndCal = CalendarFactoryUtil.getCalendar();
378 
379                     recEndCal.setTime(startDate.getTime());
380                 }
381                 else {
382                     recEndCal = (Calendar)startDate.clone();
383                 }
384 
385                 recEndCal.set(Calendar.MONTH, endDateMonth);
386                 recEndCal.set(Calendar.DATE, endDateDay);
387                 recEndCal.set(Calendar.YEAR, endDateYear);
388 
389                 recurrence.setUntil(recEndCal);
390             }
391         }
392 
393         int remindBy = ParamUtil.getInteger(actionRequest, "remindBy");
394         int firstReminder = ParamUtil.getInteger(
395             actionRequest, "firstReminder");
396         int secondReminder = ParamUtil.getInteger(
397             actionRequest, "secondReminder");
398 
399         String[] communityPermissions = PortalUtil.getCommunityPermissions(
400             actionRequest);
401         String[] guestPermissions = PortalUtil.getGuestPermissions(
402             actionRequest);
403 
404         if (eventId <= 0) {
405 
406             // Add event
407 
408             CalEventServiceUtil.addEvent(
409                 layout.getPlid(), title, description, startDateMonth,
410                 startDateDay, startDateYear, startDateHour, startDateMinute,
411                 endDateMonth, endDateDay, endDateYear, durationHour,
412                 durationMinute, allDay, timeZoneSensitive, type, repeating,
413                 recurrence, remindBy, firstReminder, secondReminder,
414                 communityPermissions, guestPermissions);
415         }
416         else {
417 
418             // Update event
419 
420             CalEventServiceUtil.updateEvent(
421                 eventId, title, description, startDateMonth, startDateDay,
422                 startDateYear, startDateHour, startDateMinute, endDateMonth,
423                 endDateDay, endDateYear, durationHour, durationMinute,
424                 allDay, timeZoneSensitive, type, repeating, recurrence,
425                 remindBy, firstReminder, secondReminder);
426         }
427     }
428 
429 }