package org.wyona.yanel.impl.resources;

import org.apache.log4j.Category;

/**
 *
 */
public class CalendarEvent {
    private static Category log = Category.getInstance(CalendarEvent.class);

    private String uid;
    private String summary;
    private String location;
    private String _class;
    private String categories;
    private String created;
    private String lastModified;
    private String dtstamp;
    private String start;
    private String end;

    /**
     *
     */
    public String getUID() {
        return uid;
    }

    /**
     *
     */
    public void setUID(String uid) {
        this.uid = uid;
    }

    /**
     *
     */
    public void setSummary(String summary) {
        this.summary = summary;
    }

    /**
     *
     */
    public void setLocation(String location) {
        this.location = location;
    }

    /**
     *
     */
    public void setClass(String _class) {
        this._class = _class;
    }

    /**
     *
     */
    public void setCategories(String categories) {
        this.categories = categories;
    }

    /**
     *
     */
    public void setCreated(String created) {
        this.created = created;
    }

    /**
     *
     */
    public void setLastModified(String lastModified) {
        this.lastModified = lastModified;
    }

    /**
     *
     */
    public void setDtstamp(String dtstamp) {
        this.dtstamp = dtstamp;
    }

    /**
     *
     */
    public void setStart(String start) {
        this.start = start;
    }

    /**
     *
     */
    public void setEnd(String end) {
        this.end = end;
    }

    /**
     *
     */
    public String toString() {
        return "ICS Event: " + uid + ", " + summary;
    }

    /**
     *
     */
    public String toXML() {
        StringBuffer sb = new StringBuffer("<?xml version=\"1.0\"?>");
        sb.append("<cal:event xmlns:cal=\"http://...\" created=\"" + created + "\" last-modified=\"" + lastModified + "\" dtstamp=\"" + dtstamp + "\" uid=\"" + uid + "\" class=\"" + _class + "\" categories=\"" + categories + "\">");
        sb.append("  <cal:summary>" + summary + "</cal:summary>");
        if(location != null) sb.append("  <cal:location>" + location + "</cal:location>");
        sb.append("  <cal:dtstart value=\"DATE\" tzid=\"" + start + "\"/>");
        sb.append("  <cal:dtend value=\"DATE\" tzid=\"" + end + "\"/>");
        sb.append("</cal:event>");
        return sb.toString();
    }

    /**
     * @param icsLine UID:..., CREATED:..., ...
     */
    public void setProperty(String icsLine) {
        if (icsLine.startsWith("UID:")) {
            setUID(icsLine.split(":")[1]);
        } else if (icsLine.startsWith("SUMMARY:")) {
            setSummary(icsLine.split(":")[1]);
        } else if (icsLine.startsWith("LOCATION:")) {
            setLocation(icsLine.split(":")[1]);
        } else if (icsLine.startsWith("CLASS:")) {
            setClass(icsLine.split(":")[1]);
        } else if (icsLine.startsWith("CATEGORIES:")) {
            setCategories(icsLine.split(":")[1]);
        } else if (icsLine.startsWith("CREATED:")) {
            setCreated(icsLine.split(":")[1]);
        } else if (icsLine.startsWith("LAST-MODIFIED:")) {
            setLastModified(icsLine.split(":")[1]);
        } else if (icsLine.startsWith("DTSTAMP:")) {
            setDtstamp(icsLine.split(":")[1]);
        } else if (icsLine.startsWith("DTSTART;")) {
            setStart(icsLine.substring(icsLine.indexOf("TZID=") + 5));
        } else if (icsLine.startsWith("DTEND;")) {
            setEnd(icsLine.substring(icsLine.indexOf("TZID=") + 5));
        } else {
            log.warn("Not implemented yet: " + icsLine);
        }
    }
}

@@ -1838,16 +1838,20 @@
 %221.0%5C%22?%3E
+%5Cn%5Cn
 %22);%0A    
@@ -2019,32 +2019,108 @@
  %22%5C%22
- class=%5C%22%22 + _class + %22%5C
+%22);%0A%09if (_class != null) sb.append(%22 class=%5C%22%22 + _class + %22%5C%22%22);%0A%09if (categories != null) sb.append(
 %22 ca
@@ -2150,17 +2150,35 @@
 es + %22%5C%22
-%3E
+%22);%0A%09sb.append(%22%3E%5Cn
 %22);%0A    
@@ -2236,16 +2236,18 @@
 summary%3E
+%5Cn
 %22);%0A    
@@ -2252,16 +2252,17 @@
       if
+ 
 (locatio
@@ -2330,16 +2330,18 @@
 ocation%3E
+%5Cn
 %22);%0A    
@@ -2401,32 +2401,34 @@
  + start + %22%5C%22/%3E
+%5Cn
 %22);%0A        sb.a
@@ -2484,16 +2484,18 @@
  + %22%5C%22/%3E
+%5Cn
 %22);%0A    

check if properties are null