[Yanel-commits] rev 37845 - in public/yanel/trunk/src/contributions/resources/calendar/src/java/org/wyona/yanel/impl/resources: . calendar

michi at wyona.com michi at wyona.com
Thu Aug 7 10:30:40 CEST 2008


Author: michi
Date: 2008-08-07 10:30:39 +0200 (Thu, 07 Aug 2008)
New Revision: 37845

Added:
   public/yanel/trunk/src/contributions/resources/calendar/src/java/org/wyona/yanel/impl/resources/calendar/CalendarEvent.java
   public/yanel/trunk/src/contributions/resources/calendar/src/java/org/wyona/yanel/impl/resources/calendar/CalendarResource.java
Removed:
   public/yanel/trunk/src/contributions/resources/calendar/src/java/org/wyona/yanel/impl/resources/CalendarEvent.java
   public/yanel/trunk/src/contributions/resources/calendar/src/java/org/wyona/yanel/impl/resources/CalendarResource.java
Log:
moved

Deleted: public/yanel/trunk/src/contributions/resources/calendar/src/java/org/wyona/yanel/impl/resources/CalendarEvent.java
===================================================================
--- public/yanel/trunk/src/contributions/resources/calendar/src/java/org/wyona/yanel/impl/resources/CalendarEvent.java	2008-08-07 08:30:09 UTC (rev 37844)
+++ public/yanel/trunk/src/contributions/resources/calendar/src/java/org/wyona/yanel/impl/resources/CalendarEvent.java	2008-08-07 08:30:39 UTC (rev 37845)
@@ -1,159 +0,0 @@
-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\"?>\n\n");
-        sb.append("<cal:event xmlns:cal=\"http://...\" created=\"" + created + "\" last-modified=\"" + lastModified + "\" dtstamp=\"" + dtstamp + "\" uid=\"" + uid + "\"");
-	if (_class != null) sb.append(" class=\"" + _class + "\"");
-	if (categories != null) sb.append(" categories=\"" + categories + "\"");
-	sb.append(">\n");
-        sb.append("  <cal:summary>" + summary + "</cal:summary>\n");
-        if (location != null) sb.append("  <cal:location>" + location + "</cal:location>\n");
-        sb.append("  <cal:dtstart value=\"DATE\" tzid=\"" + start + "\"/>\n");
-        sb.append("  <cal:dtend value=\"DATE\" tzid=\"" + end + "\"/>\n");
-        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 if (icsLine.startsWith("DESCRIPTION:")) {
-            log.warn("DESCRIPTION tag not implemented yet: " + icsLine.split(":")[1]);
-        } else if (icsLine.startsWith("ATTENDEE:")) {
-            log.warn("ATTENDEE tag not implemented yet: " + icsLine.split(":")[1]);
-        } else if (icsLine.startsWith("PRIORITY:")) {
-            log.warn("PRIORITY tag not implemented yet: " + icsLine.split(":")[1]);
-        } else if (icsLine.startsWith("STATUS:")) {
-            log.warn("STATUS tag not implemented yet: " + icsLine.split(":")[1]);
-        } else {
-            log.warn("Not implemented yet (probably some text with line-breaks!): " + icsLine);
-        }
-    }
-}

Deleted: public/yanel/trunk/src/contributions/resources/calendar/src/java/org/wyona/yanel/impl/resources/CalendarResource.java
===================================================================
--- public/yanel/trunk/src/contributions/resources/calendar/src/java/org/wyona/yanel/impl/resources/CalendarResource.java	2008-08-07 08:30:09 UTC (rev 37844)
+++ public/yanel/trunk/src/contributions/resources/calendar/src/java/org/wyona/yanel/impl/resources/CalendarResource.java	2008-08-07 08:30:39 UTC (rev 37845)
@@ -1,362 +0,0 @@
-/*
- * Copyright 2006 Wyona
- */
-
-package org.wyona.yanel.impl.resources;
-
-import org.wyona.yanel.core.Resource;
-import org.wyona.yanel.core.api.attributes.CreatableV2;
-import org.wyona.yanel.core.api.attributes.ModifiableV2;
-import org.wyona.yanel.core.api.attributes.ViewableV2;
-import org.wyona.yanel.core.attributes.viewable.View;
-import org.wyona.yanel.core.attributes.viewable.ViewDescriptor;
-
-import org.wyona.yanel.impl.resources.CalendarEvent;
-
-import org.apache.log4j.Category;
-
-import java.io.File;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.Reader;
-import java.io.Writer;
-import java.util.HashMap;
-
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.stream.StreamSource;
-import javax.xml.transform.stream.StreamResult;
-
-import javax.servlet.http.HttpServletRequest;
-
-/**
- * Calendar resource based on the ICS format
- */
-public class CalendarResource extends Resource implements ViewableV2, ModifiableV2, CreatableV2 {
-
-    private static Category log = Category.getInstance(CalendarResource.class);
-
-    /**
-     *
-     */
-    public CalendarResource() {
-    }
-
-    /**
-     *
-     */
-    public long getSize() throws Exception {
-        log.warn("Not implemented yet!");
-        return -1;
-    }
-
-    /**
-     *
-     */
-    public String getMimeType(String viewId) {
-        if(viewId == null) {
-            String mimeType = null;
-            try {
-                mimeType = getResourceConfigProperty("mime-type");
-            } catch (Exception e) {
-                log.error(e, e);
-            }
-            if (mimeType != null) {
-                return mimeType;
-            } else {
-                return "text/calendar";
-            }
-        }
-        if(viewId != null && viewId.equals("xml")) {
-            return "application/xml";
-        }
-        if (viewId != null && viewId.equals("xhtml")) {
-            return "application/xhtml+xml";
-        }
-        log.warn("No mime type for view id: " + viewId);
-        return null;
-    }
-
-    /**
-     *
-     */
-    public boolean exists() throws Exception {
-        log.warn("Not implemented yet!");
-        return false;
-    }
-
-    /**
-     * Generate view
-     * @param viewId xml, ics, xhtml
-     */
-    public View getView(String viewId) throws Exception {
-        String categories = getResourceConfigProperty("categories");
-        String classes = getResourceConfigProperty("classes");
-        String userIds = getResourceConfigProperty("user-ids");
-        log.error("DEBUG: " + categories + " " + classes + " " + userIds);
-
-        org.wyona.yarep.core.Repository dataRepo = getRealm().getRepository();
-
-        if (dataRepo.exists(new org.wyona.yarep.core.Path(getPath())) && dataRepo.isResource(new org.wyona.yarep.core.Path(getPath()))) {
-            log.error("DEBUG: ICS exists: " + new org.wyona.yarep.core.Path(getPath()));
-            if(viewId == null) {
-                View view = new View();
-                //view.setResponse(false);
-                view.setMimeType(getMimeType(null));
-                view.setInputStream(dataRepo.getInputStream(new org.wyona.yarep.core.Path(getPath())));
-                log.error("DEBUG: Return ICS!");
-                return view;
-            }
-        }
-
-
-        String eventsPath = getResourceConfigProperty("events-path");
-        if (eventsPath == null) {
-            eventsPath = getPath();
-        }
-        log.error("DEBUG: Generate calendar from XML based events: " + eventsPath);
-
-        org.wyona.yarep.core.Path[] children = dataRepo.getChildren(new org.wyona.yarep.core.Path(eventsPath));
-        //org.wyona.yarep.core.Path[] children = dataRepo.search("categories", categories);
-
-        StringBuffer calendar = new StringBuffer("<?xml version=\"1.0\"?>\n<calendar>");
-        for (int i = 0; i < children.length; i++) {
-            if (dataRepo.isResource(children[i])) {
-                InputStream in = dataRepo.getInputStream(children[i]);
-                java.io.ByteArrayOutputStream baos  = new java.io.ByteArrayOutputStream();
-                //StringBuffer event = new StringBuffer();
-
-                byte[] buffer = new byte[8192];
-                int bytesRead;
-                while ((bytesRead = in.read(buffer)) != -1) {
-                    //event.append(new String(buffer));
-                    baos.write(buffer, 0, bytesRead);
-                }
-
-                String event = baos.toString();
-                int endOfProcessingInstruction = event.indexOf("?>");
-                if (endOfProcessingInstruction > 0) {
-                    calendar.append(event.toString().substring(endOfProcessingInstruction + 2));
-                } else {
-                    log.error("No processing instruction: " + children[i]);
-                }
-            }
-        }
-        calendar.append("</calendar>");
-
-        if(viewId != null && viewId.equals("xml")) {
-            //response.getOutputStream();
-
-            View view = new View();
-            //view.setResponse(false);
-            view.setMimeType(getMimeType(viewId));
-            view.setInputStream(new java.io.StringBufferInputStream(calendar.toString()));
-            return view;
-        } else if (viewId != null && viewId.equals("xhtml")) {
-            String xslt = getRTD().getConfigFile().getParent() + File.separator + "xslt" + File.separator + "xml2xhtml.xsl";
-            java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
-            try {
-                Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new File(xslt)));
-                transformer.transform(new StreamSource(new java.io.StringBufferInputStream(calendar.toString())), new StreamResult(out));
-            } catch(Exception e) {
-                log.error(e.getMessage(), e);
-            }
-
-            View view = new View();
-            view.setMimeType(getMimeType(viewId));
-
-            view.setInputStream(new java.io.ByteArrayInputStream(out.toByteArray()));
-            return view;
-        } else {
-            String xslt = getRTD().getConfigFile().getParent() + File.separator + "xslt" + File.separator + "xml2ics.xsl";
-            java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
-            try {
-                Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new File(xslt)));
-
-                // TODO: This will cause encoding problems. See for instance http://skew.org/xml/tutorial/
-                transformer.transform(new StreamSource(new java.io.StringBufferInputStream(calendar.toString())), new StreamResult(out));
-            } catch(Exception e) {
-                log.error(e.getMessage(), e);
-                throw new Exception(e);
-            }
-
-            //response.getOutputStream();
-
-            View view = new View();
-            //view.setResponse(false);
-            view.setMimeType(getMimeType(null));
-            view.setInputStream(new java.io.ByteArrayInputStream(out.toByteArray()));
-            return view;
-        }
-    }
-
-    /**
-     *
-     */
-    public ViewDescriptor[] getViewDescriptors() {
-        log.warn("Not implemented yet!");
-        return null;
-    }
-
-    /**
-     *
-     */
-    public boolean delete() throws Exception {
-        log.warn("Not implemented yet!");
-        return false;
-    }
-
-    /**
-     *
-     */
-    public long getLastModified() throws Exception {
-        log.warn("Not implemented yet!");
-        return -1;
-    }
-
-    /**
-     * Parse ICS and write events as XML into repository
-     */
-    public void write(InputStream in) throws Exception {
-        java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(writeICS(in)));
-        //java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(in));
-        String line;
-        CalendarEvent event = null;
-        log.error("DEBUG: Parse ICS and write events as XML into repository ...");
-        while ((line = br.readLine()) != null) {
-            if (line.startsWith("BEGIN:VEVENT")) {
-                event = new CalendarEvent();
-            } else if (line.startsWith("END:VEVENT")) {
-                //log.error("DEBUG: Write event " + event.getUID() + ", " + event.toXML());
-                Writer out = getRealm().getRepository().getWriter(new org.wyona.yarep.core.Path(getResourceConfigProperty("events-path") + "/" + event.getUID() + ".xml"));
-                out.write(event.toXML());
-                out.close();
-                event = null;
-            } else {
-                if (event != null) {
-                    event.setProperty(line);
-                } else {
-                    //log.warn("Not implemented yet: " + line);
-                }
-            }
-        }
-        br.close();
-    }
-
-    /**
-     * Save/Write the actual ICS
-     */
-    private InputStream writeICS(InputStream in) throws Exception {
-        log.error("DEBUG: Write ICS as a whole to the repository: " + getPath());
-        org.wyona.yarep.core.Path path = new org.wyona.yarep.core.Path(getPath());
-        OutputStream out = getRealm().getRepository().getOutputStream(path);
-        byte[] buf = new byte[8192];
-        int bytesR;
-        while ((bytesR = in.read(buf)) != -1) {
-            out.write(buf, 0, bytesR);
-        }
-        out.close();
-        in.close();
-        return getRealm().getRepository().getInputStream(path);
-    }
-
-    /**
-     *
-     */
-    public OutputStream getOutputStream() throws Exception {
-        log.warn("Do not use this method but rather write(InputStream)!");
-        return null;
-        //return getRealm().getRepository().getOutputStream(new org.wyona.yarep.core.Path("/calendarTODO.ics"));
-    }
-
-    /**
-     *
-     */
-    public InputStream getInputStream() throws Exception {
-        log.warn("Not implemented yet!");
-        return null;
-    }
-
-    /**
-     *
-     */
-    public Reader getReader() throws Exception {
-        log.warn("Not implemented yet!");
-        return null;
-    }
-
-    /**
-     *
-     */
-    public Writer getWriter() throws Exception {
-        log.warn("Not implemented yet!");
-        return null;
-    }
-
-    /**
-     *
-     */
-    public HashMap createRTIProperties(HttpServletRequest request) {
-        HashMap map = new HashMap();
-        map.put("events-path","/new-calendar");
-        map.put("categories","none");
-        map.put("class","none");
-        map.put("user-ids","none");
-        return map;
-    }
-
-    public String getCreateName(String suggestedName) {
-        return suggestedName;
-    }
-
-    /**
-     *
-     */
-    public void create(HttpServletRequest request) {
-        String newPath = getPath();
-        log.error("DEBUG: New path: " + newPath);
-    }
-
-    /**
-     *
-     */
-    public String getPropertyType(String propertyName) {
-        if (propertyName.equals("classes")) {
-            return CreatableV2.TYPE_SELECT;
-        } else if (propertyName.equals("categories")) {
-            return CreatableV2.TYPE_SELECT;
-        } else {
-            return null;
-        }
-    }
-
-    /**
-     * Get default values
-     */
-    public Object getProperty(String name) {
-        if (name.equals("classes")) {
-            HashMap map = new HashMap();
-            map.put("*","*");
-            map.put("public","PUBLIC");
-            map.put("private","PRIVATE");
-            return map;
-        } else {
-            return null;
-        }
-    }
-
-    /**
-     *
-     */
-    public void setProperty(String name, Object value) {
-        log.warn("Not implemented yet!");
-    }
-
-    /**
-     *
-     */
-    public String[] getPropertyNames() {
-        String[] props = {"classes", "categories"};
-        return props;
-    }
-}

Copied: public/yanel/trunk/src/contributions/resources/calendar/src/java/org/wyona/yanel/impl/resources/calendar/CalendarEvent.java (from rev 37843, public/yanel/trunk/src/contributions/resources/calendar/src/java/org/wyona/yanel/impl/resources/CalendarEvent.java)
===================================================================
--- public/yanel/trunk/src/contributions/resources/calendar/src/java/org/wyona/yanel/impl/resources/calendar/CalendarEvent.java	                        (rev 0)
+++ public/yanel/trunk/src/contributions/resources/calendar/src/java/org/wyona/yanel/impl/resources/calendar/CalendarEvent.java	2008-08-07 08:30:39 UTC (rev 37845)
@@ -0,0 +1,159 @@
+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\"?>\n\n");
+        sb.append("<cal:event xmlns:cal=\"http://...\" created=\"" + created + "\" last-modified=\"" + lastModified + "\" dtstamp=\"" + dtstamp + "\" uid=\"" + uid + "\"");
+	if (_class != null) sb.append(" class=\"" + _class + "\"");
+	if (categories != null) sb.append(" categories=\"" + categories + "\"");
+	sb.append(">\n");
+        sb.append("  <cal:summary>" + summary + "</cal:summary>\n");
+        if (location != null) sb.append("  <cal:location>" + location + "</cal:location>\n");
+        sb.append("  <cal:dtstart value=\"DATE\" tzid=\"" + start + "\"/>\n");
+        sb.append("  <cal:dtend value=\"DATE\" tzid=\"" + end + "\"/>\n");
+        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 if (icsLine.startsWith("DESCRIPTION:")) {
+            log.warn("DESCRIPTION tag not implemented yet: " + icsLine.split(":")[1]);
+        } else if (icsLine.startsWith("ATTENDEE:")) {
+            log.warn("ATTENDEE tag not implemented yet: " + icsLine.split(":")[1]);
+        } else if (icsLine.startsWith("PRIORITY:")) {
+            log.warn("PRIORITY tag not implemented yet: " + icsLine.split(":")[1]);
+        } else if (icsLine.startsWith("STATUS:")) {
+            log.warn("STATUS tag not implemented yet: " + icsLine.split(":")[1]);
+        } else {
+            log.warn("Not implemented yet (probably some text with line-breaks!): " + icsLine);
+        }
+    }
+}

Copied: public/yanel/trunk/src/contributions/resources/calendar/src/java/org/wyona/yanel/impl/resources/calendar/CalendarResource.java (from rev 37843, public/yanel/trunk/src/contributions/resources/calendar/src/java/org/wyona/yanel/impl/resources/CalendarResource.java)
===================================================================
--- public/yanel/trunk/src/contributions/resources/calendar/src/java/org/wyona/yanel/impl/resources/calendar/CalendarResource.java	                        (rev 0)
+++ public/yanel/trunk/src/contributions/resources/calendar/src/java/org/wyona/yanel/impl/resources/calendar/CalendarResource.java	2008-08-07 08:30:39 UTC (rev 37845)
@@ -0,0 +1,362 @@
+/*
+ * Copyright 2006 Wyona
+ */
+
+package org.wyona.yanel.impl.resources;
+
+import org.wyona.yanel.core.Resource;
+import org.wyona.yanel.core.api.attributes.CreatableV2;
+import org.wyona.yanel.core.api.attributes.ModifiableV2;
+import org.wyona.yanel.core.api.attributes.ViewableV2;
+import org.wyona.yanel.core.attributes.viewable.View;
+import org.wyona.yanel.core.attributes.viewable.ViewDescriptor;
+
+import org.wyona.yanel.impl.resources.CalendarEvent;
+
+import org.apache.log4j.Category;
+
+import java.io.File;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
+import java.util.HashMap;
+
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.transform.stream.StreamResult;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * Calendar resource based on the ICS format
+ */
+public class CalendarResource extends Resource implements ViewableV2, ModifiableV2, CreatableV2 {
+
+    private static Category log = Category.getInstance(CalendarResource.class);
+
+    /**
+     *
+     */
+    public CalendarResource() {
+    }
+
+    /**
+     *
+     */
+    public long getSize() throws Exception {
+        log.warn("Not implemented yet!");
+        return -1;
+    }
+
+    /**
+     *
+     */
+    public String getMimeType(String viewId) {
+        if(viewId == null) {
+            String mimeType = null;
+            try {
+                mimeType = getResourceConfigProperty("mime-type");
+            } catch (Exception e) {
+                log.error(e, e);
+            }
+            if (mimeType != null) {
+                return mimeType;
+            } else {
+                return "text/calendar";
+            }
+        }
+        if(viewId != null && viewId.equals("xml")) {
+            return "application/xml";
+        }
+        if (viewId != null && viewId.equals("xhtml")) {
+            return "application/xhtml+xml";
+        }
+        log.warn("No mime type for view id: " + viewId);
+        return null;
+    }
+
+    /**
+     *
+     */
+    public boolean exists() throws Exception {
+        log.warn("Not implemented yet!");
+        return false;
+    }
+
+    /**
+     * Generate view
+     * @param viewId xml, ics, xhtml
+     */
+    public View getView(String viewId) throws Exception {
+        String categories = getResourceConfigProperty("categories");
+        String classes = getResourceConfigProperty("classes");
+        String userIds = getResourceConfigProperty("user-ids");
+        log.error("DEBUG: " + categories + " " + classes + " " + userIds);
+
+        org.wyona.yarep.core.Repository dataRepo = getRealm().getRepository();
+
+        if (dataRepo.exists(new org.wyona.yarep.core.Path(getPath())) && dataRepo.isResource(new org.wyona.yarep.core.Path(getPath()))) {
+            log.error("DEBUG: ICS exists: " + new org.wyona.yarep.core.Path(getPath()));
+            if(viewId == null) {
+                View view = new View();
+                //view.setResponse(false);
+                view.setMimeType(getMimeType(null));
+                view.setInputStream(dataRepo.getInputStream(new org.wyona.yarep.core.Path(getPath())));
+                log.error("DEBUG: Return ICS!");
+                return view;
+            }
+        }
+
+
+        String eventsPath = getResourceConfigProperty("events-path");
+        if (eventsPath == null) {
+            eventsPath = getPath();
+        }
+        log.error("DEBUG: Generate calendar from XML based events: " + eventsPath);
+
+        org.wyona.yarep.core.Path[] children = dataRepo.getChildren(new org.wyona.yarep.core.Path(eventsPath));
+        //org.wyona.yarep.core.Path[] children = dataRepo.search("categories", categories);
+
+        StringBuffer calendar = new StringBuffer("<?xml version=\"1.0\"?>\n<calendar>");
+        for (int i = 0; i < children.length; i++) {
+            if (dataRepo.isResource(children[i])) {
+                InputStream in = dataRepo.getInputStream(children[i]);
+                java.io.ByteArrayOutputStream baos  = new java.io.ByteArrayOutputStream();
+                //StringBuffer event = new StringBuffer();
+
+                byte[] buffer = new byte[8192];
+                int bytesRead;
+                while ((bytesRead = in.read(buffer)) != -1) {
+                    //event.append(new String(buffer));
+                    baos.write(buffer, 0, bytesRead);
+                }
+
+                String event = baos.toString();
+                int endOfProcessingInstruction = event.indexOf("?>");
+                if (endOfProcessingInstruction > 0) {
+                    calendar.append(event.toString().substring(endOfProcessingInstruction + 2));
+                } else {
+                    log.error("No processing instruction: " + children[i]);
+                }
+            }
+        }
+        calendar.append("</calendar>");
+
+        if(viewId != null && viewId.equals("xml")) {
+            //response.getOutputStream();
+
+            View view = new View();
+            //view.setResponse(false);
+            view.setMimeType(getMimeType(viewId));
+            view.setInputStream(new java.io.StringBufferInputStream(calendar.toString()));
+            return view;
+        } else if (viewId != null && viewId.equals("xhtml")) {
+            String xslt = getRTD().getConfigFile().getParent() + File.separator + "xslt" + File.separator + "xml2xhtml.xsl";
+            java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
+            try {
+                Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new File(xslt)));
+                transformer.transform(new StreamSource(new java.io.StringBufferInputStream(calendar.toString())), new StreamResult(out));
+            } catch(Exception e) {
+                log.error(e.getMessage(), e);
+            }
+
+            View view = new View();
+            view.setMimeType(getMimeType(viewId));
+
+            view.setInputStream(new java.io.ByteArrayInputStream(out.toByteArray()));
+            return view;
+        } else {
+            String xslt = getRTD().getConfigFile().getParent() + File.separator + "xslt" + File.separator + "xml2ics.xsl";
+            java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
+            try {
+                Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new File(xslt)));
+
+                // TODO: This will cause encoding problems. See for instance http://skew.org/xml/tutorial/
+                transformer.transform(new StreamSource(new java.io.StringBufferInputStream(calendar.toString())), new StreamResult(out));
+            } catch(Exception e) {
+                log.error(e.getMessage(), e);
+                throw new Exception(e);
+            }
+
+            //response.getOutputStream();
+
+            View view = new View();
+            //view.setResponse(false);
+            view.setMimeType(getMimeType(null));
+            view.setInputStream(new java.io.ByteArrayInputStream(out.toByteArray()));
+            return view;
+        }
+    }
+
+    /**
+     *
+     */
+    public ViewDescriptor[] getViewDescriptors() {
+        log.warn("Not implemented yet!");
+        return null;
+    }
+
+    /**
+     *
+     */
+    public boolean delete() throws Exception {
+        log.warn("Not implemented yet!");
+        return false;
+    }
+
+    /**
+     *
+     */
+    public long getLastModified() throws Exception {
+        log.warn("Not implemented yet!");
+        return -1;
+    }
+
+    /**
+     * Parse ICS and write events as XML into repository
+     */
+    public void write(InputStream in) throws Exception {
+        java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(writeICS(in)));
+        //java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(in));
+        String line;
+        CalendarEvent event = null;
+        log.error("DEBUG: Parse ICS and write events as XML into repository ...");
+        while ((line = br.readLine()) != null) {
+            if (line.startsWith("BEGIN:VEVENT")) {
+                event = new CalendarEvent();
+            } else if (line.startsWith("END:VEVENT")) {
+                //log.error("DEBUG: Write event " + event.getUID() + ", " + event.toXML());
+                Writer out = getRealm().getRepository().getWriter(new org.wyona.yarep.core.Path(getResourceConfigProperty("events-path") + "/" + event.getUID() + ".xml"));
+                out.write(event.toXML());
+                out.close();
+                event = null;
+            } else {
+                if (event != null) {
+                    event.setProperty(line);
+                } else {
+                    //log.warn("Not implemented yet: " + line);
+                }
+            }
+        }
+        br.close();
+    }
+
+    /**
+     * Save/Write the actual ICS
+     */
+    private InputStream writeICS(InputStream in) throws Exception {
+        log.error("DEBUG: Write ICS as a whole to the repository: " + getPath());
+        org.wyona.yarep.core.Path path = new org.wyona.yarep.core.Path(getPath());
+        OutputStream out = getRealm().getRepository().getOutputStream(path);
+        byte[] buf = new byte[8192];
+        int bytesR;
+        while ((bytesR = in.read(buf)) != -1) {
+            out.write(buf, 0, bytesR);
+        }
+        out.close();
+        in.close();
+        return getRealm().getRepository().getInputStream(path);
+    }
+
+    /**
+     *
+     */
+    public OutputStream getOutputStream() throws Exception {
+        log.warn("Do not use this method but rather write(InputStream)!");
+        return null;
+        //return getRealm().getRepository().getOutputStream(new org.wyona.yarep.core.Path("/calendarTODO.ics"));
+    }
+
+    /**
+     *
+     */
+    public InputStream getInputStream() throws Exception {
+        log.warn("Not implemented yet!");
+        return null;
+    }
+
+    /**
+     *
+     */
+    public Reader getReader() throws Exception {
+        log.warn("Not implemented yet!");
+        return null;
+    }
+
+    /**
+     *
+     */
+    public Writer getWriter() throws Exception {
+        log.warn("Not implemented yet!");
+        return null;
+    }
+
+    /**
+     *
+     */
+    public HashMap createRTIProperties(HttpServletRequest request) {
+        HashMap map = new HashMap();
+        map.put("events-path","/new-calendar");
+        map.put("categories","none");
+        map.put("class","none");
+        map.put("user-ids","none");
+        return map;
+    }
+
+    public String getCreateName(String suggestedName) {
+        return suggestedName;
+    }
+
+    /**
+     *
+     */
+    public void create(HttpServletRequest request) {
+        String newPath = getPath();
+        log.error("DEBUG: New path: " + newPath);
+    }
+
+    /**
+     *
+     */
+    public String getPropertyType(String propertyName) {
+        if (propertyName.equals("classes")) {
+            return CreatableV2.TYPE_SELECT;
+        } else if (propertyName.equals("categories")) {
+            return CreatableV2.TYPE_SELECT;
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * Get default values
+     */
+    public Object getProperty(String name) {
+        if (name.equals("classes")) {
+            HashMap map = new HashMap();
+            map.put("*","*");
+            map.put("public","PUBLIC");
+            map.put("private","PRIVATE");
+            return map;
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     *
+     */
+    public void setProperty(String name, Object value) {
+        log.warn("Not implemented yet!");
+    }
+
+    /**
+     *
+     */
+    public String[] getPropertyNames() {
+        String[] props = {"classes", "categories"};
+        return props;
+    }
+}



More information about the Yanel-commits mailing list