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.StringPool;
27  import com.liferay.portal.kernel.util.TimeZoneUtil;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.service.ServiceContext;
31  import com.liferay.portal.service.ServiceContextFactory;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portlet.calendar.EventDurationException;
35  import com.liferay.portlet.calendar.EventEndDateException;
36  import com.liferay.portlet.calendar.EventStartDateException;
37  import com.liferay.portlet.calendar.EventTitleException;
38  import com.liferay.portlet.calendar.NoSuchEventException;
39  import com.liferay.portlet.calendar.model.CalEvent;
40  import com.liferay.portlet.calendar.service.CalEventServiceUtil;
41  
42  import java.util.ArrayList;
43  import java.util.Calendar;
44  import java.util.List;
45  import java.util.Locale;
46  import java.util.TimeZone;
47  
48  import javax.portlet.ActionRequest;
49  import javax.portlet.ActionResponse;
50  import javax.portlet.PortletConfig;
51  import javax.portlet.RenderRequest;
52  import javax.portlet.RenderResponse;
53  
54  import org.apache.struts.action.ActionForm;
55  import org.apache.struts.action.ActionForward;
56  import org.apache.struts.action.ActionMapping;
57  
58  /**
59   * <a href="EditEventAction.java.html"><b><i>View Source</i></b></a>
60   *
61   * @author Brian Wing Shun Chan
62   */
63  public class EditEventAction extends PortletAction {
64  
65      public void processAction(
66              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
67              ActionRequest actionRequest, ActionResponse actionResponse)
68          throws Exception {
69  
70          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
71  
72          try {
73              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
74                  updateEvent(actionRequest);
75              }
76              else if (cmd.equals(Constants.DELETE)) {
77                  deleteEvent(actionRequest);
78              }
79  
80              sendRedirect(actionRequest, actionResponse);
81          }
82          catch (Exception e) {
83              if (e instanceof NoSuchEventException ||
84                  e instanceof PrincipalException) {
85  
86                  SessionErrors.add(actionRequest, e.getClass().getName());
87  
88                  setForward(actionRequest, "portlet.calendar.error");
89              }
90              else if (e instanceof EventDurationException ||
91                       e instanceof EventEndDateException ||
92                       e instanceof EventStartDateException ||
93                       e instanceof EventTitleException) {
94  
95                  SessionErrors.add(actionRequest, e.getClass().getName());
96              }
97              else {
98                  throw e;
99              }
100         }
101     }
102 
103     public ActionForward render(
104             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
105             RenderRequest renderRequest, RenderResponse renderResponse)
106         throws Exception {
107 
108         try {
109             ActionUtil.getEvent(renderRequest);
110         }
111         catch (Exception e) {
112             if (e instanceof NoSuchEventException ||
113                 e instanceof PrincipalException) {
114 
115                 SessionErrors.add(renderRequest, e.getClass().getName());
116 
117                 return mapping.findForward("portlet.calendar.error");
118             }
119             else {
120                 throw e;
121             }
122         }
123 
124         return mapping.findForward(
125             getForward(renderRequest, "portlet.calendar.edit_event"));
126     }
127 
128     protected void addWeeklyDayPos(
129         ActionRequest actionRequest, List<DayAndPosition> list, int day) {
130 
131         if (ParamUtil.getBoolean(actionRequest, "weeklyDayPos" + day)) {
132             list.add(new DayAndPosition(day, 0));
133         }
134     }
135 
136     protected void deleteEvent(ActionRequest actionRequest) throws Exception {
137         long eventId = ParamUtil.getLong(actionRequest, "eventId");
138 
139         CalEventServiceUtil.deleteEvent(eventId);
140     }
141 
142     protected void updateEvent(ActionRequest actionRequest) throws Exception {
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                     TimeZoneUtil.getTimeZone(StringPool.UTC));
227 
228                 recStartCal.setTime(startDate.getTime());
229             }
230             else {
231                 recStartCal = (Calendar)startDate.clone();
232             }
233 
234             recurrence = new TZSRecurrence(
235                 recStartCal, new Duration(1, 0, 0, 0), recurrenceType);
236 
237             recurrence.setTimeZone(timeZone);
238 
239             recurrence.setWeekStart(Calendar.SUNDAY);
240 
241             if (recurrenceType == Recurrence.DAILY) {
242                 int dailyType = ParamUtil.getInteger(
243                     actionRequest, "dailyType");
244 
245                 if (dailyType == 0) {
246                     int dailyInterval = ParamUtil.getInteger(
247                         actionRequest, "dailyInterval", 1);
248 
249                     recurrence.setInterval(dailyInterval);
250                 }
251                 else {
252                     DayAndPosition[] dayPos = {
253                         new DayAndPosition(Calendar.MONDAY, 0),
254                         new DayAndPosition(Calendar.TUESDAY, 0),
255                         new DayAndPosition(Calendar.WEDNESDAY, 0),
256                         new DayAndPosition(Calendar.THURSDAY, 0),
257                         new DayAndPosition(Calendar.FRIDAY, 0)};
258 
259                     recurrence.setByDay(dayPos);
260                 }
261             }
262             else if (recurrenceType == Recurrence.WEEKLY) {
263                 int weeklyInterval = ParamUtil.getInteger(
264                     actionRequest, "weeklyInterval", 1);
265 
266                 recurrence.setInterval(weeklyInterval);
267 
268                 List<DayAndPosition> dayPos = new ArrayList<DayAndPosition>();
269 
270                 addWeeklyDayPos(actionRequest, dayPos, Calendar.SUNDAY);
271                 addWeeklyDayPos(actionRequest, dayPos, Calendar.MONDAY);
272                 addWeeklyDayPos(actionRequest, dayPos, Calendar.TUESDAY);
273                 addWeeklyDayPos(actionRequest, dayPos, Calendar.WEDNESDAY);
274                 addWeeklyDayPos(actionRequest, dayPos, Calendar.THURSDAY);
275                 addWeeklyDayPos(actionRequest, dayPos, Calendar.FRIDAY);
276                 addWeeklyDayPos(actionRequest, dayPos, Calendar.SATURDAY);
277 
278                 if (dayPos.size() == 0) {
279                     dayPos.add(new DayAndPosition(Calendar.MONDAY, 0));
280                 }
281 
282                 recurrence.setByDay(dayPos.toArray(new DayAndPosition[0]));
283             }
284             else if (recurrenceType == Recurrence.MONTHLY) {
285                 int monthlyType = ParamUtil.getInteger(
286                     actionRequest, "monthlyType");
287 
288                 if (monthlyType == 0) {
289                     int monthlyDay = ParamUtil.getInteger(
290                         actionRequest, "monthlyDay0");
291 
292                     recurrence.setByMonthDay(new int[] {monthlyDay});
293 
294                     int monthlyInterval = ParamUtil.getInteger(
295                         actionRequest, "monthlyInterval0", 1);
296 
297                     recurrence.setInterval(monthlyInterval);
298                 }
299                 else {
300                     int monthlyPos = ParamUtil.getInteger(
301                         actionRequest, "monthlyPos");
302                     int monthlyDay = ParamUtil.getInteger(
303                         actionRequest, "monthlyDay1");
304 
305                     DayAndPosition[] dayPos = {
306                         new DayAndPosition(monthlyDay, monthlyPos)};
307 
308                     recurrence.setByDay(dayPos);
309 
310                     int monthlyInterval = ParamUtil.getInteger(
311                         actionRequest, "monthlyInterval1", 1);
312 
313                     recurrence.setInterval(monthlyInterval);
314                 }
315             }
316             else if (recurrenceType == Recurrence.YEARLY) {
317                 int yearlyType = ParamUtil.getInteger(
318                     actionRequest, "yearlyType");
319 
320                 if (yearlyType == 0) {
321                     int yearlyMonth = ParamUtil.getInteger(
322                         actionRequest, "yearlyMonth0");
323                     int yearlyDay = ParamUtil.getInteger(
324                         actionRequest, "yearlyDay0");
325 
326                     recurrence.setByMonth(new int[] {yearlyMonth});
327                     recurrence.setByMonthDay(new int[] {yearlyDay});
328 
329                     int yearlyInterval = ParamUtil.getInteger(
330                         actionRequest, "yearlyInterval0", 1);
331 
332                     recurrence.setInterval(yearlyInterval);
333                 }
334                 else {
335                     int yearlyPos = ParamUtil.getInteger(
336                         actionRequest, "yearlyPos");
337                     int yearlyDay = ParamUtil.getInteger(
338                         actionRequest, "yearlyDay1");
339                     int yearlyMonth = ParamUtil.getInteger(
340                         actionRequest, "yearlyMonth1");
341 
342                     DayAndPosition[] dayPos = {
343                         new DayAndPosition(yearlyDay, yearlyPos)};
344 
345                     recurrence.setByDay(dayPos);
346 
347                     recurrence.setByMonth(new int[] {yearlyMonth});
348 
349                     int yearlyInterval = ParamUtil.getInteger(
350                         actionRequest, "yearlyInterval1", 1);
351 
352                     recurrence.setInterval(yearlyInterval);
353                 }
354             }
355 
356             int endDateType = ParamUtil.getInteger(
357                 actionRequest, "endDateType");
358 
359             if (endDateType == 1) {
360                 int endDateOccurrence = ParamUtil.getInteger(
361                     actionRequest, "endDateOccurrence");
362 
363                 recurrence.setOccurrence(endDateOccurrence);
364             }
365             else if (endDateType == 2) {
366                 Calendar endDate = CalendarFactoryUtil.getCalendar(timeZone);
367 
368                 endDate.set(Calendar.MONTH, endDateMonth);
369                 endDate.set(Calendar.DATE, endDateDay);
370                 endDate.set(Calendar.YEAR, endDateYear);
371                 endDate.set(Calendar.HOUR_OF_DAY, startDateHour);
372                 endDate.set(Calendar.MINUTE, startDateMinute);
373                 endDate.set(Calendar.SECOND, 0);
374                 endDate.set(Calendar.MILLISECOND, 0);
375 
376                 Calendar recEndCal = null;
377 
378                 if (timeZoneSensitive) {
379                     recEndCal = CalendarFactoryUtil.getCalendar(
380                         TimeZoneUtil.getTimeZone(StringPool.UTC));
381 
382                     recEndCal.setTime(endDate.getTime());
383                 }
384                 else {
385                     recEndCal = (Calendar)endDate.clone();
386                 }
387 
388                 recurrence.setUntil(recEndCal);
389             }
390         }
391 
392         int remindBy = ParamUtil.getInteger(actionRequest, "remindBy");
393         int firstReminder = ParamUtil.getInteger(
394             actionRequest, "firstReminder");
395         int secondReminder = ParamUtil.getInteger(
396             actionRequest, "secondReminder");
397 
398         ServiceContext serviceContext = ServiceContextFactory.getInstance(
399             CalEvent.class.getName(), actionRequest);
400 
401         if (eventId <= 0) {
402 
403             // Add event
404 
405             CalEventServiceUtil.addEvent(
406                 title, description, startDateMonth, startDateDay, startDateYear,
407                 startDateHour, startDateMinute, endDateMonth, endDateDay,
408                 endDateYear, durationHour, durationMinute, allDay,
409                 timeZoneSensitive, type, repeating, recurrence, remindBy,
410                 firstReminder, secondReminder, serviceContext);
411         }
412         else {
413 
414             // Update event
415 
416             CalEventServiceUtil.updateEvent(
417                 eventId, title, description, startDateMonth, startDateDay,
418                 startDateYear, startDateHour, startDateMinute, endDateMonth,
419                 endDateDay, endDateYear, durationHour, durationMinute,
420                 allDay, timeZoneSensitive, type, repeating, recurrence,
421                 remindBy, firstReminder, secondReminder, serviceContext);
422         }
423     }
424 
425 }