simplyfications and rewrite with relational sql
authorrk <rk>
Tue, 25 Nov 2003 02:04:43 +0000 (02:04 +0000)
committerrk <rk>
Tue, 25 Nov 2003 02:04:43 +0000 (02:04 +0000)
source/mircoders/storage/DatabaseCommentToMedia.java
source/mircoders/storage/DatabaseContentToMedia.java
source/mircoders/storage/DatabaseContentToTopics.java

index 475f208..f9fe1c6 100755 (executable)
@@ -33,6 +33,7 @@ package mircoders.storage;
 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.Statement;
+import java.util.Vector;
 
 import mir.entity.EntityList;
 import mir.log.LoggerWrapper;
@@ -47,7 +48,7 @@ import mircoders.entity.EntityUploadedMedia;
  * <b>implements abstract DB connection to the comment_x_media SQL table
  *
  * @author RK, mir-coders group
- * @version $Id: DatabaseCommentToMedia.java,v 1.3.2.2 2003/11/24 23:37:18 rk Exp $
+ * @version $Id: DatabaseCommentToMedia.java,v 1.3.2.3 2003/11/25 02:04:43 rk Exp $
  *
  */
 
@@ -64,9 +65,7 @@ public class DatabaseCommentToMedia extends Database implements StorageObject{
 
   private DatabaseCommentToMedia() {
     super();
-
     logger = new LoggerWrapper("Database.CommentToMedia");
-
     hasTimestamp = false;
     theTable = "comment_x_media";
     theEntityClass = mir.entity.GenericEntity.class;
@@ -76,12 +75,7 @@ public class DatabaseCommentToMedia extends Database implements StorageObject{
       StorageObjectExc {
     if (comment != null) {
       try {
-        // TODO make this a count statement
-        if (selectByWhereClause("comment_id=" + comment.getId(), -1).size() ==
-            0)
-          return false;
-        else
-          return true;
+        return (getSize("comment_id=" + comment.getId()) == 0) ? false:true;
       }
       catch (Exception e) {
         logger.error("DatabaseCommentToMedia.hasMedia: " + e.toString());
@@ -96,34 +90,35 @@ public class DatabaseCommentToMedia extends Database implements StorageObject{
     }
   }
 
+  private EntityList getMultiMediaForComment(StorageObject store, EntityComment comment)
+      throws StorageObjectFailure {
+    
+      EntityList returnList = null;
+      if (comment != null) {
+        String id = comment.getId();
+        try {        
+          Vector extraTable = new Vector();
+          extraTable.add(theTable+" cxm");
+          // media should stay in uploaded order. this is especially important
+          // for photo stories which require a specific chronologic order.
+          // this is why we have the the second parameter "id"       
+          store.selectByWhereClause("m", extraTable, 
+            "m.id=cxm.media_id and cxm.comment_id="+id, "id", -1, -1);
+                
+        } catch (Exception e) {
+          logger.error("DatabaseCommenttToMedia.getMultiMediaForComment: " + e.toString());
+          throw new StorageObjectFailure("DatabaseContentToMedia.etMultiMediaForComment: " +
+                                       e.toString(), e);
+        }
+      }
+      return returnList;
+    }
+
   /**
    * get all the audio belonging to a comment entity
-   *
    */
   public EntityList getAudio(EntityComment comment) throws StorageObjectFailure {
-    EntityList returnList = null;
-    if (comment != null) {
-//    TODO rewrite as relational sql
-      // get all to_topic from media_x_topic
-      String id = comment.getId();
-      //this is not supported by mysql
-      String subselect = "id in (select media_id from " + theTable +
-          " where comment_id=" + id + ")";
-
-      try {
-        // media should stay in uploaded order. this is especially important
-        // for photo stories which require a specific chronologic order.
-        // this is why we have the the second parameter "id"
-        returnList = DatabaseAudio.getInstance().selectByWhereClause(subselect,
-            "id", -1);
-      }
-      catch (Exception e) {
-        logger.error("DatabaseCommentToMedia.getAudio: " + e.toString());
-        throw new StorageObjectFailure("DatabaseCommentToMedia.getAudio: " +
-                                       e.toString(), e);
-      }
-    }
-    return returnList;
+    return getMultiMediaForComment(DatabaseAudio.getInstance(), comment);
   }
 
   /**
@@ -131,122 +126,28 @@ public class DatabaseCommentToMedia extends Database implements StorageObject{
    *
    */
   public EntityList getVideo(EntityComment comment) throws StorageObjectFailure {
-    EntityList returnList = null;
-    if (comment != null) {
-//    TODO rewrite as relational sql
-      // get all to_topic from media_x_topic
-      String id = comment.getId();
-      //this is not supported by mysql
-      String subselect = "id in (select media_id from " + theTable +
-          " where comment_id=" + id + ")";
-
-      try {
-        // media should stay in uploaded order. this is especially important
-        // for photo stories which require a specific chronologic order.
-        // this is why we have the the second parameter "id"
-        returnList = DatabaseVideo.getInstance().selectByWhereClause(subselect,
-            "id", -1);
-      }
-      catch (Exception e) {
-        logger.error("DatabaseCommentToMedia.getVideo: " + e.toString());
-        throw new StorageObjectFailure("DatabaseCommentToMedia.getVideo: " +
-                                       e.toString(), e);
-      }
-    }
-    return returnList;
+    return getMultiMediaForComment(DatabaseVideo.getInstance(), comment);
   }
 
   /**
    * get all the images belonging to a comment entity
-   *
    */
-  public EntityList getImages(EntityComment comment) throws
-      StorageObjectFailure {
-    EntityList returnList = null;
-    if (comment != null) {
-//    TODO rewrite as relational sql
-      // get all to_topic from media_x_topic
-      String id = comment.getId();
-      //this is not supported by mysql
-      String subselect = "id in (select media_id from " + theTable +
-          " where comment_id=" + id + ")";
-
-      try {
-        // media should stay in uploaded order. this is especially important
-        // for photo stories which require a specific chronologic order.
-        // this is why we have the the second parameter "id"
-        returnList = DatabaseImages.getInstance().selectByWhereClause(subselect,
-            "id", -1);
-      }
-      catch (Exception e) {
-        logger.error("DatabaseCommentToMedia.getImages: " + e.toString());
-        throw new StorageObjectFailure("DatabaseCommentToMedia.getImages: " +
-                                       e.toString(), e);
-      }
-    }
-    return returnList;
+  public EntityList getImages(EntityComment comment) throws StorageObjectFailure {
+    return getMultiMediaForComment(DatabaseImages.getInstance(), comment);
   }
 
   /**
    * get all the uploaded/other Media belonging to a comment entity
-   *
    */
   public EntityList getOther(EntityComment comment) throws StorageObjectFailure {
-    /** @todo this should only fetch published media / rk */
-
-    EntityList returnList = null;
-    if (comment != null) {
-//    TODO rewrite as relational sql
-      // get all to_topic from media_x_topic
-      String id = comment.getId();
-      //this is not supported by mysql
-      String subselect = "id in (select media_id from " + theTable +
-          " where comment_id=" + id + ")";
-
-      try {
-        // media should stay in uploaded order. this is especially important
-        // for photo stories which require a specific chronologic order.
-        // this is why we have the the second parameter "id"
-        returnList = DatabaseOther.getInstance().selectByWhereClause(subselect,
-            "id");
-      }
-      catch (Exception e) {
-        logger.error("DatabaseCommentToMedia.getOther: " + e.toString());
-        throw new StorageObjectFailure("DatabaseCommentToMedia.getOther: " + e.toString(), e);
-      }
-    }
-    return returnList;
+    return getMultiMediaForComment(DatabaseOther.getInstance(), comment);
   }
 
   /**
    * get all the uploaded/other Media belonging to a comment entity
-   *
    */
-  public EntityList getUploadedMedia(EntityComment comment) throws
-      StorageObjectFailure {
-    /** @todo this should only fetch published media / rk */
-
-    EntityList returnList = null;
-    if (comment != null) {
-//    TODO rewrite as relational sql
-      // get all to_topic from media_x_topic
-      String id = comment.getId();
-      //this is not supported by mysql
-      String subselect = "id in (select media_id from " + theTable +
-          " where comment_id=" + id + ")";
-
-      try {
-        returnList = DatabaseUploadedMedia.getInstance().selectByWhereClause(
-            subselect,
-            "id");
-      }
-      catch (Exception e) {
-        logger.error("DatabaseCommentToMedia.getUploadedMedia: " + e.toString());
-        throw new StorageObjectFailure(
-            "DatabaseCommentToMedia.getUploadedMedia: " + e.toString(), e);
-      }
-    }
-    return returnList;
+  public EntityList getUploadedMedia(EntityComment comment) throws StorageObjectFailure {
+    return getMultiMediaForComment(DatabaseUploadedMedia.getInstance(), comment);
   }
 
   public void setMedia(String commentId, String[] mediaId) throws
index 854af6a..f88c157 100755 (executable)
@@ -33,6 +33,7 @@ package mircoders.storage;
 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.Statement;
+import java.util.Vector;
 
 import mir.entity.EntityList;
 import mir.log.LoggerWrapper;
@@ -47,7 +48,7 @@ import mircoders.entity.EntityUploadedMedia;
  * <b>implements abstract DB connection to the content_x_media SQL table
  *
  * @author RK, mir-coders group
- * @version $Id: DatabaseContentToMedia.java,v 1.19.2.2 2003/11/24 23:37:18 rk Exp $
+ * @version $Id: DatabaseContentToMedia.java,v 1.19.2.3 2003/11/25 02:04:43 rk Exp $
  *
  */
 
@@ -66,7 +67,6 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
     super();
 
     logger = new LoggerWrapper("Database.ContentToMedia");
-
     hasTimestamp = false;
     theTable = "content_x_media";
     theEntityClass = mir.entity.GenericEntity.class;
@@ -75,13 +75,8 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
   public boolean hasMedia(EntityContent content) throws StorageObjectFailure,
       StorageObjectExc {
     if (content != null) {
-      try {
-        // TODO make this a count statement
-        if (selectByWhereClause("content_id=" + content.getId(), -1).size() ==
-            0)
-          return false;
-        else
-          return true;
+      try {        
+        return (getSize("content_id=" + content.getId()) == 0) ? false:true;
       }
       catch (Exception e) {
         logger.error("DatabaseContentToMedia.hasMedia: " + e.toString());
@@ -96,165 +91,68 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
     }
   }
 
-  /**
-   * get all the audio belonging to a content entity
-   *
-   */
-  public EntityList getAudio(EntityContent content) throws StorageObjectFailure {
+  private EntityList getMultiMediaForContent(StorageObject store, EntityContent content)
+    throws StorageObjectFailure {
+    
     EntityList returnList = null;
     if (content != null) {
-//    TODO rewrite as relational sql
-      // get all to_topic from media_x_topic
       String id = content.getId();
-      //this is not supported by mysql
-      String subselect = "id in (select media_id from " + theTable +
-          " where content_id=" + id + ")";
-
-      try {
+      try {        
+        Vector extraTable = new Vector();
+        extraTable.add(theTable+" cxm");
         // media should stay in uploaded order. this is especially important
         // for photo stories which require a specific chronologic order.
-        // this is why we have the the second parameter "id"
-        returnList = DatabaseAudio.getInstance().selectByWhereClause(subselect,
-            "id", -1);
-      }
-      catch (Exception e) {
-        logger.error("DatabaseContentToMedia.getAudio: " + e.toString());
-        throw new StorageObjectFailure("DatabaseContentToMedia.getAudio: " +
-                                       e.toString(), e);
+        // this is why we have the the second parameter "id"       
+        store.selectByWhereClause("m", extraTable, 
+          "m.id=cxm.media_id and cxm.content_id="+id, "id", -1, -1);
+                                       
+                       } catch (Exception e) {
+        logger.error("DatabaseContentToMedia.getMultiMediaForContent: " + e.toString());
+        throw new StorageObjectFailure("DatabaseContentToMedia.etMultiMediaForContent: " +
+                                     e.toString(), e);
       }
     }
     return returnList;
   }
+    
+  /**
+   * get all the audio belonging to a content entity
+   */
+  public EntityList getAudio(EntityContent content) throws StorageObjectFailure {
+    return getMultiMediaForContent(DatabaseAudio.getInstance(), content);
+  }
 
   /**
    * get all the video belonging to a content entity
-   *
    */
   public EntityList getVideo(EntityContent content) throws StorageObjectFailure {
-    EntityList returnList = null;
-    if (content != null) {
-//    TODO rewrite as relational sql
-      // get all to_topic from media_x_topic
-      String id = content.getId();
-      //this is not supported by mysql
-      String subselect = "id in (select media_id from " + theTable +
-          " where content_id=" + id + ")";
-
-      try {
-        // media should stay in uploaded order. this is especially important
-        // for photo stories which require a specific chronologic order.
-        // this is why we have the the second parameter "id"
-        returnList = DatabaseVideo.getInstance().selectByWhereClause(subselect,
-            "id", -1);
-      }
-      catch (Exception e) {
-        logger.error("DatabaseContentToMedia.getVideo: " + e.toString());
-        throw new StorageObjectFailure("DatabaseContentToMedia.getVideo: " +
-                                       e.toString(), e);
-      }
-    }
-    return returnList;
+    return getMultiMediaForContent(DatabaseVideo.getInstance(), content);
   }
 
   /**
    * get all the images belonging to a content entity
-   *
    */
-  public EntityList getImages(EntityContent content) throws
-      StorageObjectFailure {
-    EntityList returnList = null;
-    if (content != null) {
-//    TODO rewrite as relational sql
-      // get all to_topic from media_x_topic
-      String id = content.getId();
-      //this is not supported by mysql
-      String subselect = "id in (select media_id from " + theTable +
-          " where content_id=" + id + ")";
-
-      try {
-        // media should stay in uploaded order. this is especially important
-        // for photo stories which require a specific chronologic order.
-        // this is why we have the the second parameter "id"
-        returnList = DatabaseImages.getInstance().selectByWhereClause(subselect,
-            "id", -1);
-      }
-      catch (Exception e) {
-        logger.error("DatabaseContentToMedia.getImages: " + e.toString());
-        throw new StorageObjectFailure("DatabaseContentToMedia.getImages: " +
-                                       e.toString(), e);
-      }
-    }
-    return returnList;
+  public EntityList getImages(EntityContent content) throws StorageObjectFailure {
+    return getMultiMediaForContent(DatabaseImages.getInstance(), content);
   }
 
   /**
    * get all the uploaded/other Media belonging to a content entity
-   *
    */
   public EntityList getOther(EntityContent content) throws StorageObjectFailure {
-    /** @todo this should only fetch published media / rk */
-
-    EntityList returnList = null;
-    if (content != null) {
-//    TODO rewrite as relational sql
-      // get all to_topic from media_x_topic
-      String id = content.getId();
-      //this is not supported by mysql
-      String subselect = "id in (select media_id from " + theTable +
-          " where content_id=" + id + ")";
-
-      try {
-        // media should stay in uploaded order. this is especially important
-        // for photo stories which require a specific chronologic order.
-        // this is why we have the the second parameter "id"
-        returnList = DatabaseOther.getInstance().selectByWhereClause(subselect,
-            "id");
-      }
-      catch (Exception e) {
-        logger.error("DatabaseContentToMedia.getOther: " + e.toString());
-        throw new StorageObjectFailure("DatabaseContentToMedia.getOther: " + e.toString(), e);
-      }
-    }
-    return returnList;
+    return getMultiMediaForContent(DatabaseOther.getInstance(), content);
   }
 
   /**
    * get all the uploaded/other Media belonging to a content entity
-   *
    */
-  public EntityList getUploadedMedia(EntityContent content) throws
-      StorageObjectFailure {
-    /** @todo this should only fetch published media / rk */
-
-    EntityList returnList = null;
-    if (content != null) {
-//    TODO rewrite as relational sql
-      // get all to_topic from media_x_topic
-      String id = content.getId();
-      //this is not supported by mysql
-      String subselect = "id in (select media_id from " + theTable +
-          " where content_id=" + id + ")";
-
-      try {
-        returnList = DatabaseUploadedMedia.getInstance().selectByWhereClause(
-            subselect,
-            "id");
-      }
-      catch (Exception e) {
-        logger.error("DatabaseContentToMedia.getUploadedMedia: " + e.toString());
-        throw new StorageObjectFailure(
-            "DatabaseContentToMedia.getUploadedMedia: " + e.toString(), e);
-      }
-    }
-    return returnList;
+  public EntityList getUploadedMedia(EntityContent content) throws StorageObjectFailure {
+    return getMultiMediaForContent(DatabaseUploadedMedia.getInstance(), content);
   }
 
-  public void setMedia(String contentId, String[] mediaId) throws
-      StorageObjectFailure {
-    if (contentId == null) {
-      return;
-    }
-    if (mediaId == null || mediaId[0] == null) {
+
+  public void setMedia(String contentId, String[] mediaId) throws StorageObjectFailure {
+    if (contentId == null || mediaId == null || mediaId[0] == null) {
       return;
     }
     //first delete all row with content_id=contentId
@@ -395,6 +293,7 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
   }
 
   public void deleteByMediaId(String mediaId) throws StorageObjectFailure {
+  
     if (mediaId == null) {
       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
       return;
index 3d19a17..eca6c29 100755 (executable)
@@ -268,42 +268,28 @@ public class DatabaseContentToTopics extends Database implements StorageObject{
       freeConnection(con,stmt);
     }
   }
-
-
+  
+/**
+ * Returns list of Content for a specific topic
+ * @param topic
+ * @return EntityList
+ * @throws StorageObjectFailure
+ */
   public EntityList getContent(EntityTopics topic)
     throws StorageObjectFailure {
     EntityList returnList=null;
     if (topic != null) {
-      
-      // TODO rewrite with getByWhereClauseWithExtraTables 
-            
       String id = topic.getId();
-      String select = "select content_id from " + theTable + " where topic_id=" + id;
-
-      // execute select statement
-      Connection con=null;Statement stmt=null;
       try {
-        con = getPooledCon();
-        // should be a preparedStatement because is faster
-        stmt = con.createStatement();
-        ResultSet rs = executeSql(stmt,select);
-        if (rs!=null) {
-          String topicSelect= "id IN (";
-          boolean first=true;
-          while (rs.next()) {
-            if (first==false) topicSelect+=",";
-            topicSelect += rs.getString(1);
-            first=false;
-          }
-          topicSelect+=")";
-          if (first==false)
-            returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
-        }
+        Vector extraTables = new Vector();
+        extraTables.add(theTable+" cxt");
+        returnList = DatabaseContent.getInstance()
+                      .selectByWhereClauseWithExtraTables("c",extraTables, 
+                          "c.id=cxt.content_id and cxt.topic_id="+id );    
       }
       catch (Exception e) {
         logger.error("-- get content failed");
       }
-      finally { freeConnection(con,stmt);}
     }
     return returnList;
   }