1 10 11 package org.jical; 12 13 19 20 import java.io.ByteArrayInputStream ; 21 import java.text.SimpleDateFormat ; 22 import java.util.ArrayList ; 23 import java.util.Calendar ; 24 import java.util.Collection ; 25 import java.util.Comparator ; 26 import java.util.Date ; 27 import java.util.GregorianCalendar ; 28 import java.util.TimeZone ; 29 30 import javax.xml.parsers.DocumentBuilder ; 31 import javax.xml.parsers.DocumentBuilderFactory ; 32 33 import org.w3c.dom.Document ; 34 import org.w3c.dom.Node ; 35 import org.w3c.dom.NodeList ; 36 37 46 47 public class ICalendarVEvent implements Cloneable { 48 49 private static SimpleDateFormat localDateFormatter = new SimpleDateFormat ("yyyyMMdd'T'HHmmss"); 50 private static SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmss"); 51 private static final SimpleDateFormat VEVENTformatter = new SimpleDateFormat ("yyyyMMdd'T'HHmmss'Z'"); 52 53 54 private Date dateStamp; 55 private String organizer; 56 private String organizerEmail; 57 private Date created; 58 private Date lastModified; 59 private int priority; 60 private String attach; 61 private String location; 62 private String percentComplete; 63 private String status; 64 private String comment; 65 private boolean recurrenceId; 66 private String url; 67 private String geo; 68 private float geoX; 69 private float geoY; 70 private String resources; 71 private String contact; 72 private String relatedTo; 73 private String requestStatus; 74 75 private String id; 76 77 78 89 90 public String getId() 91 { 92 return this.id; 93 } 94 95 public void setId(String id) 96 { 97 this.id = id; 98 } 99 100 private String uid; 101 102 112 113 public String getUid() 114 { 115 return uid; 116 } 117 118 public void setUid(String uid) 119 { 120 this.uid = uid; 121 } 122 123 private String transparency; 124 125 135 136 public String getTransparency() { 137 return this.transparency; 138 } 139 140 public void setTransparency(String transparency) { 141 this.transparency = transparency; 142 } 143 144 private Date dateStart; 145 146 156 157 public Date getDateStart() { 158 return this.dateStart; 159 } 160 161 public void setDateStart(Date dateStart) { 162 this.dateStart = dateStart; 163 } 164 165 private Date dateEnd; 166 167 177 178 public Date getDateEnd() { 179 return this.dateEnd; 180 } 181 182 public void setDateEnd(Date dateEnd) { 183 this.dateEnd = dateEnd; 184 } 185 186 private String duration; 187 188 199 200 public String getDuration() { 201 return this.duration; 202 } 203 204 public void setDuration(String duration) { 205 this.duration = duration; 206 210 if (getDateStart() != null && getDateEnd() == null) { 211 char durationArray[] = duration.substring(2).toCharArray(); 214 String timeBuilt = ""; 220 int totalSecs = 0; 221 for (int ctr = 0; ctr < durationArray.length; ctr++) { 222 String thisChar = String.valueOf(durationArray[ctr]); 223 int timeUnit = thisChar.indexOf("HMS"); 225 if (timeUnit != -1) { 226 if (timeUnit == 0) 228 totalSecs = totalSecs + Integer.parseInt(timeBuilt) 229 * 3600; 230 if (timeUnit == 1) 231 totalSecs = totalSecs + Integer.parseInt(timeBuilt) 232 * 60; 233 if (timeUnit == 2) 234 totalSecs = totalSecs + Integer.parseInt(timeBuilt); 235 timeBuilt = ""; 236 } else { 237 timeBuilt = timeBuilt.concat(thisChar); 239 } 240 } 241 int dateRepeatUnit = Calendar.SECOND; 243 Calendar workDateEnd = new GregorianCalendar (); 244 workDateEnd.setTime(getDateStart()); 245 workDateEnd.add(dateRepeatUnit, totalSecs); 246 setDateEnd(workDateEnd.getTime()); 247 } 248 } 249 250 private String description; 251 252 262 263 public String getDescription() { 264 return this.description; 265 } 266 267 public void setDescription(String description) { 268 this.description = description; 269 } 270 271 private String summary; 272 273 283 284 public String getSummary() { 285 return this.summary; 286 } 287 288 public void setSummary(String summary) { 289 this.summary = summary; 290 } 291 292 private int sequence; 293 294 304 305 public int getSequence() { 306 return this.sequence; 307 } 308 309 public void setSequence(int sequence) { 310 this.sequence = sequence; 311 } 312 313 private String categories; 314 315 325 326 public String getCategories() { 327 return this.categories; 328 } 329 330 public void setCategories(String categories) { 331 this.categories = categories; 332 } 333 334 private String eventClass; 335 336 346 347 public String getEventClass() { 348 return this.eventClass; 349 } 350 351 public void setEventClass(String eventClass) { 352 this.eventClass = eventClass; 353 } 355 356 private String rRule; 357 358 368 369 public String getRRule() { 370 return this.rRule; 371 } 372 373 public void setRRule(String rRule) { 374 this.rRule = rRule; 375 } 376 377 private int repeatCount; 378 379 389 390 public int getRepeatCount() { 391 return this.repeatCount; 392 } 393 394 public void setRepeatCount(int repeatCount) { 395 this.repeatCount = repeatCount; 396 } 397 398 public Collection exDateCollection; 399 400 public Collection getExDateCollection() { 401 return this.exDateCollection; 402 } 403 404 public void setExDateCollection(Collection exDateCollection) { 405 this.exDateCollection = exDateCollection; 406 } 407 408 409 private boolean exDatesExist; 410 411 421 422 public boolean isExDatesExist() { 423 return this.exDatesExist; 424 } 425 426 public void setExDatesExist(boolean exDatesExist) { 427 this.exDatesExist = exDatesExist; 428 } 429 430 431 private RepeatRules repeatRules; 432 433 437 public RepeatRules getRepeatRules() { 438 return this.repeatRules; 439 } 440 441 445 public void setRepeatRules(RepeatRules repeatRules) { 446 this.repeatRules = repeatRules; 447 } 448 449 450 public ICalendarVEvent() { 451 this.exDateCollection = new ArrayList (); 452 this.eventClass = null; 453 this.repeatCount = 0; 454 this.repeatRules = new RepeatRules(); 455 this.categories = null; 456 this.dateStart = null; 457 this.dateEnd = null; 458 this.description = null; 459 this.duration = null; 460 this.eventClass = null; 461 this.exDatesExist = false; 462 this.rRule = null; 463 this.sequence = 0; 464 this.summary = null; 465 this.transparency = null; 466 this.uid = null; 467 } 468 469 public ICalendarVEvent(Date dateStart, Date dateEnd, String description, String duration, 470 String summary, ArrayList exDates, RepeatRules repRules) 471 { 472 this.exDateCollection = exDates; 473 this.eventClass = null; 474 this.repeatCount = 0; 475 this.repeatRules = repRules; 476 this.categories = null; 477 this.dateStart = dateStart; 478 this.dateEnd = dateEnd; 479 this.description = description; 480 this.duration = duration; 481 this.eventClass = null; 482 this.exDatesExist = false; 483 this.rRule = null; 484 this.sequence = 0; 485 this.summary = summary; 486 this.transparency = null; 487 this.uid = null; 488 } 489 490 public Object clone() throws CloneNotSupportedException { 491 return super.clone(); 492 } 493 494 495 521 public String toVEvent() { 522 523 525 StringBuffer vEventBuffer = new StringBuffer (); 526 vEventBuffer.append(ICalUtil.makeVEventLines("BEGIN:VEVENT","")); 527 vEventBuffer.append(ICalUtil.makeVEventLines("UID:",this.uid)); 528 vEventBuffer.append(ICalUtil.makeVEventLines("TRANSP:",this.transparency)); 529 vEventBuffer.append(ICalUtil.makeVEventLines("DTSTART;",getVEventLocalTime(this.dateStart))); 530 vEventBuffer.append(ICalUtil.makeVEventLines("DTEND;",getVEventLocalTime(this.dateEnd))); 531 vEventBuffer.append(ICalUtil.makeVEventLines("DTSTAMP:",VEVENTformatter.format(this.dateStamp))); 532 vEventBuffer.append(ICalUtil.makeVEventLines("ORGANIZER:",this.organizer)); 533 vEventBuffer.append(ICalUtil.makeVEventLines("CREATED:",VEVENTformatter.format(this.created))); 534 vEventBuffer.append(ICalUtil.makeVEventLines("LAST-MODIFIED:",VEVENTformatter.format(this.lastModified))); 535 vEventBuffer.append(ICalUtil.makeVEventLines("SUMMARY:",this.summary)); 536 vEventBuffer.append(ICalUtil.makeVEventLines("DESCRIPTION:",this.description)); 537 vEventBuffer.append(ICalUtil.makeVEventLines("SEQUENCE:",new Integer (this.sequence).toString())); 538 vEventBuffer.append(ICalUtil.makeVEventLines("CLASS:",this.eventClass)); 539 vEventBuffer.append(ICalUtil.makeVEventLines("ATTACH:",this.attach)); 540 vEventBuffer.append(ICalUtil.makeVEventLines("END:VEVENT","")); 541 542 return vEventBuffer.toString(); 543 } 544 545 548 public String getVEventLocalTime(Date localTime) 549 { 550 if (localTime!=null) 551 return "VALUE=DATE-TIME;TZID=/softwarestudio.org/Olson_20011030_5/"+TimeZone.getDefault().getID()+":"+localDateFormatter.format(localTime); 552 else 553 return ""; 554 } 555 556 560 561 public String toXML() { 562 StringBuffer buffer = new StringBuffer (); 563 buffer.append("<vevent>"); 564 buffer.append("<uid>").append(this.uid).append("</uid>"); 565 if ( this.transparency != null && this.transparency.length() > 0) { 567 buffer.append("<transparency>"); 568 buffer.append(this.transparency); 569 buffer.append("</transparency>"); 570 } 571 if ( this.location != null && this.location.length() > 0) { 573 buffer.append("<location>"); 574 buffer.append("<![CDATA["); 575 buffer.append(this.location); 576 buffer.append("]]>"); 577 buffer.append("</location>"); 578 } 579 if ( this.url != null && this.url.length() > 0) { 580 buffer.append("<url>"); 581 buffer.append(this.url); 582 buffer.append("</url>"); 583 } 584 if ( this.organizer != null && this.organizer.length() > 0) { 585 buffer.append("<organizer>"); 586 buffer.append(this.organizer); 587 buffer.append("</organizer>"); 588 } 589 if ( this.organizerEmail != null && this.organizerEmail.length() > 0) { 590 buffer.append("<organizeremail>"); 591 buffer.append("<![CDATA["); 592 buffer.append(this.organizerEmail); 593 buffer.append("]]>"); 594 buffer.append("</organizeremail>"); 595 } 596 if ( getStatus() != null && getStatus().length() > 0) { 597 buffer.append("<status>"); 598 buffer.append(getStatus()); 599 buffer.append("</status>"); 600 } 601 buffer.append("<datestart>"); 602 if ( this.dateStart != null ) 603 buffer.append(formatter.format(this.dateStart)); 604 buffer.append("</datestart>"); 605 buffer.append("<dateend>"); 606 if ( this.dateEnd != null ) 607 buffer.append(formatter.format(this.dateEnd)); 608 buffer.append("</dateend>"); 609 610 if ( this.description != null && this.description.length() > 0) { 611 buffer.append("<description>"); 612 buffer.append("<![CDATA[").append(this.description).append("]]>"); 613 buffer.append("</description>"); 614 } 615 buffer.append("<summary>"); 616 if ( this.summary != null ) 617 buffer.append("<![CDATA[").append(this.summary).append("]]>"); 618 buffer.append("</summary>"); 619 buffer.append("<sequence>"); 620 if ( java.lang.Integer.toString(this.sequence) != null ) 621 buffer.append(java.lang.Integer.toString(this.sequence)); 622 buffer.append("</sequence>"); 623 if ( this.categories != null && this.categories.length() > 0) { 624 buffer.append("<categories>"); 625 buffer.append(this.categories); 626 buffer.append("</categories>"); 627 } 628 if ( this.eventClass != null && this.eventClass.length() > 0) { 629 buffer.append("<eventclass>"); 630 buffer.append(this.eventClass); 631 buffer.append("</eventclass>"); 632 } 633 buffer.append("</vevent>"); 634 return buffer.toString(); 635 } 636 637 638 642 public void fromXML(String inXML) { 643 try { 644 DocumentBuilderFactory docuBuilderFactory = DocumentBuilderFactory 645 .newInstance(); 646 docuBuilderFactory.setValidating(false); 647 DocumentBuilder docuBuilder = docuBuilderFactory 648 .newDocumentBuilder(); 649 ByteArrayInputStream bais = new ByteArrayInputStream (inXML 650 .getBytes()); 651 Document doc = docuBuilder.parse(bais); 652 NodeList nl = doc.getFirstChild().getChildNodes(); 653 654 for (int ctr = 0; ctr < nl.getLength(); ctr++) { 655 Node ni = nl.item(ctr); 656 String nodeName = ni.getNodeName(); 658 Node nodeChild = ni.getFirstChild(); 659 String nodeValue = ""; 660 try { 661 nodeValue = nodeChild.getNodeValue(); 662 } catch (Exception e) { 663 } 665 if (nodeValue != null) { 667 if (nodeName.equals("uid")) 668 this.setUid(nodeValue); 669 else if (nodeName.equals("transparency")) 670 this.setTransparency(nodeValue); 671 else if (nodeName.equals("datestart")) 672 this.setDateStart((Date ) formatter.parse(nodeValue)); 673 else if (nodeName.equals("dateend")) 674 this.setDateEnd((Date ) formatter.parse(nodeValue)); 675 else if (nodeName.equals("description")) 676 this.setDescription(nodeValue); 677 else if (nodeName.equals("summary")) 678 this.setSummary(nodeValue); 679 else if (nodeName.equals("categories")) 680 this.setCategories(nodeValue); 681 else if (nodeName.equals("eventclass")) 682 this.setEventClass(nodeValue); 683 else if (nodeName.equals("location")) 685 this.setLocation(nodeValue); 686 else if (nodeName.equals("url")) 687 this.setUrl(nodeValue); 688 else if (nodeName.equals("organizer")) 689 this.setOrganizer(nodeValue); 690 else if (nodeName.equals("organizeremail")) 691 this.setOrganizerEmail(nodeValue); 692 else if (nodeName.equals("status")) 693 this.setPercentComplete(nodeValue); 694 } 695 } 696 } catch (Exception e) { 697 System.err.println("XML Parser exception: fromXML:" + e); 698 } 699 } 700 701 702 public boolean equals(Object o) { 703 if (o instanceof ICalendarVEvent) { 704 ICalendarVEvent e = (ICalendarVEvent) o; 705 return uid.equals(e.uid) && dateStart.equals(e.dateStart) 706 && dateEnd.equals(e.dateEnd); 707 } 708 return false; 709 } 710 711 public int hashCode() { 712 return uid.hashCode() ^ dateStart.hashCode() ^ dateEnd.hashCode(); 713 } 714 715 public static class StartDateComparator implements Comparator { 716 public int compare(Object o1, Object o2) { 717 ICalendarVEvent e1 = (ICalendarVEvent) o1; 718 ICalendarVEvent e2 = (ICalendarVEvent) o2; 719 return e1.getDateStart().compareTo(e2.getDateStart()); 720 } 721 722 public boolean equals(Object obj) { 723 return (obj instanceof StartDateComparator); 724 } 725 } 726 727 public static class StartDateUIDComparator implements Comparator { 728 public int compare(Object o1, Object o2) { 729 ICalendarVEvent e1 = (ICalendarVEvent) o1; 730 ICalendarVEvent e2 = (ICalendarVEvent) o2; 731 int out = e1.getDateStart().compareTo(e2.getDateStart()); 732 if (out == 0) { 733 out = e1.getUid().compareTo(e2.getUid()); 734 } 735 return out; 736 } 737 738 public boolean equals(Object obj) { 739 return (obj instanceof StartDateUIDComparator); 740 } 741 } 742 743 753 public Date getCreated() { 754 return created; 755 } 756 public void setCreated(Date created) { 757 this.created = created; 758 } 759 769 public Date getDateStamp() { 770 return dateStamp; 771 } 772 public void setDateStamp(Date dateStamp) { 773 this.dateStamp = dateStamp; 774 } 775 785 public Date getLastModified() { 786 return lastModified; 787 } 788 public void setLastModified(Date lastModified) { 789 this.lastModified = lastModified; 790 } 791 801 public String getOrganizer() { 802 return organizer; 803 } 804 public void setOrganizer(String organizer) { 805 this.organizer = organizer; 806 } 807 808 818 public int getPriority() { 819 return priority; 820 } 821 public void setPriority(int priority) { 822 this.priority = priority; 823 } 824 834 public String getAttach() { 835 return attach; 836 } 837 public void setAttach(String attach) { 838 this.attach = attach; 839 } 840 850 public String getLocation() { 851 return location; 852 } 853 public void setLocation(String location) { 854 this.location = location; 855 } 856 866 public String getPercentComplete() { 867 return percentComplete; 868 } 869 public void setPercentComplete(String percentComplete) { 870 this.percentComplete = percentComplete; 871 } 872 public boolean isRecurrenceId() { 873 return recurrenceId; 874 } 875 public void setRecurrenceId(boolean recurrenceId) { 876 this.recurrenceId = recurrenceId; 877 } 878 888 public String getUrl() { 889 return url; 890 } 891 public void setUrl(String url) { 892 this.url = url; 893 } 894 904 public String getOrganizerEmail() { 905 return organizerEmail; 906 } 907 public void setOrganizerEmail(String organizerEmail) { 908 this.organizerEmail = organizerEmail; 909 } 910 public String getStatus() 911 { 912 return status; 913 } 914 public void setStatus(String s) 915 { 916 status = s; 917 } 918 919 public String getComment() { 920 return comment; 921 } 922 public void setComment(String comment) { 923 this.comment = comment; 924 } 925 public String getGeo() { 926 return geo; 927 } 928 public void setGeo(String geo) { 929 this.geo = geo; 930 } 931 public float getGeoX() { 932 return geoX; 933 } 934 public void setGeoX(float geoX) { 935 this.geoX = geoX; 936 } 937 public float getGeoY() { 938 return geoY; 939 } 940 public void setGeoY(float geoY) { 941 this.geoY = geoY; 942 } 943 public String getResources() { 944 return resources; 945 } 946 public void setResources(String resources) { 947 this.resources = resources; 948 } 949 public String getContact() { 950 return contact; 951 } 952 public void setContact(String contact) { 953 this.contact = contact; 954 } 955 public String getRelatedTo() { 956 return relatedTo; 957 } 958 public void setRelatedTo(String relatedTo) { 959 this.relatedTo = relatedTo; 960 } 961 public String getRequestStatus() { 962 return requestStatus; 963 } 964 public void setRequestStatus(String requestStatus) { 965 this.requestStatus = requestStatus; 966 } 967 } 968 969 | Popular Tags |