001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.calendar.action;
022:
023: import com.liferay.portal.kernel.cal.DayAndPosition;
024: import com.liferay.portal.kernel.cal.Duration;
025: import com.liferay.portal.kernel.cal.Recurrence;
026: import com.liferay.portal.kernel.util.CalendarFactoryUtil;
027: import com.liferay.portal.kernel.util.Constants;
028: import com.liferay.portal.kernel.util.LocaleUtil;
029: import com.liferay.portal.kernel.util.ParamUtil;
030: import com.liferay.portal.kernel.util.TimeZoneUtil;
031: import com.liferay.portal.model.Layout;
032: import com.liferay.portal.model.User;
033: import com.liferay.portal.security.auth.PrincipalException;
034: import com.liferay.portal.struts.PortletAction;
035: import com.liferay.portal.util.PortalUtil;
036: import com.liferay.portal.util.WebKeys;
037: import com.liferay.portlet.calendar.EventDurationException;
038: import com.liferay.portlet.calendar.EventEndDateException;
039: import com.liferay.portlet.calendar.EventStartDateException;
040: import com.liferay.portlet.calendar.EventTitleException;
041: import com.liferay.portlet.calendar.NoSuchEventException;
042: import com.liferay.portlet.calendar.service.CalEventServiceUtil;
043: import com.liferay.util.servlet.SessionErrors;
044:
045: import java.util.ArrayList;
046: import java.util.Calendar;
047: import java.util.List;
048: import java.util.Locale;
049: import java.util.TimeZone;
050:
051: import javax.portlet.ActionRequest;
052: import javax.portlet.ActionResponse;
053: import javax.portlet.PortletConfig;
054: import javax.portlet.RenderRequest;
055: import javax.portlet.RenderResponse;
056:
057: import org.apache.struts.action.ActionForm;
058: import org.apache.struts.action.ActionForward;
059: import org.apache.struts.action.ActionMapping;
060:
061: /**
062: * <a href="EditEventAction.java.html"><b><i>View Source</i></b></a>
063: *
064: * @author Brian Wing Shun Chan
065: *
066: */
067: public class EditEventAction extends PortletAction {
068:
069: public void processAction(ActionMapping mapping, ActionForm form,
070: PortletConfig config, ActionRequest req, ActionResponse res)
071: throws Exception {
072:
073: String cmd = ParamUtil.getString(req, Constants.CMD);
074:
075: try {
076: if (cmd.equals(Constants.ADD)
077: || cmd.equals(Constants.UPDATE)) {
078: updateEvent(req);
079: } else if (cmd.equals(Constants.DELETE)) {
080: deleteEvent(req);
081: }
082:
083: sendRedirect(req, res);
084: } catch (Exception e) {
085: if (e instanceof NoSuchEventException
086: || e instanceof PrincipalException) {
087:
088: SessionErrors.add(req, e.getClass().getName());
089:
090: setForward(req, "portlet.calendar.error");
091: } else if (e instanceof EventDurationException
092: || e instanceof EventEndDateException
093: || e instanceof EventStartDateException
094: || e instanceof EventTitleException) {
095:
096: SessionErrors.add(req, e.getClass().getName());
097: } else {
098: throw e;
099: }
100: }
101: }
102:
103: public ActionForward render(ActionMapping mapping, ActionForm form,
104: PortletConfig config, RenderRequest req, RenderResponse res)
105: throws Exception {
106:
107: try {
108: ActionUtil.getEvent(req);
109: } catch (Exception e) {
110: if (e instanceof NoSuchEventException
111: || e instanceof PrincipalException) {
112:
113: SessionErrors.add(req, e.getClass().getName());
114:
115: return mapping.findForward("portlet.calendar.error");
116: } else {
117: throw e;
118: }
119: }
120:
121: return mapping.findForward(getForward(req,
122: "portlet.calendar.edit_event"));
123: }
124:
125: protected void deleteEvent(ActionRequest req) throws Exception {
126: long eventId = ParamUtil.getLong(req, "eventId");
127:
128: CalEventServiceUtil.deleteEvent(eventId);
129: }
130:
131: protected void updateEvent(ActionRequest req) throws Exception {
132: Layout layout = (Layout) req.getAttribute(WebKeys.LAYOUT);
133:
134: long eventId = ParamUtil.getLong(req, "eventId");
135:
136: String title = ParamUtil.getString(req, "title");
137: String description = ParamUtil.getString(req, "description");
138:
139: int startDateMonth = ParamUtil
140: .getInteger(req, "startDateMonth");
141: int startDateDay = ParamUtil.getInteger(req, "startDateDay");
142: int startDateYear = ParamUtil.getInteger(req, "startDateYear");
143: int startDateHour = ParamUtil.getInteger(req, "startDateHour");
144: int startDateMinute = ParamUtil.getInteger(req,
145: "startDateMinute");
146: int startDateAmPm = ParamUtil.getInteger(req, "startDateAmPm");
147:
148: if (startDateAmPm == Calendar.PM) {
149: startDateHour += 12;
150: }
151:
152: int durationHour = ParamUtil.getInteger(req, "durationHour");
153: int durationMinute = ParamUtil
154: .getInteger(req, "durationMinute");
155: boolean allDay = ParamUtil.getBoolean(req, "allDay");
156: boolean timeZoneSensitive = ParamUtil.getBoolean(req,
157: "timeZoneSensitive");
158: String type = ParamUtil.getString(req, "type");
159:
160: int endDateMonth = ParamUtil.getInteger(req, "endDateMonth");
161: int endDateDay = ParamUtil.getInteger(req, "endDateDay");
162: int endDateYear = ParamUtil.getInteger(req, "endDateYear");
163:
164: boolean repeating = false;
165:
166: int recurrenceType = ParamUtil
167: .getInteger(req, "recurrenceType");
168:
169: if (recurrenceType != Recurrence.NO_RECURRENCE) {
170: repeating = true;
171: }
172:
173: Locale locale = null;
174: TimeZone timeZone = null;
175:
176: if (timeZoneSensitive) {
177: User user = PortalUtil.getUser(req);
178:
179: locale = user.getLocale();
180: timeZone = user.getTimeZone();
181: } else {
182: locale = LocaleUtil.getDefault();
183: timeZone = TimeZoneUtil.getDefault();
184: }
185:
186: Calendar startDate = CalendarFactoryUtil.getCalendar(timeZone,
187: locale);
188:
189: startDate.set(Calendar.MONTH, startDateMonth);
190: startDate.set(Calendar.DATE, startDateDay);
191: startDate.set(Calendar.YEAR, startDateYear);
192: startDate.set(Calendar.HOUR_OF_DAY, startDateHour);
193: startDate.set(Calendar.MINUTE, startDateMinute);
194: startDate.set(Calendar.SECOND, 0);
195: startDate.set(Calendar.MILLISECOND, 0);
196:
197: if (allDay) {
198: startDate.set(Calendar.HOUR_OF_DAY, 0);
199: startDate.set(Calendar.MINUTE, 0);
200: startDate.set(Calendar.SECOND, 0);
201: startDate.set(Calendar.MILLISECOND, 0);
202:
203: durationHour = 24;
204: durationMinute = 0;
205: }
206:
207: Recurrence recurrence = null;
208:
209: if (repeating) {
210: Calendar recStartCal = null;
211:
212: if (timeZoneSensitive) {
213: recStartCal = CalendarFactoryUtil.getCalendar();
214:
215: recStartCal.setTime(startDate.getTime());
216: } else {
217: recStartCal = (Calendar) startDate.clone();
218: }
219:
220: recurrence = new Recurrence(recStartCal, new Duration(1, 0,
221: 0, 0), recurrenceType);
222:
223: recurrence.setWeekStart(Calendar.SUNDAY);
224:
225: if (recurrenceType == Recurrence.DAILY) {
226: int dailyType = ParamUtil.getInteger(req, "dailyType");
227:
228: if (dailyType == 0) {
229: int dailyInterval = ParamUtil.getInteger(req,
230: "dailyInterval");
231:
232: // LEP-3468
233:
234: if (dailyInterval <= 0) {
235: dailyInterval = 1;
236: }
237:
238: recurrence.setInterval(dailyInterval);
239: } else {
240: DayAndPosition[] dayPos = {
241: new DayAndPosition(Calendar.MONDAY, 0),
242: new DayAndPosition(Calendar.TUESDAY, 0),
243: new DayAndPosition(Calendar.WEDNESDAY, 0),
244: new DayAndPosition(Calendar.THURSDAY, 0),
245: new DayAndPosition(Calendar.FRIDAY, 0) };
246:
247: recurrence.setByDay(dayPos);
248: }
249: } else if (recurrenceType == Recurrence.WEEKLY) {
250: int weeklyInterval = ParamUtil.getInteger(req,
251: "weeklyInterval");
252:
253: recurrence.setInterval(weeklyInterval);
254:
255: List dayPos = new ArrayList();
256:
257: _addWeeklyDayPos(req, dayPos, Calendar.SUNDAY);
258: _addWeeklyDayPos(req, dayPos, Calendar.MONDAY);
259: _addWeeklyDayPos(req, dayPos, Calendar.TUESDAY);
260: _addWeeklyDayPos(req, dayPos, Calendar.WEDNESDAY);
261: _addWeeklyDayPos(req, dayPos, Calendar.THURSDAY);
262: _addWeeklyDayPos(req, dayPos, Calendar.FRIDAY);
263: _addWeeklyDayPos(req, dayPos, Calendar.SATURDAY);
264:
265: if (dayPos.size() == 0) {
266: dayPos.add(new DayAndPosition(Calendar.MONDAY, 0));
267: }
268:
269: recurrence.setByDay((DayAndPosition[]) dayPos
270: .toArray(new DayAndPosition[0]));
271: } else if (recurrenceType == Recurrence.MONTHLY) {
272: int monthlyType = ParamUtil.getInteger(req,
273: "monthlyType");
274:
275: if (monthlyType == 0) {
276: int monthlyDay = ParamUtil.getInteger(req,
277: "monthlyDay0");
278:
279: recurrence.setByMonthDay(new int[] { monthlyDay });
280:
281: int monthlyInterval = ParamUtil.getInteger(req,
282: "monthlyInterval0");
283:
284: recurrence.setInterval(monthlyInterval);
285: } else {
286: int monthlyPos = ParamUtil.getInteger(req,
287: "monthlyPos");
288: int monthlyDay = ParamUtil.getInteger(req,
289: "monthlyDay1");
290:
291: DayAndPosition[] dayPos = { new DayAndPosition(
292: monthlyDay, monthlyPos) };
293:
294: recurrence.setByDay(dayPos);
295:
296: int monthlyInterval = ParamUtil.getInteger(req,
297: "monthlyInterval1");
298:
299: recurrence.setInterval(monthlyInterval);
300: }
301: } else if (recurrenceType == Recurrence.YEARLY) {
302: int yearlyType = ParamUtil
303: .getInteger(req, "yearlyType");
304:
305: if (yearlyType == 0) {
306: int yearlyMonth = ParamUtil.getInteger(req,
307: "yearlyMonth0");
308: int yearlyDay = ParamUtil.getInteger(req,
309: "yearlyDay0");
310:
311: recurrence.setByMonth(new int[] { yearlyMonth });
312: recurrence.setByMonthDay(new int[] { yearlyDay });
313:
314: int yearlyInterval = ParamUtil.getInteger(req,
315: "yearlyInterval0");
316:
317: recurrence.setInterval(yearlyInterval);
318: } else {
319: int yearlyPos = ParamUtil.getInteger(req,
320: "yearlyPos");
321: int yearlyDay = ParamUtil.getInteger(req,
322: "yearlyDay1");
323: int yearlyMonth = ParamUtil.getInteger(req,
324: "yearlyMonth1");
325:
326: DayAndPosition[] dayPos = { new DayAndPosition(
327: yearlyDay, yearlyPos) };
328:
329: recurrence.setByDay(dayPos);
330:
331: recurrence.setByMonth(new int[] { yearlyMonth });
332:
333: int yearlyInterval = ParamUtil.getInteger(req,
334: "yearlyInterval1");
335:
336: recurrence.setInterval(yearlyInterval);
337: }
338: }
339:
340: int endDateType = ParamUtil.getInteger(req, "endDateType");
341:
342: if (endDateType == 1) {
343: int endDateOccurrence = ParamUtil.getInteger(req,
344: "endDateOccurrence");
345:
346: recurrence.setOccurrence(endDateOccurrence);
347: } else if (endDateType == 2) {
348: Calendar recEndCal = null;
349:
350: if (timeZoneSensitive) {
351: recEndCal = CalendarFactoryUtil.getCalendar();
352:
353: recEndCal.setTime(startDate.getTime());
354: } else {
355: recEndCal = (Calendar) startDate.clone();
356: }
357:
358: recEndCal.set(Calendar.MONTH, endDateMonth);
359: recEndCal.set(Calendar.DATE, endDateDay);
360: recEndCal.set(Calendar.YEAR, endDateYear);
361:
362: recurrence.setUntil(recEndCal);
363: }
364: }
365:
366: String remindBy = ParamUtil.getString(req, "remindBy");
367: int firstReminder = ParamUtil.getInteger(req, "firstReminder");
368: int secondReminder = ParamUtil
369: .getInteger(req, "secondReminder");
370:
371: String[] communityPermissions = req
372: .getParameterValues("communityPermissions");
373: String[] guestPermissions = req
374: .getParameterValues("guestPermissions");
375:
376: if (eventId <= 0) {
377:
378: // Add event
379:
380: CalEventServiceUtil.addEvent(layout.getPlid(), title,
381: description, startDateMonth, startDateDay,
382: startDateYear, startDateHour, startDateMinute,
383: endDateMonth, endDateDay, endDateYear,
384: durationHour, durationMinute, allDay,
385: timeZoneSensitive, type, repeating, recurrence,
386: remindBy, firstReminder, secondReminder,
387: communityPermissions, guestPermissions);
388: } else {
389:
390: // Update event
391:
392: CalEventServiceUtil.updateEvent(eventId, title,
393: description, startDateMonth, startDateDay,
394: startDateYear, startDateHour, startDateMinute,
395: endDateMonth, endDateDay, endDateYear,
396: durationHour, durationMinute, allDay,
397: timeZoneSensitive, type, repeating, recurrence,
398: remindBy, firstReminder, secondReminder);
399: }
400: }
401:
402: private void _addWeeklyDayPos(ActionRequest req, List list, int day) {
403: if (ParamUtil.getBoolean(req, "weeklyDayPos" + day)) {
404: list.add(new DayAndPosition(day, 0));
405: }
406: }
407:
408: }
|