001: package org.claros.intouch.calendar.utility;
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.Date;
008:
009: import org.claros.intouch.calendar.models.CalendarObjectWrap;
010:
011: /**
012: * @author Umut Gokbayrak
013: */
014: public class Utility {
015: /**
016: * Input olarak gelen tarihi parametreye gore ait oldugu gunun basina veya
017: * sonuna gonderir. Ornegin haftanin ilk gununu buldugumuzda calendar objemizi
018: * o gunun ilk salisesi 00:00.000 saatine cevirir. Bunun icin isBegin parametresi
019: * true olmalidir.
020: * @param cal
021: * @param isBegin
022: * @return
023: */
024: public static Timestamp getLimitTimestampFromCalendar(Calendar cal,
025: boolean isBegin) {
026: SimpleDateFormat sdf = new SimpleDateFormat(
027: "yyyy-MM-dd HH:mm:ss.SSS");
028: SimpleDateFormat sdfBegin = new SimpleDateFormat(
029: "yyyy-MM-dd 00:00:00.000");
030: SimpleDateFormat sdfEnd = new SimpleDateFormat(
031: "yyyy-MM-dd 23:59:59.999");
032:
033: Calendar calTmp = Calendar.getInstance();
034: if (isBegin) {
035: Timestamp begin = Timestamp.valueOf(sdfBegin.format(cal
036: .getTime()));
037: calTmp.setTime(new Date(begin.getTime()));
038: calTmp.add(Calendar.MILLISECOND, -1);
039: } else {
040: Timestamp end = Timestamp.valueOf(sdfEnd.format(cal
041: .getTime()));
042: calTmp.setTime(new Date(end.getTime()));
043: calTmp.add(Calendar.MILLISECOND, 1);
044: }
045: return Timestamp.valueOf(sdf.format(calTmp.getTime()));
046: }
047:
048: /**
049: *
050: * @param cal
051: * @param timeVar
052: * @return
053: */
054: public static ArrayList getBeginEndTimestamps(Calendar cal,
055: int timeVar) {
056: Timestamp begin = null;
057: Timestamp end = null;
058: cal.setFirstDayOfWeek(Calendar.SUNDAY);
059:
060: switch (timeVar) {
061: case Constants.DAY:
062: begin = getLimitTimestampFromCalendar(cal, true);
063: end = getLimitTimestampFromCalendar(cal, false);
064: break;
065: case Constants.WEEK:
066: // Eger buraya gelmis olan gun pazartesi degilse pazartesi yapmaliyiz.
067: int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
068: while (dayOfWeek != Calendar.SUNDAY) {
069: cal.add(Calendar.DATE, -1);
070: dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
071: }
072:
073: Calendar sunday = (Calendar) cal.clone();
074: sunday.add(Calendar.DATE, 6);
075: begin = getLimitTimestampFromCalendar(cal, true);
076: end = getLimitTimestampFromCalendar(sunday, false);
077: sunday = null;
078: break;
079: case Constants.MONTH:
080: cal.set(Calendar.DAY_OF_MONTH, 1);
081:
082: Calendar lastDay = (Calendar) cal.clone();
083: int l = lastDay.getActualMaximum(Calendar.DAY_OF_MONTH);
084: lastDay.set(Calendar.DAY_OF_MONTH, l);
085:
086: begin = getLimitTimestampFromCalendar(cal, true);
087: end = getLimitTimestampFromCalendar(lastDay, false);
088: lastDay = null;
089: break;
090: }
091:
092: ArrayList out = new ArrayList();
093: out.add(begin);
094: out.add(end);
095: return out;
096: }
097:
098: /**
099: * Tekrarlanan olaylar�n belirtilen zaman aral���nda (begin -> end) ger�ekle�ip
100: * ger�ekle�medi�ini anlamak i�in o ajanda kayd� belirtilen kriter ile
101: * end tarihinden k���k oldu�u s�rece ileriye do�ru sarmak gereklidir. Bu
102: * metod bu i�lemi yapar.
103: * @param tmpApp ajanda kayd�
104: * @param timeCriteria ileriye do�ru sar�lan kriter. gun/hafta/ay/y�l vs...
105: * @param ascend ka�ar ka�ar artt�r�lmal� roll ederken.
106: * @param begin hangi zaman aral���na kadar sar�yoruz. ba�lang�� parametresi
107: * @param end hangi zaman aral���na kadar sar�yoruz. biti� parametresi
108: * @return sar�lm�� halde ajanda kayd�n� d�nd�r�r
109: */
110: public static CalendarObjectWrap rollAppointment(
111: CalendarObjectWrap tmpApp, int timeCriteria, int ascend,
112: Timestamp begin, Timestamp end) {
113: Timestamp tmpDate = tmpApp.getRecordDate();
114:
115: while (tmpDate.before(begin)) {
116: Calendar tmpCal = Calendar.getInstance();
117: tmpCal.setTime(new Date(tmpDate.getTime()));
118: tmpCal.add(timeCriteria, ascend);
119: tmpDate = new Timestamp(tmpCal.getTime().getTime());
120: }
121: if (tmpDate.before(end)) {
122: // tekrarlanan olaylarda olay�n tekrarland��� ve o hafta/ay/y�l i�inde yer ald��� tarihi burada buluyoruz.
123: tmpApp.setOccuringDate(tmpDate);
124: return tmpApp;
125: }
126: return null;
127: }
128:
129: public static CalendarObjectWrap rollAppointmentOnce(
130: CalendarObjectWrap tmpApp, int timeCriteria, int ascend,
131: Timestamp end) {
132: Timestamp tmpDate = tmpApp.getOccuringDate();
133:
134: Calendar tmpCal = Calendar.getInstance();
135: tmpCal.setTime(new Date(tmpDate.getTime()));
136: tmpCal.add(timeCriteria, ascend);
137: tmpDate = new Timestamp(tmpCal.getTime().getTime());
138:
139: if (tmpDate.before(end)) {
140: tmpApp.setOccuringDate(tmpDate);
141: return tmpApp;
142: }
143: return null;
144: }
145:
146: /**
147: * @param cal
148: * @param b
149: * @return
150: */
151: public static Timestamp getHourLimitTimestampFromCalendar(
152: Calendar cal, boolean isBegin) {
153: SimpleDateFormat sdf = new SimpleDateFormat(
154: "yyyy-MM-dd HH:mm:ss.SSS");
155: SimpleDateFormat sdfBegin = new SimpleDateFormat(
156: "yyyy-MM-dd HH:00:00.000");
157: SimpleDateFormat sdfEnd = new SimpleDateFormat(
158: "yyyy-MM-dd HH:59:59.999");
159:
160: Calendar calTmp = Calendar.getInstance();
161: if (isBegin) {
162: Timestamp begin = Timestamp.valueOf(sdfBegin.format(cal
163: .getTime()));
164: calTmp.setTime(new Date(begin.getTime()));
165: calTmp.add(Calendar.MILLISECOND, -1);
166: } else {
167: Timestamp end = Timestamp.valueOf(sdfEnd.format(cal
168: .getTime()));
169: calTmp.setTime(new Date(end.getTime()));
170: calTmp.add(Calendar.MILLISECOND, 1);
171: }
172: return Timestamp.valueOf(sdf.format(calTmp.getTime()));
173: }
174: }
|