1 32 33 package org.jical; 34 35 41 42 import java.text.SimpleDateFormat ; 43 import java.util.ArrayList ; 44 import java.util.Calendar ; 45 import java.util.Collection ; 46 import java.util.Date ; 47 import java.util.GregorianCalendar ; 48 import java.util.Iterator ; 49 import java.util.Set ; 50 import java.util.TimeZone ; 51 import java.util.TreeSet ; 52 import java.util.logging.Logger ; 53 54 public class ICalendar { 55 56 private String calScale; 57 private String prodId; 58 private String iCalVersion; 59 private TimeZone defaultTimeZone; 60 public Collection icaltimeZoneCollection; 61 62 public Collection icalEventCollection; 64 65 private static SimpleDateFormat hoursMinutes = new SimpleDateFormat ("HHmm"); 66 private static SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMdd'T'HHmmss'Z'"); 67 private static SimpleDateFormat dateFormatter = new SimpleDateFormat ("yyyyMMddHHmmss"); 68 private static SimpleDateFormat dateOnlyFormat = new SimpleDateFormat ("yyyyMMdd"); 69 private static SimpleDateFormat dayOfWeek = new SimpleDateFormat ("EEEEEEEE"); 70 private static SimpleDateFormat monthOfYear = new SimpleDateFormat ("MMMMMMMMMMMM"); 71 private static SimpleDateFormat weekNumber = new SimpleDateFormat ("ww"); 72 private static SimpleDateFormat dayNumber = new SimpleDateFormat ("d"); 73 private static SimpleDateFormat year = new SimpleDateFormat ("yyyy"); 74 private static Date DEFAULTSTARTDATE = new Date (1); 75 76 Calendar repeatDateStart = new GregorianCalendar (); 77 Calendar repeatDateEnd = new GregorianCalendar (); 78 private Logger logger = Logger.getLogger(this.getClass().getName()); 79 80 84 public Set sortedExpandedEvents; 85 86 87 private String organizer; 88 89 90 private String FBUrl; 91 92 93 private String organizerEmail; 94 95 96 public ICalendar() { 97 icaltimeZoneCollection = new ArrayList (); 98 icalEventCollection = new ArrayList (); 99 defaultTimeZone = TimeZone.getDefault(); 100 sortedExpandedEvents = new TreeSet ( new ICalendarVEvent.StartDateUIDComparator() ); 101 } 102 103 106 public String getCalScale () 107 { 108 return calScale; 109 } 110 111 114 public void setCalScale (String calScale) 115 { 116 this.calScale = calScale; 117 } 118 119 122 public String getProdId () 123 { 124 return prodId; 125 } 126 127 130 public void setProdId (String prodId) 131 { 132 this.prodId = prodId; 133 } 134 135 138 public String getVersion() 139 { 140 return iCalVersion; 141 } 142 143 146 public void setVersion (String iCalVersion) 147 { 148 this.iCalVersion = iCalVersion; 149 } 150 151 154 public String getOrganizer() { 155 return this.organizer; 156 } 157 158 161 public void setOrganizer(String organizer) { 162 this.organizer = organizer; 163 } 164 165 168 public String getFBUrl() { 169 return this.FBUrl; 170 } 171 172 175 public void setFBUrl(String FBUrl) { 176 this.FBUrl = FBUrl; 177 } 178 179 182 public String getOrganizerEmail() { 183 return this.organizerEmail; 184 } 185 186 189 public void setOrganizerEmail(String organizerEmail) { 190 this.organizerEmail = organizerEmail; 191 } 192 197 public Date getDateFrom(String dateRangeOrDaysForward) throws Exception 198 { 199 try 200 { 201 return (Date )dateFormatter.parse(dateRangeOrDaysForward.substring(0,14)); 202 } 203 catch (Exception e) 204 { 205 throw e; 206 } 207 } 208 public Date getDateTo(String dateRangeOrDaysForward) throws Exception 209 { 210 try 212 { 213 return (Date )dateFormatter.parse(dateRangeOrDaysForward.substring(15)); 214 } 215 catch (Exception e) 216 { 217 throw e; 218 } 219 } 220 223 public long getDaysForwardNumeric(String dateRangeOrDaysForward) throws Exception 224 { 225 try 226 { 227 return java.lang.Integer.parseInt(dateRangeOrDaysForward); 228 } 229 catch (Exception e) 230 { 231 throw e; 232 } 233 } 234 237 public Date getDateToFromDaysForward(long daysForward) 238 { 239 Date dateTo = new Date (); 240 long rollMicroSecs = 86400000 * daysForward; 241 dateTo.setTime(dateTo.getTime() + (rollMicroSecs)); 242 return dateTo; 243 } 244 245 public void getIcalExpandedEvents(Date dateFrom, Date dateTo, String timeRange) 246 { 247 250 251 int timeFrom = 0; int timeTo = 2400; if (timeRange != null) 254 { 255 timeFrom=Integer.parseInt(timeRange.substring(0,4)); 256 timeTo=Integer.parseInt(timeRange.substring(5)); 257 } 258 259 formatter.setTimeZone(TimeZone.getTimeZone("GMT")); 260 261 for(Iterator i=icalEventCollection.iterator(); i.hasNext(); ) 262 { 263 ICalendarVEvent icalEvent = (ICalendarVEvent)i.next(); 264 Date dateStart = icalEvent.getDateStart(); 265 Date dateEnd = icalEvent.getDateEnd(); 266 267 if ( (dateStart.after(dateFrom) 268 && dateStart.before(dateTo)) 269 || dateStart.equals(dateFrom) 270 || dateStart.equals(dateTo) ) 271 { 272 logger.fine("Create This date: "+dateStart); 273 createExpandedEvent(dateStart, dateEnd, icalEvent, dateFrom, dateTo, timeFrom, timeTo); 275 } 276 277 if (icalEvent.getRRule() != null) 278 { 279 logger.fine("Repeat Rule is not null"); 280 285 313 314 Calendar newCal = new GregorianCalendar (); 316 newCal.setTime(icalEvent.getDateStart()); 317 setRepeatDateStart(newCal); 318 newCal = new GregorianCalendar (); 319 newCal.setTime(icalEvent.getDateEnd()); 320 setRepeatDateEnd(newCal); 321 322 getNextRepeatDate(icalEvent); 323 324 logger.fine("Repeat Date next date: "+getRepeatDateStart().getTime() 325 + " Interval "+icalEvent.getRepeatRules().interval 326 + " Repeat Unit "+icalEvent.getRepeatRules().dateRepeatUnit); 327 328 Date repeatUntilDate = icalEvent.getRepeatRules().repeatUntilDate; 329 if (repeatUntilDate == null) 330 repeatUntilDate = dateTo; 331 if (repeatUntilDate.after(dateTo)) 332 repeatUntilDate = dateTo; 333 334 int createCtr = 0; 337 338 while (getRepeatDateStart() != null 339 && !getRepeatDateStart().getTime().after(repeatUntilDate)) 340 { 341 processRepeatEvent(icalEvent.getRepeatRules(), icalEvent.getRepeatRules().dateRepeatUnit, icalEvent, dateFrom, dateTo, timeFrom, timeTo); 342 343 getNextRepeatDate(icalEvent); 345 346 logger.fine("Repeat Date next date: "+repeatDateStart.getTime() 347 + " Interval "+icalEvent.getRepeatRules().interval 348 + " Repeat Unit "+icalEvent.getRepeatRules().dateRepeatUnit); 349 350 } 353 } 354 } 355 } 356 357 public void getNextRepeatDate(ICalendarVEvent icalEvent) 358 { 359 logger.fine("dateStart: "+getRepeatDateStart().getTime()); 360 Calendar newDate = getRepeatDateStart(); 361 Calendar newDateEnd = getRepeatDateEnd(); 362 if (icalEvent.getRepeatRules().dateRepeatUnit==Calendar.DAY_OF_WEEK) 363 { 364 logger.fine("DAY_OF_WEEK: "+getRepeatDateStart().getTime() + " date: "+ newDate.getTime()); 365 newDate.add(Calendar.DATE, 7 * icalEvent.getRepeatRules().interval); 366 logger.fine("DAY_OF_WEEK2 : "+newDate.getTime()); 367 setRepeatDateStart(newDate); 368 newDate = getRepeatDateEnd(); 369 newDate.add(Calendar.DATE, 7 * icalEvent.getRepeatRules().interval); 370 setRepeatDateEnd(newDate); 371 } 372 else 373 { 374 logger.fine("about to step to next repeat event unit:"+icalEvent.getRepeatRules().dateRepeatUnit 376 + " Interval: " +icalEvent.getRepeatRules().interval); 377 378 newDate.add(icalEvent.getRepeatRules().dateRepeatUnit, icalEvent.getRepeatRules().interval); 379 setRepeatDateStart(newDate); 380 381 newDateEnd.add(icalEvent.getRepeatRules().dateRepeatUnit, icalEvent.getRepeatRules().interval); 382 setRepeatDateEnd(newDateEnd); 383 logger.fine("After date add - DateStart: "+getRepeatDateStart().getTime()); 384 } 385 } 386 387 public void processRepeatEvent(RepeatRules repeatRules, int dateConstraint, 388 ICalendarVEvent icalEvent, Date dateFrom, Date dateTo, int timeFrom, int timeTo){ 389 393 if (dateConstraint == Calendar.YEAR){ 394 if (repeatRules.repeatByYearDay !=null) 395 repeatByYearDay(repeatDateStart, repeatDateEnd, repeatRules, dateConstraint,icalEvent, dateFrom, dateTo, timeFrom, timeTo); 396 else if (repeatRules.repeatByMonth !=null) 397 repeatByMonth(repeatDateStart, repeatDateEnd, repeatRules, dateConstraint,icalEvent, dateFrom, dateTo, timeFrom, timeTo); 398 else { 401 logger.fine("Year: Create: "+getRepeatDateStart().getTime()); 403 createExpandedEvent(repeatDateStart.getTime(), repeatDateEnd.getTime(), icalEvent, dateFrom, dateTo, timeFrom, timeTo); 404 } 405 } 406 else if (dateConstraint == Calendar.MONTH) 407 { 408 logger.fine("Repeat by Month, repeat by monthday:"+repeatRules.repeatByMonthDay ); 409 logger.fine("Repeat by Month, repeat by day:"+repeatRules.repeatByDay ); 410 if (repeatRules.repeatByMonthDay != null) 411 { 412 repeatByMonthDay(repeatDateStart, repeatDateEnd, repeatRules, dateConstraint, icalEvent, dateFrom, dateTo, timeFrom, timeTo); 413 } 414 else if (repeatRules.repeatByDay != null) 415 { 416 repeatByDay(repeatRules, dateConstraint,icalEvent, dateFrom, dateTo, timeFrom, timeTo); 417 } 418 else { 419 logger.fine("MONTH: Create: "+getRepeatDateStart().getTime()); 421 createExpandedEvent(repeatDateStart.getTime(), repeatDateEnd.getTime(), icalEvent, dateFrom, dateTo, timeFrom, timeTo); 422 } 423 } 424 else if (dateConstraint == Calendar.DAY_OF_WEEK) 426 { 427 if (repeatRules.repeatByDay != null) 428 { 429 logger.fine("RepeatByDay:"+repeatRules.repeatByDay); 430 repeatByWeekDay(repeatRules, dateConstraint, icalEvent, dateFrom, dateTo, timeFrom, timeTo); 431 logger.fine("End RepeatByDay"); 432 } 433 else 434 { 435 logger.fine("Create Weekly Event"+getRepeatDateStart().getTime()); 436 createExpandedEvent(getRepeatDateStart().getTime(), getRepeatDateEnd().getTime(), icalEvent, dateFrom, dateTo, timeFrom, timeTo); 437 } 438 } 439 else if (dateConstraint == Calendar.DATE) 440 { 441 logger.fine("DATE: Create: "+getRepeatDateStart().getTime()); 443 createExpandedEvent(repeatDateStart.getTime(), repeatDateEnd.getTime(), icalEvent, dateFrom, dateTo, timeFrom, timeTo); 444 } 445 else if (dateConstraint == Calendar.HOUR) 446 { 447 logger.fine("HOUR: Create: "+getRepeatDateStart().getTime()); 448 createExpandedEvent(repeatDateStart.getTime(), repeatDateEnd.getTime(), icalEvent, dateFrom, dateTo, timeFrom, timeTo); 449 } 450 else if (dateConstraint == Calendar.MINUTE) 451 { 452 logger.fine("MINUTE: Create: "+getRepeatDateStart().getTime()); 453 createExpandedEvent(repeatDateStart.getTime(), repeatDateEnd.getTime(), icalEvent, dateFrom, dateTo, timeFrom, timeTo); 454 } 455 else if (dateConstraint == Calendar.SECOND) 456 { 457 logger.fine("SECOND: Create: "+getRepeatDateStart().getTime()); 458 createExpandedEvent(repeatDateStart.getTime(), repeatDateEnd.getTime(), icalEvent, dateFrom, dateTo, timeFrom, timeTo); 459 } 460 else { 461 logger.severe("NO MATCHING INTERVAL"); 462 } 463 } 464 public void repeatByYearDay(Calendar repeatDateStart, Calendar repeatDateEnd, RepeatRules repeatRules, int dateRepeatUnit, 465 ICalendarVEvent icalEvent, Date dateFrom, Date dateTo, int timeFrom, int timeTo) 466 { 467 473 java.util.StringTokenizer st = new java.util.StringTokenizer (repeatRules.repeatByYearDay,","); 474 while (st.hasMoreTokens()) 475 { 476 String rule = st.nextToken(); 477 int firstLast = 0; 478 int dayOfYear = 0; 479 if (rule.startsWith("-")) 480 { 481 firstLast = 1; 483 } 484 try { 485 dayOfYear = Integer.parseInt(rule); 486 } 487 catch (Exception e){ 488 logger.severe("Error parsing integer value from repeatByYearDay: " +rule); 489 return; 490 } 491 Calendar getDate = new GregorianCalendar (); 493 getDate.setTime(repeatDateStart.getTime()); 494 if (firstLast == 0) 495 { 496 getDate.set(Calendar.DAY_OF_MONTH,1); 498 getDate.set(Calendar.MONTH,1); 499 getDate.add(Calendar.DATE,dayOfYear - 1); 500 } else 501 { 502 getDate.set(Calendar.MONTH,Calendar.DECEMBER); 503 getDate.set(Calendar.DAY_OF_MONTH,31); 504 getDate.add(Calendar.DATE, -1 * dayOfYear); 505 } 506 repeatDateStart.set(Calendar.DAY_OF_MONTH, 1); 508 repeatDateStart.set(Calendar.MONTH, getDate.get(Calendar.MONTH)); 509 repeatDateStart.set(Calendar.DAY_OF_MONTH, getDate.get(Calendar.DAY_OF_MONTH)); 510 repeatDateEnd.set(Calendar.DAY_OF_MONTH, 1); 511 repeatDateEnd.set(Calendar.MONTH, getDate.get(Calendar.MONTH)); 512 repeatDateEnd.set(Calendar.DAY_OF_MONTH, getDate.get(Calendar.DAY_OF_MONTH)); 513 514 processRepeatEvent(repeatRules, Calendar.DATE, icalEvent, dateFrom, dateTo, timeFrom, timeTo); 517 } 518 } 519 520 public void repeatByMonthDay(Calendar repeatDateStart, Calendar repeatDateEnd, RepeatRules repeatRules, int dateRepeatUnit, 521 ICalendarVEvent icalEvent, Date dateFrom, Date dateTo, int timeFrom, int timeTo) 522 { 523 528 java.util.StringTokenizer st = new java.util.StringTokenizer (repeatRules.repeatByMonthDay,","); 529 while (st.hasMoreTokens()) 530 { 531 String rule = st.nextToken(); 532 int firstLast = 0; 533 int dayOfMonth = 0; 534 535 logger.fine("rule:"+rule); 536 537 if (rule.startsWith("-")) 538 { 539 firstLast = 1; 541 } 542 try { 543 dayOfMonth = Integer.parseInt(rule); 544 } 545 catch (Exception e){ 546 logger.severe("Error parsing integer value from repeatByYearDay: " +rule); 547 return; 548 } 549 Calendar getDate = new GregorianCalendar (); 551 getDate.setTime(repeatDateStart.getTime()); 552 if (firstLast == 0) 553 { 554 getDate.set(Calendar.DAY_OF_MONTH,1); 556 getDate.add(Calendar.DATE,dayOfMonth-1); 557 } else 558 { 559 getDate.set(Calendar.DAY_OF_MONTH,1); 561 getDate.add(Calendar.MONTH,1); 562 getDate.add(Calendar.DATE,-1); 563 getDate.set(Calendar.DATE,1+dayOfMonth); 565 } 566 repeatDateStart.set(Calendar.DAY_OF_MONTH, 1); 568 repeatDateStart.set(Calendar.MONTH, getDate.get(Calendar.MONTH)); 569 repeatDateStart.set(Calendar.DAY_OF_MONTH, getDate.get(Calendar.DAY_OF_MONTH)); 570 repeatDateEnd.set(Calendar.DAY_OF_MONTH, 1); 571 repeatDateEnd.set(Calendar.MONTH, getDate.get(Calendar.MONTH)); 572 repeatDateEnd.set(Calendar.DAY_OF_MONTH, getDate.get(Calendar.DAY_OF_MONTH)); 573 574 processRepeatEvent(repeatRules, Calendar.DATE, icalEvent, dateFrom, dateTo, timeFrom, timeTo); 577 } 578 } 579 580 public void repeatByMonth(Calendar repeatDateStart, Calendar repeatDateEnd, RepeatRules repeatRules, int dateRepeatUnit, 581 ICalendarVEvent icalEvent, Date dateFrom, Date dateTo, int timeFrom, int timeTo) 582 { 583 589 java.util.StringTokenizer st = new java.util.StringTokenizer (repeatRules.repeatByMonth,","); 590 while (st.hasMoreTokens()) 591 { 592 int monthOfYear = 0; 593 String rule = st.nextToken(); 594 try 595 { 596 monthOfYear = Integer.parseInt(rule); 597 } 598 catch (Exception e) 599 { 600 logger.severe("Error parsing integer value from repeatByYearDay: " +rule); 601 return; 602 } 603 604 606 repeatDateStart.set(Calendar.MONTH, monthOfYear - 1); 608 if (repeatDateStart.get(Calendar.MONTH) != monthOfYear - 1) 609 { 610 611 logger.severe("Error setting MonthOfYear for date: " +repeatDateStart.getTime() 612 +" to Month "+ monthOfYear 613 +" From DateStartMonth"+repeatDateStart.get(Calendar.MONTH) 614 +" Event: "+ icalEvent.getSummary()); 615 return; 616 } 617 618 repeatDateEnd.set(Calendar.MONTH, monthOfYear - 1); 620 if (repeatDateEnd.get(Calendar.MONTH) != monthOfYear - 1) 621 { 622 logger.severe("Error setting MonthOfYear for date: " +repeatDateEnd.getTime() 623 +" to Month "+ monthOfYear 624 +" From DateEndMonth"+repeatDateEnd.get(Calendar.MONTH) 625 +" Event: "+ icalEvent.getSummary()); 626 return; 627 } 628 processRepeatEvent(repeatRules, Calendar.MONTH, icalEvent, dateFrom, dateTo, timeFrom, timeTo); 631 } 632 } 633 636 public void repeatByWeekDay(RepeatRules repeatRules, int dateRepeatUnit, 637 ICalendarVEvent icalEvent, Date dateFrom, Date dateTo, int timeFrom, int timeTo) 638 { 639 640 java.util.StringTokenizer st = new java.util.StringTokenizer (repeatRules.repeatByDay,","); 644 while (st.hasMoreTokens()) 645 { 646 647 String rule = st.nextToken(); 648 int dayOfWeek = ("SUMOTUWETHFRSA".indexOf(rule.substring(0,2)) / 2) + 1; 649 if (dayOfWeek == -1) 650 return; 651 652 int iDayCtr = 0; 654 Calendar workDateFrom = new GregorianCalendar (); 655 workDateFrom.setTime(repeatDateStart.getTime()); 656 Calendar workDateTo = new GregorianCalendar (); 657 workDateTo.setTime(repeatDateEnd.getTime()); 658 logger.fine("Going to loop 7 days from "+workDateFrom.getTime() +" as long as "+workDateTo.getTime()+ " is before "+dateTo 659 +" and this" +!workDateTo.equals(dateTo)+ " is true"); 660 while(iDayCtr <= 7 && ( workDateTo.getTime().before(dateTo) && !workDateTo.equals(dateTo))) 662 { 663 iDayCtr++; 664 workDateFrom.add(Calendar.DATE,1); 665 workDateTo.add(Calendar.DATE,1); 666 logger.fine("Up to "+workDateFrom.getTime()); 667 if (workDateFrom.get(Calendar.DAY_OF_WEEK) == dayOfWeek) 668 { 669 logger.fine("Create expanded event "+workDateFrom.getTime()); 670 createExpandedEvent(workDateFrom.getTime(), workDateTo.getTime(), icalEvent, dateFrom, dateTo, timeFrom, timeTo); 671 break; 673 } 674 } 675 676 } 677 678 } 679 public void repeatByDay(RepeatRules repeatRules, int dateRepeatUnit, 680 ICalendarVEvent icalEvent, Date dateFrom, Date dateTo, int timeFrom, int timeTo) 681 { 682 688 java.util.StringTokenizer st = new java.util.StringTokenizer (repeatRules.repeatByDay,","); 689 while (st.hasMoreTokens()) 690 { 691 String rule = st.nextToken(); 692 693 logger.fine("Next Rule: "+rule); 694 695 int firstLast = 0; 696 int sequenceInMonth = 9999; 697 int dayOfWeek = 0; 698 699 Calendar workDateFrom = new GregorianCalendar (); 700 workDateFrom.setTime(repeatDateStart.getTime()); 701 Calendar workDateTo = new GregorianCalendar (); 702 workDateTo.setTime(repeatDateEnd.getTime()); 703 704 if (rule.startsWith("-")) 705 { 706 firstLast = 1; 708 try{ 709 sequenceInMonth = Integer.parseInt(rule.substring(1,1)); 710 dayOfWeek = ("SUMOTUWETHFRSA".indexOf(rule.substring(2,4)) / 2) + 1; 714 } 715 catch(Exception e){ 716 logger.severe("Error Parsing Day RRULE, rule: " +rule); 717 e.printStackTrace(System.err); 718 return; 719 } 720 } 721 else if ("0123456789".indexOf(rule.substring(0,1)) != -1) 723 { 724 try{ 725 sequenceInMonth = Integer.parseInt(rule.substring(0)); 727 dayOfWeek = ("SUMOTUWETHFRSA".indexOf(rule.substring(1,2)) / 2) + 1; 728 } 729 catch(Exception e){ 730 logger.severe("Error Parsing Day RRULE, rule: " +rule); 731 e.printStackTrace(System.err); 732 return; 733 } 734 } 735 else 736 { 737 dayOfWeek = ("SUMOTUWETHFRSA".indexOf(rule.substring(0,2)) / 2) + 1;; 739 } 740 741 logger.fine("RepeatRulesBySetPos"+repeatRules.repeatBySetPos); 742 if (repeatRules.repeatBySetPos != null){ 743 sequenceInMonth = repeatRules.getRepeatBySetPos().intValue(); 744 } 745 746 752 753 if (sequenceInMonth == 9999){ 755 if (dateRepeatUnit == Calendar.MONTH) { 756 int thisMonth = workDateFrom.get(Calendar.MONTH); 759 while (workDateFrom.get(Calendar.MONTH) == thisMonth) 760 { 761 if (workDateFrom.get(Calendar.DAY_OF_WEEK) == dayOfWeek) 762 { 763 setRepeatDateStart(workDateFrom); 764 setRepeatDateEnd(workDateTo); 765 processRepeatEvent(repeatRules, Calendar.DATE, icalEvent, dateFrom, dateTo, timeFrom, timeTo); 766 } 767 workDateFrom.add(Calendar.DATE, 1); 768 workDateTo.add(Calendar.DATE, 1); 769 setRepeatDateStart(workDateFrom); 770 setRepeatDateEnd(workDateTo); 771 } 772 } 773 if (dateRepeatUnit == Calendar.DAY_OF_WEEK) { 774 int weekCount = 1; 777 while (weekCount < 8) 778 { 779 if (workDateFrom.get(Calendar.DAY_OF_WEEK) == dayOfWeek) 780 processRepeatEvent(repeatRules, Calendar.MONTH, icalEvent, dateFrom, dateTo, timeFrom, timeTo); 781 workDateFrom.add(Calendar.DATE,1); 782 workDateTo.add(Calendar.DATE,1); 783 setRepeatDateStart(workDateFrom); 784 setRepeatDateEnd(workDateTo); 785 weekCount++; 786 } 787 } 788 } 789 else 790 { 791 logger.fine("Attempting nth DD in Month from "+ workDateFrom.getTime()); 792 Calendar getDate = new GregorianCalendar (); 794 getDate.setTime(workDateFrom.getTime()); 795 int thisMonth = workDateFrom.get(Calendar.MONTH); 796 int dayIncrement = 0; 797 int occuranceCtr = 0; 798 logger.fine("sequenceInMonth: "+sequenceInMonth); 799 if (sequenceInMonth < 0) 800 { 801 getDate.set(Calendar.DAY_OF_MONTH,1); 803 getDate.add(Calendar.MONTH,1); 805 getDate.add(Calendar.DATE,-1); 807 logger.fine("Start from last day of month: "+getDate.getTime()); 808 dayIncrement = -1; 810 811 } 812 else { 813 getDate.set(Calendar.DAY_OF_MONTH,1); 814 dayIncrement = +1; 815 } 816 817 while (getDate.get(Calendar.MONTH) == thisMonth) 818 { 819 if (getDate.get(Calendar.DAY_OF_WEEK) == dayOfWeek) 820 { 821 logger.fine("Found that day of week:"+getDate.getTime()); 822 823 occuranceCtr++; 824 if (occuranceCtr == sequenceInMonth * dayIncrement) 825 { 826 workDateFrom.set(Calendar.DAY_OF_MONTH, getDate.get(Calendar.DAY_OF_MONTH)); 827 workDateTo.set(Calendar.DAY_OF_MONTH, getDate.get(Calendar.DAY_OF_MONTH)); 828 setRepeatDateStart(workDateFrom); 829 setRepeatDateEnd(workDateTo); 830 logger.fine("Found one!"+workDateFrom); 831 processRepeatEvent(repeatRules, Calendar.DATE, icalEvent, dateFrom, dateTo, timeFrom, timeTo); 832 return; 833 } 834 } 835 836 getDate.add(Calendar.DATE,dayIncrement); 837 logger.fine("Next date is : "+getDate.getTime()); 838 } 839 } 840 841 } } 843 public void createExpandedEvent(Date dateStartIn, Date dateEndIn, ICalendarVEvent iCalEventIn, Date dateFrom, Date dateTo, int timeFrom, int timeTo) 844 { 845 848 849 logger.fine("Create Expanded Event: "+dateStartIn); 850 851 if (dateEndIn == null) { 853 dateEndIn = iCalEventIn.getDateEnd(); 854 } 855 856 if (dateEndIn.before(dateFrom) || dateStartIn.after(dateTo)) 858 return; 859 860 861 862 int testTimeStart = Integer.parseInt(hoursMinutes.format(dateStartIn)); 863 int testTimeEnd = Integer.parseInt(hoursMinutes.format(dateEndIn)); 864 865 876 877 if ( testTimeStart < timeFrom 879 && testTimeEnd > timeTo){} 880 else if (testTimeStart >= timeFrom 882 && testTimeEnd <= timeTo){} 883 else if (testTimeEnd > timeTo 885 && testTimeStart <= timeTo) {} 886 else if (testTimeEnd >= timeFrom 888 && testTimeStart < timeFrom) {} 889 else if (testTimeStart == 0 891 && testTimeEnd == 0) {} 892 else 893 return; 894 895 if (iCalEventIn.isExDatesExist()) 896 { 897 boolean foundExDate = false; 898 899 for(Iterator itr=iCalEventIn.exDateCollection.iterator(); itr.hasNext(); ) 900 { 901 String exDate = (String )itr.next(); 902 if (exDate.startsWith(dateOnlyFormat.format(dateStartIn))) 904 return; 905 } 906 } 907 908 if (iCalEventIn.getRepeatRules().repeatUntilCount <= iCalEventIn.getRepeatCount()) 909 { 910 return; 911 } 912 913 iCalEventIn.setRepeatCount(iCalEventIn.getRepeatCount()+1); 914 try { 915 ICalendarVEvent iCalEventNew = (ICalendarVEvent) iCalEventIn.clone(); 916 iCalEventNew.setDateStart(dateStartIn); 917 iCalEventNew.setDateEnd(dateEndIn); 918 919 if ((iCalEventNew.getOrganizer() == null || iCalEventNew.getOrganizer().length() == 0) && 920 (iCalEventNew.getOrganizer() == null || iCalEventNew.getOrganizer().length() == 0)) 921 { 922 iCalEventNew.setOrganizer(getOrganizer()); 924 } 925 if (sortedExpandedEvents.contains(iCalEventNew)) 927 { 928 if (iCalEventNew.isRecurrenceId()) 929 { 930 sortedExpandedEvents.remove(iCalEventNew); 932 } 933 } 934 sortedExpandedEvents.add( iCalEventNew ); 935 936 logger.fine("Create This date: "+dateStartIn); 937 logger.fine("Create UID: "+iCalEventNew.getUid()); 938 logger.fine("RepeatSummary: "+iCalEventNew.getSummary()); 939 } 940 catch ( Exception ex ) { 941 logger.severe(ex.toString()); 942 ex.printStackTrace(); 943 } 944 945 946 947 } 948 949 public String getFBString(String dateRangeOrDaysForward) throws Exception 950 { 951 Date dateFrom = null; 952 Date dateTo = null; 953 try 954 { 955 long daysForward = getDaysForwardNumeric(dateRangeOrDaysForward); 956 try 957 { 958 return getFBString(daysForward); 959 } 960 catch (Exception e) 961 { 962 logger.severe("Cannot create FBURL due to error: " +e); 963 return ""; 964 } 965 } 966 catch (Exception e) 967 { 968 try 969 { 970 dateFrom = getDateFrom(dateRangeOrDaysForward); 971 dateTo = getDateTo(dateRangeOrDaysForward); 972 973 } 974 catch (Exception ee) 975 { 976 logger.severe("1.Unable to read your input dates: " 977 + dateRangeOrDaysForward 978 + "They must be of the form ccYYmmDDhhMMss-ccYYmmDDhhMMss" 979 + ee); 980 throw ee; 981 } 982 } 983 return getFBString(dateFrom , dateTo); 984 } 985 986 public String getFBString(long daysForward) 987 { 988 Date dateFrom = new Date (); 989 Date dateTo = getDateToFromDaysForward(daysForward); 990 994 try 995 { 996 dateFrom = (Date )dateOnlyFormat.parse(dateOnlyFormat.format(dateFrom)); 997 dateTo = (Date )dateFormatter.parse(dateOnlyFormat.format(dateTo) + "235959"); 998 } 999 catch (Exception e) 1000 { 1001 logger.severe(e.toString()); 1002 } 1003 return getFBString(dateFrom, dateTo); 1004 } 1005 1006 public String getFBString(Date dateFrom, Date dateTo) 1007 { 1008 1011 1012 1030 if (this.getFBUrl() == null 1031 || this.getOrganizerEmail() == null 1032 || this.getOrganizer() == null) { 1033 logger.severe("Cannot create FBURL unless FBURL, OrganizerEmail are Organizer provided to ICalendar"); 1034 return ""; 1035 } 1036 1037 StringBuffer FBString = new StringBuffer ("BEGIN:VCALENDAR\nBEGIN:VFREEBUSY\n"); 1038 FBString.append("ORGANIZER:").append(this.getOrganizerEmail()).append("\n"); 1039 FBString.append("DTSTART:").append(formatter.format(dateFrom)).append("\n"); 1040 FBString.append("DTEND:").append(formatter.format(dateTo)).append("\n"); 1041 1042 1045 Date trueStartDate = new Date (0); 1046 1047 1052 1053 1054 1055 getIcalExpandedEvents(trueStartDate, dateTo, null); 1056 1057 Iterator eeIterator = sortedExpandedEvents.iterator(); 1058 while (eeIterator.hasNext()) 1059 { 1060 ICalendarVEvent icalEvent = (ICalendarVEvent) eeIterator.next(); 1061 1062 1073 Date icalDateStart = icalEvent.getDateStart(); 1074 Date icalDateEnd = icalEvent.getDateEnd(); 1075 1076 1082 Date thisTimeFrom = dateFrom; 1083 Date thisTimeTo = dateTo; 1084 1085 if ((thisTimeFrom.after(icalDateStart) 1087 || thisTimeFrom.equals(icalDateStart)) 1088 && (thisTimeTo.before(icalDateEnd) 1089 || thisTimeTo.equals(icalDateEnd))) 1090 { 1091 icalEvent.setDateStart(thisTimeFrom); 1093 icalEvent.setDateEnd(thisTimeTo); 1094 FBString.append(createFBRow(formatter, icalEvent.getDateStart(), icalEvent.getDateEnd())); 1095 } 1096 else if (thisTimeFrom.before(icalDateStart) 1098 && thisTimeTo.after(icalDateEnd)) 1099 { 1100 FBString.append(createFBRow(formatter, icalEvent.getDateStart(), icalEvent.getDateEnd())); 1102 } 1103 else if (thisTimeFrom.before(icalDateEnd) 1105 && thisTimeTo.after(icalDateEnd)) 1106 { 1107 icalEvent.setDateStart(thisTimeFrom); 1109 icalEvent.setDateEnd(icalDateEnd); 1110 FBString.append(createFBRow(formatter, icalEvent.getDateStart(), icalEvent.getDateEnd())); 1111 } 1112 else if (thisTimeFrom.before(icalDateStart) 1114 && thisTimeTo.after(icalDateStart)) 1115 { 1116 icalEvent.setDateStart(icalDateStart); 1118 icalEvent.setDateEnd(thisTimeTo); 1119 FBString.append(createFBRow(formatter, icalEvent.getDateStart(), icalEvent.getDateEnd())); 1120 } 1121 else { 1122 } 1124 } 1125 1126 FBString.append("URL:"); 1128 FBString.append( this.getFBUrl()); FBString.append( this.getOrganizer()); 1130 FBString.append( ".ifb"); 1131 FBString.append( "\n"); 1132 FBString.append( "END:VFREEBUSY\nEND:VCALENDAR"); 1133 FBString.append( "\n"); 1134 return FBString.toString(); 1135 } 1136 1137 public String createFBRow(SimpleDateFormat formatter, Date repeatDateStart, Date repeatDateEnd) 1138 { 1139 StringBuffer FBData = new StringBuffer (); 1140 FBData.append("FREEBUSY;FBTYPE=BUSY:"); 1141 FBData.append(formatter.format(repeatDateStart)); 1142 FBData.append("/"); 1143 FBData.append(formatter.format(repeatDateEnd)); 1144 FBData.append("\n"); 1145 return FBData.toString(); 1146 } 1147 1148 1153 public String getVCalendar() 1154 { 1155 StringBuffer vCalBuffer = new StringBuffer (); 1156 vCalBuffer.append(ICalUtil.makeVEventLines("BEGIN:VCALENDAR","")); 1157 vCalBuffer.append(ICalUtil.makeVEventLines("PRODID:",this.getProdId())); 1158 vCalBuffer.append(ICalUtil.makeVEventLines("VERSION:",this.getVersion())); 1159 vCalBuffer.append("\n"); 1160 1161 for(Iterator i=icalEventCollection.iterator(); i.hasNext(); ) 1163 { 1164 ICalendarVEvent icalEvent = (ICalendarVEvent)i.next(); 1165 vCalBuffer.append(icalEvent.toVEvent()).append("\n"); 1166 } 1167 1168 vCalBuffer.append(ICalUtil.makeVEventLines("END:VCALENDAR","")); 1169 1170 return vCalBuffer.toString(); 1171 } 1172 1173 1174 public String getJiCalXML(String dateRangeOrDaysForward, String timeRange) throws Exception 1175 { 1176 Date dateFrom = null; 1177 Date dateTo = null; 1178 try 1179 { 1180 long daysForward = getDaysForwardNumeric(dateRangeOrDaysForward); 1181 try 1182 { 1183 return getJiCalXML(daysForward, timeRange); 1184 } 1185 catch (Exception e) 1186 { 1187 logger.severe("Error:" + e); 1188 } 1189 } 1190 catch (Exception e) 1191 { 1192 try 1193 { 1194 dateFrom = getDateFrom(dateRangeOrDaysForward); 1195 dateTo = getDateTo(dateRangeOrDaysForward); 1196 1197 } 1198 catch (Exception ee) 1199 { 1200 logger.severe("Unable to read your input dates: " 1201 + dateRangeOrDaysForward 1202 + "They must be of the form ccYYmmDDhhMMss-ccYYmmDDhhMMss" 1203 + ee); 1204 throw ee; 1205 } 1206 } 1207 return getJiCalXML(dateFrom , dateTo, timeRange); 1208 } 1209 public String getJiCalXML(long daysForward, String timeRange) 1210 { 1211 Date dateFrom = new Date (); 1212 Date dateTo = getDateToFromDaysForward(daysForward); 1213 1214 1218 try 1219 { 1220 dateFrom = (Date )dateOnlyFormat.parse(dateOnlyFormat.format(dateFrom)); 1221 dateTo = (Date )dateFormatter.parse(dateOnlyFormat.format(dateTo) + "235959"); 1222 } 1223 catch (Exception e) 1224 { 1225 } 1226 return getJiCalXML(dateFrom, dateTo, timeRange); 1227 } 1228 1229 public String getJiCalXML(Date dateFrom, Date dateTo, String timeRange) 1230 { 1231 if (this.getOrganizerEmail() == null 1232 || this.getOrganizer() == null) { 1233 logger.severe("Cannot create XML unless OrganizerEmail are Organizer provided to ICalendar"); 1234 return ""; 1235 } 1236 1237 1244 1245 StringBuffer XMLString= new StringBuffer ("<jicalxml>\n"); 1246 XMLString.append("\t<organizer>").append( this.getOrganizer()).append("</organizer>\n"); 1247 XMLString.append("\t<organizeremail>").append(this.getOrganizerEmail()).append("</organizeremail>\n"); 1248 XMLString.append("\t<datestart>").append(dateFormatter.format(dateFrom)).append("</datestart>\n"); 1249 XMLString.append("\t<dateend>").append(dateFormatter.format(dateTo)).append("</dateend>\n"); 1250 XMLString.append("<vevents>\n"); 1251 1254 Date trueStartDate = new Date (0); 1255 getIcalExpandedEvents(trueStartDate, dateTo, timeRange); 1256 1257 Iterator eeIterator = sortedExpandedEvents.iterator(); 1258 while (eeIterator.hasNext()) 1259 { 1260 ICalendarVEvent icalEvent = (ICalendarVEvent) eeIterator.next(); 1261 1272 Date icalDateStart = icalEvent.getDateStart(); 1273 Date icalDateEnd = icalEvent.getDateEnd(); 1274 1275 1281 Date thisTimeFrom = dateFrom; 1282 Date thisTimeTo = dateTo; 1283 1284 if ((thisTimeFrom.after(icalDateStart) 1286 || thisTimeFrom.equals(icalDateStart)) 1287 && (thisTimeTo.before(icalDateEnd) 1288 || thisTimeTo.equals(icalDateEnd))) 1289 { 1290 icalEvent.setDateStart(thisTimeFrom); 1292 icalEvent.setDateEnd(thisTimeTo); 1293 XMLString.append("\t\t"); 1294 XMLString.append(icalEvent.toXML()); 1295 XMLString.append("\n"); 1296 } 1297 else if (thisTimeFrom.before(icalDateStart) 1299 && thisTimeTo.after(icalDateEnd)) 1300 { 1301 XMLString.append("\t\t"); 1304 XMLString.append(icalEvent.toXML()); 1305 XMLString.append("\n"); 1306 } 1307 else if (thisTimeFrom.before(icalDateEnd) 1309 && thisTimeTo.after(icalDateEnd)) 1310 { 1311 icalEvent.setDateStart(thisTimeFrom); 1314 icalEvent.setDateEnd(icalDateEnd); 1315 XMLString.append("\t\t"); 1316 XMLString.append(icalEvent.toXML()); 1317 XMLString.append("\n"); 1318 } 1319 else if (thisTimeFrom.before(icalDateStart) 1321 && thisTimeTo.after(icalDateStart)) 1322 { 1323 icalEvent.setDateStart(icalDateStart); 1326 icalEvent.setDateEnd(thisTimeTo); 1327 XMLString.append("\t\t"); 1328 XMLString.append(icalEvent.toXML()); 1329 XMLString.append("\n"); 1330 } 1331 else { 1332 } 1334 } 1335 1336 XMLString.append("</vevents></jicalxml>\n"); 1337 return XMLString.toString(); 1338 } 1339 1340 public String getJiCaldisplayXML(String dateRangeOrDaysForward, String timeRange) throws Exception 1341 { 1342 DateTimeRange dtr = new DateTimeRange(); 1343 dtr.calcDateTimeRange(dateRangeOrDaysForward,timeRange); 1344 1345 return getJiCaldisplayXML(dtr.dateFrom , dtr.dateTo, timeRange); 1377 } 1378 1398 public String getJiCaldisplayXML(Date dateFrom, Date dateTo, String timeRange) 1399 { 1400 if (this.getOrganizerEmail() == null 1401 || this.getOrganizer() == null) { 1402 logger.severe("Cannot create XML unless OrganizerEmail are Organizer provided to ICalendar"); 1403 return ""; 1404 } 1405 1406 1411 1412 StringBuffer XMLString = new StringBuffer ("<jicaldisplay>\n"); 1413 XMLString.append("\t<organizer>").append( this.getOrganizer()).append("</organizer>\n"); 1414 XMLString.append("\t<organizeremail>").append(this.getOrganizerEmail()).append("</organizeremail>\n"); 1415 XMLString.append("\t<datestart>").append(dateFormatter.format(dateFrom)).append("</datestart>\n"); 1416 XMLString.append("\t<dateend>").append(dateFormatter.format(dateTo)).append("</dateend>\n"); 1417 1418 1422 1424 Date trueStartDate = new Date (0); 1425 getIcalExpandedEvents(trueStartDate, dateTo, timeRange); 1426 1427 1431 1432 1434 Calendar repeatXMLDateStart = new GregorianCalendar (); 1435 repeatXMLDateStart.setTime(dateFrom); 1436 1437 XMLString.append("<days>\n"); 1438 1439 while (repeatXMLDateStart != null 1440 && ! repeatXMLDateStart.getTime().after(dateTo)) 1441 { 1442 XMLString.append("\t\t<day>\n"); 1443 XMLString.append("\t\t\t<dayofweek>").append(dayOfWeek.format(repeatXMLDateStart.getTime())).append("</dayofweek>\n"); 1444 XMLString.append("\t\t\t<monthofyear>").append(monthOfYear.format(repeatXMLDateStart.getTime())).append("</monthofyear>\n"); 1445 XMLString.append("\t\t\t<weeknumber>").append(weekNumber.format(repeatXMLDateStart.getTime())).append("</weeknumber>\n"); 1446 XMLString.append("\t\t\t<date>").append(dateOnlyFormat.format(repeatXMLDateStart.getTime())).append("</date>\n"); 1447 XMLString.append("\t\t\t<dayofweeknum>").append(repeatDateStart.get(Calendar.DAY_OF_WEEK)).append("</dayofweeknum>\n"); 1449 XMLString.append("\t\t\t<dayofmonth>").append(dayNumber.format(repeatDateStart.getTime())).append("</dayofmonth>\n"); 1450 XMLString.append("\t\t\t<year>").append(year.format(repeatDateStart.getTime())).append("</year>\n"); 1451 XMLString.append("\t\t\t<vevents>\n"); 1452 Calendar thisDateFrom = new GregorianCalendar (); 1454 thisDateFrom.setTime(repeatXMLDateStart.getTime()); 1455 thisDateFrom.set(Calendar.HOUR_OF_DAY, 0); 1456 thisDateFrom.set(Calendar.MINUTE, 0); 1457 thisDateFrom.set(Calendar.SECOND, 0); 1458 1459 Date thisTimeFrom = thisDateFrom.getTime(); 1460 Calendar thisDateTo = new GregorianCalendar (); 1462 thisDateTo.setTime(repeatXMLDateStart.getTime()); 1463 thisDateTo.set(Calendar.HOUR_OF_DAY,24); 1464 thisDateTo.set(java.util.Calendar.MINUTE,0); 1465 thisDateTo.set(java.util.Calendar.SECOND,0); 1466 Date thisTimeTo = thisDateTo.getTime(); 1467 1468 Iterator eeIterator = sortedExpandedEvents.iterator(); 1469 while (eeIterator.hasNext()) 1470 { 1471 ICalendarVEvent icalEvent = (ICalendarVEvent) eeIterator.next(); 1472 1483 Date icalDateStart = icalEvent.getDateStart(); 1484 Date icalDateEnd = icalEvent.getDateEnd(); 1485 1486 1492 thisDateFrom.setTime(icalDateStart); 1493 thisDateTo.setTime(icalDateEnd); 1494 1495 if ((thisTimeFrom.after(icalDateStart) 1497 || thisTimeFrom.equals(icalDateStart)) 1498 && (thisTimeTo.before(icalDateEnd) 1499 || thisTimeTo.equals(icalDateEnd))) 1500 { 1501 icalEvent.setDateStart(thisTimeFrom); 1503 icalEvent.setDateEnd(thisTimeTo); 1504 XMLString.append("\t\t"); 1505 XMLString.append(icalEvent.toXML()); 1506 XMLString.append("\n"); 1507 } 1508 else if (thisTimeFrom.before(icalDateStart) 1510 && thisTimeTo.after(icalDateEnd)) 1511 { 1512 XMLString.append("\t\t"); 1515 XMLString.append(icalEvent.toXML()); 1516 XMLString.append("\n"); 1517 } 1518 else if (thisTimeFrom.before(icalDateEnd) 1520 && thisTimeTo.after(icalDateEnd)) 1521 { 1522 icalEvent.setDateStart(thisTimeFrom); 1525 icalEvent.setDateEnd(icalDateEnd); 1526 XMLString.append("\t\t"); 1527 XMLString.append(icalEvent.toXML()); 1528 XMLString.append("\n"); 1529 } 1530 else if (thisTimeFrom.before(icalDateStart) 1532 && thisTimeTo.after(icalDateStart)) 1533 { 1534 icalEvent.setDateStart(icalDateStart); 1537 icalEvent.setDateEnd(thisTimeTo); 1538 XMLString.append("\t\t"); 1539 XMLString.append(icalEvent.toXML()); 1540 XMLString.append("\n"); 1541 } 1542 else { 1543 } 1545 } 1546 1547 XMLString.append("\t\t\t</vevents>"); 1548 XMLString.append("\t\t</day>\n"); 1549 1550 repeatXMLDateStart.add(Calendar.HOUR_OF_DAY, 24); 1552 1553 } 1554 1555 XMLString.append("\t</days>\n"); 1556 XMLString.append("</jicaldisplay>\n"); 1557 return XMLString.toString(); 1558 } 1559 1562 public Calendar getRepeatDateEnd() { 1563 return repeatDateEnd; 1564 } 1565 1568 public void setRepeatDateEnd(Calendar repeatDateEndIn) { 1569 this.repeatDateEnd = repeatDateEndIn; 1570 } 1571 1574 public Calendar getRepeatDateStart() { 1575 return repeatDateStart; 1576 } 1577 1580 public void setRepeatDateStart(Calendar repeatDateStartIn) { 1581 this.repeatDateStart = repeatDateStartIn; 1582 } 1583} 1584 | Popular Tags |