added cache in EntityContent for comments, media, etc.. this cache must
authormh <mh>
Sun, 24 Feb 2002 17:00:09 +0000 (17:00 +0000)
committermh <mh>
Sun, 24 Feb 2002 17:00:09 +0000 (17:00 +0000)
remain transient..

source/mircoders/entity/EntityContent.java
source/mircoders/storage/DatabaseContentToMedia.java

index 46fa13d..e6fbbe0 100755 (executable)
@@ -32,6 +32,12 @@ public class EntityContent extends Entity
   String mirconf_mailLinkName = MirConfig.getProp("Producer.MailLinkName");
   String mirconf_imageRoot    = MirConfig.getProp("Producer.ImageRoot");
 
+  //this should always be transient i.e it can never be stored in the db
+  //or ObjectStore. (so the ObjectStore should only be caching what comes
+  //directly out of the DB. @todo confirm this with rk. -mh
+  HashMap _entCache = new HashMap();
+  Boolean _hasMedia = null;
+
        // constructors
 
        public EntityContent()
@@ -157,18 +163,22 @@ public class EntityContent extends Entity
   public TemplateModel get(java.lang.String key) throws TemplateModelException
   {
     if (key!=null) {
+      if (_entCache.containsKey(key)) {
+        return (TemplateModel)_entCache.get(key);
+      }
       if (key.equals("to_comments")) {
         try {
-          return getComments();
-        }
-        catch (Exception ex) {
+          _entCache.put(key, getComments());
+          return (TemplateModel)_entCache.get(key);
+        catch (Exception ex) {
           theLog.printWarning("-- getComments: could not fetch data " + ex.toString());
           throw new TemplateModelException(ex.toString());
         }
       }
       if (key.equals("to_media_images")) {
         try {
-          return getImagesForContent();
+          _entCache.put(key, getImagesForContent());
+          return (TemplateModel)_entCache.get(key);
         }
         catch (Exception ex) {
           theLog.printWarning("-- getImagesForContent: could not fetch data " + ex.toString());
@@ -177,7 +187,8 @@ public class EntityContent extends Entity
       }
       if (key.equals("to_media_audio")) {
         try {
-          return getAudioForContent();
+          _entCache.put(key, getAudioForContent());
+          return (TemplateModel)_entCache.get(key);
         }
         catch (Exception ex) {
           theLog.printWarning("-- getAudioForContent: could not fetch data " + ex.toString());
@@ -186,7 +197,8 @@ public class EntityContent extends Entity
       }
       if (key.equals("to_media_video")) {
         try {
-          return getVideoForContent();
+          _entCache.put(key, getVideoForContent());
+          return (TemplateModel)_entCache.get(key);
         }
         catch (Exception ex) {
           theLog.printWarning("-- getVideoForContent: could not fetch data " + ex.toString());
@@ -195,7 +207,8 @@ public class EntityContent extends Entity
       }
       if (key.equals("to_media_other")) {
         try {
-          return getOtherMediaForContent();
+          _entCache.put(key, getOtherMediaForContent());
+          return (TemplateModel)_entCache.get(key);
         }
         catch (Exception ex) {
           theLog.printWarning("-- getOtherMediaForContent: could not fetch data " + ex.toString());
@@ -204,7 +217,8 @@ public class EntityContent extends Entity
       }
       else if (key.equals("to_media_icon")) {
         try {
-          return getUploadedMediaForNewswire();
+          _entCache.put(key, getUploadedMediaForNewswire());
+          return (TemplateModel)_entCache.get(key);
         }
         catch (Exception ex) {
           theLog.printWarning("-- getUploadedMediaForNewswire: could not fetch data " + ex.toString());
@@ -213,7 +227,9 @@ public class EntityContent extends Entity
       }
       else if (key.equals("to_topics")) {
         try {
-          DatabaseContentToTopics.getInstance().getTopics(this);
+          _entCache.put(key, 
+                        DatabaseContentToTopics.getInstance().getTopics(this));
+          return (TemplateModel)_entCache.get(key);
         }
         catch (Exception ex) {
           theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
@@ -340,30 +356,51 @@ public class EntityContent extends Entity
     return returnHash;
   }
 
+  private boolean hasMedia() throws StorageObjectException
+  {
+    if (_hasMedia == null) {
+      _hasMedia =
+        new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
+    }
+    return _hasMedia.booleanValue();
+  }
+
   //######## @todo all of the following getBlahForContent should have
   // and optimized version where LIMIT=1 sql for list view.
   private EntityList getImagesForContent()
     throws StorageObjectException, TemplateModelException
   {
-    return DatabaseContentToMedia.getInstance().getImages(this);
+    if (hasMedia())
+      return DatabaseContentToMedia.getInstance().getImages(this);
+    else
+      return null;
   }
 
   private EntityList getAudioForContent()
     throws StorageObjectException, TemplateModelException
   {
-    return DatabaseContentToMedia.getInstance().getAudio(this) ;
+    if (hasMedia())
+      return DatabaseContentToMedia.getInstance().getAudio(this) ;
+    else
+      return null;
   }
 
   private EntityList getVideoForContent()
     throws StorageObjectException, TemplateModelException
   {
-    return DatabaseContentToMedia.getInstance().getVideo(this) ;
+    if (hasMedia())
+      return DatabaseContentToMedia.getInstance().getVideo(this) ;
+    else
+      return null;
   }
 
   private EntityList getOtherMediaForContent()
     throws StorageObjectException, TemplateModelException
   {
-    return DatabaseContentToMedia.getInstance().getOther(this);
+    if (hasMedia())
+      return DatabaseContentToMedia.getInstance().getOther(this);
+    else
+      return null;
   }
 
 }
index 1015e6c..1569bb4 100755 (executable)
@@ -63,6 +63,28 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
     return returnList;
   }
 
+  public boolean hasMedia(EntityContent content)
+    throws StorageObjectException {
+    String wc = "content_id="+content.getId();
+    if( content != null) {
+      try {
+        if(selectByWhereClause(wc,-1).size() == 0)
+          return false;
+        else
+          return true;
+      } catch (Exception e) {
+        theLog.printError("-- hasMedia failed " + e.toString());
+        throw new StorageObjectException("-- hasMedia failed " + e.toString());
+      }
+    } else {
+      theLog.printError("-- hasMedia failed: content is NULL");
+      throw new StorageObjectException("-- hasMedia failed: content is NULL");
+    }
+  }
+      
+
+
+
   /**
    * get all the audio belonging to a content entity
    *