KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jical > RepeatRules


1 /*
2  *
3  * Created on August 3, 2002, 9:01 PM
4  *
5  * Stores a repeat rule object for
6  * manipulation by the expand events methods.
7  *
8  */

9
10 package org.jical;
11
12 /**
13  *
14  * @author sfg
15  * RFC 2445
16  *
17  */

18
19 import java.text.SimpleDateFormat JavaDoc;
20 import java.util.Calendar JavaDoc;
21 import java.util.Date JavaDoc;
22
23 /**
24  * @hibernate.class
25  * table="ICALREPEATRULES"
26  * dynamic-update="false"
27  * dynamic-insert="false"
28  *
29  * @hibernate.discriminator
30  * column="class"
31  */

32
33 public class RepeatRules {
34
35     private String JavaDoc id;
36
37    /**
38     *
39     * @hibernate.id
40     * generator-class="uuid.hex"
41     * column="ID"
42     *
43     * @hibernate.column
44     * name="ID"
45     * sql-type="VARCHAR(255)"
46     *
47     */

48
49     public String JavaDoc getId()
50     {
51         return this.id;
52     }
53
54     public void setId(String JavaDoc id)
55     {
56         this.id = id;
57     }
58
59     private String JavaDoc uid;
60
61    /**
62     *
63     * @hibernate.property
64     * column="UID"
65     *
66     * @hibernate.column
67     * name="uid"
68     * sql-type="VARCHAR(255)"
69     *
70     */

71
72     public String JavaDoc getUid()
73     {
74         return uid;
75     }
76
77     public void setUid(String JavaDoc uid)
78     {
79         this.uid = uid;
80     }
81
82     int interval = 0;
83
84    /**
85     *
86     * @hibernate.property
87     * column="INTERVAL"
88     *
89     * @hibernate.column
90     * name="uid"
91     * sql-type="INTEGER"
92     *
93     */

94
95     public int getInterval()
96     {
97         return interval;
98     }
99
100     public void setInterval(int interval)
101     {
102         this.interval = interval;
103     }
104
105     int dateRepeatUnit = Calendar.DATE;
106
107    /**
108     *
109     * @hibernate.property
110     * column="DATEREPEATUNIT"
111     *
112     * @hibernate.column
113     * name="dateRepeatUnit"
114     * sql-type="INTEGER"
115     *
116     */

117
118     public int getDateRepeatUnit()
119     {
120         return dateRepeatUnit;
121     }
122
123     public void setDateRepeatUnit(int dateRepeatUnit)
124     {
125         this.dateRepeatUnit = dateRepeatUnit;
126     }
127
128     String JavaDoc frequency = null;
129
130    /**
131     *
132     * @hibernate.property
133     * column="FREQUENCY"
134     *
135     * @hibernate.column
136     * name="frequency"
137     * sql-type="VARCHAR(255)"
138     *
139     */

140
141     public String JavaDoc getFrequency()
142     {
143         return frequency;
144     }
145
146     public void setFrequency(String JavaDoc frequency)
147     {
148         this.frequency = frequency;
149     }
150
151     String JavaDoc repeatUntil = null;
152
153    /**
154     *
155     * @hibernate.property
156     * column="REPEATUNTIL"
157     *
158     * @hibernate.column
159     * name="repeatUntil"
160     * sql-type="VARCHAR(255)"
161     *
162     */

163
164     public String JavaDoc getRepeatUntil()
165     {
166         return repeatUntil;
167     }
168
169     public void setRepeatUntil(String JavaDoc repeatUntil)
170     {
171         this.repeatUntil = repeatUntil;
172     }
173
174     String JavaDoc repeatByDay = null;
175
176    /**
177     *
178     * @hibernate.property
179     * column="REPEATBYDAY"
180     *
181     * @hibernate.column
182     * name="repeatByDay"
183     * sql-type="VARCHAR(255)"
184     *
185     */

186
187     public String JavaDoc getRepeatByDay()
188     {
189         return repeatByDay;
190     }
191
192     public void setRepeatByDay(String JavaDoc repeatByDay)
193     {
194         this.repeatByDay = repeatByDay;
195     }
196
197     String JavaDoc repeatByMonth = null;
198
199    /**
200     *
201     * @hibernate.property
202     * column="REPEATBYMONTH"
203     *
204     * @hibernate.column
205     * name="repeatByMonth"
206     * sql-type="VARCHAR(255)"
207     *
208     */

209
210     public String JavaDoc getRepeatByMonth()
211     {
212         return repeatByMonth;
213     }
214
215     public void setRepeatByMonth(String JavaDoc repeatByMonth)
216     {
217         this.repeatByMonth = repeatByMonth;
218     }
219
220     String JavaDoc repeatBySecond = null;
221
222    /**
223     *
224     * @hibernate.property
225     * column="REPEATBYSECOND"
226     *
227     * @hibernate.column
228     * name="repeatBySecond"
229     * sql-type="VARCHAR(255)"
230     *
231     */

232
233     public String JavaDoc getRepeatBySecond()
234     {
235         return repeatBySecond;
236     }
237
238     public void setRepeatBySecond(String JavaDoc repeatBySecond)
239     {
240         this.repeatBySecond = repeatBySecond;
241     }
242
243     String JavaDoc repeatByMinute = null;
244
245    /**
246     *
247     * @hibernate.property
248     * column="REPEATBYMINUTE"
249     *
250     * @hibernate.column
251     * name="repeatByMinute"
252     * sql-type="VARCHAR(255)"
253     *
254     */

255
256     public String JavaDoc getRepeatByMinute()
257     {
258         return repeatByMinute;
259     }
260
261     public void setRepeatByMinute(String JavaDoc repeatByMinute)
262     {
263         this.repeatByMinute = repeatByMinute;
264     }
265
266     String JavaDoc repeatByHour = null;
267
268    /**
269     *
270     * @hibernate.property
271     * column="REPEATBYHOUR"
272     *
273     * @hibernate.column
274     * name="repeatByHour"
275     * sql-type="VARCHAR(255)"
276     *
277     */

278
279     public String JavaDoc getRepeatByHour()
280     {
281         return repeatByHour;
282     }
283
284     public void setRepeatByHour(String JavaDoc repeatByHour)
285     {
286         this.repeatByHour = repeatByHour;
287     }
288
289     String JavaDoc repeatByMonthDay = null;
290
291    /**
292     *
293     * @hibernate.property
294     * column="REPEATBYMONTHDAY"
295     *
296     * @hibernate.column
297     * name="repeatByMonthDay"
298     * sql-type="VARCHAR(255)"
299     *
300     */

301
302     public String JavaDoc getRepeatByMonthDay()
303     {
304         return repeatByMonthDay;
305     }
306
307     public void setRepeatByMonthDay(String JavaDoc repeatByMonthDay)
308     {
309         this.repeatByMonthDay = repeatByMonthDay;
310     }
311
312     String JavaDoc repeatByYearDay = null;
313
314    /**
315     *
316     * @hibernate.property
317     * column="REPEATBYYEARDAY"
318     *
319     * @hibernate.column
320     * name="repeatByYearDay"
321     * sql-type="VARCHAR(255)"
322     *
323     */

324
325     public String JavaDoc getRepeatByYearDay()
326     {
327         return repeatByYearDay;
328     }
329
330     public void setRepeatByYearDay(String JavaDoc repeatByYearDay)
331     {
332         this.repeatByYearDay = repeatByYearDay;
333     }
334
335     String JavaDoc repeatByWeekNo = null;
336
337    /**
338     *
339     * @hibernate.property
340     * column="REPEATBYWEEKNO"
341     *
342     * @hibernate.column
343     * name="repeatByWeekNo"
344     * sql-type="VARCHAR(255)"
345     *
346     */

347
348     public String JavaDoc getRepeatByWeekNo()
349     {
350         return repeatByWeekNo;
351     }
352
353     public void setRepeatByWeekNo(String JavaDoc repeatByWeekNo)
354     {
355         this.repeatByWeekNo = repeatByWeekNo;
356     }
357
358     // Altered to Integer as was using -1 to represent null while -1 was a valid value!!!
359
Integer JavaDoc repeatBySetPos = null;
360
361    /**
362     *
363     * @hibernate.property
364     * column="REPEATBYSETPOS"
365     *
366     * @hibernate.column
367     * name="repeatBySetPos"
368     * sql-type="INTEGER"
369     *
370     */

371
372     public Integer JavaDoc getRepeatBySetPos()
373     {
374         return repeatBySetPos;
375     }
376
377     public void setRepeatBySetPos(Integer JavaDoc repeatBySetPos)
378     {
379         this.repeatBySetPos = repeatBySetPos;
380     }
381
382     int repeatUntilCount = 99999999;
383
384    /**
385     *
386     * @hibernate.property
387     * column="REPEATUNTILCOUNT"
388     *
389     * @hibernate.column
390     * name="repeatUntilCount"
391     * sql-type="INTEGER"
392     *
393     */

394
395     public int getRepeatUntilCount()
396     {
397         return repeatUntilCount;
398     }
399
400     public void setRepeatUntilCount(int repeatUntilCount)
401     {
402         this.repeatUntilCount = repeatUntilCount;
403     }
404
405     Date JavaDoc repeatUntilDate = null;
406
407    /**
408     *
409     * @hibernate.property
410     * column="REPEATUNTILDATE"
411     *
412     * @hibernate.column
413     * name="repeatUntilDate"
414     * sql-type="DATE"
415     *
416     */

417
418     public Date JavaDoc getRepeatUntilDate()
419     {
420         return repeatUntilDate;
421     }
422
423     public void setRepeatUntilDate(Date JavaDoc repeatUntilDate)
424     {
425         this.repeatUntilDate = repeatUntilDate;
426     }
427
428
429     private SimpleDateFormat JavaDoc dateFormatter = new SimpleDateFormat JavaDoc("yyyyMMddHHmmss");
430     
431     public RepeatRules() {
432     }
433     
434     public void parseRepeatRules (String JavaDoc rRule)
435     {
436         int startPoint = -1;
437         int ii = 0;
438         while (rRule != null && ii < 10)
439         {
440             ii++;
441             String JavaDoc thisRule = null;
442             startPoint = rRule.indexOf(";");
443             if (startPoint != -1)
444             {
445                 thisRule = rRule.substring(0,startPoint);
446                 rRule = rRule.substring(startPoint + 1);
447             }
448             else
449             {
450                 thisRule = rRule;
451                 rRule = null;
452             }
453             // Now evaluate the parameter..
454
// This is a weird one! Evolution-EndDate?
455
if (thisRule.startsWith("X-EVOLUTION-ENDDATE="))
456             {
457                 startPoint = thisRule.indexOf(":");
458                 String JavaDoc newRule = thisRule.substring(0,startPoint);
459                 String JavaDoc evoEndDate = newRule.substring(20);
460                 thisRule = thisRule.substring(startPoint +1);
461             }
462
463             if (thisRule.startsWith("FREQ="))
464             {
465                 frequency = thisRule.substring(5);
466             }
467             else if (thisRule.startsWith("INTERVAL="))
468             {
469                 try{
470                     Long JavaDoc Lint = (new Long JavaDoc(thisRule.substring(9)));
471                     interval = Lint.intValue();
472                 }
473                 catch (Exception JavaDoc e){
474                     System.err.println("INTERVAL Parse Error on " + thisRule + e);
475                 }
476             }
477             else if (thisRule.startsWith("UNTIL="))
478             {
479                 repeatUntil = thisRule;
480                 if (repeatUntil != null) {
481                     repeatUntil = repeatUntil.substring(6);
482                     //UNTIL=20020919
483
//UNTIL=20020919T000000
484
if (repeatUntil.length() == 8) {
485                         repeatUntil = repeatUntil + "235959";
486                     }
487                     try
488                     {
489                         if (repeatUntil.charAt(8) == 'T')
490                         {
491                             repeatUntil = repeatUntil.substring(0,8) + repeatUntil.substring(9);
492                         }
493                         /*
494                          * When frequency is more than one day and UNTIL time = 000000
495                          * time must be adjusted to 235959
496                          */

497                         if ((dateRepeatUnit != Calendar.HOUR) &&
498                             (dateRepeatUnit != Calendar.MINUTE) &&
499                             (dateRepeatUnit != Calendar.SECOND))
500                         {
501                             if (repeatUntil.substring(8).equals("000000")) {
502                                 repeatUntil = repeatUntil.substring(0,8) + "235959";
503                             }
504                         }
505                         repeatUntilDate = dateFormatter.parse(repeatUntil);
506                     }
507                     catch (Exception JavaDoc e)
508                     {
509                         System.err.println("Exception getting RepeatUntilDate" +e);
510                         System.err.println("Exception for date string" +thisRule);
511                         repeatUntilDate = null;
512                     }
513                 }
514             }
515             else if (thisRule.startsWith("BYDAY="))
516             {
517                 // Example: MO,TU,TH,FR
518
repeatByDay = thisRule.substring(6);
519             }
520             else if (thisRule.startsWith("BYMONTH="))
521             {
522                 // Example: 1 or 11 or 12
523
// Comma delim list potentially.
524
repeatByMonth = thisRule.substring(8);
525             }
526             else if (thisRule.startsWith("BYSECOND="))
527             {
528                 // Example: 1 or 11 or 12
529
// Comma delim list potentially.
530
repeatBySecond = thisRule.substring(9);
531             }
532             else if (thisRule.startsWith("BYMINUTE="))
533             {
534                 // Example: 1 or 11 or 12
535
// Comma delim list potentially.
536
repeatByMinute = thisRule.substring(9);
537             }
538             else if (thisRule.startsWith("BYHOUR="))
539             {
540                 // Example: 1 or 11 or 12
541
// Comma delim list potentially.
542
repeatByHour = thisRule.substring(7);
543             }
544             else if (thisRule.startsWith("BYMONTHDAY="))
545             {
546                 // Example: 1,11,12
547
// Comma delim list potentially.
548
repeatByMonthDay = thisRule.substring(11);
549             }
550             else if (thisRule.startsWith("BYYEARDAY="))
551             {
552                 // Example: -50 50th last day of year.
553
// Comma delim list potentially.
554
repeatByYearDay = thisRule.substring(10);
555             }
556             else if (thisRule.startsWith("BYWEEKNO="))
557             {
558                 // Example: 50 50th week of year.
559
// Comma delim list potentially.
560
repeatByWeekNo = thisRule.substring(9);
561             }
562             /*
563                 The BYSETPOS rule part specifies a COMMA character (US-ASCII decimal
564                 44) separated list of values which corresponds to the nth occurrence
565                 within the set of events specified by the rule. Valid values are 1 to
566                 366 or -366 to -1. It MUST only be used in conjunction with another
567                 BYxxx rule part. For example "the last work day of the month" could
568                 be represented as:
569
570                 RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1
571              */

572             else if (thisRule.startsWith("BYSETPOS="))
573             {
574                 //repeatBySetPos = new Integer(thisRule.substring(9)).intValue();
575
// No need to create an object, use static method
576
repeatBySetPos = new Integer JavaDoc(thisRule.substring(9));
577             }
578             else if (thisRule.startsWith("COUNT="))
579             {
580                 // Single integer.
581
try{
582                     //repeatUntilCount = new Integer(thisRule.substring(6)).intValue();
583
// No need to create an object, use static method
584
repeatUntilCount = Integer.parseInt(thisRule.substring(6));
585                     
586                 }
587                 catch(Exception JavaDoc e){
588                     System.err.println("BYSETPOS= Parse Error on " + thisRule + e);
589                 }
590             }
591         }
592         
593         // Now settle the dateRepeatUnit!
594
if (frequency.equalsIgnoreCase("YEARLY"))
595         {
596             dateRepeatUnit = Calendar.YEAR;
597         } else if (frequency.equalsIgnoreCase("MONTHLY"))
598         {
599             dateRepeatUnit = Calendar.MONTH;
600         } else if (frequency.equalsIgnoreCase("WEEKLY"))
601         {
602             dateRepeatUnit = Calendar.DAY_OF_WEEK;
603         } else if (frequency.equalsIgnoreCase("DAILY"))
604         {
605             dateRepeatUnit = Calendar.DATE;
606         } else if (frequency.equalsIgnoreCase("HOURLY"))
607         {
608             dateRepeatUnit = Calendar.HOUR;
609         } else if (frequency.equalsIgnoreCase("MINUTELY"))
610         {
611             dateRepeatUnit = Calendar.MINUTE;
612         } else if (frequency.equalsIgnoreCase("SECONDLY"))
613         {
614             dateRepeatUnit = Calendar.SECOND;
615         }
616         else {
617             System.err.println("RepeatRules =- No Matching rule for frequency " +frequency);
618         }
619     }
620     
621 }
622
Popular Tags