added cache in EntityContent for comments, media, etc.. this cache must
[mir.git] / source / mircoders / storage / DatabaseContentToMedia.java
index 5bfe5af..1569bb4 100755 (executable)
@@ -39,7 +39,7 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
     this.hasTimestamp = false;
     this.theTable="content_x_media";
   }
-  
+
   /**
    * get all the media-files belonging to a content entity
    *
@@ -62,7 +62,75 @@ 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
+   *
+   */
+  public EntityList getAudio(EntityContent content)
+    throws StorageObjectException {
+    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 = DatabaseAudio.getInstance().selectByWhereClause(subselect,-1);
+      } catch (Exception e) {
+        theLog.printDebugInfo("-- get audio failed " + e.toString());
+        throw new StorageObjectException("-- get audio failed " + e.toString());
+      }
+    }
+    return returnList;
+  }
+
+  /**
+   * get all the video belonging to a content entity
+   *
+   */
+  public EntityList getVideo(EntityContent content)
+    throws StorageObjectException {
+    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 = DatabaseVideo.getInstance().selectByWhereClause(subselect,-1);
+      } catch (Exception e) {
+        theLog.printDebugInfo("-- get video failed " + e.toString());
+        throw new StorageObjectException("-- get video failed " + e.toString());
+      }
+    }
+    return returnList;
+  }
+
   /**
    * get all the images belonging to a content entity
    *
@@ -88,23 +156,60 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
 
 
   /**
-   * get all the uploaded Media belonging to a content entity
+   * get all the uploaded/other Media belonging to a content entity
+   *
+   */
+  public EntityList getOther(EntityContent content)
+    throws StorageObjectException
+  {
+    /** @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 = DatabaseOther.getInstance().selectByWhereClause(subselect,
+                                                                    "id");
+      } catch (Exception e) {
+        e.printStackTrace();
+        theLog.printDebugInfo("-- get Other failed " + e.toString());
+        throw new StorageObjectException("-- get Other failed "
+                                        + e.toString());
+      }
+    }
+    return returnList;
+  }
+
+  /**
+   * get all the uploaded/other Media belonging to a content entity
    *
    */
   public EntityList getUploadedMedia(EntityContent content)
-    throws StorageObjectException {
+    throws StorageObjectException
+  {
+    /** @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+")";
+      String subselect = "id in (select media_id from " + theTable +
+                                " where content_id=" + id+")";
 
       try {
-        returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,-1);
+        returnList = DatabaseUploadedMedia.getInstance().selectByWhereClause(subselect,
+                                                                    "id");
       } catch (Exception e) {
+        e.printStackTrace();
         theLog.printDebugInfo("-- get uploadedMedia failed " + e.toString());
-        throw new StorageObjectException("-- get uploadedMedia failed " + e.toString());
+        throw new StorageObjectException("-- get uploadedMedia failed "
+                                        + e.toString());
       }
     }
     return returnList;
@@ -121,7 +226,7 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
     }
     //first delete all row with content_id=contentId
     String sql = "delete from "+ theTable +" where content_id=" + contentId;
-  
+
     Connection con=null;Statement stmt=null;
     try {
       con = getPooledCon();
@@ -134,7 +239,7 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
     } finally {
       freeConnection(con,stmt);
     }
-  
+
     //now insert
     //first delete all row with content_id=contentId
     for (int i=0;i<mediaId.length;i++) {
@@ -159,10 +264,10 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
     if (contentId == null && mediaId == null) {
       return;
     }
-    
+
     Connection con=null;Statement stmt=null;
     //now insert
-    
+
     String sql = "insert into "+ theTable +" (content_id,media_id) values ("
           + contentId + "," + mediaId + ")";
     try {
@@ -178,7 +283,7 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
       freeConnection(con,stmt);
     }
   }
-  
+
   public void setMedia(String contentId, String mediaId)
     throws StorageObjectException {
     if (contentId == null && mediaId == null) {
@@ -268,7 +373,7 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
       freeConnection(con,stmt);
     }
   }
-  
+
   public void delete(String contentId, String mediaId)
     throws StorageObjectException {
     if (mediaId == null || contentId==null) {
@@ -331,15 +436,15 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
     }
     return returnList;
   }
-  
+
 /**
  * Returns a EntityList with all content-objects having a relation to a media
  */
-  
+
 public EntityList getContent()
     throws StorageObjectException {
     EntityList returnList=null;
-    
+
     String select = "select distinct content_id from " + theTable;
     // execute select statement
     Connection con=null;Statement stmt=null;