fixing scaling images -> nullpointer, patch by yossarian
[mir.git] / source / mircoders / storage / DatabaseContentToMedia.java
index 2f5b3b5..11a2b35 100755 (executable)
-package mircoders.storage;
-
-import java.lang.*;
-import java.sql.*;
-import java.io.*;
-import java.util.*;
+/*
+ * Copyright (C) 2001, 2002 The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with  any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two.  You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries.  If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
+ */
 
-import freemarker.template.*;
+package mircoders.storage;
 
-import mir.storage.*;
-import mir.entity.*;
-import mir.misc.*;
+import java.sql.Connection;
+import java.sql.Statement;
+import java.util.ArrayList;
 
-import mircoders.entity.*;
+import mir.entity.EntityList;
+import mir.log.LoggerWrapper;
+import mir.storage.Database;
+import mir.storage.DatabaseFailure;
+import mircoders.entity.EntityUploadedMedia;
 
 /**
- * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
+ * <b>implements abstract DB connection to the content_x_media SQL table
  *
+ * @author RK, mir-coders group
+ * @version $Id: DatabaseContentToMedia.java,v 1.19.2.10 2005/03/26 11:26:28 zapata Exp $
  *
  */
 
-public class DatabaseContentToMedia extends Database implements StorageObject{
+public class DatabaseContentToMedia extends Database {
 
   private static DatabaseContentToMedia instance;
 
-  public static DatabaseContentToMedia getInstance()
-    throws StorageObjectException {
+  public synchronized static DatabaseContentToMedia getInstance() {
     if (instance == null) {
       instance = new DatabaseContentToMedia();
-      instance.myselfDatabase = instance;
     }
     return instance;
   }
 
-  private DatabaseContentToMedia()
-    throws StorageObjectException {
-
+  private DatabaseContentToMedia() {
     super();
-    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) {
-      // 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;
+    logger = new LoggerWrapper("Database.ContentToMedia");
+    mainTable = "content_x_media";
+    entityClass = mir.entity.GenericEntity.class;
   }
-  
-  /**
-   * 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());
-      }
+  public void addMedia(String contentId, String mediaId) throws
+      DatabaseFailure {
+    if (contentId == null && mediaId == null) {
+      return;
     }
-    return returnList;
-  }
 
+    Connection con = null;
+    Statement stmt = null;
+    //now insert
 
-  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;
+    String sql = "insert into " + mainTable + " (content_id,media_id) values ("
+        + contentId + "," + mediaId + ")";
     try {
-      con = getPooledCon();
+      con = obtainConnection();
       // 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);
+      int rs = executeUpdate(stmt, sql);
     }
-  
-    //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();
-        int rs = executeUpdate(stmt,sql);
-      } catch (Exception e) {
-        theLog.printDebugInfo("-- set topics failed -- insert");
-      } finally {
-        freeConnection(con,stmt);
-      }
+    catch (Exception e) {
+      logger.error("-- add media failed -- insert");
+      throw new DatabaseFailure("-- add media failed -- insert ", e);
     }
-  }
-
-  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);
+    finally {
+      freeConnection(con, stmt);
     }
   }
-  
-  public void setMedia(String contentId, String mediaId) {
+
+  public void setMedia(String contentId, String mediaId) throws
+      DatabaseFailure {
     if (contentId == null && mediaId == null) {
       return;
     }
     //first delete all row with content_id=contentId
-    String sql = "delete from "+ theTable +" where content_id=" + contentId;
+    String sql = "delete from " + mainTable + " where content_id=" + contentId;
 
-    Connection con=null;Statement stmt=null;
+    Connection con = null;
+    Statement stmt = null;
     try {
-      con = getPooledCon();
+      con = obtainConnection();
       // 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);
+      int rs = executeUpdate(stmt, sql);
+    }
+    catch (Exception e) {
+      logger.error("-- set media failed -- delete");
+      throw new DatabaseFailure("-- set media failed -- delete ", e);
+    }
+    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 + ")";
+    sql = "insert into " + mainTable + " (content_id,media_id) values ("
+        + contentId + "," + mediaId + ")";
     try {
-      con = getPooledCon();
+      con = obtainConnection();
       // 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);
+      int rs = executeUpdate(stmt, sql);
+    }
+    catch (Exception e) {
+      logger.error("-- set media failed -- insert");
+      throw new DatabaseFailure("-- set media failed -- insert ", e);
+    }
+    finally {
+      freeConnection(con, stmt);
     }
   }
 
-  public void deleteByContentId(String contentId) {
+  public void deleteByContentId(String contentId) throws DatabaseFailure {
     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;
+    String sql = "delete from " + mainTable + " where content_id=" + contentId;
 
-    Connection con=null;Statement stmt=null;
+    Connection con = null;
+    Statement stmt = null;
     try {
-      con = getPooledCon();
+      con = obtainConnection();
       // should be a preparedStatement because is faster
       stmt = con.createStatement();
-      int rs = executeUpdate(stmt,sql);
-    } catch (Exception e) {
-      //theLog.printDebugInfo("-- delete topics failed  ");
-    } finally {
-      freeConnection(con,stmt);
+      int rs = executeUpdate(stmt, sql);
+    }
+    catch (Exception e) {
+      logger.error("-- delete by contentId failed  ");
+      throw new DatabaseFailure(
+          "-- delete by content id failed -- delete ", e);
+    }
+    finally {
+      freeConnection(con, stmt);
     }
   }
 
-  public void deleteByMediaId(String mediaId) {
+  public void deleteByMediaId(String mediaId) throws DatabaseFailure {
+
     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 media_id=" + mediaId;
+    String sql = "delete from " + mainTable + " where media_id=" + mediaId;
 
-    Connection con=null;Statement stmt=null;
+    Connection con = null;
+    Statement stmt = null;
     try {
-      con = getPooledCon();
+      con = obtainConnection();
       // should be a preparedStatement because is faster
       stmt = con.createStatement();
-      int rs = executeUpdate(stmt,sql);
-      theLog.printDebugInfo("-- delete media success ");
-    } catch (Exception e) {
-      theLog.printDebugInfo("-- delete media failed ");
-    } finally {
-      freeConnection(con,stmt);
+      int rs = executeUpdate(stmt, sql);
+      logger.debug("-- delete media success ");
+    }
+    catch (Exception e) {
+      logger.error("-- delete media failed ");
+      throw new DatabaseFailure("-- delete by media id failed -- ", e);
+    }
+    finally {
+      freeConnection(con, stmt);
     }
   }
-  
-  public void delete(String contentId, String mediaId) {
-    if (mediaId == null || contentId==null) {
-      theLog.printDebugInfo("-- delete media failed -- missing parameter");
+
+  public void delete(String contentId, String mediaId) throws
+      DatabaseFailure {
+    if (mediaId == null || contentId == null) {
+      logger.debug("-- 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;
+    String sql = "delete from " + mainTable + " where media_id=" + mediaId +
+        " and content_id= " + contentId;
 
-    Connection con=null;Statement stmt=null;
+    Connection con = null;
+    Statement stmt = null;
     try {
-      con = getPooledCon();
+      con = obtainConnection();
       // 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);
+      int rs = executeUpdate(stmt, sql);
+      logger.debug("-- delete content_x_media success ");
+    }
+    catch (Exception e) {
+      logger.error("-- delete content_x_media failed ");
+      throw new DatabaseFailure("-- delete content_x_media failed -- ", e);
+    }
+    finally {
+      freeConnection(con, stmt);
     }
   }
 
+  public EntityList getContent(EntityUploadedMedia media) throws
+      DatabaseFailure {
 
-  public EntityList getContent(EntityMedia media) {
-    EntityList returnList=null;
+    EntityList returnList = null;
     if (media != null) {
+
       String id = media.getId();
-      String select = "select content_id from " + theTable + " where media_id=" + id;
+      ArrayList extraTables = new ArrayList();
+      extraTables.add(mainTable + " cxm");
 
-      // execute select statement
-      Connection con=null;Statement stmt=null;
+      String mediaSelect = "cxm.content_id=c.id and cxm.media_id="+id;
       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);
-        }
+        returnList = DatabaseContent.getInstance().selectByWhereClause("c",
+          extraTables, mediaSelect, "c.id" );
+
+      }
+      catch (Exception e) {
+        logger.error("-- get content failed");
+        throw new DatabaseFailure("-- get content failed -- ", e);
       }
-      catch (Exception e) {theLog.printDebugInfo("-- get content failed");}
-      finally { freeConnection(con,stmt);}
     }
     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;
+
+  /**
+   * Returns a EntityList with all content-objects having a relation to a media
+   */
+
+  public EntityList getContent() throws DatabaseFailure {
+
+    EntityList returnList = null;
+
+    ArrayList extraTables = new ArrayList();
+    extraTables.add(mainTable + " cxm");
+
+    String mediaSelect = "cxm.content_id=c.id";
     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);}
+      returnList = DatabaseContent.getInstance().selectByWhereClause("c",
+        extraTables, mediaSelect, "c.webdb_lastchange desc" );
 
+    }
+    catch (Exception e) {
+      logger.error("-- get content failed");
+      throw new DatabaseFailure("-- get content failed -- ", e);
+    }
     return returnList;
   }