001: package org.claros.intouch.calendar.services;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005: import java.sql.Timestamp;
006: import java.text.DecimalFormat;
007: import java.util.Calendar;
008: import java.util.Date;
009: import java.util.List;
010:
011: import javax.servlet.ServletException;
012: import javax.servlet.http.HttpServletRequest;
013: import javax.servlet.http.HttpServletResponse;
014:
015: import org.claros.commons.auth.models.AuthProfile;
016: import org.claros.intouch.calendar.controllers.CalendarController;
017: import org.claros.intouch.calendar.controllers.CalendarDBController;
018: import org.claros.intouch.calendar.models.CalendarDailyItems;
019: import org.claros.intouch.calendar.models.CalendarHourItems;
020: import org.claros.intouch.calendar.models.CalendarObject;
021: import org.claros.intouch.calendar.models.CalendarObjectWrap;
022: import org.claros.intouch.common.services.BaseService;
023:
024: public class SaveEventService extends BaseService {
025:
026: /**
027: *
028: */
029: private static final long serialVersionUID = 1L;
030:
031: /**
032: * @param request the request send by the client to the server
033: * @param response the response send by the server to the client
034: * @throws ServletException if an error occurred
035: * @throws IOException if an error occurred
036: */
037: public void doPost(HttpServletRequest request,
038: HttpServletResponse response) throws ServletException,
039: IOException {
040: response.setHeader("Expires", "-1");
041: response.setHeader("Pragma", "no-cache");
042: response.setHeader("Cache-control", "no-cache");
043: response.setHeader("Content-Type", "text/html; charset=utf-8");
044: PrintWriter out = response.getWriter();
045:
046: DecimalFormat df = new DecimalFormat("00");
047:
048: try {
049: String sEventId = request.getParameter("eventId");
050:
051: String sBegin = request.getParameter("begin");
052: String sEnd = request.getParameter("end");
053: String sColor = request.getParameter("color");
054: String sRepeatType = request.getParameter("repeatType");
055: String sDescription = request.getParameter("description");
056: String sLocation = request.getParameter("location");
057: String sReminderDays = request.getParameter("reminderDays");
058: String sReminderMethod = request
059: .getParameter("reminderMethod");
060: String sYear = request.getParameter("year");
061: String sMonth = df.format(Integer.parseInt(request
062: .getParameter("month")));
063: String sDay = df.format(Integer.parseInt(request
064: .getParameter("day")));
065:
066: Timestamp recordDate = Timestamp.valueOf(sYear + "-"
067: + sMonth + "-" + sDay + " " + sBegin + ":00.000");
068: Timestamp endDate = Timestamp.valueOf(sYear + "-" + sMonth
069: + "-" + sDay + " " + sEnd + ":00.000");
070:
071: Timestamp ts = Timestamp.valueOf(sYear + "-" + sMonth + "-"
072: + sDay + " 00:00:00.000");
073: Calendar cal = Calendar.getInstance();
074: cal.setTime(new Date(ts.getTime()));
075: CalendarDailyItems items = CalendarController
076: .populateDailyContent(getAuthProfile(request), cal,
077: false);
078: List hrs = items.getHours();
079: CalendarHourItems hr = null;
080:
081: for (int i = 0; i < hrs.size(); i++) {
082: hr = (CalendarHourItems) hrs.get(i);
083: List apps = hr.getAppointments();
084: if (apps != null) {
085: CalendarObjectWrap w = null;
086: for (int j = 0; j < apps.size(); j++) {
087: w = (CalendarObjectWrap) apps.get(j);
088:
089: Timestamp ocEnd = w.getEndDate();
090: Calendar cl = Calendar.getInstance();
091: cl.setTimeInMillis(ocEnd.getTime());
092: Timestamp ocBegin = w.getOccuringDate();
093: Calendar cl2 = Calendar.getInstance();
094: cl2.setTimeInMillis(ocBegin.getTime());
095: ocEnd = Timestamp
096: .valueOf(cl2.get(Calendar.YEAR)
097: + "-"
098: + df
099: .format(cl2
100: .get(Calendar.MONTH) + 1)
101: + "-"
102: + df.format(cl2
103: .get(Calendar.DATE))
104: + " "
105: + df
106: .format(cl
107: .get(Calendar.HOUR_OF_DAY))
108: + ":"
109: + df.format(cl
110: .get(Calendar.MINUTE))
111: + ":00.000");
112:
113: if ((recordDate.before(w.getOccuringDate()) && (endDate
114: .equals(w.getOccuringDate()) || endDate
115: .before(w.getOccuringDate())))
116: || (recordDate.equals(ocEnd) || recordDate
117: .after(ocEnd))) {
118: // do nothing it is valid
119: System.out.println("ab-> " + recordDate
120: + " ae->" + endDate + " xb->"
121: + w.getOccuringDate() + " xe->"
122: + ocEnd);
123: } else {
124: if (!sEventId.equals(w.getId().toString())) {
125: out.print("collapse");
126: return;
127: }
128: }
129: }
130: }
131: }
132:
133: AuthProfile auth = getAuthProfile(request);
134: CalendarObject item = new CalendarObject();
135: item.setColor(sColor);
136: item.setDescription(sDescription);
137: item.setEndDate(endDate);
138: if (sEventId != null && !sEventId.equals("null")
139: && sEventId.length() > 0) {
140: try {
141: item.setId(new Long(sEventId));
142: } catch (NumberFormatException nf) {
143: }
144: }
145: item.setLocation(sLocation);
146: item.setRecordDate(recordDate);
147: item.setReminderDays(new Integer(sReminderDays));
148: item.setReminderMethod(new Integer(sReminderMethod));
149: item.setRepeatType(new Integer(sRepeatType));
150: item.setUsername(auth.getUsername());
151: // this item is modified so we shall set the alarm again.
152: item.setRemindedBefore("false");
153: item.setLastDismissedAt(null);
154:
155: CalendarDBController.saveEvent(auth, item);
156:
157: List events = CalendarDBController
158: .getCalendarObjectsByUser(auth, false);
159: Long lastId = null;
160: if (events != null) {
161: CalendarObject tmp = (CalendarObject) events.get(0);
162: lastId = tmp.getId();
163: }
164: out.print("ok" + lastId.longValue());
165: } catch (Exception e) {
166: out.print("fail");
167: }
168: }
169:
170: }
|