merged 1.1 branch into head
[mir.git] / source / mircoders / storage / DatabaseCommentToMedia.java
index bc7fdf0..8d6f0a1 100755 (executable)
 package mircoders.storage;
 
 import java.sql.Connection;
-import java.sql.ResultSet;
 import java.sql.Statement;
-import java.util.Vector;
+import java.util.ArrayList;
 
 import mir.entity.EntityList;
 import mir.log.LoggerWrapper;
 import mir.storage.Database;
-import mir.storage.StorageObject;
-import mir.storage.StorageObjectExc;
-import mir.storage.StorageObjectFailure;
-import mircoders.entity.EntityComment;
+import mir.storage.DatabaseFailure;
 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.5 2004/11/06 20:10:37 idfx Exp $
- *
- */
-
-public class DatabaseCommentToMedia extends Database implements StorageObject{
+public class DatabaseCommentToMedia extends Database {
 
   private static DatabaseCommentToMedia instance;
 
@@ -65,141 +53,14 @@ public class DatabaseCommentToMedia extends Database implements StorageObject{
 
   private DatabaseCommentToMedia() {
     super();
+
     logger = new LoggerWrapper("Database.CommentToMedia");
-    hasTimestamp = false;
     mainTable = "comment_x_media";
-    theEntityClass = mir.entity.GenericEntity.class;
-  }
-
-  public boolean hasMedia(EntityComment comment) throws StorageObjectFailure,
-      StorageObjectExc {
-    if (comment != null) {
-      try {
-        return (getSize("comment_id=" + comment.getId()) == 0) ? false:true;
-      }
-      catch (Exception e) {
-        logger.error("DatabaseCommentToMedia.hasMedia: " + e.toString());
-        throw new StorageObjectFailure("DatabaseCommentToMedia.hasMedia: " +
-                                       e.toString(), e);
-      }
-    }
-    else {
-      logger.error("DatabaseCommentToMedia.hasMedia: comment == null");
-      throw new StorageObjectExc(
-          "DatabaseCommentToMedia.hasMedia: comment == null");
-    }
-  }
-
-  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(mainTable+" 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 {
-    return getMultiMediaForComment(DatabaseAudio.getInstance(), comment);
-  }
-
-  /**
-   * get all the video belonging to a comment entity
-   *
-   */
-  public EntityList getVideo(EntityComment comment) throws StorageObjectFailure {
-    return getMultiMediaForComment(DatabaseVideo.getInstance(), comment);
-  }
-
-  /**
-   * get all the images belonging to a comment entity
-   */
-  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 {
-    return getMultiMediaForComment(DatabaseOther.getInstance(), comment);
-  }
-
-  /**
-   * get all the uploaded/other Media belonging to a comment entity
-   */
-  public EntityList getUploadedMedia(EntityComment comment) throws StorageObjectFailure {
-    return getMultiMediaForComment(DatabaseUploadedMedia.getInstance(), comment);
-  }
-
-  public void setMedia(String commentId, String[] mediaId) throws
-      StorageObjectFailure {
-    if (commentId == null) {
-      return;
-    }
-    if (mediaId == null || mediaId[0] == null) {
-      return;
-    }
-    //first delete all row with comment_id=commentId
-    String sql = "delete from " + mainTable + " where comment_id=" + commentId;
-
-    Connection con = null;
-    Statement stmt = null;
-    try {
-      con = obtainConnection();
-      // should be a preparedStatement because is faster
-      stmt = con.createStatement();
-      ResultSet rs = executeSql(stmt, sql);
-    }
-    catch (Exception e) {
-      logger.error("-- set media failed -- delete");
-      throw new StorageObjectFailure("-- set media failed -- delete", e);
-    }
-    finally {
-      freeConnection(con, stmt);
-    }
-
-    //now insert
-    //first delete all row with comment_id=commentId
-    for (int i = 0; i < mediaId.length; i++) {
-      sql = "insert into " + mainTable + " (comment_id,media_id) values ("
-          + commentId + "," + mediaId[i] + ")";
-      try {
-        con = obtainConnection();
-        // should be a preparedStatement because is faster
-        stmt = con.createStatement();
-        int rs = executeUpdate(stmt, sql);
-      }
-      catch (Exception e) {
-        logger.error("-- set topics failed -- insert");
-        throw new StorageObjectFailure("-- set topics failed -- insert ", e);
-      }
-      finally {
-        freeConnection(con, stmt);
-      }
-    }
+    entityClass = mir.entity.GenericEntity.class;
   }
 
   public void addMedia(String commentId, String mediaId) throws
-      StorageObjectFailure {
+      DatabaseFailure {
     if (commentId == null && mediaId == null) {
       return;
     }
@@ -218,7 +79,7 @@ public class DatabaseCommentToMedia extends Database implements StorageObject{
     }
     catch (Exception e) {
       logger.error("-- add media failed -- insert");
-      throw new StorageObjectFailure("-- add media failed -- insert ", e);
+      throw new DatabaseFailure("-- add media failed -- insert ", e);
     }
     finally {
       freeConnection(con, stmt);
@@ -226,7 +87,7 @@ public class DatabaseCommentToMedia extends Database implements StorageObject{
   }
 
   public void setMedia(String commentId, String mediaId) throws
-      StorageObjectFailure {
+      DatabaseFailure {
     if (commentId == null && mediaId == null) {
       return;
     }
@@ -243,7 +104,7 @@ public class DatabaseCommentToMedia extends Database implements StorageObject{
     }
     catch (Exception e) {
       logger.error("-- set media failed -- delete");
-      throw new StorageObjectFailure("-- set media failed -- delete ", e);
+      throw new DatabaseFailure("-- set media failed -- delete ", e);
     }
     finally {
       freeConnection(con, stmt);
@@ -262,14 +123,14 @@ public class DatabaseCommentToMedia extends Database implements StorageObject{
     }
     catch (Exception e) {
       logger.error("-- set media failed -- insert");
-      throw new StorageObjectFailure("-- set media failed -- insert ", e);
+      throw new DatabaseFailure("-- set media failed -- insert ", e);
     }
     finally {
       freeConnection(con, stmt);
     }
   }
 
-  public void deleteByCommentId(String commentId) throws StorageObjectFailure {
+  public void deleteByCommentId(String commentId) throws DatabaseFailure {
     if (commentId == null) {
       //theLog.printDebugInfo("-- delete topics failed -- no comment id");
       return;
@@ -287,7 +148,7 @@ public class DatabaseCommentToMedia extends Database implements StorageObject{
     }
     catch (Exception e) {
       logger.error("-- delete by commentId failed  ");
-      throw new StorageObjectFailure(
+      throw new DatabaseFailure(
           "-- delete by comment id failed -- delete ", e);
     }
     finally {
@@ -295,7 +156,7 @@ public class DatabaseCommentToMedia extends Database implements StorageObject{
     }
   }
 
-  public void deleteByMediaId(String mediaId) throws StorageObjectFailure {
+  public void deleteByMediaId(String mediaId) throws DatabaseFailure {
     if (mediaId == null) {
       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
       return;
@@ -314,7 +175,7 @@ public class DatabaseCommentToMedia extends Database implements StorageObject{
     }
     catch (Exception e) {
       logger.error("-- delete media failed ");
-      throw new StorageObjectFailure("-- delete by media id failed -- ", e);
+      throw new DatabaseFailure("-- delete by media id failed -- ", e);
     }
     finally {
       freeConnection(con, stmt);
@@ -322,7 +183,7 @@ public class DatabaseCommentToMedia extends Database implements StorageObject{
   }
 
   public void delete(String commentId, String mediaId) throws
-      StorageObjectFailure {
+      DatabaseFailure {
     if (mediaId == null || commentId == null) {
       logger.debug("-- delete media failed -- missing parameter");
       return;
@@ -342,7 +203,7 @@ public class DatabaseCommentToMedia extends Database implements StorageObject{
     }
     catch (Exception e) {
       logger.error("-- delete comment_x_media failed ");
-      throw new StorageObjectFailure("-- delete comment_x_media failed -- ", e);
+      throw new DatabaseFailure("-- delete comment_x_media failed -- ", e);
     }
     finally {
       freeConnection(con, stmt);
@@ -350,13 +211,13 @@ public class DatabaseCommentToMedia extends Database implements StorageObject{
   }
 
   public EntityList getComment(EntityUploadedMedia media) throws
-      StorageObjectFailure {
+      DatabaseFailure {
 
     EntityList returnList = null;
     if (media != null) {
 
       String id = media.getId();
-      Vector extraTables = new Vector();
+      ArrayList extraTables = new ArrayList();
       extraTables.add(mainTable + " cxm");
 
       String mediaSelect = "cxm.comment_id=c.id and cxm.media_id="+id;
@@ -367,7 +228,7 @@ public class DatabaseCommentToMedia extends Database implements StorageObject{
       }
       catch (Exception e) {
         logger.error("-- get comment failed");
-        throw new StorageObjectFailure("-- get comment failed -- ", e);
+        throw new DatabaseFailure("-- get comment failed -- ", e);
       }
     }
     return returnList;
@@ -378,10 +239,10 @@ public class DatabaseCommentToMedia extends Database implements StorageObject{
    *  a relation to a media
    */
 
-  public EntityList getComment() throws StorageObjectFailure {
+  public EntityList getComment() throws DatabaseFailure {
     EntityList returnList = null;
 
-    Vector extraTables = new Vector();
+    ArrayList extraTables = new ArrayList();
     extraTables.add(mainTable + " cxm");
 
     String mediaSelect = "cxm.comment_id=c.id";
@@ -392,7 +253,7 @@ public class DatabaseCommentToMedia extends Database implements StorageObject{
     }
     catch (Exception e) {
       logger.error("-- get comment failed");
-      throw new StorageObjectFailure("-- get comment failed -- ", e);
+      throw new DatabaseFailure("-- get comment failed -- ", e);
     }
     return returnList;