1 package net.stff.ical.beans;
2
3
4 /***
5 * A Bean to store ICal Vevent attributes
6 *
7 *@author buntekuh
8 *@created 1. März 2004
9 */
10 import java.text.DateFormat;
11 import java.text.ParsePosition;
12 import java.text.SimpleDateFormat;
13 import java.util.Calendar;
14 import java.util.Date;
15 import java.util.GregorianCalendar;
16 import java.util.Set;
17
18 public class VEvent implements Comparable{
19 protected static DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
20 protected String description;
21 protected String location;
22 protected String summary;
23 protected String uid;
24 protected String status;
25 protected String timezone;
26 protected String vclass;
27 protected String url;
28 protected Calendar dtstart;
29 protected Calendar dtend;
30 protected Date dtstamp;
31 protected String categories;
32 protected Recurrence recurrence;
33 protected Object obj;
34
35 public final static SimpleDateFormat DATEFORMAT = new SimpleDateFormat("dd/MM/yyyy");
36 public final static SimpleDateFormat TIMEFORMAT = new SimpleDateFormat("HH:mm");
37 public final static String REGEX = "[:,;.///#-]+";
38 public VEvent(){
39
40 dtstamp = new Date();
41 uid= "stff"+(new Date()).getTime();
42 recurrence= new Recurrence();
43 }
44
45 public void setRrule(String s){
46 if (s == null) return;
47 recurrence= new Recurrence(s);
48
49 }
50 public void addExdate(String s){
51 recurrence.addExdate(parseDate(s));
52 }
53
54 public void setExDates(Set dates){
55 recurrence.setExDates(dates);
56 }
57
58 public Set getExDates(){
59 if (recurrence == null) return null;
60 return recurrence.getExDates();
61 }
62
63 public int compareTo(Object o) throws ClassCastException{
64
65 VEvent event= (VEvent)o;
66 return getDtstart().compareTo(event.getDtstart());
67 }
68
69 public void setDescription(String description){
70 this.description = description;
71 }
72
73 public void setLocation(String location){
74 this.location = location;
75 }
76
77 public void setSummary(String summary){
78 this.summary = summary;
79 }
80
81 public void setUid(String uid){
82 this.uid = uid;
83 }
84
85 public void setStatus(String status){
86 this.status = status;
87 }
88
89 public void setVclass(String vclass){
90 this.vclass = vclass;
91 }
92
93 public void setDtstart(Date dtstart){
94 this.dtstart= Calendar.getInstance();
95 this.dtstart.setTime(dtstart);
96 }
97
98 public void setDtstart(String s){
99 this.dtstart= Calendar.getInstance();
100 this.dtstart.setTime(parseDate(s));
101 }
102
103 public void setDtend(Date dtend){
104 if (dtend == null){
105 this.dtend= null;
106 return;
107 }
108 this.dtend= Calendar.getInstance();
109 this.dtend.setTime(dtend);
110 }
111
112 public void setDtend(String s){
113 this.dtend= Calendar.getInstance();
114 this.dtend.setTime(parseDate(s));
115 }
116
117 public void setDtstamp(Date dtstamp){
118 this.dtstamp = dtstamp;
119 }
120
121 public void setDtstamp(String s){
122 this.dtstamp = parseDate(s);
123 }
124
125 public String getDescription(){
126 return description;
127 }
128
129 public String getLocation(){
130 return location;
131 }
132
133 public String getSummary(){
134 return summary;
135 }
136
137 public String getUid(){
138
139
140
141
142 return uid;
143 }
144
145 public String getValidUid(){
146 if (uid == null){
147
148 setUid(new Date().toString() + "@stoffwechsel-dev.net");
149 }
150
151 return uid;
152 }
153
154 public String getStatus(){
155 return status;
156 }
157
158 public String getVclass(){
159 return vclass;
160 }
161
162 public Date getDtstart(){
163 if (dtstart == null) return null;
164 return dtstart.getTime();
165 }
166 public Calendar getDtstartCal(){
167 return dtstart;
168 }
169
170 public String getDtstartString(){
171 if (dtstart == null) return null;
172 return dateToString(dtstart.getTime());
173 }
174
175 public Date getDtend(){
176 if (dtend == null) return null;
177 return dtend.getTime();
178 }
179
180 public String getDtendString(){
181 if (dtend == null) return null;
182 return dateToString(dtend.getTime());
183 }
184
185 public Date getDtstamp(){
186 return dtstamp;
187 }
188
189 public String toString(){
190 StringBuffer buff = new StringBuffer();
191
192 buff.append("BEGIN:VEVENT\r\n");
193
194 buff.append("UID:");
195 buff.append(getUid());
196 buff.append("\r\n");
197
198 if (location != null){
199 buff.append("LOCATION:");
200 buff.append(getLocation());
201 buff.append("\r\n");
202 }
203
204 if (summary != null){
205 buff.append("SUMMARY:");
206 buff.append(getSummary());
207 buff.append("\r\n");
208 }
209
210 if (description != null){
211 buff.append("DESCRIPTION:");
212 buff.append(getDescription());
213 buff.append("\r\n");
214 }
215
216 if (status != null){
217 buff.append("STATUS:");
218 buff.append(getStatus());
219 buff.append("\r\n");
220 }
221
222 if (vclass != null){
223 buff.append("CLASS:");
224 buff.append(getVclass());
225 buff.append("\r\n");
226 }
227
228 if (url != null){
229 buff.append("URL:");
230 buff.append(getUrl());
231 buff.append("\r\n");
232 }
233
234 if (dtstart != null){
235 buff.append("DTSTART:");
236 buff.append(getDtstartString());
237 buff.append("\r\n");
238 }
239
240 if (dtend != null){
241 buff.append("DTEND:");
242 buff.append(getDtendString());
243 buff.append("\r\n");
244 }
245
246 if (dtstamp != null){
247 buff.append("DTSTAMP:");
248 buff.append(getDtstampString());
249 buff.append("\r\n");
250 }
251 if (recurrence.getRule() != null){
252 buff.append("RRULE:");
253 buff.append(recurrence.toString());
254 buff.append("\r\n");
255 buff.append(recurrence.getExDatesAsString());
256 }
257
258 buff.append("END:VEVENT\r\n\r\n");
259
260 return buff.toString();
261 }
262
263 public String getDtstampString(){
264 return dateToString(dtstamp);
265 }
266
267 public static String dateToString(Date d){
268 if (d == null) return "null";
269 Calendar cal = new GregorianCalendar();
270
271 cal.setTime(d);
272 dateFormat.setCalendar(cal);
273
274 return dateFormat.format(d);
275 }
276
277 public static Date parseDate(String s){
278 return parseDate(s, new ParsePosition(0));
279 }
280
281 public static Date parseDate(String s, int i){
282 return parseDate(s, new ParsePosition(i));
283 }
284
285 public static Date parseDate(String s, ParsePosition pos){
286 s = s.trim();
287
288 return dateFormat.parse(s, pos);
289 }
290
291
292 /***
293 * @return Returns the endDate.
294 */
295 public String getEndDate() {
296 if (dtend == null) return "";
297 return DATEFORMAT.format(dtend.getTime());
298
299 }
300
301 /***
302 * @param endDate The endDate to set.
303 */
304 public void setEndDate(String endDate) {
305 try{
306 String[] d= endDate.split(REGEX);
307 if (d.length == 3){
308 if (dtend == null) dtend= Calendar.getInstance();
309 dtend.set(Integer.parseInt(d[2]), Integer.parseInt(d[1])-1, Integer.parseInt(d[0]));
310 }
311 }
312 catch(NumberFormatException e){
313 System.out.println("Variable "+endDate+" must represent Integers instead!");
314 e.printStackTrace();
315 }
316 }
317
318 /***
319 * @return Returns the endTime.
320 */
321 public String getEndTime() {
322 if (dtend == null) return null;
323 return TIMEFORMAT.format(dtend.getTime());
324
325 }
326
327 /***
328 * @param endTime The endTime to set.
329 */
330 public void setEndTime(String endTime) {
331 if (endTime.equals("")){
332 dtend= null;
333 return;
334 }
335
336 if (dtend == null){
337 dtend= Calendar.getInstance();
338 }
339 try{
340 String[] d= endTime.split(REGEX);
341 if (d.length == 2){
342 dtend.set(Calendar.HOUR_OF_DAY, Integer.parseInt(d[0]));
343 dtend.set(Calendar.MINUTE, Integer.parseInt(d[1]));
344 }
345 }
346 catch(NumberFormatException e){
347 System.out.println("Variable "+endTime+" must represent Integers instead!");
348 }
349 }
350
351 /***
352 * @return Returns the startDate.
353 */
354 public String getStartDate() {
355 String s= DATEFORMAT.format(dtstart.getTime());
356 return s;
357
358 }
359
360 /***
361 * @param startDate The startDate as a String formatted to the default Locale.
362 */
363 public void setStartDate(String startDate) {
364 try{
365 String[] d= startDate.split(REGEX);
366 if (d.length == 3){
367 dtstart.set(Integer.parseInt(d[2]), Integer.parseInt(d[1])-1, Integer.parseInt(d[0]));
368 }
369 }
370 catch(NumberFormatException e){
371 System.out.println("Variable "+startDate+" must represent Integers instead!");
372 }
373 }
374
375 /***
376 * @return Returns the startTime.
377 */
378 public String getStartTime() {
379 return TIMEFORMAT.format(dtstart.getTime());
380 }
381
382 /***
383 * @param startTime The startTime to set.
384 */
385 public void setStartTime(String startTime) {
386 try{
387 String[] d= startTime.split(REGEX);
388 if (d.length == 2){
389 dtstart.set(Calendar.HOUR_OF_DAY, Integer.parseInt(d[0]));
390 dtstart.set(Calendar.MINUTE, Integer.parseInt(d[1]));
391 }
392 }
393 catch(NumberFormatException e){
394 System.out.println("Variable "+startTime+" must represent Integers instead!");
395 }
396 }
397
398 /***
399 * @return Returns the endHour.
400 */
401 public int getEndHour() {
402 if (dtend == null) return -1;
403 return dtend.get(Calendar.HOUR_OF_DAY);
404 }
405 /***
406 * @param endHour The endHour to set.
407 */
408 public void setEndHour(int endHour) {
409 if (dtend == null){
410 dtend= Calendar.getInstance();
411 }
412 dtend.set(Calendar.HOUR_OF_DAY, endHour);
413 }
414 /***
415 * @return Returns the endMinute.
416 */
417 public int getEndMinute() {
418 if (dtend == null) return -1;
419 return dtend.get(Calendar.MINUTE);
420 }
421 /***
422 * @param endMinute The endMinute to set.
423 */
424 public void setEndMinute(int endMinute) {
425 if (dtend == null){
426 dtend= Calendar.getInstance();
427 }
428 dtend.set(Calendar.MINUTE, endMinute);
429 }
430 /***
431 * @return Returns the startHour.
432 */
433 public int getStartHour() {
434 if (dtstart == null) return -1;
435 return dtstart.get(Calendar.HOUR_OF_DAY);
436 }
437 /***
438 * @param startHour The startHour to set.
439 */
440 public void setStartHour(int startHour) {
441 if (dtstart == null){
442 dtstart= Calendar.getInstance();
443 }
444 dtstart.set(Calendar.HOUR_OF_DAY, startHour);
445 }
446 /***
447 * @return Returns the startMinute.
448 */
449 public int getStartMinute() {
450 if (dtstart == null) return -1;
451 return dtstart.get(Calendar.MINUTE);
452 }
453 /***
454 * @param startMinute The startMinute to set.
455 */
456 public void setStartMinute(int startMinute) {
457 if (dtstart == null){
458 dtstart= Calendar.getInstance();
459 }
460 dtstart.set(Calendar.MINUTE, startMinute);
461 }
462 /***
463 * @return Returns the timezone.
464 */
465 public String getTimezone(){
466 return timezone;
467 }
468
469 /***
470 * @param timezone The timezone to set.
471 */
472 public void setTimezone(String timezone){
473 this.timezone = timezone;
474 }
475
476 /***
477 * @return Returns the url.
478 */
479 public String getUrl(){
480 return url;
481 }
482
483 /***
484 * @param url The url to set.
485 */
486 public void setUrl(String url){
487 this.url = url;
488 }
489
490 /***
491 * @return Returns the obj.
492 */
493 public Object getObj(){
494 return obj;
495 }
496
497 /***
498 * @param obj The obj to set.
499 */
500 public void setObj(Object obj){
501 this.obj = obj;
502 }
503 /***
504 * @return Returns the categories.
505 */
506 public String getCategories() {
507 return categories;
508 }
509 /***
510 * @param categories The categories to set.
511 */
512 public void setCategories(String categories) {
513 this.categories = categories;
514 }
515
516 public boolean equals(Object o){
517 try{
518 VEvent event= (VEvent)o;
519 if (event.getUid().equals(uid)) return true;
520 return false;
521 }
522 catch(Exception e){
523 return false;
524 }
525 }
526 public int hashCode(){
527 return uid.hashCode();
528 }
529 /***
530 * @return Returns the recurrence.
531 */
532 public Recurrence getRecurrence() {
533 return recurrence;
534 }
535 /***
536 * @param recurrence The recurrence to set.
537 */
538 public void setRecurrence(Recurrence recurrence) {
539 this.recurrence = recurrence;
540 }
541 public void recompute(){
542 if (recurrence == null) return;
543 recurrence.recompute(this);
544 }
545 public String getRrule(){
546 if (recurrence == null) return null;
547 return recurrence.getRule();
548 }
549 /***
550 * @return Returns the recurs.
551 */
552 public boolean isRecurs() {
553 return (recurrence.getRule() != null);
554 }
555 public boolean isAllday(){
556 return (dtend == null);
557 }
558 /***
559 * @param recurs The recurs to set.
560 */
561 public void setRecurs(boolean recurs) {
562 if (!recurs) recurrence= new Recurrence();
563 }
564 }