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