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