Author: ieb
Date: Wed Dec 10 21:05:43 2008
New Revision: 725581

URL: http://svn.apache.org/viewvc?rev=725581&view=rev
Log:
         SHINDIG-773

Patch from Chico Charlesworth

Patch fixes three separate issues with persisting ActivityDb:
 - Fixed NullPointerException by defaulting templateParamsDb variable to a new 
ConcurrentHashMap in ActivityDb.
 - Fixed TransientObjectException (object references an unsaved transient 
instance - save the transient instance before flushing: 
org.apache.shindig.social.opensocial.jpa.MediaItemDb) by adding cascade = all 
in @ManyToMany for mediaItems variable in ActivityDb
 - Fixed IllegalArgumentException (can not set long field 
org.apache.shindig.social.opensocial.jpa.ActivityDb.objectId to 
java.util.ArrayList) by fixing the @ManyToOne relationship in 
ActivityTemplateParamsDb.

Thanks

Modified:
    
incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ActivityDb.java
    
incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ActivityTemplateParamsDb.java

Modified: 
incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ActivityDb.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ActivityDb.java?rev=725581&r1=725580&r2=725581&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ActivityDb.java
 (original)
+++ 
incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ActivityDb.java
 Wed Dec 10 21:05:43 2008
@@ -17,12 +17,20 @@
  */
 package org.apache.shindig.social.opensocial.jpa;
 
+import static javax.persistence.CascadeType.ALL;
 import static javax.persistence.GenerationType.IDENTITY;
 
 import org.apache.shindig.social.opensocial.jpa.api.DbObject;
 import org.apache.shindig.social.opensocial.model.Activity;
 import org.apache.shindig.social.opensocial.model.MediaItem;
 
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.concurrent.ConcurrentHashMap;
+
 import javax.persistence.Basic;
 import javax.persistence.Column;
 import javax.persistence.Entity;
@@ -41,14 +49,6 @@
 import javax.persistence.Transient;
 import javax.persistence.Version;
 
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.concurrent.ConcurrentHashMap;
-import static javax.persistence.CascadeType.ALL;
-
 /**
  * Activity model object stored in the "activity" table.
  */
@@ -131,11 +131,11 @@
 
   /**
    * A list of shared media items associated with this activity, joined by the 
table
-   * "activity_media" such that activity_media.activity_id = activity.oid and 
+   * "activity_media" such that activity_media.activity_id = activity.oid and
    * activity_media.media_id = media.oid. Media items may be shared amongst
    * many activities or other entities.
    */
-  @ManyToMany(targetEntity = MediaItemDb.class)
+  @ManyToMany(targetEntity = MediaItemDb.class, cascade = ALL)
   @JoinTable(name = "activity_media",
       joinColumns = @JoinColumn
       ( name = "activity_id", referencedColumnName = "oid"),
@@ -197,9 +197,9 @@
    * unfortunately JPA wont do Map<String,String> so this is handled in the 
prePersist and postLoad
    * hook.
    */
-  @OneToMany(targetEntity = ActivityTemplateParamsDb.class, mappedBy = 
"activities", cascade = ALL)
+  @OneToMany(targetEntity = ActivityTemplateParamsDb.class, mappedBy = 
"activity", cascade = ALL)
   @MapKey(name = "name")
-  protected Map<String, ActivityTemplateParamsDb> templateParamsDb;
+  protected Map<String, ActivityTemplateParamsDb> templateParamsDb = new 
ConcurrentHashMap<String, ActivityTemplateParamsDb>();
 
   /**
    * The transient store for templateParamers loaded by the postLoad hook and 
persisted by the
@@ -248,7 +248,7 @@
     this.userId = userId;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see org.apache.shindig.social.opensocial.model.Activity#getAppId()
    */
@@ -256,7 +256,7 @@
     return appId;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#setAppId(java.lang.String)
    */
@@ -264,7 +264,7 @@
     this.appId = appId;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see org.apache.shindig.social.opensocial.model.Activity#getBody()
    */
@@ -272,7 +272,7 @@
     return body;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#setBody(java.lang.String)
    */
@@ -280,7 +280,7 @@
     this.body = body;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see org.apache.shindig.social.opensocial.model.Activity#getBodyId()
    */
@@ -288,7 +288,7 @@
     return bodyId;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#setBodyId(java.lang.String)
    */
@@ -296,7 +296,7 @@
     this.bodyId = bodyId;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see org.apache.shindig.social.opensocial.model.Activity#getExternalId()
    */
@@ -304,7 +304,7 @@
     return externalId;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#setExternalId(java.lang.String)
    */
@@ -312,7 +312,7 @@
     this.externalId = externalId;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see org.apache.shindig.social.opensocial.model.Activity#getId()
    */
@@ -320,7 +320,7 @@
     return id;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#setId(java.lang.String)
    */
@@ -328,7 +328,7 @@
     this.id = id;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see org.apache.shindig.social.opensocial.model.Activity#getUpdated()
    */
@@ -339,7 +339,7 @@
     return new Date(updated.getTime());
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#setUpdated(java.util.Date)
    */
@@ -351,7 +351,7 @@
     }
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see org.apache.shindig.social.opensocial.model.Activity#getMediaItems()
    */
@@ -359,7 +359,7 @@
     return mediaItems;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#setMediaItems(java.util.List)
    */
@@ -367,7 +367,7 @@
     this.mediaItems = mediaItems;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see org.apache.shindig.social.opensocial.model.Activity#getPostedTime()
    */
@@ -375,7 +375,7 @@
     return postedTime;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#setPostedTime(java.lang.Long)
    */
@@ -383,7 +383,7 @@
     this.postedTime = postedTime;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see org.apache.shindig.social.opensocial.model.Activity#getPriority()
    */
@@ -391,7 +391,7 @@
     return priority;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#setPriority(java.lang.Float)
    */
@@ -399,7 +399,7 @@
     this.priority = priority;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#getStreamFaviconUrl()
    */
@@ -407,7 +407,7 @@
     return streamFaviconUrl;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#setStreamFaviconUrl(java.lang.String)
    */
@@ -415,7 +415,7 @@
     this.streamFaviconUrl = streamFaviconUrl;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#getStreamSourceUrl()
    */
@@ -423,7 +423,7 @@
     return streamSourceUrl;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#setStreamSourceUrl(java.lang.String)
    */
@@ -431,7 +431,7 @@
     this.streamSourceUrl = streamSourceUrl;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see org.apache.shindig.social.opensocial.model.Activity#getStreamTitle()
    */
@@ -439,7 +439,7 @@
     return streamTitle;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#setStreamTitle(java.lang.String)
    */
@@ -447,7 +447,7 @@
     this.streamTitle = streamTitle;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see org.apache.shindig.social.opensocial.model.Activity#getStreamUrl()
    */
@@ -455,7 +455,7 @@
     return streamUrl;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#setStreamUrl(java.lang.String)
    */
@@ -463,7 +463,7 @@
     this.streamUrl = streamUrl;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#getTemplateParams()
    */
@@ -471,7 +471,7 @@
     return templateParams;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#setTemplateParams(java.util.Map)
    */
@@ -479,7 +479,7 @@
     this.templateParams = templateParams;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see org.apache.shindig.social.opensocial.model.Activity#getTitle()
    */
@@ -487,7 +487,7 @@
     return title;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#setTitle(java.lang.String)
    */
@@ -495,7 +495,7 @@
     this.title = title;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see org.apache.shindig.social.opensocial.model.Activity#getTitleId()
    */
@@ -503,7 +503,7 @@
     return titleId;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#setTitleId(java.lang.String)
    */
@@ -511,7 +511,7 @@
     this.titleId = titleId;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see org.apache.shindig.social.opensocial.model.Activity#getUrl()
    */
@@ -519,7 +519,7 @@
     return url;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#setUrl(java.lang.String)
    */
@@ -527,7 +527,7 @@
     this.url = url;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see org.apache.shindig.social.opensocial.model.Activity#getUserId()
    */
@@ -535,7 +535,7 @@
     return userId;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see 
org.apache.shindig.social.opensocial.model.Activity#setUserId(java.lang.String)
    */
@@ -543,7 +543,7 @@
     this.userId = userId;
   }
 
-  /** 
+  /**
    * [EMAIL PROTECTED]
    * @see org.apache.shindig.social.opensocial.jpa.api.DbObject#getObjectId()
    */
@@ -557,6 +557,7 @@
    */
   @PrePersist
   public void populateDbFields() {
+    System.out.println("---------------- populateDbFields");
     // add new entries
     for (Entry<String, String> e : templateParams.entrySet()) {
       ActivityTemplateParamsDb a = templateParamsDb.get(e.getKey());
@@ -564,8 +565,9 @@
         a = new ActivityTemplateParamsDb();
         a.name = e.getKey();
         a.value = e.getValue();
-        a.activities = new ArrayList<Activity>();
-        a.activities.add(this);
+        a.activity = this;
+        //a.activities = new ArrayList<Activity>();
+        //a.activities.add(this);
         templateParamsDb.put(e.getKey(), a);
       } else {
         a.value = e.getValue();
@@ -581,6 +583,7 @@
     for (String r : toRemove) {
       templateParamsDb.remove(r);
     }
+    System.out.println("---------------- DONE populateDbFields");
   }
 
   /**
@@ -589,10 +592,12 @@
    */
   @PostLoad
   public void loadTransientFields() {
+    System.out.println("---------------- loadTransientFields");
     templateParams = new ConcurrentHashMap<String, String>();
     for (Entry<String, ActivityTemplateParamsDb> e : 
templateParamsDb.entrySet()) {
       templateParams.put(e.getKey(), e.getValue().value);
     }
+    System.out.println("---------------- DONE loadTransientFields");
   }
 
 }

Modified: 
incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ActivityTemplateParamsDb.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ActivityTemplateParamsDb.java?rev=725581&r1=725580&r2=725581&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ActivityTemplateParamsDb.java
 (original)
+++ 
incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ActivityTemplateParamsDb.java
 Wed Dec 10 21:05:43 2008
@@ -17,24 +17,10 @@
  */
 package org.apache.shindig.social.opensocial.jpa;
 
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations under the License.
- */
+import static javax.persistence.CascadeType.ALL;
+import static javax.persistence.GenerationType.IDENTITY;
 
+import org.apache.shindig.social.opensocial.jpa.api.DbObject;
 import org.apache.shindig.social.opensocial.model.Activity;
 
 import javax.persistence.Basic;
@@ -47,19 +33,15 @@
 import javax.persistence.Table;
 import javax.persistence.Version;
 
-import java.util.Collection;
-import static javax.persistence.GenerationType.IDENTITY;
-import static javax.persistence.CascadeType.ALL;
-
 /**
- * 
+ *
  */
 @Entity
 @Table(name = "template_params")
-public class ActivityTemplateParamsDb {
+public class ActivityTemplateParamsDb implements DbObject {
   /**
-   * The internal object ID used for references to this object. Should be 
generated 
-   * by the underlying storage mechanism
+   * The internal object ID used for references to this object. Should be
+   * generated by the underlying storage mechanism
    */
   @Id
   @GeneratedValue(strategy = IDENTITY)
@@ -67,23 +49,26 @@
   protected long objectId;
 
   /**
-   * An optimistic locking field
+   * An optimistic locking field.
    */
   @Version
   @Column(name = "version")
   protected long version;
 
   /**
-   * Create a link to the activities joining activity_id here to oid in 
activities.
+   * Create a link to the activities joining activity_id here to oid in
+   * activities.
    */
+
   @ManyToOne(targetEntity = ActivityDb.class, cascade = ALL)
   @JoinColumn(name = "activity_id", referencedColumnName = "oid")
-  protected Collection<Activity> activities;
+  protected Activity activity;
   // TODO: <openjpa-1.2.0-r422266:683325 fatal user error>
   // org.apache.openjpa.persistence.ArgumentException: The type of field
-  // 
"org.apache.shindig.social.opensocial.jpa.ActivityTemplateParamsDb.activities" 
isn't supported
-  // by declared persistence strategy "ManyToOne". Please choose a different 
strategy.
-
+  // 
"org.apache.shindig.social.opensocial.jpa.ActivityTemplateParamsDb.activities"
+  // isn't supported
+  // by declared persistence strategy "ManyToOne". Please choose a different
+  // strategy.
 
   /**
    * The name of the value in the template parameters.
@@ -99,4 +84,13 @@
   @Column(name = "template_value")
   protected String value;
 
+  /**
+   * [EMAIL PROTECTED]
+   *
+   * @see org.apache.shindig.social.opensocial.jpa.api.DbObject#getObjectId()
+   */
+  public long getObjectId() {
+    return objectId;
+  }
+
 }


Reply via email to