1-n-relation content_media
authoridfx <idfx>
Sat, 1 Sep 2001 23:54:25 +0000 (23:54 +0000)
committeridfx <idfx>
Sat, 1 Sep 2001 23:54:25 +0000 (23:54 +0000)
source/mircoders/entity/EntityMedia.java [new file with mode: 0755]
source/mircoders/servlet/ServletModuleContent.java
source/mircoders/servlet/ServletModuleOpenIndy.java
source/mircoders/storage/DatabaseContentToMedia.java [new file with mode: 0755]

diff --git a/source/mircoders/entity/EntityMedia.java b/source/mircoders/entity/EntityMedia.java
new file mode 100755 (executable)
index 0000000..a0f9321
--- /dev/null
@@ -0,0 +1,40 @@
+package mircoders.entity;
+
+import java.lang.*;
+import java.io.*;
+import java.util.*;
+import java.sql.*;
+
+import mir.entity.*;
+import mir.misc.*;
+import mir.storage.*;
+
+import mircoders.storage.*;
+/**
+ * Diese Klasse enthält die Daten eines MetaObjekts
+ *
+ * @author RK
+ * @version 29.6.1999
+ */
+
+
+public class EntityMedia extends AbstractEntity implements Entity
+{
+  private static int instances;
+
+  public EntityMedia(){
+    super();
+    instances++;
+  }
+
+  public EntityMedia(StorageObject theStorage)
+  {
+    this();
+    setStorage(theStorage);
+  }
+
+  public void finalize() {
+    instances--;
+    super.finalize();
+  }
+}
index e01d76f..6797d15 100755 (executable)
@@ -254,6 +254,7 @@ public class ServletModuleContent extends ServletModule
     String  mediaIdParam = req.getParameter("mid");
     String  idParam = req.getParameter("cid");
     if (idParam == null||mediaIdParam==null) throw new ServletModuleException("smod content :: attach :: cid/mid missing");
+    
     try {
       EntityContent entContent = (EntityContent)mainModule.getById(idParam);
       entContent.attach(mediaIdParam);
@@ -261,6 +262,12 @@ public class ServletModuleContent extends ServletModule
     catch(ModuleException e) {
       theLog.printError("smod content :: attach :: could not get entityContent");
     }
+
+    try{
+      DatabaseContentToMedia.getInstance().setMedia(idParam,mediaIdParam);
+    } catch(Exception e){
+      theLog.printError("set media failed");
+    }
     _showObject(idParam, req, res);
   }
 
@@ -268,6 +275,9 @@ public class ServletModuleContent extends ServletModule
   {
     String  idParam = req.getParameter("cid");
     if (idParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
+    
+    //1-1-relation
+    //schould be deleted soon
     try {
       EntityContent entContent = (EntityContent)mainModule.getById(idParam);
       entContent.dettach();
@@ -275,6 +285,13 @@ public class ServletModuleContent extends ServletModule
     catch(ModuleException e) {
       theLog.printError("smod content :: dettach :: could not get entityContent");
     }
+    
+    //1-n-relation
+    try{
+      DatabaseContentToMedia.getInstance().deleteByContentId(idParam);
+    } catch(Exception e){
+      theLog.printError("remove media failed");
+    }
     _showObject(idParam, req, res);
   }
 
index eaedd49..6e8877f 100755 (executable)
@@ -140,6 +140,7 @@ public class ServletModuleOpenIndy extends ServletModule
     throws ServletModuleException
   {
     SimpleHash mergeData = new SimpleHash();
+    boolean setMedia=false;
 
     try {
 
@@ -184,6 +185,7 @@ public class ServletModuleOpenIndy extends ServletModule
           if (fileType>=0) {
             entImage.setImage(mediaData, fileType);
             withValues.put("to_media",mediaId);
+            setMedia=true;
           }
           else
             theLog.printDebugInfo("Wrong file uploaded!" + fileName);
@@ -202,6 +204,13 @@ public class ServletModuleOpenIndy extends ServletModule
 
       // inserting  content into database
       String id = contentModule.add(withValues);
+      
+      // inserting content and media id in table content_x_media
+      try{
+        DatabaseContentToMedia.getInstance().setMedia(id,mediaId);
+      } catch (Exception e) {
+        theLog.printError("setting content_x_topic failed");
+      }
 
 
       // producing new page
@@ -214,6 +223,7 @@ public class ServletModuleOpenIndy extends ServletModule
       new ProducerContent().handle(null, null, false, false,id);
 
       // sync the server
+      //should be configureable
       int exitValue = Helper.rsync();
 
     }
diff --git a/source/mircoders/storage/DatabaseContentToMedia.java b/source/mircoders/storage/DatabaseContentToMedia.java
new file mode 100755 (executable)
index 0000000..9a47575
--- /dev/null
@@ -0,0 +1,215 @@
+package mircoders.storage;
+
+import java.lang.*;
+import java.sql.*;
+import java.io.*;
+import java.util.*;
+
+import freemarker.template.*;
+
+import mir.storage.*;
+import mir.entity.*;
+import mir.misc.*;
+
+import mircoders.entity.*;
+
+/**
+ * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
+ *
+ *
+ */
+
+public class DatabaseContentToMedia extends Database implements StorageObject{
+
+  private static DatabaseContentToMedia instance;
+
+  public static DatabaseContentToMedia getInstance()
+    throws StorageObjectException {
+    if (instance == null) {
+      instance = new DatabaseContentToMedia();
+      instance.myselfDatabase = instance;
+    }
+    return instance;
+  }
+
+  private DatabaseContentToMedia()
+    throws StorageObjectException {
+
+    super();
+    this.hasTimestamp = false;
+    this.theTable="content_x_media";
+  }
+
+
+  public EntityList getMedia(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 = DatabaseMedia.getInstance().selectByWhereClause(subselect,-1);
+      } catch (Exception e) {
+        theLog.printDebugInfo("-- get media failed " + e.toString());
+      }
+    }
+    return returnList;
+  }
+
+
+  public void setMedia(String contentId, String[] mediaId) {
+    if (contentId == null){
+      return;
+    }
+    if (mediaId==null || mediaId[0]==null) {
+      return;
+    }
+    //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();
+      // should be a preparedStatement because is faster
+      stmt = con.createStatement();
+      ResultSet rs = executeSql(stmt,sql);
+    } catch (Exception e) {
+      theLog.printDebugInfo("-- set media failed -- delete");
+    } 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 "+ theTable +" (content_id,media_id) values ("
+            + contentId + "," + mediaId[i] + ")";
+      try {
+        con = getPooledCon();
+        // should be a preparedStatement because is faster
+        stmt = con.createStatement();
+        ResultSet rs = executeSql(stmt,sql);
+      } catch (Exception e) {
+        theLog.printDebugInfo("-- set topics failed -- insert");
+      } finally {
+        freeConnection(con,stmt);
+      }
+    }
+  }
+
+
+  public void setMedia(String contentId, String mediaId) {
+    if (contentId == null && mediaId == null) {
+      return;
+    }
+    //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();
+      // should be a preparedStatement because is faster
+      stmt = con.createStatement();
+      int rs = executeUpdate(stmt,sql);
+    } catch (Exception e) {
+      theLog.printDebugInfo("-- set media failed -- delete");
+    } finally {
+      freeConnection(con,stmt);
+    }
+
+    //now insert
+    //first delete all row with content_id=contentId
+
+    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("-- set media failed -- insert");
+    } finally {
+      freeConnection(con,stmt);
+    }
+  }
+
+  public void deleteByContentId(String contentId) {
+    if (contentId == null) {
+      //theLog.printDebugInfo("-- delete topics failed -- no content id");
+      return;
+    }
+    //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();
+      // should be a preparedStatement because is faster
+      stmt = con.createStatement();
+      ResultSet rs = executeSql(stmt,sql);
+    } catch (Exception e) {
+      //theLog.printDebugInfo("-- delete topics failed  ");
+    } finally {
+      freeConnection(con,stmt);
+    }
+  }
+
+  public void deleteByMediaId(String mediaId) {
+    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 topic_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);
+      theLog.printDebugInfo("-- delete media success ");
+    } catch (Exception e) {
+      theLog.printDebugInfo("-- delete media failed ");
+    } finally {
+      freeConnection(con,stmt);
+    }
+  }
+
+
+  public EntityList getContent(EntityMedia media) {
+    EntityList returnList=null;
+    if (media != null) {
+      String id = media.getId();
+      String select = "select content_id from " + theTable + " where media_id=" + id;
+
+      // 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,-1);
+        }
+      }
+      catch (Exception e) {theLog.printDebugInfo("-- get content failed");}
+      finally { freeConnection(con,stmt);}
+    }
+    return returnList;
+  }
+
+}