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