more media handling stuff.
[mir.git] / source / mircoders / storage / DatabaseContentToMedia.java
index 9a47575..3a1fb52 100755 (executable)
@@ -39,8 +39,11 @@ 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
+   *
+   */
   public EntityList getMedia(EntityContent content) {
     EntityList returnList=null;
     if (content != null) {
@@ -57,6 +60,49 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
     }
     return returnList;
   }
+  
+  /**
+   * get all the images belonging to a content entity
+   *
+   */
+  public EntityList getImages(EntityContent content) {
+    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 = DatabaseImages.getInstance().selectByWhereClause(subselect,-1);
+      } catch (Exception e) {
+        theLog.printDebugInfo("-- get images failed " + e.toString());
+      }
+    }
+    return returnList;
+  }
+
+
+  /**
+   * get all the uploaded Media belonging to a content entity
+   *
+   */
+  public EntityList getUploadedMedia(EntityContent content) {
+    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,-1);
+      } catch (Exception e) {
+        theLog.printDebugInfo("-- get uploadedMedia failed " + e.toString());
+      }
+    }
+    return returnList;
+  }
 
 
   public void setMedia(String contentId, String[] mediaId) {
@@ -90,7 +136,7 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
         con = getPooledCon();
         // should be a preparedStatement because is faster
         stmt = con.createStatement();
-        ResultSet rs = executeSql(stmt,sql);
+        int rs = executeUpdate(stmt,sql);
       } catch (Exception e) {
         theLog.printDebugInfo("-- set topics failed -- insert");
       } finally {
@@ -99,7 +145,28 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
     }
   }
 
-
+  public void addMedia(String contentId, String mediaId) {
+    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 {
+      con = getPooledCon();
+      // should be a preparedStatement because is faster
+      stmt = con.createStatement();
+      int rs = executeUpdate(stmt,sql);
+    } catch (Exception e) {
+      theLog.printDebugInfo("-- add media failed -- insert");
+    } finally {
+      freeConnection(con,stmt);
+    }
+  }
+  
   public void setMedia(String contentId, String mediaId) {
     if (contentId == null && mediaId == null) {
       return;
@@ -149,7 +216,7 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
       con = getPooledCon();
       // should be a preparedStatement because is faster
       stmt = con.createStatement();
-      ResultSet rs = executeSql(stmt,sql);
+      int rs = executeUpdate(stmt,sql);
     } catch (Exception e) {
       //theLog.printDebugInfo("-- delete topics failed  ");
     } finally {
@@ -163,14 +230,14 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
       return;
     }
     //delete all row with content_id=contentId
-    String sql = "delete from "+ theTable +" where topic_id=" + mediaId;
+    String sql = "delete from "+ theTable +" where media_id=" + mediaId;
 
     Connection con=null;Statement stmt=null;
     try {
       con = getPooledCon();
       // should be a preparedStatement because is faster
       stmt = con.createStatement();
-      ResultSet rs = executeSql(stmt,sql);
+      int rs = executeUpdate(stmt,sql);
       theLog.printDebugInfo("-- delete media success ");
     } catch (Exception e) {
       theLog.printDebugInfo("-- delete media failed ");
@@ -178,6 +245,28 @@ public class DatabaseContentToMedia extends Database implements StorageObject{
       freeConnection(con,stmt);
     }
   }
+  
+  public void delete(String contentId, String mediaId) {
+    if (mediaId == null || contentId==null) {
+      theLog.printDebugInfo("-- delete media failed -- missing parameter");
+      return;
+    }
+    //delete all row with content_id=contentId and media_id=mediaId
+    String sql = "delete from "+ theTable +" where media_id=" + mediaId + " and content_id= "+contentId;
+
+    Connection con=null;Statement stmt=null;
+    try {
+      con = getPooledCon();
+      // should be a preparedStatement because is faster
+      stmt = con.createStatement();
+      int rs = executeUpdate(stmt,sql);
+      theLog.printDebugInfo("-- delete content_x_media success ");
+    } catch (Exception e) {
+      theLog.printDebugInfo("-- delete content_x_media failed ");
+    } finally {
+      freeConnection(con,stmt);
+    }
+  }
 
 
   public EntityList getContent(EntityMedia media) {
@@ -211,5 +300,39 @@ 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() {
+    EntityList returnList=null;
+    
+    String select = "select distinct content_id from " + theTable;
+    // 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 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");
+      }
+    }
+    catch (Exception e) {theLog.printDebugInfo("-- get content failed");}
+    finally { freeConnection(con,stmt);}
+
+    return returnList;
+  }
 
 }