[Bedework-commit] calendarapi r1388 - in trunk: calsvc/src/org/bedework/calsvc calsvci/src/org/bedework/calsvci icalendar/src/org/bedework/icalendar

svnadmin at bedework.org svnadmin at bedework.org
Wed Sep 15 11:05:12 EDT 2010


Author: douglm
Date: 2010-09-15 11:05:11 -0400 (Wed, 15 Sep 2010)
New Revision: 1388

Modified:
   trunk/calsvc/src/org/bedework/calsvc/CalSvc.java
   trunk/calsvci/src/org/bedework/calsvci/CalSvcIPars.java
   trunk/icalendar/src/org/bedework/icalendar/IcalCallback.java
   trunk/icalendar/src/org/bedework/icalendar/IcalTranslator.java
   trunk/icalendar/src/org/bedework/icalendar/SAICalCallback.java
Log:
Pay some attention to the ACCEPT values or the data type in the calendar-data element. Allow for different default content-types for caldav and web service.

Implement generation of XML and produce that when appropriate

Modified: trunk/calsvc/src/org/bedework/calsvc/CalSvc.java
===================================================================
--- trunk/calsvc/src/org/bedework/calsvc/CalSvc.java	2010-09-09 14:12:11 UTC (rev 1387)
+++ trunk/calsvc/src/org/bedework/calsvc/CalSvc.java	2010-09-15 15:05:11 UTC (rev 1388)
@@ -1286,6 +1286,10 @@
     public URIgen getURIgen() throws CalFacadeException {
       return null;
     }
+
+    public boolean getTimezonesByReference() throws CalFacadeException {
+      return pars.getTimezonesByReference();
+    }
   }
 
   /* Remove trailing "/" from user principals.

Modified: trunk/calsvci/src/org/bedework/calsvci/CalSvcIPars.java
===================================================================
--- trunk/calsvci/src/org/bedework/calsvci/CalSvcIPars.java	2010-09-09 14:12:11 UTC (rev 1387)
+++ trunk/calsvci/src/org/bedework/calsvci/CalSvcIPars.java	2010-09-15 15:05:11 UTC (rev 1388)
@@ -67,6 +67,8 @@
    */
   private boolean sessionless;
 
+  private boolean timezonesByReference;
+
   /** True if this is a web application
    */
   private boolean webMode;
@@ -132,6 +134,34 @@
                            null); // dbpars
   }
 
+  /** Return new parameters for caldav.
+   *
+   * @param authUser    String authenticated user of the application
+   * @param allowSuperUser  true to allow superuser mode in non-admin mode
+   * @param dbPars
+   * @param timezonesByReference true if we are not including the full tz specification
+   * @return CalSvcIPars
+   */
+  public static CalSvcIPars getCaldavPars(final String authUser,
+                     final boolean allowSuperUser,
+                     final DbConfig dbPars,
+                     final boolean timezonesByReference) {
+    CalSvcIPars pars = new CalSvcIPars(authUser,
+                                       null,    // calsuite
+                                       false,   // publicAdmin
+                                       allowSuperUser,   // allow SuperUser
+                                       false,  // service
+                                       false,  // adminCanEditAllPublicCategories
+                                       false,  // adminCanEditAllPublicLocations
+                                       false,  // adminCanEditAllPublicSponsors
+                                       true,    // sessionless
+                                       dbPars);
+
+    pars.setTimezonesByReference(timezonesByReference);
+
+    return pars;
+  }
+
   /** Constructor we want for this object.
    *
    * @param authUser    String authenticated user of the application
@@ -262,6 +292,20 @@
   }
 
   /**
+   * @param val boolean true if we are not including the full tz specification..
+   */
+  public void setTimezonesByReference(final boolean val) {
+    timezonesByReference = val;
+  }
+
+  /**
+   * @return true if we are not including the full tz specification
+   */
+  public boolean getTimezonesByReference() {
+    return timezonesByReference;
+  }
+
+  /**
    * @param val boolean true if this is a web client..
    */
   public void setWebMode(final boolean val) {
@@ -329,6 +373,9 @@
                                        getAdminCanEditAllPublicContacts(),
                                        getSessionsless(),
                                        getDbPars());
+
+    pars.setTimezonesByReference(getTimezonesByReference());
+
     return pars;
   }
 }

Modified: trunk/icalendar/src/org/bedework/icalendar/IcalCallback.java
===================================================================
--- trunk/icalendar/src/org/bedework/icalendar/IcalCallback.java	2010-09-09 14:12:11 UTC (rev 1387)
+++ trunk/icalendar/src/org/bedework/icalendar/IcalCallback.java	2010-09-15 15:05:11 UTC (rev 1388)
@@ -176,5 +176,11 @@
    * @throws CalFacadeException
    */
   public URIgen getURIgen() throws CalFacadeException;
+
+  /**
+   * @return true if we are not including the full tz specification
+   * @throws CalFacadeException
+   */
+  public boolean getTimezonesByReference() throws CalFacadeException;
 }
 

Modified: trunk/icalendar/src/org/bedework/icalendar/IcalTranslator.java
===================================================================
--- trunk/icalendar/src/org/bedework/icalendar/IcalTranslator.java	2010-09-09 14:12:11 UTC (rev 1387)
+++ trunk/icalendar/src/org/bedework/icalendar/IcalTranslator.java	2010-09-15 15:05:11 UTC (rev 1388)
@@ -32,16 +32,26 @@
 import org.bedework.icalendar.Icalendar.TimeZoneInfo;
 
 import edu.rpi.cmt.calendar.IcalDefs;
+import edu.rpi.cmt.calendar.PropertyIndex.DataType;
+import edu.rpi.cmt.calendar.PropertyIndex.ParameterInfoIndex;
+import edu.rpi.cmt.calendar.PropertyIndex.PropertyInfoIndex;
 import edu.rpi.cmt.timezones.Timezones;
+import edu.rpi.sss.util.xml.XmlEmit;
+import edu.rpi.sss.util.xml.XmlEmit.NameSpace;
+import edu.rpi.sss.util.xml.tagdefs.XcalTags;
 
 import net.fortuna.ical4j.data.CalendarOutputter;
 import net.fortuna.ical4j.data.CalendarParserImpl;
 import net.fortuna.ical4j.data.ParserException;
 import net.fortuna.ical4j.data.UnfoldingReader;
 import net.fortuna.ical4j.model.Calendar;
+import net.fortuna.ical4j.model.Component;
 import net.fortuna.ical4j.model.ComponentList;
+import net.fortuna.ical4j.model.Parameter;
+import net.fortuna.ical4j.model.ParameterList;
 import net.fortuna.ical4j.model.Property;
 import net.fortuna.ical4j.model.PropertyList;
+import net.fortuna.ical4j.model.Recur;
 import net.fortuna.ical4j.model.TimeZone;
 import net.fortuna.ical4j.model.TimeZoneRegistry;
 import net.fortuna.ical4j.model.component.CalendarComponent;
@@ -49,8 +59,10 @@
 import net.fortuna.ical4j.model.component.VFreeBusy;
 import net.fortuna.ical4j.model.component.VTimeZone;
 import net.fortuna.ical4j.model.component.VToDo;
+import net.fortuna.ical4j.model.property.ExRule;
 import net.fortuna.ical4j.model.property.Method;
 import net.fortuna.ical4j.model.property.ProdId;
+import net.fortuna.ical4j.model.property.RRule;
 import net.fortuna.ical4j.model.property.TzId;
 import net.fortuna.ical4j.model.property.Version;
 
@@ -66,6 +78,8 @@
 import java.util.Iterator;
 import java.util.TreeSet;
 
+import javax.xml.namespace.QName;
+
 /** Object to provide translation between a bedework entity and an Icalendar format.
  *
  * @author Mike Douglass   douglm rpi.edu
@@ -247,6 +261,310 @@
     }
   }
 
+  /** Write a collection of calendar data as xml
+   *
+   * @param vals
+   * @param methodType    int value fromIcalendar
+   * @param xml for output
+   * @throws CalFacadeException
+   */
+  public void writeXmlCalendar(final Collection vals,
+                               final int methodType,
+                               final XmlEmit xml) throws CalFacadeException {
+    try {
+      xml.addNs(new NameSpace(XcalTags.namespace, "X"), true);
+
+      xml.openTag(XcalTags.icalendar);
+      xml.openTag(XcalTags.vcalendar);
+
+      xml.openTag(XcalTags.properties);
+
+      xmlProp(xml, Property.PRODID, XcalTags.text, prodId);
+      xmlProp(xml, Property.VERSION, XcalTags.text,
+              Version.VERSION_2_0.getValue());
+
+      xml.closeTag(XcalTags.properties);
+
+      boolean componentsOpen = false;
+
+      if (!cb.getTimezonesByReference()) {
+        Calendar cal = newIcal(methodType); // To collect timezones
+
+        addIcalTimezones(cal, vals);
+
+        // Emit timezones
+        for (Object o: cal.getComponents()) {
+          if (!(o instanceof VTimeZone)) {
+            continue;
+          }
+
+          if (!componentsOpen) {
+            xml.openTag(XcalTags.components);
+            componentsOpen = true;
+          }
+
+          xmlComponent(xml, (Component)o);
+        }
+      }
+
+      Iterator it = vals.iterator();
+      while (it.hasNext()) {
+        Object o = it.next();
+
+        if (o instanceof EventInfo) {
+          EventInfo ei = (EventInfo)o;
+          BwEvent ev = ei.getEvent();
+
+          EventTimeZonesRegistry tzreg = new EventTimeZonesRegistry(this, ev);
+
+          Component comp;
+          if (ev.getEntityType() == IcalDefs.entityTypeFreeAndBusy) {
+            comp = VFreeUtil.toVFreeBusy(ev);
+          } else {
+            comp = VEventUtil.toIcalComponent(ev, false, tzreg,
+                                              cb.getURIgen());
+          }
+
+          if (!componentsOpen) {
+            xml.openTag(XcalTags.components);
+            componentsOpen = true;
+          }
+
+          xmlComponent(xml, comp);
+        }
+      }
+
+      if (componentsOpen) {
+        xml.closeTag(XcalTags.components);
+      }
+
+      xml.closeTag(XcalTags.vcalendar);
+      xml.closeTag(XcalTags.icalendar);
+    } catch (CalFacadeException cfe) {
+      throw cfe;
+    } catch (Throwable t) {
+      throw new CalFacadeException(t);
+    }
+  }
+
+  private void xmlComponent(final XmlEmit xml,
+                            final Component val) throws CalFacadeException {
+    try {
+      QName tag = openTag(xml, val.getName());
+
+      PropertyList pl = val.getProperties();
+
+      if (pl.size() > 0) {
+        xml.openTag(XcalTags.properties);
+
+        for (Object po: pl) {
+          xmlProperty(xml, (Property)po);
+        }
+        xml.closeTag(XcalTags.properties);
+      }
+
+      ComponentList cl = null;
+
+      if (val instanceof VTimeZone) {
+        cl = ((VTimeZone)val).getObservances();
+      } else if (val instanceof VEvent) {
+        cl = ((VEvent)val).getAlarms();
+      } else if (val instanceof VToDo) {
+        cl = ((VToDo)val).getAlarms();
+      }
+
+      if ((cl != null) && (cl.size() > 0)){
+        xml.openTag(XcalTags.components);
+
+        for (Object o: cl) {
+          xmlComponent(xml, (Component)o);
+        }
+
+        xml.closeTag(XcalTags.components);
+      }
+
+      xml.closeTag(tag);
+    } catch (CalFacadeException cfe) {
+      throw cfe;
+    } catch (Throwable t) {
+      throw new CalFacadeException(t);
+    }
+  }
+
+  private void xmlProperty(final XmlEmit xml,
+                           final Property val) throws CalFacadeException {
+    try {
+      QName tag = openTag(xml, val.getName());
+
+      ParameterList pl = val.getParameters();
+
+      if (pl.size() > 0) {
+        xml.openTag(XcalTags.parameters);
+
+        Iterator pli = pl.iterator();
+        while (pli.hasNext()) {
+          xmlParameter(xml, (Parameter)pli.next());
+        }
+        xml.closeTag(XcalTags.parameters);
+      }
+
+      PropertyInfoIndex pii = PropertyInfoIndex.lookupPname(val.getName());
+
+      QName ptype = XcalTags.text;
+
+      if (pii != null) {
+        DataType dtype = pii.getPtype();
+        if (dtype != null) {
+          ptype = dtype.getXcalType();
+        }
+      }
+
+      if (ptype.equals(XcalTags.recur)) {
+        // Emit individual parts of recur rule
+
+        xml.openTag(ptype);
+
+        Recur r;
+
+        if (val instanceof ExRule) {
+          r = ((ExRule)val).getRecur();
+        } else {
+          r = ((RRule)val).getRecur();
+        }
+
+        xml.property(XcalTags.freq, r.getFrequency());
+        xmlProp(xml, XcalTags.wkst, r.getWeekStartDay());
+        xmlProp(xml, XcalTags.until, r.getUntil().toString());
+        xmlProp(xml, XcalTags.count, String.valueOf(r.getCount()));
+        xmlProp(xml, XcalTags.interval, String.valueOf(r.getInterval()));
+        xmlProp(xml, XcalTags.bymonth, r.getMonthList());
+        xmlProp(xml, XcalTags.byweekno, r.getWeekNoList());
+        xmlProp(xml, XcalTags.byyearday, r.getYearDayList());
+        xmlProp(xml, XcalTags.bymonthday, r.getMonthDayList());
+        xmlProp(xml, XcalTags.byday, r.getDayList());
+        xmlProp(xml, XcalTags.byhour, r.getHourList());
+        xmlProp(xml, XcalTags.byminute, r.getMinuteList());
+        xmlProp(xml, XcalTags.bysecond, r.getSecondList());
+        xmlProp(xml, XcalTags.bysetpos, r.getSetPosList());
+
+        xml.closeTag(ptype);
+      } else {
+        xml.property(ptype, val.getValue());
+      }
+
+      xml.closeTag(tag);
+    } catch (CalFacadeException cfe) {
+      throw cfe;
+    } catch (Throwable t) {
+      throw new CalFacadeException(t);
+    }
+  }
+
+  private QName openTag(final XmlEmit xml,
+                        final String name) throws CalFacadeException {
+    QName tag = new QName(XcalTags.namespace, name.toLowerCase());
+
+    try {
+      xml.openTag(tag);
+
+      return tag;
+    } catch (Throwable t) {
+      throw new CalFacadeException(t);
+    }
+  }
+
+  private void xmlProp(final XmlEmit xml,
+                       final QName tag,
+                       final String val) throws CalFacadeException {
+    if (val == null) {
+      return;
+    }
+
+    try {
+      xml.property(tag, val);
+    } catch (Throwable t) {
+      throw new CalFacadeException(t);
+    }
+  }
+
+  private void xmlProp(final XmlEmit xml,
+                       final QName tag,
+                       final Collection val) throws CalFacadeException {
+    if ((val == null) || val.isEmpty()) {
+      return;
+    }
+
+    try {
+      xml.property(tag, val.toString());
+    } catch (Throwable t) {
+      throw new CalFacadeException(t);
+    }
+  }
+
+  private void xmlProp(final XmlEmit xml,
+                       final String pname,
+                       final QName ptype,
+                       final String val) throws CalFacadeException {
+    QName tag = new QName(XcalTags.namespace, pname.toLowerCase());
+
+    try {
+      xml.openTag(tag);
+      xml.property(ptype, val);
+      xml.closeTag(tag);
+    } catch (Throwable t) {
+      throw new CalFacadeException(t);
+    }
+  }
+
+  private void xmlParameter(final XmlEmit xml,
+                            final Parameter val) throws CalFacadeException {
+    try {
+      ParameterInfoIndex pii = ParameterInfoIndex.lookupPname(val.getName());
+
+      QName ptype = XcalTags.text;
+
+      if (pii != null) {
+        DataType dtype = pii.getPtype();
+        if (dtype != null) {
+          ptype = dtype.getXcalType();
+        }
+      }
+
+      if (ptype.equals(XcalTags.text)) {
+        QName tag = new QName(XcalTags.namespace, val.getName().toLowerCase());
+        xml.property(tag, val.getValue());
+      } else {
+        QName tag = openTag(xml, val.getName());
+        xml.property(ptype, val.getValue());
+        xml.closeTag(tag);
+      }
+    } catch (CalFacadeException cfe) {
+      throw cfe;
+    } catch (Throwable t) {
+      throw new CalFacadeException(t);
+    }
+  }
+
+  /** Write a collection of calendar data as iCalendar
+   *
+   * @param vals
+   * @param methodType    int value fromIcalendar
+   * @param wtr Writer for output
+   * @throws CalFacadeException
+   */
+  public void writeCalendar(final Collection vals,
+                            final int methodType,
+                            final Writer wtr) throws CalFacadeException {
+    try {
+      Calendar cal = toIcal(vals, methodType);
+      writeCalendar(cal, wtr);
+    } catch (CalFacadeException cfe) {
+      throw cfe;
+    } catch (Throwable t) {
+      throw new CalFacadeException(t);
+    }
+  }
+
   /** Turn a collection of events into a calendar
    *
    * @param vals
@@ -542,8 +860,14 @@
 
     if (!ev.getSuppressed()) {
       /* Add it to the calendar */
-      cal.getComponents().add(VEventUtil.toIcalComponent(ev, false, tzreg,
-                                                         uriGen));
+      Component comp;
+
+      if (ev.getEntityType() == IcalDefs.entityTypeFreeAndBusy) {
+        comp = VFreeUtil.toVFreeBusy(ev);
+      } else {
+        comp = VEventUtil.toIcalComponent(ev, false, tzreg, uriGen);
+      }
+      cal.getComponents().add(comp);
     }
 
     if (val.getNumOverrides() == 0) {
@@ -644,6 +968,38 @@
     }
   }
 
+  /* If the start or end dates references a timezone, we retrieve the timezone definition
+   * and add it to the calendar.
+   */
+  private void addIcalTimezones(final Calendar cal,
+                                final Collection vals) throws CalFacadeException {
+    TreeSet<String> added = new TreeSet<String>();
+
+    Iterator it = vals.iterator();
+    while (it.hasNext()) {
+      Object o = it.next();
+
+      if (o instanceof EventInfo) {
+        EventInfo ei = (EventInfo)o;
+        BwEvent ev = ei.getEvent();
+
+        if (!ev.getSuppressed()) {
+          /* Add referenced timezones to the calendar */
+          addIcalTimezones(cal, ev, added,
+                           new EventTimeZonesRegistry(this, ev));
+        }
+
+        if (ei.getNumOverrides() > 0) {
+          for (EventInfo oei: ei.getOverrides()) {
+            ev = oei.getEvent();
+            addIcalTimezones(cal, ev, added,
+                             new EventTimeZonesRegistry(this, ev));
+          }
+        }
+      }
+    }
+  }
+
   /* If the start or end date references a timezone, we retrieve the timezone definition
    * and add it to the calendar.
    */
@@ -660,6 +1016,10 @@
       if (ev.getEndType() == BwEvent.endTypeDate) {
         addIcalTimezone(cal, ev.getDtend().getTzid(), added, tzreg);
       }
+
+      //if (ev.getRecurrenceId() != null) {
+      //  addIcalTimezone(cal, ev.getRecurrenceId().getTzid(), added, tzreg);
+      //}
     }
   }
 

Modified: trunk/icalendar/src/org/bedework/icalendar/SAICalCallback.java
===================================================================
--- trunk/icalendar/src/org/bedework/icalendar/SAICalCallback.java	2010-09-09 14:12:11 UTC (rev 1387)
+++ trunk/icalendar/src/org/bedework/icalendar/SAICalCallback.java	2010-09-15 15:05:11 UTC (rev 1388)
@@ -114,4 +114,8 @@
   public URIgen getURIgen() throws CalFacadeException {
     return null;
   }
+
+  public boolean getTimezonesByReference() throws CalFacadeException {
+    return false;
+  }
 }



More information about the Bedework-commit mailing list