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