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.struts.PortletAction;
39 import com.liferay.portal.util.PortalUtil;
40 import com.liferay.portal.util.WebKeys;
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.service.CalEventServiceUtil;
47
48 import java.util.ArrayList;
49 import java.util.Calendar;
50 import java.util.List;
51 import java.util.Locale;
52 import java.util.TimeZone;
53
54 import javax.portlet.ActionRequest;
55 import javax.portlet.ActionResponse;
56 import javax.portlet.PortletConfig;
57 import javax.portlet.RenderRequest;
58 import javax.portlet.RenderResponse;
59
60 import org.apache.struts.action.ActionForm;
61 import org.apache.struts.action.ActionForward;
62 import org.apache.struts.action.ActionMapping;
63
64
69 public class EditEventAction extends PortletAction {
70
71 public void processAction(
72 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
73 ActionRequest actionRequest, ActionResponse actionResponse)
74 throws Exception {
75
76 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
77
78 try {
79 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
80 updateEvent(actionRequest);
81 }
82 else if (cmd.equals(Constants.DELETE)) {
83 deleteEvent(actionRequest);
84 }
85
86 sendRedirect(actionRequest, actionResponse);
87 }
88 catch (Exception e) {
89 if (e instanceof NoSuchEventException ||
90 e instanceof PrincipalException) {
91
92 SessionErrors.add(actionRequest, e.getClass().getName());
93
94 setForward(actionRequest, "portlet.calendar.error");
95 }
96 else if (e instanceof EventDurationException ||
97 e instanceof EventEndDateException ||
98 e instanceof EventStartDateException ||
99 e instanceof EventTitleException) {
100
101 SessionErrors.add(actionRequest, e.getClass().getName());
102 }
103 else {
104 throw e;
105 }
106 }
107 }
108
109 public ActionForward render(
110 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
111 RenderRequest renderRequest, RenderResponse renderResponse)
112 throws Exception {
113
114 try {
115 ActionUtil.getEvent(renderRequest);
116 }
117 catch (Exception e) {
118 if (e instanceof NoSuchEventException ||
119 e instanceof PrincipalException) {
120
121 SessionErrors.add(renderRequest, e.getClass().getName());
122
123 return mapping.findForward("portlet.calendar.error");
124 }
125 else {
126 throw e;
127 }
128 }
129
130 return mapping.findForward(
131 getForward(renderRequest, "portlet.calendar.edit_event"));
132 }
133
134 protected void addWeeklyDayPos(
135 ActionRequest actionRequest, List<DayAndPosition> list, int day) {
136
137 if (ParamUtil.getBoolean(actionRequest, "weeklyDayPos" + day)) {
138 list.add(new DayAndPosition(day, 0));
139 }
140 }
141
142 protected void deleteEvent(ActionRequest actionRequest) throws Exception {
143 long eventId = ParamUtil.getLong(actionRequest, "eventId");
144
145 CalEventServiceUtil.deleteEvent(eventId);
146 }
147
148 protected void updateEvent(ActionRequest actionRequest) throws Exception {
149 Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
150
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 String[] communityPermissions = PortalUtil.getCommunityPermissions(
399 actionRequest);
400 String[] guestPermissions = PortalUtil.getGuestPermissions(
401 actionRequest);
402
403 if (eventId <= 0) {
404
405
407 CalEventServiceUtil.addEvent(
408 layout.getPlid(), title, description, startDateMonth,
409 startDateDay, startDateYear, startDateHour, startDateMinute,
410 endDateMonth, endDateDay, endDateYear, durationHour,
411 durationMinute, allDay, timeZoneSensitive, type, repeating,
412 recurrence, remindBy, firstReminder, secondReminder,
413 communityPermissions, guestPermissions);
414 }
415 else {
416
417
419 CalEventServiceUtil.updateEvent(
420 eventId, title, description, startDateMonth, startDateDay,
421 startDateYear, startDateHour, startDateMinute, endDateMonth,
422 endDateDay, endDateYear, durationHour, durationMinute,
423 allDay, timeZoneSensitive, type, repeating, recurrence,
424 remindBy, firstReminder, secondReminder);
425 }
426 }
427
428 }