[Bedework-commit] calendarapi r661 - in trunk: calCore/resources/hbms calCore/src/org/bedework/calcore/hibernate calFacade/src/org/bedework/calfacade calFacade/src/org/bedework/calfacade/base calFacade/src/org/bedework/calfacade/wrappers calsvc/src/org/bedework/calsvc/indexing

svnadmin at bedework.org svnadmin at bedework.org
Mon Jul 7 10:29:07 EDT 2008


Author: douglm
Date: 2008-07-07 10:29:05 -0400 (Mon, 07 Jul 2008)
New Revision: 661

Added:
   trunk/calFacade/src/org/bedework/calfacade/BwCollectionLastmod.java
   trunk/calFacade/src/org/bedework/calfacade/base/BwLastMod.java
Modified:
   trunk/calCore/resources/hbms/Calendar.hbm.xml
   trunk/calCore/src/org/bedework/calcore/hibernate/CoreCalendars.java
   trunk/calFacade/src/org/bedework/calfacade/BwCalendar.java
   trunk/calFacade/src/org/bedework/calfacade/wrappers/CalendarWrapper.java
   trunk/calsvc/src/org/bedework/calsvc/indexing/BwIndexLuceneImpl.java
Log:
Changes to avoid source of StaleStateExceptions from hibernate.

Move collection lastmod out of the BwCalendar class into a separate table and class BwLastMod (and BwCollectionLastmod.

Update touchCalendar to only update the lastmod object.


Modified: trunk/calCore/resources/hbms/Calendar.hbm.xml
===================================================================
--- trunk/calCore/resources/hbms/Calendar.hbm.xml	2008-07-02 17:32:47 UTC (rev 660)
+++ trunk/calCore/resources/hbms/Calendar.hbm.xml	2008-07-07 14:29:05 UTC (rev 661)
@@ -83,12 +83,10 @@
 
     <property name="calType" column="caltype" type="integer" not-null="true"/>
 
-    <property name="lastmod" type="string" >
-      <column name="bw_lastmod" length="16" not-null="true" />
-    </property>
+    <one-to-one name="lastmod"
+                class="org.bedework.calfacade.BwCollectionLastmod"
+                cascade="save-update,delete"/>
 
-    <property name="sequence"  column="bwsequence" type="integer" />
-
     <property name="created" type="string">
       <column name="bw_created" length="16" not-null="true" />
     </property>
@@ -137,6 +135,31 @@
        calendar queries
        ================================================================= -->
 
+  <class name="org.bedework.calfacade.BwCollectionLastmod"
+         table="bw_collection_lastmods" >
+    <cache usage="read-write"/>
+
+    <id name="id" column="bw_id">
+      <generator class="foreign">
+        <param name="property">dbEntity</param>
+      </generator>
+    </id>
+
+    <one-to-one name="dbEntity"
+                class="org.bedework.calfacade.BwCalendar"
+                constrained="true"/>
+
+    <property name="timestamp" type="string" >
+      <column name="bw_timestamp" length="16" not-null="true" />
+    </property>
+
+    <property name="sequence"  column="bw_sequence" type="integer" />
+  </class>
+
+  <!-- =================================================================
+       calendar queries
+       ================================================================= -->
+
   <query name="getNamedCalendars"><![CDATA[
     from org.bedework.calfacade.BwCalendar as cal
       where cal.name=:name

Modified: trunk/calCore/src/org/bedework/calcore/hibernate/CoreCalendars.java
===================================================================
--- trunk/calCore/src/org/bedework/calcore/hibernate/CoreCalendars.java	2008-07-02 17:32:47 UTC (rev 660)
+++ trunk/calCore/src/org/bedework/calcore/hibernate/CoreCalendars.java	2008-07-07 14:29:05 UTC (rev 661)
@@ -32,6 +32,7 @@
 import org.bedework.calfacade.BwCalendar;
 import org.bedework.calfacade.BwSystem;
 import org.bedework.calfacade.BwUser;
+import org.bedework.calfacade.base.BwLastMod;
 import org.bedework.calfacade.exc.CalFacadeException;
 import org.bedework.calfacade.sysevents.SysEvent;
 
@@ -180,9 +181,11 @@
     // CALWRAPPER - if we're not cloning can we avoid this?
     //val = (BwCalendar)getSess().merge(val);
 
-    val = (BwCalendar)getSess().merge(val);
-    val.updateLastmod();
-    getSess().update(val);
+    //val = (BwCalendar)getSess().merge(val);
+
+    BwLastMod lm = val.getLastmod();
+    lm.updateLastmod();
+    getSess().update(lm);
   }
 
   /* ====================================================================

Modified: trunk/calFacade/src/org/bedework/calfacade/BwCalendar.java
===================================================================
--- trunk/calFacade/src/org/bedework/calfacade/BwCalendar.java	2008-07-02 17:32:47 UTC (rev 660)
+++ trunk/calFacade/src/org/bedework/calfacade/BwCalendar.java	2008-07-07 14:29:05 UTC (rev 661)
@@ -55,6 +55,7 @@
 
 import org.bedework.calfacade.annotations.Wrapper;
 import org.bedework.calfacade.annotations.ical.NoProxy;
+import org.bedework.calfacade.base.BwLastMod;
 import org.bedework.calfacade.base.BwShareableContainedDbentity;
 import org.bedework.calfacade.base.CategorisedEntity;
 import org.bedework.calfacade.base.CollatableEntity;
@@ -65,9 +66,6 @@
 import edu.rpi.sss.util.xml.tagdefs.AppleIcalTags;
 import edu.rpi.sss.util.xml.tagdefs.CaldavTags;
 
-import net.fortuna.ical4j.model.DateTime;
-import net.fortuna.ical4j.model.property.LastModified;
-
 import java.net.URLEncoder;
 import java.util.Collection;
 import java.util.Date;
@@ -242,13 +240,8 @@
   /** UTC datetime */
   private String created;
 
-  /** UTC datetime */
-  private String lastmod;
+  private BwLastMod lastmod;
 
-  /** Ensure uniqueness - lastmod only down to second.
-   */
-  private int sequence;
-
   private Set<BwCategory> categories = null;
 
   private Collection<BwProperty> properties;
@@ -281,7 +274,7 @@
     /* Set the lastmod and created */
 
     Date dt = new Date();
-    setLastmod(DateTimeUtil.isoDateTimeUTC(dt));
+    setLastmod(new BwCollectionLastmod(this, dt));
     setCreated(DateTimeUtil.isoDateTimeUTC(dt));
   }
 
@@ -417,33 +410,17 @@
   /**
    * @param val
    */
-  public void setLastmod(String val) {
+  public void setLastmod(BwLastMod val) {
     lastmod = val;
   }
 
   /**
-   * @return String lastmod
+   * @return BwLastMod lastmod
    */
-  public String getLastmod() {
+  public BwLastMod getLastmod() {
     return lastmod;
   }
 
-  /** Set the sequence
-   *
-   * @param val    sequence number
-   */
-  public void setSequence(int val) {
-    sequence = val;
-  }
-
-  /** Get the sequence
-   *
-   * @return int    the sequence
-   */
-  public int getSequence() {
-    return sequence;
-  }
-
   /** Set the refresh rate in seconds
    *
    * @param val    type
@@ -977,7 +954,6 @@
     cal.setCalType(getCalType());
     cal.setCreated(getCreated());
     cal.setLastmod(getLastmod());
-    cal.setSequence(getSequence());
     cal.setCategories(getCategories());
     cal.setProperties(getProperties());
     cal.setAliasUri(getAliasUri());
@@ -996,8 +972,7 @@
   /** Update last mod fields
    */
   public void updateLastmod() {
-    setLastmod(new LastModified(new DateTime(true)).getValue());
-    setSequence(getSequence() + 1);
+    getLastmod().updateLastmod();
   }
 
   /** Set the single valued named property
@@ -1131,8 +1106,6 @@
     sb.append(getCreated());
     sb.append(", lastmod=");
     sb.append(getLastmod());
-    sb.append(", sequence=");
-    sb.append(getSequence());
 
     if (getNumCategories() > 0) {
       sb.append(",\n categories=");
@@ -1164,7 +1137,6 @@
     cal.setCalType(getCalType());
     cal.setCreated(getCreated());
     cal.setLastmod(getLastmod());
-    cal.setSequence(getSequence());
     cal.setCategories(getCategories());
     cal.setProperties(getProperties());
     cal.setAliasUri(getAliasUri());

Added: trunk/calFacade/src/org/bedework/calfacade/BwCollectionLastmod.java
===================================================================
--- trunk/calFacade/src/org/bedework/calfacade/BwCollectionLastmod.java	                        (rev 0)
+++ trunk/calFacade/src/org/bedework/calfacade/BwCollectionLastmod.java	2008-07-07 14:29:05 UTC (rev 661)
@@ -0,0 +1,60 @@
+/* **********************************************************************
+    Copyright 2006 Rensselaer Polytechnic Institute. All worldwide rights reserved.
+
+    Redistribution and use of this distribution in source and binary forms,
+    with or without modification, are permitted provided that:
+       The above copyright notice and this permission notice appear in all
+        copies and supporting documentation;
+
+        The name, identifiers, and trademarks of Rensselaer Polytechnic
+        Institute are not used in advertising or publicity without the
+        express prior written permission of Rensselaer Polytechnic Institute;
+
+    DISCLAIMER: The software is distributed" AS IS" without any express or
+    implied warranty, including but not limited to, any implied warranties
+    of merchantability or fitness for a particular purpose or any warrant)'
+    of non-infringement of any current or pending patent rights. The authors
+    of the software make no representations about the suitability of this
+    software for any particular purpose. The entire risk as to the quality
+    and performance of the software is with the user. Should the software
+    prove defective, the user assumes the cost of all necessary servicing,
+    repair or correction. In particular, neither Rensselaer Polytechnic
+    Institute, nor the authors of the software are liable for any indirect,
+    special, consequential, or incidental damages related to the software,
+    to the maximum extent the law permits.
+*/
+package org.bedework.calfacade;
+
+import org.bedework.calfacade.base.BwLastMod;
+
+import java.util.Date;
+
+/** Concrete class so we can map this in hibernate
+ *
+ * @author Mike Douglass
+ * @version 1.0
+ */
+public class BwCollectionLastmod extends BwLastMod<BwCalendar> {
+  /** No arg constructor for hibernate
+   *
+   */
+  public BwCollectionLastmod() {
+    super(null);
+  }
+
+  /** No date constructor
+   *
+   * @param dbEntity
+   */
+  public BwCollectionLastmod(BwCalendar dbEntity) {
+    super(dbEntity);
+  }
+
+  /** Constructor to set last mod
+   * @param dbEntity
+   * @param dt
+   */
+  public BwCollectionLastmod(BwCalendar dbEntity, Date dt) {
+    super(dbEntity, dt);
+  }
+}

Added: trunk/calFacade/src/org/bedework/calfacade/base/BwLastMod.java
===================================================================
--- trunk/calFacade/src/org/bedework/calfacade/base/BwLastMod.java	                        (rev 0)
+++ trunk/calFacade/src/org/bedework/calfacade/base/BwLastMod.java	2008-07-07 14:29:05 UTC (rev 661)
@@ -0,0 +1,218 @@
+/* **********************************************************************
+    Copyright 2006 Rensselaer Polytechnic Institute. All worldwide rights reserved.
+
+    Redistribution and use of this distribution in source and binary forms,
+    with or without modification, are permitted provided that:
+       The above copyright notice and this permission notice appear in all
+        copies and supporting documentation;
+
+        The name, identifiers, and trademarks of Rensselaer Polytechnic
+        Institute are not used in advertising or publicity without the
+        express prior written permission of Rensselaer Polytechnic Institute;
+
+    DISCLAIMER: The software is distributed" AS IS" without any express or
+    implied warranty, including but not limited to, any implied warranties
+    of merchantability or fitness for a particular purpose or any warrant)'
+    of non-infringement of any current or pending patent rights. The authors
+    of the software make no representations about the suitability of this
+    software for any particular purpose. The entire risk as to the quality
+    and performance of the software is with the user. Should the software
+    prove defective, the user assumes the cost of all necessary servicing,
+    repair or correction. In particular, neither Rensselaer Polytechnic
+    Institute, nor the authors of the software are liable for any indirect,
+    special, consequential, or incidental damages related to the software,
+    to the maximum extent the law permits.
+*/
+package org.bedework.calfacade.base;
+
+import org.bedework.calfacade.CalFacadeDefs;
+import org.bedework.calfacade.annotations.NoWrap;
+import org.bedework.calfacade.util.DateTimeUtil;
+
+import net.fortuna.ical4j.model.DateTime;
+import net.fortuna.ical4j.model.property.LastModified;
+
+import java.io.Serializable;
+import java.util.Date;
+
+/** This is used to store the last modification times for some entities. We do
+ * this to avoid the overhead and errors caused by versioning when not needed.
+ *
+ * <p>For example, we frequently update a calendar collections last mod when
+ * events are changed. We need this lastmod to flag collection that have changed,
+ * on the other hand we don't need the possibility of StaleStateExceptions.
+ *
+ * <p>The touch method will update this object only and save it avoiding most
+ * of tha overhead.
+ *
+ * @author Mike Douglass
+ * @version 1.0
+ * @param <T>
+ */
+public class BwLastMod<T extends BwDbentity>
+    implements Comparable<BwLastMod>, Serializable {
+  private int id = CalFacadeDefs.unsavedItemKey;
+
+  private T dbEntity;
+
+  /** UTC datetime */
+  private String timestamp;
+
+  /** Ensure uniqueness - lastmod only down to second.
+   */
+  private int sequence;
+
+  /** No date constructor
+   *
+   * @param dbEntity
+   */
+  public BwLastMod(T dbEntity) {
+    this.dbEntity = dbEntity;
+  }
+
+  /** Constructor to set last mod
+   * @param dbEntity
+   * @param dt
+   */
+  public BwLastMod(T dbEntity, Date dt) {
+    this(dbEntity);
+    setTimestamp(DateTimeUtil.isoDateTimeUTC(dt));
+  }
+
+  /**
+   * @param val
+   */
+  public void setId(int val) {
+    id = val;
+  }
+
+  /**
+   * @return int id
+   */
+  public int getId() {
+    return id;
+  }
+
+  /**
+   * @param val
+   */
+  public void setDbEntity(T val) {
+    dbEntity = val;
+  }
+
+  /**
+   * @return T
+   */
+  public T getDbEntity() {
+    return dbEntity;
+  }
+
+  /**
+   * @param val
+   */
+  public void setTimestamp(String val) {
+    timestamp = val;
+  }
+
+  /**
+   * @return String lastmod
+   */
+  public String getTimestamp() {
+    return timestamp;
+  }
+
+  /** Set the sequence
+   *
+   * @param val    sequence number
+   */
+  public void setSequence(int val) {
+    sequence = val;
+  }
+
+  /** Get the sequence
+   *
+   * @return int    the sequence
+   */
+  public int getSequence() {
+    return sequence;
+  }
+
+  /**
+   * @return true if this entity is not saved.
+   */
+  public boolean unsaved() {
+    return getId() == CalFacadeDefs.unsavedItemKey;
+  }
+
+  /* ====================================================================
+   *                   Convenience methods
+   * ==================================================================== */
+
+  /** Update last mod fields
+   */
+  public void updateLastmod() {
+    setTimestamp(new LastModified(new DateTime(true)).getValue());
+    setSequence(getSequence() + 1);
+  }
+
+  /** Return a value that can be used for etag and ctag support
+   *
+   * @return String tag value
+   */
+  public String getTagValue() {
+    return getTimestamp() + getSequence();
+  }
+
+  /** Add our stuff to the StringBuilder
+   *
+   * @param sb    StringBuilder for result
+   */
+  protected void toStringSegment(StringBuilder sb) {
+    sb.append("id=");
+    sb.append(getId());
+    sb.append(", timestamp=");
+    sb.append(getTimestamp());
+    sb.append(", sequence=");
+    sb.append(getSequence());
+  }
+
+  /* ====================================================================
+   *                   Object methods
+   * The following are required for a db object.
+   * ==================================================================== */
+
+  /** Make visible
+   * @return Object of class BwLastMod
+   */
+  @NoWrap
+  public Object clone() {
+    return null;
+  }
+
+  /* (non-Javadoc)
+   * @see java.lang.Comparable#compareTo(java.lang.Object)
+   */
+  @NoWrap
+  public int compareTo(BwLastMod that) {
+    if (this == that) {
+      return 0;
+    }
+
+    return getTagValue().compareTo(that.getTagValue());
+  }
+
+  @NoWrap
+  public int hashCode() {
+    return getTagValue().hashCode();
+  }
+
+  /* We always use the compareTo method
+   */
+  public boolean equals(Object obj) {
+    if (this == obj) {
+      return true;
+    }
+
+    return compareTo((BwLastMod)obj) == 0;
+  }
+}

Modified: trunk/calFacade/src/org/bedework/calfacade/wrappers/CalendarWrapper.java
===================================================================
--- trunk/calFacade/src/org/bedework/calfacade/wrappers/CalendarWrapper.java	2008-07-02 17:32:47 UTC (rev 660)
+++ trunk/calFacade/src/org/bedework/calfacade/wrappers/CalendarWrapper.java	2008-07-07 14:29:05 UTC (rev 661)
@@ -29,6 +29,7 @@
 import org.bedework.calfacade.BwCategory;
 import org.bedework.calfacade.BwProperty;
 import org.bedework.calfacade.BwUser;
+import org.bedework.calfacade.base.BwLastMod;
 import org.bedework.calfacade.exc.CalFacadeException;
 import org.bedework.calfacade.util.AccessUtilI;
 
@@ -304,7 +305,7 @@
   /**
    * @param val
    */
-  public void setLastmod(String val) {
+  public void setLastmod(BwLastMod val) {
     // Can come from constructor
     if (entity != null) {
       entity.setLastmod(val);
@@ -312,27 +313,13 @@
   }
 
   /**
-   * @return String lastmod
+   * @return BwLastMod lastmod
    */
-  public String getLastmod() {
+  public BwLastMod getLastmod() {
     return entity.getLastmod();
   }
 
   /* (non-Javadoc)
-   * @see org.bedework.calfacade.BwCalendar#setSequence(int)
-   */
-  public void setSequence(int val) {
-    entity.setSequence(val);
-  }
-
-  /* (non-Javadoc)
-   * @see org.bedework.calfacade.BwCalendar#getSequence()
-   */
-  public int getSequence() {
-    return entity.getSequence();
-  }
-
-  /* (non-Javadoc)
    * @see org.bedework.calfacade.BwCalendar#setAliasUri(java.lang.String)
    */
   public void setAliasUri(String val) {

Modified: trunk/calsvc/src/org/bedework/calsvc/indexing/BwIndexLuceneImpl.java
===================================================================
--- trunk/calsvc/src/org/bedework/calsvc/indexing/BwIndexLuceneImpl.java	2008-07-02 17:32:47 UTC (rev 660)
+++ trunk/calsvc/src/org/bedework/calsvc/indexing/BwIndexLuceneImpl.java	2008-07-07 14:29:05 UTC (rev 661)
@@ -386,7 +386,7 @@
       created = cal.getCreated();
       creator = cal.getCreator();
       description = cal.getDescription();
-      lastmod = cal.getLastmod();
+      lastmod = cal.getLastmod().getTimestamp();
       owner = cal.getOwner();
       summary = cal.getSummary();
     } else if (o instanceof BwEvent) {
@@ -581,7 +581,7 @@
       created = cal.getCreated();
       creator = cal.getCreator();
       description = cal.getDescription();
-      lastmod = cal.getLastmod();
+      lastmod = cal.getLastmod().getTimestamp();
       owner = cal.getOwner();
       summary = cal.getSummary();
     } else if (o instanceof BwEvent) {



More information about the Bedework-commit mailing list