1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.calendar.action;
16  
17  import com.liferay.portal.kernel.cal.DayAndPosition;
18  import com.liferay.portal.kernel.cal.Duration;
19  import com.liferay.portal.kernel.cal.Recurrence;
20  import com.liferay.portal.kernel.cal.TZSRecurrence;
21  import com.liferay.portal.kernel.servlet.SessionErrors;
22  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
23  import com.liferay.portal.kernel.util.Constants;
24  import com.liferay.portal.kernel.util.LocaleUtil;
25  import com.liferay.portal.kernel.util.ParamUtil;
26  import com.liferay.portal.kernel.util.TimeZoneUtil;
27  import com.liferay.portal.model.Layout;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.struts.PortletAction;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.calendar.EventDurationException;
34  import com.liferay.portlet.calendar.EventEndDateException;
35  import com.liferay.portlet.calendar.EventStartDateException;
36  import com.liferay.portlet.calendar.EventTitleException;
37  import com.liferay.portlet.calendar.NoSuchEventException;
38  import com.liferay.portlet.calendar.service.CalEventServiceUtil;
39  
40  import java.util.ArrayList;
41  import java.util.Calendar;
42  import java.util.List;
43  import java.util.Locale;
44  import java.util.TimeZone;
45  
46  import javax.portlet.ActionRequest;
47  import javax.portlet.ActionResponse;
48  import javax.portlet.PortletConfig;
49  import javax.portlet.RenderRequest;
50  import javax.portlet.RenderResponse;
51  
52  import org.apache.struts.action.ActionForm;
53  import org.apache.struts.action.ActionForward;
54  import org.apache.struts.action.ActionMapping;
55  
56  /**
57   * <a href="EditEventAction.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Brian Wing Shun Chan
60   */
61  public class EditEventAction extends PortletAction {
62  
63      public void processAction(
64              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
65              ActionRequest actionRequest, ActionResponse actionResponse)
66          throws Exception {
67  
68          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
69  
70          try {
71              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
72                  updateEvent(actionRequest);
73              }
74              else if (cmd.equals(Constants.DELETE)) {
75                  deleteEvent(actionRequest);
76              }
77  
78              sendRedirect(actionRequest, actionResponse);
79          }
80          catch (Exception e) {
81              if (e instanceof NoSuchEventException ||
82                  e instanceof PrincipalException) {
83  
84                  SessionErrors.add(actionRequest, e.getClass().getName());
85  
86                  setForward(actionRequest, "portlet.calendar.error");
87              }
88              else if (e instanceof EventDurationException ||
89                       e instanceof EventEndDateException ||
90                       e instanceof EventStartDateException ||
91                       e instanceof EventTitleException) {
92  
93                  SessionErrors.add(actionRequest, e.getClass().getName());
94              }
95              else {
96                  throw e;
97              }
98          }
99      }
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         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
142 
143         long eventId = ParamUtil.getLong(actionRequest, "eventId");
144 
145         String title = ParamUtil.getString(actionRequest, "title");
146         String description = ParamUtil.getString(actionRequest, "description");
147 
148         int startDateMonth = ParamUtil.getInteger(
149             actionRequest, "startDateMonth");
150         int startDateDay = ParamUtil.getInteger(actionRequest, "startDateDay");
151         int startDateYear = ParamUtil.getInteger(
152             actionRequest, "startDateYear");
153         int startDateHour = ParamUtil.getInteger(
154             actionRequest, "startDateHour");
155         int startDateMinute = ParamUtil.getInteger(
156             actionRequest, "startDateMinute");
157         int startDateAmPm = ParamUtil.getInteger(
158             actionRequest, "startDateAmPm");
159 
160         if (startDateAmPm == Calendar.PM) {
161             startDateHour += 12;
162         }
163 
164         int durationHour = ParamUtil.getInteger(actionRequest, "durationHour");
165         int durationMinute = ParamUtil.getInteger(
166             actionRequest, "durationMinute");
167         boolean allDay = ParamUtil.getBoolean(actionRequest, "allDay");
168         boolean timeZoneSensitive = ParamUtil.getBoolean(
169             actionRequest, "timeZoneSensitive");
170         String type = ParamUtil.getString(actionRequest, "type");
171 
172         int endDateMonth = ParamUtil.getInteger(actionRequest, "endDateMonth");
173         int endDateDay = ParamUtil.getInteger(actionRequest, "endDateDay");
174         int endDateYear = ParamUtil.getInteger(actionRequest, "endDateYear");
175 
176         boolean repeating = false;
177 
178         int recurrenceType = ParamUtil.getInteger(
179             actionRequest, "recurrenceType");
180 
181         if (recurrenceType != Recurrence.NO_RECURRENCE) {
182             repeating = true;
183         }
184 
185         Locale locale = null;
186         TimeZone timeZone = null;
187 
188         if (timeZoneSensitive) {
189             User user = PortalUtil.getUser(actionRequest);
190 
191             locale = user.getLocale();
192             timeZone = user.getTimeZone();
193         }
194         else {
195             locale = LocaleUtil.getDefault();
196             timeZone = TimeZoneUtil.getDefault();
197         }
198 
199         Calendar startDate = CalendarFactoryUtil.getCalendar(timeZone, locale);
200 
201         startDate.set(Calendar.MONTH, startDateMonth);
202         startDate.set(Calendar.DATE, startDateDay);
203         startDate.set(Calendar.YEAR, startDateYear);
204         startDate.set(Calendar.HOUR_OF_DAY, startDateHour);
205         startDate.set(Calendar.MINUTE, startDateMinute);
206         startDate.set(Calendar.SECOND, 0);
207         startDate.set(Calendar.MILLISECOND, 0);
208 
209         if (allDay) {
210             startDate.set(Calendar.HOUR_OF_DAY, 0);
211             startDate.set(Calendar.MINUTE, 0);
212             startDate.set(Calendar.SECOND, 0);
213             startDate.set(Calendar.MILLISECOND, 0);
214 
215             durationHour = 24;
216             durationMinute = 0;
217         }
218 
219         TZSRecurrence recurrence = null;
220 
221         if (repeating) {
222             Calendar recStartCal = null;
223 
224             if (timeZoneSensitive) {
225                 recStartCal = CalendarFactoryUtil.getCalendar();
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 recEndCal = null;
366 
367                 if (timeZoneSensitive) {
368                     recEndCal = CalendarFactoryUtil.getCalendar();
369 
370                     recEndCal.setTime(startDate.getTime());
371                 }
372                 else {
373                     recEndCal = (Calendar)startDate.clone();
374                 }
375 
376                 recEndCal.set(Calendar.MONTH, endDateMonth);
377                 recEndCal.set(Calendar.DATE, endDateDay);
378                 recEndCal.set(Calendar.YEAR, endDateYear);
379 
380                 recurrence.setUntil(recEndCal);
381             }
382         }
383 
384         int remindBy = ParamUtil.getInteger(actionRequest, "remindBy");
385         int firstReminder = ParamUtil.getInteger(
386             actionRequest, "firstReminder");
387         int secondReminder = ParamUtil.getInteger(
388             actionRequest, "secondReminder");
389 
390         String[] communityPermissions = PortalUtil.getCommunityPermissions(
391             actionRequest);
392         String[] guestPermissions = PortalUtil.getGuestPermissions(
393             actionRequest);
394 
395         if (eventId <= 0) {
396 
397             // Add event
398 
399             CalEventServiceUtil.addEvent(
400                 layout.getPlid(), title, description, startDateMonth,
401                 startDateDay, startDateYear, startDateHour, startDateMinute,
402                 endDateMonth, endDateDay, endDateYear, durationHour,
403                 durationMinute, allDay, timeZoneSensitive, type, repeating,
404                 recurrence, remindBy, firstReminder, secondReminder,
405                 communityPermissions, guestPermissions);
406         }
407         else {
408 
409             // Update event
410 
411             CalEventServiceUtil.updateEvent(
412                 eventId, title, description, startDateMonth, startDateDay,
413                 startDateYear, startDateHour, startDateMinute, endDateMonth,
414                 endDateDay, endDateYear, durationHour, durationMinute,
415                 allDay, timeZoneSensitive, type, repeating, recurrence,
416                 remindBy, firstReminder, secondReminder);
417         }
418     }
419 
420 }