organising imports
[mir.git] / source / mircoders / entity / EntityImages.java
index a19577d..ea96e81 100755 (executable)
@@ -34,11 +34,14 @@ package mircoders.entity;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.sql.Statement;
 
 import mir.config.MirPropertiesConfiguration;
+import mir.log.LoggerWrapper;
 import mir.misc.FileUtil;
 import mir.misc.WebdbImage;
 import mir.storage.StorageObject;
@@ -52,7 +55,7 @@ import org.postgresql.largeobject.LargeObjectManager;
  * Diese Klasse enth?lt die Daten eines MetaObjekts
  *
  * @author RK, mh, mir-coders
- * @version $Id: EntityImages.java,v 1.14 2003/01/25 17:50:34 idfx Exp $
+ * @version $Id: EntityImages.java,v 1.19 2003/03/08 17:18:19 idfx Exp $
  */
 
 
@@ -62,6 +65,8 @@ public class EntityImages extends EntityUploadedMedia
   public EntityImages()
   {
     super();
+
+    logger = new LoggerWrapper("Entity.UploadedMedia.Images");
   }
 
   public EntityImages(StorageObject theStorage) {
@@ -73,12 +78,12 @@ public class EntityImages extends EntityUploadedMedia
   // methods
 
 
-  public InputStream getImage() throws StorageObjectFailure
-  {
-    theLog.printDebugInfo("--getimage started");
-    java.sql.Connection con=null;Statement stmt=null;
-    BlobInputStream in; InputStream img_in = null;
-
+  public InputStream getImage() throws StorageObjectFailure {
+    logger.debug("EntityImages.getimage started");
+    java.sql.Connection con=null;
+    Statement stmt=null;
+    BlobInputStream in;
+    InputStream img_in = null;
     try {
       con = theStorageObject.getPooledCon();
       con.setAutoCommit(false);
@@ -99,43 +104,49 @@ public class EntityImages extends EntityUploadedMedia
         rs.close();
       }
     }
-    catch (Exception e) {
-      e.printStackTrace();
-      theLog.printError("EntityImages -- getImage failed"+e.toString());
+    catch (Throwable t) {
+      logger.error("EntityImages.getImage failed: " + t.toString());
+      t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
+
       try {
         con.setAutoCommit(true);
-      } catch (Exception e2) {
-        e.printStackTrace();
-        theLog.printError(
-            "EntityImages -- getImage reseting transaction mode failed"
-            +e2.toString());
       }
-      theStorageObject.freeConnection(con,stmt);
-      throwStorageObjectFailure(e, "EntityImages -- getImage failed: ");
+      catch (Throwable e) {
+        logger.error("EntityImages.getImage resetting transaction mode failed: " + e.toString());
+        e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
+      }
+
+      try {
+        theStorageObject.freeConnection(con, stmt);
+      }
+      catch (Throwable e) {
+        logger.error("EntityImages.getImage freeing connection failed: " +e.toString());
+      }
+
+      throwStorageObjectFailure(t, "EntityImages -- getImage failed: ");
     }
-    //}
     return img_in;
   }
 
   public void setImage(InputStream in, String type)
-      throws StorageObjectFailure {
+        throws StorageObjectFailure {
 
-    if (in!=null) {
-      java.sql.Connection con=null;PreparedStatement pstmt=null;
+    if (in != null) {
+      Connection con = null;
+      PreparedStatement pstmt = null;
       File f = null;
       try {
-
-        theLog.printDebugInfo("settimage :: making internal representation of image");
+        logger.debug("EntityImages.settimage :: making internal representation of image");
 
         File tempDir = new File(MirPropertiesConfiguration.instance().getString("TempDir"));
         f = File.createTempFile("mir", ".tmp", tempDir);
         FileUtil.write(f, in);
         WebdbImage webdbImage= new WebdbImage(f, type);
-        theLog.printDebugInfo("settimage :: made internal representation of image");
+        logger.debug("EntityImages.settimage :: made internal representation of image");
 
         con = theStorageObject.getPooledCon();
         con.setAutoCommit(false);
-        theLog.printDebugInfo("settimage :: trying to insert image");
+        logger.debug("EntityImages.settimage :: trying to insert image");
 
         // setting values
         LargeObjectManager lom;
@@ -154,31 +165,24 @@ public class EntityImages extends EntityUploadedMedia
         lobImage.close();
         lobIcon.close();
 
-        setValueForProperty("img_height",
-                          new Integer(webdbImage.getImageHeight()).toString());
-        setValueForProperty("img_width",
-                          new Integer(webdbImage.getImageWidth()).toString());
-        setValueForProperty("icon_height",
-                          new Integer(webdbImage.getIconHeight()).toString());
-        setValueForProperty("icon_width",
-                          new Integer(webdbImage.getIconWidth()).toString());
+        setValueForProperty("img_height", new Integer(webdbImage.getImageHeight()).toString());
+        setValueForProperty("img_width", new Integer(webdbImage.getImageWidth()).toString());
+        setValueForProperty("icon_height", new Integer(webdbImage.getIconHeight()).toString());
+        setValueForProperty("icon_width", new Integer(webdbImage.getIconWidth()).toString());
         setValueForProperty("image_data", new Integer(oidImage).toString());
         setValueForProperty("icon_data", new Integer(oidIcon).toString());
         update();
-      }
-      catch (Exception e) {
+      } catch (Exception e) {
         throwStorageObjectFailure(e, "settimage :: setImage gescheitert: ");
-      }
-      finally {
+      } finally {
         try {
           if (con!=null)
             con.setAutoCommit(true);
           // get rid of the temp. file
           f.delete();
+        } catch (SQLException e) {
+          throwStorageObjectFailure(e,"Resetting transaction-mode failed");
         }
-        catch (Exception e) {
-        }
-
         if (con!=null)
           theStorageObject.freeConnection(con,pstmt);
       }
@@ -191,10 +195,11 @@ public class EntityImages extends EntityUploadedMedia
    *
    * It will also take care of closing the OutputStream.
    */
-  public InputStream getIcon() throws StorageObjectFailure
-  {
-    java.sql.Connection con=null;Statement stmt=null;
-    BlobInputStream in=null;ImageInputStream img_in=null;
+  public InputStream getIcon() throws StorageObjectFailure {
+    Connection con=null;
+    Statement stmt=null;
+    BlobInputStream in=null;
+    ImageInputStream img_in=null;
 
     try {
       con = theStorageObject.getPooledCon();
@@ -202,8 +207,7 @@ public class EntityImages extends EntityUploadedMedia
       LargeObjectManager lom;
       java.sql.Connection jCon;
       stmt = con.createStatement();
-      ResultSet rs = theStorageObject.executeSql(stmt,
-          "select icon_data from images where id="+getId());
+      ResultSet rs = theStorageObject.executeSql(stmt, "select icon_data from images where id="+getId());
       jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
            .getNativeConnection();
       lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
@@ -217,20 +221,25 @@ public class EntityImages extends EntityUploadedMedia
         rs.close();
       }
     }
-    catch (Exception e) {
-      e.printStackTrace();
-      theLog.printError("EntityImages -- getIcon failed"+e.toString());
+    catch (Throwable t) {
+      logger.error("EntityImages.getIcon failed: "+t.toString());
+      t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
+
       try {
         con.setAutoCommit(true);
       }
-      catch (Exception e2) {
-        e.printStackTrace();
-        theLog.printError(
-            "EntityImages -- getIcon reseting transaction mode failed"
-            +e2.toString());
+      catch (SQLException e) {
+        logger.error("EntityImages.getIcon resetting transaction mode failed: " + e.toString());
+        e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
+      }
+      try {
+        theStorageObject.freeConnection(con, stmt);
+      }
+      catch (Throwable e) {
+       logger.error("EntityImages -- freeing connection failed: " + e.getMessage());
       }
-      theStorageObject.freeConnection(con,stmt);
-      throwStorageObjectFailure(e, "EntityImages -- getIcon failed:");
+
+      throwStorageObjectFailure(t, "EntityImages -- getIcon failed:");
     }
 
     return img_in;
@@ -243,12 +252,11 @@ public class EntityImages extends EntityUploadedMedia
   private class ImageInputStream extends InputStream {
 
     InputStream _in;
-    java.sql.Connection _con;
+    Connection _con;
     Statement _stmt;
 
-    public ImageInputStream(BlobInputStream in, java.sql.Connection con,
-                            Statement stmt )
-    {
+    public ImageInputStream(BlobInputStream in, Connection con,
+                            Statement stmt ) {
       _in = in;
       _con = con;
       _stmt = stmt;
@@ -256,12 +264,10 @@ public class EntityImages extends EntityUploadedMedia
 
     public void close () throws IOException {
       _in.close();
-
       try {
         _con.setAutoCommit(true);
         theStorageObject.freeConnection(_con,_stmt);
-      }
-      catch (Exception e) {
+      } catch (Exception e) {
         throw new IOException("close(): "+e.toString());
       }
     }
@@ -269,6 +275,5 @@ public class EntityImages extends EntityUploadedMedia
     public int read() throws IOException {
       return _in.read();
     }
-
   }
 }