001: package org.claros.intouch.calendar.controllers;
002:
003: import java.sql.Timestamp;
004: import java.text.SimpleDateFormat;
005: import java.util.ArrayList;
006: import java.util.Calendar;
007: import java.util.Collections;
008: import java.util.Date;
009: import java.util.List;
010:
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013: import org.claros.commons.auth.models.AuthProfile;
014: import org.claros.commons.utility.Formatter;
015: import org.claros.intouch.calendar.models.CalendarDailyItems;
016: import org.claros.intouch.calendar.models.CalendarHourItems;
017: import org.claros.intouch.calendar.models.CalendarObject;
018: import org.claros.intouch.calendar.models.CalendarObjectWrap;
019: import org.claros.intouch.calendar.models.CalendarWeeklyItems;
020: import org.claros.intouch.calendar.utility.Constants;
021: import org.claros.intouch.calendar.utility.Utility;
022:
023: /**
024: * @author Umut Gokbayrak
025: */
026: public class CalendarController {
027: private static Log log = LogFactory
028: .getLog(CalendarController.class);
029:
030: /**
031: * @param auth
032: * @param cal
033: * @return
034: */
035: public static CalendarDailyItems populateDailyContent(
036: AuthProfile auth, Calendar cal, boolean getAll)
037: throws Exception {
038: CalendarDailyItems out = new CalendarDailyItems();
039: ArrayList apps = getAppointments(auth, cal, Constants.DAY,
040: getAll);
041:
042: CalendarHourItems tmp = null;
043: Timestamp begin, end = null;
044:
045: SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd "
046: + "00:00:00.000");
047: Timestamp beginDay = Timestamp.valueOf(sdf
048: .format(cal.getTime()));
049:
050: // Timestamp beginDay = Utility.getLimitTimestampFromCalendar(cal, true);
051: cal.setTime(new Date(beginDay.getTime()));
052:
053: int iCalHours = 24;
054:
055: CalendarObjectWrap app = null;
056: for (int i = 0; i < iCalHours; i++) {
057: tmp = new CalendarHourItems();
058: tmp.setHour(new Integer(cal.get(Calendar.HOUR_OF_DAY)));
059:
060: for (int j = 0; j < apps.size(); j++) {
061: app = (CalendarObjectWrap) apps.get(j);
062:
063: begin = Utility.getHourLimitTimestampFromCalendar(cal,
064: true);
065: end = Utility.getHourLimitTimestampFromCalendar(cal,
066: false);
067:
068: if (app.getOccuringDate().equals(begin)
069: || (app.getOccuringDate().before(end) && app
070: .getOccuringDate().after(begin))) {
071: tmp.addAppointment(app);
072: }
073: }
074: out.addHour(tmp);
075: cal.add(Calendar.HOUR_OF_DAY, 1);
076: }
077: return out;
078: }
079:
080: /**
081: * Ekranda g�stermek �zere haftal�k i�eri�i haz�rlayan metoddur. Gerekli tekrarlanan
082: * objeleri roll ettirir ve geriye g�stermeye haz�r bir arraylist ve i�erisinde
083: * AgendaDailyItem objeleri d�nd�r�r.
084: * @param cif
085: * @param cal
086: * @return
087: */
088: public static ArrayList populateWeekContent(AuthProfile auth,
089: Calendar cal) {
090: ArrayList out = new ArrayList();
091: ArrayList apps = getAppointments(auth, cal, Constants.WEEK,
092: false);
093:
094: CalendarDailyItems tmp = null;
095: Timestamp begin = null;
096: Timestamp end = null;
097: CalendarObjectWrap app = null;
098: for (int i = 0; i < 7; i++) {
099: tmp = new CalendarDailyItems();
100: tmp.setDate(new Timestamp(cal.getTime().getTime()));
101: begin = Utility.getLimitTimestampFromCalendar(cal, true);
102: end = Utility.getLimitTimestampFromCalendar(cal, false);
103:
104: for (int j = 0; j < apps.size(); j++) {
105: app = (CalendarObjectWrap) apps.get(j);
106: if (app.getOccuringDate().before(end)
107: && app.getOccuringDate().after(begin)) {
108: tmp.addAppointment(app);
109: }
110: }
111: out.add(tmp);
112: cal.add(Calendar.DATE, 1);
113: }
114: return out;
115: }
116:
117: /**
118: * Ayl�k olarak listelenecek kay�tlar�n listesini �eker.
119: * @param cif
120: * @param cal
121: * @return
122: */
123: public static ArrayList populateMonthContent(AuthProfile auth,
124: Calendar cal) {
125: ArrayList out = new ArrayList();
126: ArrayList apps = getAppointments(auth, cal, Constants.MONTH,
127: false);
128:
129: cal.set(Calendar.DAY_OF_MONTH, 1);
130: int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
131:
132: int emptyDaysInFirstWeek = 0;
133: Calendar tmpCal = (Calendar) cal.clone();
134: while (dayOfWeek != Calendar.SUNDAY) {
135: tmpCal.add(Calendar.DATE, -1);
136: emptyDaysInFirstWeek++;
137: dayOfWeek = tmpCal.get(Calendar.DAY_OF_WEEK);
138: }
139:
140: int daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
141: int totalDays = emptyDaysInFirstWeek + daysInMonth;
142: int weeks = (int) Math.ceil((double) totalDays / 7);
143: int exactDays = weeks * 7;
144: // int month = cal.get(Calendar.MONTH);
145:
146: cal.add(Calendar.DATE, -emptyDaysInFirstWeek);
147: CalendarDailyItems day = null;
148: CalendarWeeklyItems week = new CalendarWeeklyItems();
149: int weekCounter = 0;
150: CalendarObjectWrap app = null;
151: Timestamp begin = null;
152: Timestamp end = null;
153: for (int i = 0; i < exactDays; i++) {
154: day = new CalendarDailyItems();
155: begin = Utility.getLimitTimestampFromCalendar(cal, true);
156: end = Utility.getLimitTimestampFromCalendar(cal, false);
157:
158: for (int j = 0; j < apps.size(); j++) {
159: app = (CalendarObjectWrap) apps.get(j);
160: if (app.getOccuringDate().before(end)
161: && app.getOccuringDate().after(begin)) {
162: String desc = app.getDescription();
163: if (desc == null) {
164: desc = Formatter.formatDate(app
165: .getOccuringDate(), "HH:mm");
166: } else if (desc.length() > 15) {
167: desc = desc.substring(0, 15) + "...";
168: }
169: app.setDescription(desc);
170: day.addAppointment(app);
171: }
172: }
173:
174: day.setDate(new Timestamp(cal.getTime().getTime()));
175: week.addDay(day);
176: cal.add(Calendar.DATE, 1);
177: weekCounter++;
178: if (weekCounter >= 7) {
179: week.setWeekInYear(cal.get(Calendar.WEEK_OF_YEAR) - 1);
180: out.add(week);
181: week = new CalendarWeeklyItems();
182: weekCounter = 0;
183: }
184: }
185: return out;
186: }
187:
188: /**
189: * @param cif
190: * @param cal
191: * @return
192: */
193: private static ArrayList getAppointments(AuthProfile auth,
194: Calendar cal, int timeLimit, boolean getAll) {
195: ArrayList out = new ArrayList();
196:
197: try {
198: ArrayList res = Utility.getBeginEndTimestamps(cal,
199: timeLimit);
200: Timestamp begin = (Timestamp) res.get(0);
201: Timestamp end = (Timestamp) res.get(1);
202:
203: List allApps = CalendarDBController
204: .getCalendarObjectsByUser(auth, getAll);
205:
206: CalendarObject tmpCal = null;
207: CalendarObjectWrap tmpApp = null;
208: Timestamp tmpDate = null;
209: for (int i = 0; i < allApps.size(); i++) {
210: tmpCal = (CalendarObject) allApps.get(i);
211: tmpApp = new CalendarObjectWrap(tmpCal);
212:
213: tmpDate = tmpApp.getRecordDate();
214:
215: switch (tmpApp.getRepeatType().intValue()) {
216: case Constants.REPEAT_TYPE_ONCE:
217: if (tmpDate.after(begin) && tmpDate.before(end)) {
218: tmpApp.setOccuringDate(tmpDate);
219: out.add(tmpApp.clone());
220: }
221: break;
222: case Constants.REPEAT_TYPE_MONTH:
223: tmpApp = Utility.rollAppointment(tmpApp,
224: Calendar.MONTH, 1, begin, end);
225: if (tmpApp != null) {
226: out.add(tmpApp.clone());
227: }
228: break;
229: case Constants.REPEAT_TYPE_YEAR:
230: tmpApp = Utility.rollAppointment(tmpApp,
231: Calendar.YEAR, 1, begin, end);
232: if (tmpApp != null) {
233: out.add(tmpApp.clone());
234: }
235: break;
236: case Constants.REPEAT_TYPE_WEEK:
237: tmpApp = Utility.rollAppointment(tmpApp,
238: Calendar.WEEK_OF_YEAR, 1, begin, end);
239: if (tmpApp != null) {
240: out.add(tmpApp.clone());
241: if (timeLimit == Constants.MONTH) {
242: while (tmpApp != null
243: && (tmpApp.getOccuringDate() == null || tmpApp
244: .getOccuringDate().before(
245: end))) {
246: tmpApp = Utility.rollAppointmentOnce(
247: tmpApp, Calendar.WEEK_OF_YEAR,
248: 1, end);
249: if (tmpApp != null) {
250: out.add(tmpApp.clone());
251: }
252: }
253: }
254: }
255: break;
256: case Constants.REPEAT_TYPE_TWO_WEEK:
257: tmpApp = Utility.rollAppointment(tmpApp,
258: Calendar.WEEK_OF_YEAR, 2, begin, end);
259: if (tmpApp != null) {
260: out.add(tmpApp.clone());
261: if (timeLimit == Constants.MONTH) {
262: while (tmpApp != null
263: && (tmpApp.getOccuringDate() == null || tmpApp
264: .getOccuringDate().before(
265: end))) {
266: tmpApp = Utility.rollAppointmentOnce(
267: tmpApp, Calendar.WEEK_OF_YEAR,
268: 2, end);
269: if (tmpApp != null) {
270: out.add(tmpApp.clone());
271: }
272: }
273: }
274: }
275: break;
276: }
277: }
278: } catch (Exception e) {
279: log.error(e);
280: }
281: Collections.sort(out);
282:
283: return out;
284: }
285:
286: /**
287: *
288: * @param auth
289: * @return
290: * @throws Exception
291: */
292: public static List getAlertsByUser(AuthProfile auth)
293: throws Exception {
294: Calendar cal = Calendar.getInstance();
295: CalendarDailyItems items = populateDailyContent(auth, cal,
296: false);
297: return parseAlerts(items);
298: }
299:
300: /**
301: *
302: * @param auth
303: * @return
304: * @throws Exception
305: */
306: public static List getAlertsForAll(AuthProfile auth)
307: throws Exception {
308: Calendar cal = Calendar.getInstance();
309: CalendarDailyItems items = populateDailyContent(auth, cal, true);
310: return parseAlerts(items);
311: }
312:
313: /**
314: *
315: * @param items
316: * @return
317: */
318: private static List parseAlerts(CalendarDailyItems items) {
319: Timestamp now = new Timestamp(new Date().getTime());
320: List lst = items.getHours();
321: List out = new ArrayList();
322: if (lst != null) {
323: CalendarHourItems tmpH = null;
324: CalendarObjectWrap obj = null;
325: for (int j = 0; j < lst.size(); j++) {
326: tmpH = (CalendarHourItems) lst.get(j);
327: List apps = tmpH.getAppointments();
328:
329: for (int i = 0; i < apps.size(); i++) {
330: obj = (CalendarObjectWrap) apps.get(i);
331:
332: // check to see if this item is dismissed but
333: // it my be a recurring event and can be dismissed
334: // by many times. so we shall check if it was last
335: // dismissed before today
336: Calendar cal = Calendar.getInstance();
337: cal.setTime(new Date(now.getTime()));
338: Timestamp lastDismissed = obj.getLastDismissedAt();
339: ArrayList beginEnd = Utility.getBeginEndTimestamps(
340: cal, Constants.DAY);
341: Timestamp beginToday = (Timestamp) beginEnd.get(0);
342:
343: boolean recurringAlert = false;
344: if (lastDismissed != null
345: && lastDismissed.before(beginToday)) {
346: recurringAlert = true;
347: }
348: if (recurringAlert
349: || obj.getRemindedBefore() == null
350: || obj.getRemindedBefore().equals("false")) {
351: Timestamp occursAt = obj.getOccuringDate();
352: int remindBefore = obj.getReminderDays() * 1000;
353: Calendar cal2 = Calendar.getInstance();
354: cal2.setTime(new Date(occursAt.getTime()));
355: cal2
356: .add(Calendar.MILLISECOND,
357: 0 - remindBefore);
358: occursAt = new Timestamp(cal2.getTime()
359: .getTime());
360:
361: if (now.after(occursAt)) {
362: out.add(obj);
363: }
364: }
365: }
366: }
367: }
368: return out;
369: }
370: }
|