--- /dev/null
+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;
+ }
+
+}