1.1 restoration
[mir.git] / source / mircoders / storage / DatabaseContentToMedia.java
index 1572cb2..14a4a53 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2001, 2002  The Mir-coders group
+ * Copyright (C) 2001, 2002 The Mir-coders group
  *
  * This file is part of Mir.
  *
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * In addition, as a special exception, The Mir-coders gives permission to link
- * the code of this program with the com.oreilly.servlet library, any library
- * licensed under the Apache Software License, The Sun (tm) Java Advanced
- * Imaging library (JAI), The Sun JIMI library (or with modified versions of
- * the above that use the same license as the above), and distribute linked
- * combinations including the two.  You must obey the GNU General Public
- * License in all respects for all of the code used other than the above
- * mentioned libraries.  If you modify this file, you may extend this exception
- * to your version of the file, but you are not obligated to do so.  If you do
- * not wish to do so, delete this exception statement from your version.
+ * the code of this program with  any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two.  You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries.  If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
  */
 
 package mircoders.storage;
@@ -34,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;
@@ -48,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.17 2003/04/09 02:06:10 zapata Exp $
+ * @version $Id: DatabaseContentToMedia.java,v 1.19.2.6 2004/11/21 22:07:14 zapata Exp $
  *
  */
 
@@ -56,14 +56,9 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
 
   private static DatabaseContentToMedia instance;
 
-  public static DatabaseContentToMedia getInstance() {
+  public synchronized static DatabaseContentToMedia getInstance() {
     if (instance == null) {
-      synchronized (DatabaseContentToMedia.class) {
-        if (instance == null) {
-          instance = new DatabaseContentToMedia();
-          instance.myselfDatabase = instance;
-        }
-      }
+      instance = new DatabaseContentToMedia();
     }
     return instance;
   }
@@ -72,48 +67,16 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
     super();
 
     logger = new LoggerWrapper("Database.ContentToMedia");
-
     hasTimestamp = false;
-    theTable = "content_x_media";
+    mainTable = "content_x_media";
     theEntityClass = mir.entity.GenericEntity.class;
   }
 
-  /**
-   * get all the media-files belonging to a content entity
-   *
-   */
-  public EntityList getMedia(EntityContent content) throws StorageObjectFailure {
-    EntityList returnList = null;
-    if (content != null) {
-      // get all to_topic from media_x_topic
-      String id = content.getId();
-      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 = DatabaseMedia.getInstance().selectByWhereClause(subselect,
-            "id", -1);
-      }
-      catch (Throwable e) {
-        logger.debug("-- get media failed " + e.toString());
-        throw new StorageObjectFailure("-- get media failed ", e);
-      }
-    }
-    return returnList;
-  }
-
   public boolean hasMedia(EntityContent content) throws StorageObjectFailure,
       StorageObjectExc {
     if (content != null) {
       try {
-        if (selectByWhereClause("content_id=" + content.getId(), -1).size() ==
-            0)
-          return false;
-        else
-          return true;
+        return (getSize("content_id=" + content.getId()) == 0) ? false:true;
       }
       catch (Exception e) {
         logger.error("DatabaseContentToMedia.hasMedia: " + e.toString());
@@ -128,169 +91,77 @@ 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) {
-      // 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 {
+        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"
-        returnList = DatabaseAudio.getInstance().selectByWhereClause(subselect,
-            "id", -1);
-      }
-      catch (Exception e) {
-        logger.error("DatabaseContentToMedia.getAudio: " + e.toString());
-        throw new StorageObjectFailure("DatabaseContentToMedia.getAudio: " +
-                                       e.toString(), e);
+        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) {
-      // 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) {
-      // 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) {
-      // 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) {
-      // 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
-    String sql = "delete from " + theTable + " where content_id=" + contentId;
+    String sql = "delete from " + mainTable + " where content_id=" + contentId;
 
     Connection con = null;
     Statement stmt = null;
     try {
-      con = getPooledCon();
+      con = obtainConnection();
       // should be a preparedStatement because is faster
       stmt = con.createStatement();
       ResultSet rs = executeSql(stmt, sql);
@@ -306,10 +177,10 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
     //now insert
     //first delete all row with content_id=contentId
     for (int i = 0; i < mediaId.length; i++) {
-      sql = "insert into " + theTable + " (content_id,media_id) values ("
+      sql = "insert into " + mainTable + " (content_id,media_id) values ("
           + contentId + "," + mediaId[i] + ")";
       try {
-        con = getPooledCon();
+        con = obtainConnection();
         // should be a preparedStatement because is faster
         stmt = con.createStatement();
         int rs = executeUpdate(stmt, sql);
@@ -334,10 +205,10 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
     Statement stmt = null;
     //now insert
 
-    String sql = "insert into " + theTable + " (content_id,media_id) values ("
+    String sql = "insert into " + mainTable + " (content_id,media_id) values ("
         + contentId + "," + mediaId + ")";
     try {
-      con = getPooledCon();
+      con = obtainConnection();
       // should be a preparedStatement because is faster
       stmt = con.createStatement();
       int rs = executeUpdate(stmt, sql);
@@ -357,12 +228,12 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
       return;
     }
     //first delete all row with content_id=contentId
-    String sql = "delete from " + theTable + " where content_id=" + contentId;
+    String sql = "delete from " + mainTable + " where content_id=" + contentId;
 
     Connection con = null;
     Statement stmt = null;
     try {
-      con = getPooledCon();
+      con = obtainConnection();
       // should be a preparedStatement because is faster
       stmt = con.createStatement();
       int rs = executeUpdate(stmt, sql);
@@ -378,10 +249,10 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
     //now insert
     //first delete all row with content_id=contentId
 
-    sql = "insert into " + theTable + " (content_id,media_id) values ("
+    sql = "insert into " + mainTable + " (content_id,media_id) values ("
         + contentId + "," + mediaId + ")";
     try {
-      con = getPooledCon();
+      con = obtainConnection();
       // should be a preparedStatement because is faster
       stmt = con.createStatement();
       int rs = executeUpdate(stmt, sql);
@@ -401,12 +272,12 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
       return;
     }
     //delete all row with content_id=contentId
-    String sql = "delete from " + theTable + " where content_id=" + contentId;
+    String sql = "delete from " + mainTable + " where content_id=" + contentId;
 
     Connection con = null;
     Statement stmt = null;
     try {
-      con = getPooledCon();
+      con = obtainConnection();
       // should be a preparedStatement because is faster
       stmt = con.createStatement();
       int rs = executeUpdate(stmt, sql);
@@ -422,17 +293,18 @@ 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;
     }
     //delete all row with content_id=contentId
-    String sql = "delete from " + theTable + " where media_id=" + mediaId;
+    String sql = "delete from " + mainTable + " where media_id=" + mediaId;
 
     Connection con = null;
     Statement stmt = null;
     try {
-      con = getPooledCon();
+      con = obtainConnection();
       // should be a preparedStatement because is faster
       stmt = con.createStatement();
       int rs = executeUpdate(stmt, sql);
@@ -454,13 +326,13 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
       return;
     }
     //delete all row with content_id=contentId and media_id=mediaId
-    String sql = "delete from " + theTable + " where media_id=" + mediaId +
+    String sql = "delete from " + mainTable + " where media_id=" + mediaId +
         " and content_id= " + contentId;
 
     Connection con = null;
     Statement stmt = null;
     try {
-      con = getPooledCon();
+      con = obtainConnection();
       // should be a preparedStatement because is faster
       stmt = con.createStatement();
       int rs = executeUpdate(stmt, sql);
@@ -477,42 +349,24 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
 
   public EntityList getContent(EntityUploadedMedia media) throws
       StorageObjectFailure {
+
     EntityList returnList = null;
     if (media != null) {
+
       String id = media.getId();
-      String select = "select content_id from " + theTable + " where media_id=" +
-          id;
+      Vector extraTables = new Vector();
+      extraTables.add(mainTable + " cxm");
 
-      // execute select statement
-      Connection con = null;
-      Statement stmt = null;
+      String mediaSelect = "cxm.content_id=c.id and cxm.media_id="+id;
       try {
-        con = getPooledCon();
-        // should be a preparedStatement because is faster
-        stmt = con.createStatement();
-        ResultSet rs = executeSql(stmt, select);
-        if (rs != null) {
-          String mediaSelect = "id IN (";
-          boolean first = true;
-          while (rs.next()) {
-            if (first == false)
-              mediaSelect += ",";
-            mediaSelect += rs.getString(1);
-            first = false;
-          }
-          mediaSelect += ")";
-          if (first == false)
-            returnList = DatabaseContent.getInstance().selectByWhereClause(
-                mediaSelect, -1);
-        }
+        returnList = DatabaseContent.getInstance().selectByWhereClause("c",
+          extraTables, mediaSelect, "c.id" );
+
       }
       catch (Exception e) {
         logger.error("-- get content failed");
         throw new StorageObjectFailure("-- get content failed -- ", e);
       }
-      finally {
-        freeConnection(con, stmt);
-      }
     }
     return returnList;
   }
@@ -522,40 +376,22 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
    */
 
   public EntityList getContent() throws StorageObjectFailure {
+
     EntityList returnList = null;
 
-    String select = "select distinct content_id from " + theTable;
-    // execute select statement
-    Connection con = null;
-    Statement stmt = null;
+    Vector extraTables = new Vector();
+    extraTables.add(mainTable + " cxm");
+
+    String mediaSelect = "cxm.content_id=c.id";
     try {
-      con = getPooledCon();
-      // should be a preparedStatement because is faster
-      stmt = con.createStatement();
-      ResultSet rs = executeSql(stmt, select);
-      if (rs != null) {
-        String mediaSelect = "id IN (";
-        boolean first = true;
-        while (rs.next()) {
-          if (first == false)
-            mediaSelect += ",";
-          mediaSelect += rs.getString(1);
-          first = false;
-        }
-        mediaSelect += ")";
-        if (first == false)
-          returnList = DatabaseContent.getInstance().selectByWhereClause(
-              mediaSelect, "webdb_lastchange desc");
-      }
+      returnList = DatabaseContent.getInstance().selectByWhereClause("c",
+        extraTables, mediaSelect, "c.webdb_lastchange desc" );
+
     }
     catch (Exception e) {
       logger.error("-- get content failed");
       throw new StorageObjectFailure("-- get content failed -- ", e);
     }
-    finally {
-      freeConnection(con, stmt);
-    }
-
     return returnList;
   }