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.ArrayList;
008: import java.util.Calendar;
009: import java.util.Date;
010: import java.util.List;
011:
012: import javax.servlet.ServletException;
013: import javax.servlet.http.HttpServletRequest;
014: import javax.servlet.http.HttpServletResponse;
015:
016: import org.claros.commons.auth.models.AuthProfile;
017: import org.claros.commons.utility.Formatter;
018: import org.claros.commons.utility.Utility;
019: import org.claros.intouch.calendar.controllers.CalendarController;
020: import org.claros.intouch.calendar.models.CalendarDailyItems;
021: import org.claros.intouch.calendar.models.CalendarHourItems;
022: import org.claros.intouch.calendar.models.CalendarObjectWrap;
023: import org.claros.intouch.calendar.models.CalendarWeeklyItems;
024: import org.claros.intouch.common.services.BaseService;
025:
026: public class GetEventsService extends BaseService {
027: private static final long serialVersionUID = 1587912882541089504L;
028: private static final String days[] = new String[] { "sunday.long",
029: "monday.long", "tuesday.long", "wednesday.long",
030: "thursday.long", "friday.long", "saturday.long" };
031: private static final String months[] = new String[] {
032: "january.long", "february.long", "march.long",
033: "april.long", "may.long", "june.long", "july.long",
034: "august.long", "september.long", "october.long",
035: "november.long", "december.long" };
036: private static DecimalFormat df = new DecimalFormat("00");
037:
038: /**
039: *
040: */
041: public void doPost(HttpServletRequest request,
042: HttpServletResponse response) throws ServletException,
043: IOException {
044: response.setHeader("Expires", "-1");
045: response.setHeader("Pragma", "no-cache");
046: response.setHeader("Cache-control", "no-cache");
047: response.setHeader("Content-Type", "text/xml; charset=utf-8");
048: PrintWriter out = response.getWriter();
049:
050: try {
051: String type = request.getParameter("type");
052: String year = request.getParameter("year");
053: String month = df.format(Integer.parseInt(request
054: .getParameter("month")));
055: String day = df.format(Integer.parseInt(request
056: .getParameter("day")));
057: AuthProfile auth = getAuthProfile(request);
058:
059: out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
060: out.write("<data>");
061:
062: out.print("<real-month>" + month + "</real-month>");
063:
064: out.print("<events>");
065:
066: Timestamp ts = Timestamp.valueOf(year + "-" + month + "-"
067: + day + " 00:00:00.000");
068: Calendar cal = Calendar.getInstance();
069: cal.setTime(new Date(ts.getTime()));
070: Calendar cal2 = (Calendar) cal.clone();
071:
072: if (type.equals("daily")) {
073: CalendarDailyItems items = CalendarController
074: .populateDailyContent(auth, cal, false);
075: List lst = items.getHours();
076: if (lst != null) {
077: CalendarHourItems tmpH = null;
078: for (int j = 0; j < lst.size(); j++) {
079: tmpH = (CalendarHourItems) lst.get(j);
080: List lstApp = tmpH.getAppointments();
081:
082: printAppointments(lstApp, out);
083: }
084: }
085: out.print("</events>");
086: out.print("<date-display>"
087: + getDateDisplayed(cal2, year, month, day,
088: request) + " </date-display>");
089: } else if (type.equals("weekly")) {
090: // print the events for week
091: ArrayList lst = CalendarController.populateWeekContent(
092: auth, cal);
093: CalendarDailyItems tmp = null;
094: if (lst != null) {
095: for (int i = 0; i < lst.size(); i++) {
096: tmp = (CalendarDailyItems) lst.get(i);
097: printAppointments(tmp.getAppointments(), out);
098: }
099: }
100:
101: out.print("</events>");
102:
103: // now print the days for week
104: if (lst != null) {
105: for (int i = 0; i < lst.size(); i++) {
106: tmp = (CalendarDailyItems) lst.get(i);
107: Calendar calT = Calendar.getInstance();
108: calT.setTimeInMillis(tmp.getDate().getTime());
109: String dD = df.format(calT.get(Calendar.DATE));
110: String dM = df
111: .format(calT.get(Calendar.MONTH) + 1);
112: String dY = "" + calT.get(Calendar.YEAR);
113:
114: out.print("<day" + i + ">" + dD + "-" + dM
115: + "-" + dY + "</day" + i + ">");
116: }
117: }
118:
119: out.print("<date-display>"
120: + getDateDisplayed(cal2, year, month, day,
121: request) + " </date-display>");
122: } else if (type.equals("monthly")) {
123: ArrayList lst = CalendarController
124: .populateMonthContent(auth, cal);
125: out
126: .print("<week-count>" + lst.size()
127: + "</week-count>");
128:
129: CalendarWeeklyItems weekTmp = null;
130: for (int i = 0; i < lst.size(); i++) {
131: out.print("<week-event>");
132: weekTmp = (CalendarWeeklyItems) lst.get(i);
133: out.print("<week-in-year>"
134: + weekTmp.getWeekInYear()
135: + "</week-in-year>");
136: CalendarDailyItems dailyTmp = null;
137: ArrayList lstDays = weekTmp.getDays();
138: for (int j = 0; j < lstDays.size(); j++) {
139: dailyTmp = (CalendarDailyItems) lstDays.get(j);
140: out.print(" <day-event>");
141: out.print("<day-event-day>"
142: + Formatter.formatDate(dailyTmp
143: .getDate(), "dd")
144: + "</day-event-day>");
145: out.print("<day-event-month>"
146: + Formatter.formatDate(dailyTmp
147: .getDate(), "MM")
148: + "</day-event-month>");
149: out.print("<day-event-year>"
150: + Formatter.formatDate(dailyTmp
151: .getDate(), "yyyy")
152: + "</day-event-year>");
153: printAppointments(dailyTmp.getAppointments(),
154: out);
155: out.print(" </day-event>");
156: }
157: out.print("</week-event>");
158: }
159: out.print("</events>");
160: out.print("<date-display>"
161: + getDateDisplayed(cal2, year, month, day,
162: request) + " </date-display>");
163: }
164: out.print("<result>0</result>");
165: out.write("</data>");
166: } catch (Exception e) {
167: out.print("</events>");
168: out.print("<result>1</result>");
169: out.write("</data>");
170: }
171:
172: }
173:
174: private static void printAppointments(List lstApp, PrintWriter out) {
175: Calendar calTmp2 = Calendar.getInstance();
176: Calendar calTmp = Calendar.getInstance();
177: CalendarObjectWrap tmp = null;
178:
179: for (int i = 0; i < lstApp.size(); i++) {
180: tmp = (CalendarObjectWrap) lstApp.get(i);
181: calTmp.setTimeInMillis(tmp.getEndDate().getTime());
182: calTmp2.setTimeInMillis(tmp.getOccuringDate().getTime());
183: out.print("<event>");
184: out.print("<id>" + tmp.getId() + " </id>");
185: out.print("<year>" + calTmp2.get(Calendar.YEAR)
186: + " </year>");
187: out.print("<month>"
188: + df.format(calTmp2.get(Calendar.MONTH) + 1)
189: + " </month>");
190: out.print("<day>" + df.format(calTmp2.get(Calendar.DATE))
191: + " </day>");
192: out.print("<begin>"
193: + df.format(calTmp2.get(Calendar.HOUR_OF_DAY))
194: + ":" + df.format(calTmp2.get(Calendar.MINUTE))
195: + " </begin>");
196: out.print("<end>"
197: + df.format(calTmp.get(Calendar.HOUR_OF_DAY)) + ":"
198: + df.format(calTmp.get(Calendar.MINUTE))
199: + " </end>");
200: out.print("<color>" + tmp.getColor() + " </color>");
201: out.print("<description>"
202: + Utility.htmlSpecialChars(tmp.getDescription())
203: + " </description>");
204: out
205: .print("<hasReminder>"
206: + (tmp.getReminderDays() != null
207: && tmp.getReminderDays().equals(
208: "-1") ? "false" : "true")
209: + " </hasReminder>");
210: out.print("<repeatType>" + tmp.getRepeatType()
211: + " </repeatType>");
212: out
213: .print("<location>" + tmp.getLocation()
214: + " </location>");
215: out.print("<reminderDays>" + tmp.getReminderDays()
216: + " </reminderDays>");
217: out.print("<reminderMethod>" + tmp.getReminderMethod()
218: + " </reminderMethod>");
219: out.print("</event>");
220: }
221: }
222:
223: /**
224: *
225: * @param cal
226: * @param year
227: * @param month
228: * @param day
229: * @param request
230: * @return
231: */
232: private String getDateDisplayed(Calendar cal, String year,
233: String month, String day, HttpServletRequest request) {
234: cal.setFirstDayOfWeek(Calendar.SUNDAY);
235: int dayWeek = cal.get(Calendar.DAY_OF_WEEK) - 1;
236:
237: String sDayWeek = getText(request, days[dayWeek]);
238: String sDayMonth = getText(request, months[Integer
239: .parseInt(month) - 1]);
240: String type = request.getParameter("type");
241:
242: String str = "";
243: if (type.equals("daily")) {
244: str = day + " " + sDayMonth + " " + year + ", " + sDayWeek;
245: } else if (type.equals("weekly")) {
246:
247: } else if (type.equals("monthly")) {
248: str = sDayMonth + " " + year;
249: }
250: return str;
251: }
252: }
|