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