1-n-content-media, tomcat-session-tracking without cookies, and more
[mir.git] / source / mircoders / storage / DatabaseContentToMedia.java
index 9a47575..e154fcb 100755 (executable)
@@ -90,7 +90,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 +99,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 +170,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 +184,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 +199,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 +254,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;
+  }
 
 }