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