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