1
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
71 public class EditEventAction extends PortletAction {
72
73 public void processAction(
74 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
75 ActionRequest actionRequest, ActionResponse actionResponse)
76 throws Exception {
77
78 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
79
80 try {
81 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
82 updateEvent(actionRequest);
83 }
84 else if (cmd.equals(Constants.DELETE)) {
85 deleteEvent(actionRequest);
86 }
87
88 sendRedirect(actionRequest, actionResponse);
89 }
90 catch (Exception e) {
91 if (e instanceof NoSuchEventException ||
92 e instanceof PrincipalException) {
93
94 SessionErrors.add(actionRequest, e.getClass().getName());
95
96 setForward(actionRequest, "portlet.calendar.error");
97 }
98 else if (e instanceof EventDurationException ||
99 e instanceof EventEndDateException ||
100 e instanceof EventStartDateException ||
101 e instanceof EventTitleException) {
102
103 SessionErrors.add(actionRequest, e.getClass().getName());
104 }
105 else {
106 throw e;
107 }
108 }
109 }
110
111 public ActionForward render(
112 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
113 RenderRequest renderRequest, RenderResponse renderResponse)
114 throws Exception {
115
116 try {
117 ActionUtil.getEvent(renderRequest);
118 }
119 catch (Exception e) {
120 if (e instanceof NoSuchEventException ||
121 e instanceof PrincipalException) {
122
123 SessionErrors.add(renderRequest, e.getClass().getName());
124
125 return mapping.findForward("portlet.calendar.error");
126 }
127 else {
128 throw e;
129 }
130 }
131
132 return mapping.findForward(
133 getForward(renderRequest, "portlet.calendar.edit_event"));
134 }
135
136 protected void addWeeklyDayPos(
137 ActionRequest actionRequest, List<DayAndPosition> list, int day) {
138
139 if (ParamUtil.getBoolean(actionRequest, "weeklyDayPos" + day)) {
140 list.add(new DayAndPosition(day, 0));
141 }
142 }
143
144 protected void deleteEvent(ActionRequest actionRequest) throws Exception {
145 long eventId = ParamUtil.getLong(actionRequest, "eventId");
146
147 CalEventServiceUtil.deleteEvent(eventId);
148 }
149
150 protected void updateEvent(ActionRequest actionRequest) throws Exception {
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 TZSRecurrence 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 TZSRecurrence(
242 recStartCal, new Duration(1, 0, 0, 0), recurrenceType);
243
244 recurrence.setTimeZone(timeZone);
245
246 recurrence.setWeekStart(Calendar.SUNDAY);
247
248 if (recurrenceType == Recurrence.DAILY) {
249 int dailyType = ParamUtil.getInteger(
250 actionRequest, "dailyType");
251
252 if (dailyType == 0) {
253 int dailyInterval = ParamUtil.getInteger(
254 actionRequest, "dailyInterval", 1);
255
256 recurrence.setInterval(dailyInterval);
257 }
258 else {
259 DayAndPosition[] dayPos = {
260 new DayAndPosition(Calendar.MONDAY, 0),
261 new DayAndPosition(Calendar.TUESDAY, 0),
262 new DayAndPosition(Calendar.WEDNESDAY, 0),
263 new DayAndPosition(Calendar.THURSDAY, 0),
264 new DayAndPosition(Calendar.FRIDAY, 0)};
265
266 recurrence.setByDay(dayPos);
267 }
268 }
269 else if (recurrenceType == Recurrence.WEEKLY) {
270 int weeklyInterval = ParamUtil.getInteger(
271 actionRequest, "weeklyInterval", 1);
272
273 recurrence.setInterval(weeklyInterval);
274
275 List<DayAndPosition> dayPos = new ArrayList<DayAndPosition>();
276
277 addWeeklyDayPos(actionRequest, dayPos, Calendar.SUNDAY);
278 addWeeklyDayPos(actionRequest, dayPos, Calendar.MONDAY);
279 addWeeklyDayPos(actionRequest, dayPos, Calendar.TUESDAY);
280 addWeeklyDayPos(actionRequest, dayPos, Calendar.WEDNESDAY);
281 addWeeklyDayPos(actionRequest, dayPos, Calendar.THURSDAY);
282 addWeeklyDayPos(actionRequest, dayPos, Calendar.FRIDAY);
283 addWeeklyDayPos(actionRequest, dayPos, Calendar.SATURDAY);
284
285 if (dayPos.size() == 0) {
286 dayPos.add(new DayAndPosition(Calendar.MONDAY, 0));
287 }
288
289 recurrence.setByDay(dayPos.toArray(new DayAndPosition[0]));
290 }
291 else if (recurrenceType == Recurrence.MONTHLY) {
292 int monthlyType = ParamUtil.getInteger(
293 actionRequest, "monthlyType");
294
295 if (monthlyType == 0) {
296 int monthlyDay = ParamUtil.getInteger(
297 actionRequest, "monthlyDay0");
298
299 recurrence.setByMonthDay(new int[] {monthlyDay});
300
301 int monthlyInterval = ParamUtil.getInteger(
302 actionRequest, "monthlyInterval0", 1);
303
304 recurrence.setInterval(monthlyInterval);
305 }
306 else {
307 int monthlyPos = ParamUtil.getInteger(
308 actionRequest, "monthlyPos");
309 int monthlyDay = ParamUtil.getInteger(
310 actionRequest, "monthlyDay1");
311
312 DayAndPosition[] dayPos = {
313 new DayAndPosition(monthlyDay, monthlyPos)};
314
315 recurrence.setByDay(dayPos);
316
317 int monthlyInterval = ParamUtil.getInteger(
318 actionRequest, "monthlyInterval1", 1);
319
320 recurrence.setInterval(monthlyInterval);
321 }
322 }
323 else if (recurrenceType == Recurrence.YEARLY) {
324 int yearlyType = ParamUtil.getInteger(
325 actionRequest, "yearlyType");
326
327 if (yearlyType == 0) {
328 int yearlyMonth = ParamUtil.getInteger(
329 actionRequest, "yearlyMonth0");
330 int yearlyDay = ParamUtil.getInteger(
331 actionRequest, "yearlyDay0");
332
333 recurrence.setByMonth(new int[] {yearlyMonth});
334 recurrence.setByMonthDay(new int[] {yearlyDay});
335
336 int yearlyInterval = ParamUtil.getInteger(
337 actionRequest, "yearlyInterval0", 1);
338
339 recurrence.setInterval(yearlyInterval);
340 }
341 else {
342 int yearlyPos = ParamUtil.getInteger(
343 actionRequest, "yearlyPos");
344 int yearlyDay = ParamUtil.getInteger(
345 actionRequest, "yearlyDay1");
346 int yearlyMonth = ParamUtil.getInteger(
347 actionRequest, "yearlyMonth1");
348
349 DayAndPosition[] dayPos = {
350 new DayAndPosition(yearlyDay, yearlyPos)};
351
352 recurrence.setByDay(dayPos);
353
354 recurrence.setByMonth(new int[] {yearlyMonth});
355
356 int yearlyInterval = ParamUtil.getInteger(
357 actionRequest, "yearlyInterval1", 1);
358
359 recurrence.setInterval(yearlyInterval);
360 }
361 }
362
363 int endDateType = ParamUtil.getInteger(
364 actionRequest, "endDateType");
365
366 if (endDateType == 1) {
367 int endDateOccurrence = ParamUtil.getInteger(
368 actionRequest, "endDateOccurrence");
369
370 recurrence.setOccurrence(endDateOccurrence);
371 }
372 else if (endDateType == 2) {
373 Calendar recEndCal = null;
374
375 if (timeZoneSensitive) {
376 recEndCal = CalendarFactoryUtil.getCalendar();
377
378 recEndCal.setTime(startDate.getTime());
379 }
380 else {
381 recEndCal = (Calendar)startDate.clone();
382 }
383
384 recEndCal.set(Calendar.MONTH, endDateMonth);
385 recEndCal.set(Calendar.DATE, endDateDay);
386 recEndCal.set(Calendar.YEAR, endDateYear);
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
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
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 }