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