1.1 restoration
[mir.git] / source / mircoders / storage / DatabaseContentToMedia.java
index 5f98ad6..14a4a53 100755 (executable)
-/*\r
- * Copyright (C) 2001, 2002  The Mir-coders group\r
- *\r
- * This file is part of Mir.\r
- *\r
- * Mir is free software; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; either version 2 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * Mir is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with Mir; if not, write to the Free Software\r
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
- *\r
- * In addition, as a special exception, The Mir-coders gives permission to link\r
- * the code of this program with the com.oreilly.servlet library, any library\r
- * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
- * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
- * the above that use the same license as the above), and distribute linked\r
- * combinations including the two.  You must obey the GNU General Public\r
- * License in all respects for all of the code used other than the above\r
- * mentioned libraries.  If you modify this file, you may extend this exception\r
- * to your version of the file, but you are not obligated to do so.  If you do\r
- * not wish to do so, delete this exception statement from your version.\r
- */\r
-\r
-package mircoders.storage;\r
-\r
-import java.sql.Connection;\r
-import java.sql.ResultSet;\r
-import java.sql.Statement;\r
-\r
-import mir.entity.EntityList;\r
-import mir.log.LoggerWrapper;\r
-import mir.storage.Database;\r
-import mir.storage.StorageObject;\r
-import mir.storage.StorageObjectExc;\r
-import mir.storage.StorageObjectFailure;\r
-import mircoders.entity.EntityContent;\r
-import mircoders.entity.EntityUploadedMedia;\r
-\r
-/**\r
- * <b>implements abstract DB connection to the content_x_media SQL table\r
- *\r
- * @author RK, mir-coders group\r
- * @version $Id: DatabaseContentToMedia.java,v 1.15 2003/03/06 05:40:40 zapata Exp $\r
- *\r
- */\r
-\r
-public class DatabaseContentToMedia extends Database implements StorageObject{\r
-\r
-  private static DatabaseContentToMedia instance;\r
-\r
-  // the following *has* to be sychronized cause this static method\r
-  // could get preemted and we could end up with 2 instances of DatabaseFoo.\r
-  // see the "Singletons with needles and thread" article at JavaWorld -mh\r
-  public synchronized static DatabaseContentToMedia getInstance() throws\r
-      StorageObjectFailure {\r
-    if (instance == null) {\r
-      instance = new DatabaseContentToMedia();\r
-      instance.myselfDatabase = instance;\r
-    }\r
-    return instance;\r
-  }\r
-\r
-  private DatabaseContentToMedia() throws StorageObjectFailure {\r
-    super();\r
-    logger = new LoggerWrapper("Database.ContentToMedia");\r
-\r
-    hasTimestamp = false;\r
-    theTable = "content_x_media";\r
-\r
-    theEntityClass = mir.entity.GenericEntity.class;\r
-  }\r
-\r
-  /**\r
-   * get all the media-files belonging to a content entity\r
-   *\r
-   */\r
-  public EntityList getMedia(EntityContent content) throws StorageObjectFailure {\r
-    EntityList returnList = null;\r
-    if (content != null) {\r
-      // get all to_topic from media_x_topic\r
-      String id = content.getId();\r
-      String subselect = "id in (select media_id from " + theTable +\r
-          " where content_id=" + id + ")";\r
-\r
-      try {\r
-        // media should stay in uploaded order. this is especially important\r
-        // for photo stories which require a specific chronologic order.\r
-        // this is why we have the the second parameter "id"\r
-        returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,\r
-            "id", -1);\r
-      }\r
-      catch (Throwable e) {\r
-        logger.debug("-- get media failed " + e.toString());\r
-        throw new StorageObjectFailure("-- get media failed ", e);\r
-      }\r
-    }\r
-    return returnList;\r
-  }\r
-\r
-  public boolean hasMedia(EntityContent content) throws StorageObjectFailure,\r
-      StorageObjectExc {\r
-    if (content != null) {\r
-      try {\r
-        if (selectByWhereClause("content_id=" + content.getId(), -1).size() ==\r
-            0)\r
-          return false;\r
-        else\r
-          return true;\r
-      }\r
-      catch (Exception e) {\r
-        logger.error("DatabaseContentToMedia.hasMedia: " + e.toString());\r
-        throw new StorageObjectFailure("DatabaseContentToMedia.hasMedia: " +\r
-                                       e.toString(), e);\r
-      }\r
-    }\r
-    else {\r
-      logger.error("DatabaseContentToMedia.hasMedia: content == null");\r
-      throw new StorageObjectExc(\r
-          "DatabaseContentToMedia.hasMedia: content == null");\r
-    }\r
-  }\r
-\r
-  /**\r
-   * get all the audio belonging to a content entity\r
-   *\r
-   */\r
-  public EntityList getAudio(EntityContent content) throws StorageObjectFailure {\r
-    EntityList returnList = null;\r
-    if (content != null) {\r
-      // get all to_topic from media_x_topic\r
-      String id = content.getId();\r
-      //this is not supported by mysql\r
-      String subselect = "id in (select media_id from " + theTable +\r
-          " where content_id=" + id + ")";\r
-\r
-      try {\r
-        // media should stay in uploaded order. this is especially important\r
-        // for photo stories which require a specific chronologic order.\r
-        // this is why we have the the second parameter "id"\r
-        returnList = DatabaseAudio.getInstance().selectByWhereClause(subselect,\r
-            "id", -1);\r
-      }\r
-      catch (Exception e) {\r
-        logger.error("DatabaseContentToMedia.getAudio: " + e.toString());\r
-        throw new StorageObjectFailure("DatabaseContentToMedia.getAudio: " +\r
-                                       e.toString(), e);\r
-      }\r
-    }\r
-    return returnList;\r
-  }\r
-\r
-  /**\r
-   * get all the video belonging to a content entity\r
-   *\r
-   */\r
-  public EntityList getVideo(EntityContent content) throws StorageObjectFailure {\r
-    EntityList returnList = null;\r
-    if (content != null) {\r
-      // get all to_topic from media_x_topic\r
-      String id = content.getId();\r
-      //this is not supported by mysql\r
-      String subselect = "id in (select media_id from " + theTable +\r
-          " where content_id=" + id + ")";\r
-\r
-      try {\r
-        // media should stay in uploaded order. this is especially important\r
-        // for photo stories which require a specific chronologic order.\r
-        // this is why we have the the second parameter "id"\r
-        returnList = DatabaseVideo.getInstance().selectByWhereClause(subselect,\r
-            "id", -1);\r
-      }\r
-      catch (Exception e) {\r
-        logger.error("DatabaseContentToMedia.getVideo: " + e.toString());\r
-        throw new StorageObjectFailure("DatabaseContentToMedia.getVideo: " +\r
-                                       e.toString(), e);\r
-      }\r
-    }\r
-    return returnList;\r
-  }\r
-\r
-  /**\r
-   * get all the images belonging to a content entity\r
-   *\r
-   */\r
-  public EntityList getImages(EntityContent content) throws\r
-      StorageObjectFailure {\r
-    EntityList returnList = null;\r
-    if (content != null) {\r
-      // get all to_topic from media_x_topic\r
-      String id = content.getId();\r
-      //this is not supported by mysql\r
-      String subselect = "id in (select media_id from " + theTable +\r
-          " where content_id=" + id + ")";\r
-\r
-      try {\r
-        // media should stay in uploaded order. this is especially important\r
-        // for photo stories which require a specific chronologic order.\r
-        // this is why we have the the second parameter "id"\r
-        returnList = DatabaseImages.getInstance().selectByWhereClause(subselect,\r
-            "id", -1);\r
-      }\r
-      catch (Exception e) {\r
-        logger.error("DatabaseContentToMedia.getImages: " + e.toString());\r
-        throw new StorageObjectFailure("DatabaseContentToMedia.getImages: " +\r
-                                       e.toString(), e);\r
-      }\r
-    }\r
-    return returnList;\r
-  }\r
-\r
-  /**\r
-   * get all the uploaded/other Media belonging to a content entity\r
-   *\r
-   */\r
-  public EntityList getOther(EntityContent content) throws StorageObjectFailure {\r
-    /** @todo this should only fetch published media / rk */\r
-\r
-    EntityList returnList = null;\r
-    if (content != null) {\r
-      // get all to_topic from media_x_topic\r
-      String id = content.getId();\r
-      //this is not supported by mysql\r
-      String subselect = "id in (select media_id from " + theTable +\r
-          " where content_id=" + id + ")";\r
-\r
-      try {\r
-        // media should stay in uploaded order. this is especially important\r
-        // for photo stories which require a specific chronologic order.\r
-        // this is why we have the the second parameter "id"\r
-        returnList = DatabaseOther.getInstance().selectByWhereClause(subselect,\r
-            "id");\r
-      }\r
-      catch (Exception e) {\r
-        logger.error("DatabaseContentToMedia.getOther: " + e.toString());\r
-        throw new StorageObjectFailure("DatabaseContentToMedia.getOther: " + e.toString(), e);\r
-      }\r
-    }\r
-    return returnList;\r
-  }\r
-\r
-  /**\r
-   * get all the uploaded/other Media belonging to a content entity\r
-   *\r
-   */\r
-  public EntityList getUploadedMedia(EntityContent content) throws\r
-      StorageObjectFailure {\r
-    /** @todo this should only fetch published media / rk */\r
-\r
-    EntityList returnList = null;\r
-    if (content != null) {\r
-      // get all to_topic from media_x_topic\r
-      String id = content.getId();\r
-      //this is not supported by mysql\r
-      String subselect = "id in (select media_id from " + theTable +\r
-          " where content_id=" + id + ")";\r
-\r
-      try {\r
-        returnList = DatabaseUploadedMedia.getInstance().selectByWhereClause(\r
-            subselect,\r
-            "id");\r
-      }\r
-      catch (Exception e) {\r
-        logger.error("DatabaseContentToMedia.getUploadedMedia: " + e.toString());\r
-        throw new StorageObjectFailure(\r
-            "DatabaseContentToMedia.getUploadedMedia: " + e.toString(), e);\r
-      }\r
-    }\r
-    return returnList;\r
-  }\r
-\r
-  public void setMedia(String contentId, String[] mediaId) throws\r
-      StorageObjectFailure {\r
-    if (contentId == null) {\r
-      return;\r
-    }\r
-    if (mediaId == null || mediaId[0] == null) {\r
-      return;\r
-    }\r
-    //first delete all row with content_id=contentId\r
-    String sql = "delete from " + theTable + " where content_id=" + contentId;\r
-\r
-    Connection con = null;\r
-    Statement stmt = null;\r
-    try {\r
-      con = getPooledCon();\r
-      // should be a preparedStatement because is faster\r
-      stmt = con.createStatement();\r
-      ResultSet rs = executeSql(stmt, sql);\r
-    }\r
-    catch (Exception e) {\r
-      logger.error("-- set media failed -- delete");\r
-      throw new StorageObjectFailure("-- set media failed -- delete", e);\r
-    }\r
-    finally {\r
-      freeConnection(con, stmt);\r
-    }\r
-\r
-    //now insert\r
-    //first delete all row with content_id=contentId\r
-    for (int i = 0; i < mediaId.length; i++) {\r
-      sql = "insert into " + theTable + " (content_id,media_id) values ("\r
-          + contentId + "," + mediaId[i] + ")";\r
-      try {\r
-        con = getPooledCon();\r
-        // should be a preparedStatement because is faster\r
-        stmt = con.createStatement();\r
-        int rs = executeUpdate(stmt, sql);\r
-      }\r
-      catch (Exception e) {\r
-        logger.error("-- set topics failed -- insert");\r
-        throw new StorageObjectFailure("-- set topics failed -- insert ", e);\r
-      }\r
-      finally {\r
-        freeConnection(con, stmt);\r
-      }\r
-    }\r
-  }\r
-\r
-  public void addMedia(String contentId, String mediaId) throws\r
-      StorageObjectFailure {\r
-    if (contentId == null && mediaId == null) {\r
-      return;\r
-    }\r
-\r
-    Connection con = null;\r
-    Statement stmt = null;\r
-    //now insert\r
-\r
-    String sql = "insert into " + theTable + " (content_id,media_id) values ("\r
-        + contentId + "," + mediaId + ")";\r
-    try {\r
-      con = getPooledCon();\r
-      // should be a preparedStatement because is faster\r
-      stmt = con.createStatement();\r
-      int rs = executeUpdate(stmt, sql);\r
-    }\r
-    catch (Exception e) {\r
-      logger.error("-- add media failed -- insert");\r
-      throw new StorageObjectFailure("-- add media failed -- insert ", e);\r
-    }\r
-    finally {\r
-      freeConnection(con, stmt);\r
-    }\r
-  }\r
-\r
-  public void setMedia(String contentId, String mediaId) throws\r
-      StorageObjectFailure {\r
-    if (contentId == null && mediaId == null) {\r
-      return;\r
-    }\r
-    //first delete all row with content_id=contentId\r
-    String sql = "delete from " + theTable + " where content_id=" + contentId;\r
-\r
-    Connection con = null;\r
-    Statement stmt = null;\r
-    try {\r
-      con = getPooledCon();\r
-      // should be a preparedStatement because is faster\r
-      stmt = con.createStatement();\r
-      int rs = executeUpdate(stmt, sql);\r
-    }\r
-    catch (Exception e) {\r
-      logger.error("-- set media failed -- delete");\r
-      throw new StorageObjectFailure("-- set media failed -- delete ", e);\r
-    }\r
-    finally {\r
-      freeConnection(con, stmt);\r
-    }\r
-\r
-    //now insert\r
-    //first delete all row with content_id=contentId\r
-\r
-    sql = "insert into " + theTable + " (content_id,media_id) values ("\r
-        + contentId + "," + mediaId + ")";\r
-    try {\r
-      con = getPooledCon();\r
-      // should be a preparedStatement because is faster\r
-      stmt = con.createStatement();\r
-      int rs = executeUpdate(stmt, sql);\r
-    }\r
-    catch (Exception e) {\r
-      logger.error("-- set media failed -- insert");\r
-      throw new StorageObjectFailure("-- set media failed -- insert ", e);\r
-    }\r
-    finally {\r
-      freeConnection(con, stmt);\r
-    }\r
-  }\r
-\r
-  public void deleteByContentId(String contentId) throws StorageObjectFailure {\r
-    if (contentId == null) {\r
-      //theLog.printDebugInfo("-- delete topics failed -- no content id");\r
-      return;\r
-    }\r
-    //delete all row with content_id=contentId\r
-    String sql = "delete from " + theTable + " where content_id=" + contentId;\r
-\r
-    Connection con = null;\r
-    Statement stmt = null;\r
-    try {\r
-      con = getPooledCon();\r
-      // should be a preparedStatement because is faster\r
-      stmt = con.createStatement();\r
-      int rs = executeUpdate(stmt, sql);\r
-    }\r
-    catch (Exception e) {\r
-      logger.error("-- delete by contentId failed  ");\r
-      throw new StorageObjectFailure(\r
-          "-- delete by content id failed -- delete ", e);\r
-    }\r
-    finally {\r
-      freeConnection(con, stmt);\r
-    }\r
-  }\r
-\r
-  public void deleteByMediaId(String mediaId) throws StorageObjectFailure {\r
-    if (mediaId == null) {\r
-      //theLog.printDebugInfo("-- delete topics failed -- no topic id");\r
-      return;\r
-    }\r
-    //delete all row with content_id=contentId\r
-    String sql = "delete from " + theTable + " where media_id=" + mediaId;\r
-\r
-    Connection con = null;\r
-    Statement stmt = null;\r
-    try {\r
-      con = getPooledCon();\r
-      // should be a preparedStatement because is faster\r
-      stmt = con.createStatement();\r
-      int rs = executeUpdate(stmt, sql);\r
-      logger.debug("-- delete media success ");\r
-    }\r
-    catch (Exception e) {\r
-      logger.error("-- delete media failed ");\r
-      throw new StorageObjectFailure("-- delete by media id failed -- ", e);\r
-    }\r
-    finally {\r
-      freeConnection(con, stmt);\r
-    }\r
-  }\r
-\r
-  public void delete(String contentId, String mediaId) throws\r
-      StorageObjectFailure {\r
-    if (mediaId == null || contentId == null) {\r
-      logger.debug("-- delete media failed -- missing parameter");\r
-      return;\r
-    }\r
-    //delete all row with content_id=contentId and media_id=mediaId\r
-    String sql = "delete from " + theTable + " where media_id=" + mediaId +\r
-        " and content_id= " + contentId;\r
-\r
-    Connection con = null;\r
-    Statement stmt = null;\r
-    try {\r
-      con = getPooledCon();\r
-      // should be a preparedStatement because is faster\r
-      stmt = con.createStatement();\r
-      int rs = executeUpdate(stmt, sql);\r
-      logger.debug("-- delete content_x_media success ");\r
-    }\r
-    catch (Exception e) {\r
-      logger.error("-- delete content_x_media failed ");\r
-      throw new StorageObjectFailure("-- delete content_x_media failed -- ", e);\r
-    }\r
-    finally {\r
-      freeConnection(con, stmt);\r
-    }\r
-  }\r
-\r
-  public EntityList getContent(EntityUploadedMedia media) throws\r
-      StorageObjectFailure {\r
-    EntityList returnList = null;\r
-    if (media != null) {\r
-      String id = media.getId();\r
-      String select = "select content_id from " + theTable + " where media_id=" +\r
-          id;\r
-\r
-      // execute select statement\r
-      Connection con = null;\r
-      Statement stmt = null;\r
-      try {\r
-        con = getPooledCon();\r
-        // should be a preparedStatement because is faster\r
-        stmt = con.createStatement();\r
-        ResultSet rs = executeSql(stmt, select);\r
-        if (rs != null) {\r
-          String mediaSelect = "id IN (";\r
-          boolean first = true;\r
-          while (rs.next()) {\r
-            if (first == false)\r
-              mediaSelect += ",";\r
-            mediaSelect += rs.getString(1);\r
-            first = false;\r
-          }\r
-          mediaSelect += ")";\r
-          if (first == false)\r
-            returnList = DatabaseContent.getInstance().selectByWhereClause(\r
-                mediaSelect, -1);\r
-        }\r
-      }\r
-      catch (Exception e) {\r
-        logger.error("-- get content failed");\r
-        throw new StorageObjectFailure("-- get content failed -- ", e);\r
-      }\r
-      finally {\r
-        freeConnection(con, stmt);\r
-      }\r
-    }\r
-    return returnList;\r
-  }\r
-\r
-  /**\r
-   * Returns a EntityList with all content-objects having a relation to a media\r
-   */\r
-\r
-  public EntityList getContent() throws StorageObjectFailure {\r
-    EntityList returnList = null;\r
-\r
-    String select = "select distinct content_id from " + theTable;\r
-    // execute select statement\r
-    Connection con = null;\r
-    Statement stmt = null;\r
-    try {\r
-      con = getPooledCon();\r
-      // should be a preparedStatement because is faster\r
-      stmt = con.createStatement();\r
-      ResultSet rs = executeSql(stmt, select);\r
-      if (rs != null) {\r
-        String mediaSelect = "id IN (";\r
-        boolean first = true;\r
-        while (rs.next()) {\r
-          if (first == false)\r
-            mediaSelect += ",";\r
-          mediaSelect += rs.getString(1);\r
-          first = false;\r
-        }\r
-        mediaSelect += ")";\r
-        if (first == false)\r
-          returnList = DatabaseContent.getInstance().selectByWhereClause(\r
-              mediaSelect, "webdb_lastchange desc");\r
-      }\r
-    }\r
-    catch (Exception e) {\r
-      logger.error("-- get content failed");\r
-      throw new StorageObjectFailure("-- get content failed -- ", e);\r
-    }\r
-    finally {\r
-      freeConnection(con, stmt);\r
-    }\r
-\r
-    return returnList;\r
-  }\r
-\r
-}\r
+/*
+ * Copyright (C) 2001, 2002 The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * 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  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;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.util.Vector;
+
+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.EntityContent;
+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.6 2004/11/21 22:07:14 zapata Exp $
+ *
+ */
+
+public class DatabaseContentToMedia extends Database implements StorageObject{
+
+  private static DatabaseContentToMedia instance;
+
+  public synchronized static DatabaseContentToMedia getInstance() {
+    if (instance == null) {
+      instance = new DatabaseContentToMedia();
+    }
+    return instance;
+  }
+
+  private DatabaseContentToMedia() {
+    super();
+
+    logger = new LoggerWrapper("Database.ContentToMedia");
+    hasTimestamp = false;
+    mainTable = "content_x_media";
+    theEntityClass = mir.entity.GenericEntity.class;
+  }
+
+  public boolean hasMedia(EntityContent content) throws StorageObjectFailure,
+      StorageObjectExc {
+    if (content != null) {
+      try {
+        return (getSize("content_id=" + content.getId()) == 0) ? false:true;
+      }
+      catch (Exception e) {
+        logger.error("DatabaseContentToMedia.hasMedia: " + e.toString());
+        throw new StorageObjectFailure("DatabaseContentToMedia.hasMedia: " +
+                                       e.toString(), e);
+      }
+    }
+    else {
+      logger.error("DatabaseContentToMedia.hasMedia: content == null");
+      throw new StorageObjectExc(
+          "DatabaseContentToMedia.hasMedia: content == null");
+    }
+  }
+
+  private EntityList getMultiMediaForContent(StorageObject store, EntityContent content)
+    throws StorageObjectFailure {
+
+    EntityList returnList = null;
+    if (content != null) {
+      String id = content.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.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 {
+    return getMultiMediaForContent(DatabaseVideo.getInstance(), content);
+  }
+
+  /**
+   * get all the images belonging to a content entity
+   */
+  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 {
+    return getMultiMediaForContent(DatabaseOther.getInstance(), content);
+  }
+
+  /**
+   * get all the uploaded/other Media belonging to a content entity
+   */
+  public EntityList getUploadedMedia(EntityContent content) throws StorageObjectFailure {
+    return getMultiMediaForContent(DatabaseUploadedMedia.getInstance(), content);
+  }
+
+
+  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 " + mainTable + " where content_id=" + contentId;
+
+    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 content_id=contentId
+    for (int i = 0; i < mediaId.length; i++) {
+      sql = "insert into " + mainTable + " (content_id,media_id) values ("
+          + contentId + "," + 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);
+      }
+    }
+  }
+
+  public void addMedia(String contentId, String mediaId) throws
+      StorageObjectFailure {
+    if (contentId == null && mediaId == null) {
+      return;
+    }
+
+    Connection con = null;
+    Statement stmt = null;
+    //now insert
+
+    String sql = "insert into " + mainTable + " (content_id,media_id) values ("
+        + contentId + "," + mediaId + ")";
+    try {
+      con = obtainConnection();
+      // should be a preparedStatement because is faster
+      stmt = con.createStatement();
+      int rs = executeUpdate(stmt, sql);
+    }
+    catch (Exception e) {
+      logger.error("-- add media failed -- insert");
+      throw new StorageObjectFailure("-- add media failed -- insert ", e);
+    }
+    finally {
+      freeConnection(con, stmt);
+    }
+  }
+
+  public void setMedia(String contentId, String mediaId) throws
+      StorageObjectFailure {
+    if (contentId == null && mediaId == null) {
+      return;
+    }
+    //first delete all row with content_id=contentId
+    String sql = "delete from " + mainTable + " where content_id=" + contentId;
+
+    Connection con = null;
+    Statement stmt = null;
+    try {
+      con = obtainConnection();
+      // should be a preparedStatement because is faster
+      stmt = con.createStatement();
+      int rs = executeUpdate(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 content_id=contentId
+
+    sql = "insert into " + mainTable + " (content_id,media_id) values ("
+        + contentId + "," + mediaId + ")";
+    try {
+      con = obtainConnection();
+      // should be a preparedStatement because is faster
+      stmt = con.createStatement();
+      int rs = executeUpdate(stmt, sql);
+    }
+    catch (Exception e) {
+      logger.error("-- set media failed -- insert");
+      throw new StorageObjectFailure("-- set media failed -- insert ", e);
+    }
+    finally {
+      freeConnection(con, stmt);
+    }
+  }
+
+  public void deleteByContentId(String contentId) throws StorageObjectFailure {
+    if (contentId == null) {
+      //theLog.printDebugInfo("-- delete topics failed -- no content id");
+      return;
+    }
+    //delete all row with content_id=contentId
+    String sql = "delete from " + mainTable + " where content_id=" + contentId;
+
+    Connection con = null;
+    Statement stmt = null;
+    try {
+      con = obtainConnection();
+      // should be a preparedStatement because is faster
+      stmt = con.createStatement();
+      int rs = executeUpdate(stmt, sql);
+    }
+    catch (Exception e) {
+      logger.error("-- delete by contentId failed  ");
+      throw new StorageObjectFailure(
+          "-- delete by content id failed -- delete ", e);
+    }
+    finally {
+      freeConnection(con, stmt);
+    }
+  }
+
+  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 " + mainTable + " where media_id=" + mediaId;
+
+    Connection con = null;
+    Statement stmt = null;
+    try {
+      con = obtainConnection();
+      // should be a preparedStatement because is faster
+      stmt = con.createStatement();
+      int rs = executeUpdate(stmt, sql);
+      logger.debug("-- delete media success ");
+    }
+    catch (Exception e) {
+      logger.error("-- delete media failed ");
+      throw new StorageObjectFailure("-- delete by media id failed -- ", e);
+    }
+    finally {
+      freeConnection(con, stmt);
+    }
+  }
+
+  public void delete(String contentId, String mediaId) throws
+      StorageObjectFailure {
+    if (mediaId == null || contentId == null) {
+      logger.debug("-- delete media failed -- missing parameter");
+      return;
+    }
+    //delete all row with content_id=contentId and media_id=mediaId
+    String sql = "delete from " + mainTable + " where media_id=" + mediaId +
+        " and content_id= " + contentId;
+
+    Connection con = null;
+    Statement stmt = null;
+    try {
+      con = obtainConnection();
+      // should be a preparedStatement because is faster
+      stmt = con.createStatement();
+      int rs = executeUpdate(stmt, sql);
+      logger.debug("-- delete content_x_media success ");
+    }
+    catch (Exception e) {
+      logger.error("-- delete content_x_media failed ");
+      throw new StorageObjectFailure("-- delete content_x_media failed -- ", e);
+    }
+    finally {
+      freeConnection(con, stmt);
+    }
+  }
+
+  public EntityList getContent(EntityUploadedMedia media) throws
+      StorageObjectFailure {
+
+    EntityList returnList = null;
+    if (media != null) {
+
+      String id = media.getId();
+      Vector extraTables = new Vector();
+      extraTables.add(mainTable + " cxm");
+
+      String mediaSelect = "cxm.content_id=c.id and cxm.media_id="+id;
+      try {
+        returnList = DatabaseContent.getInstance().selectByWhereClause("c",
+          extraTables, mediaSelect, "c.id" );
+
+      }
+      catch (Exception e) {
+        logger.error("-- get content failed");
+        throw new StorageObjectFailure("-- get content failed -- ", e);
+      }
+    }
+    return returnList;
+  }
+
+  /**
+   * Returns a EntityList with all content-objects having a relation to a media
+   */
+
+  public EntityList getContent() throws StorageObjectFailure {
+
+    EntityList returnList = null;
+
+    Vector extraTables = new Vector();
+    extraTables.add(mainTable + " cxm");
+
+    String mediaSelect = "cxm.content_id=c.id";
+    try {
+      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);
+    }
+    return returnList;
+  }
+
+}