1.1 restoration
[mir.git] / source / mircoders / entity / EntityImages.java
index 82be8d5..1cff969 100755 (executable)
 
 package mircoders.entity;
 
-import java.io.File;
-import java.io.IOException;
+import java.io.ByteArrayOutputStream;
 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.storage.StorageObject;
 import mir.storage.StorageObjectFailure;
+import mir.util.StreamCopier;
 import mircoders.media.ImageProcessor;
 
-import org.postgresql.largeobject.BlobInputStream;
-import org.postgresql.largeobject.LargeObject;
-import org.postgresql.largeobject.LargeObjectManager;
-
 /**
  *
  * @author RK, mh, mir-coders
- * @version $Id: EntityImages.java,v 1.21.2.4 2004/01/18 17:30:58 zapata Exp $
+ * @version $Id: EntityImages.java,v 1.21.2.5 2004/11/21 22:07:13 zapata Exp $
  */
 
 
@@ -77,134 +67,52 @@ public class EntityImages extends EntityUploadedMedia
     setStorage(theStorage);
   }
 
-  //
-  // methods
-
-
+  /**
+   * Retrieves the image data
+   */
   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 = storageObject.getPooledCon();
-      con.setAutoCommit(false);
-      LargeObjectManager lom;
-      java.sql.Connection jCon;
-      stmt = con.createStatement();
-      ResultSet rs = storageObject.executeSql(stmt,
-          "select image_data from images where id="+getId());
-      jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
-           .getNativeConnection();
-      lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
-      if(rs!=null) {
-        if (rs.next()) {
-          LargeObject lob = lom.open(rs.getInt(1));
-          in = (BlobInputStream)lob.getInputStream();
-          img_in = new ImageInputStream(in, con, stmt);
-        }
-        rs.close();
-      }
+      return storageObject.getBinaryField("select image_data from images where id="+getId());
     }
-    catch (Throwable t) {
-      logger.error("EntityImages.getImage failed: " + t.toString());
-      t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
-
-      try {
-        con.setAutoCommit(true);
-      }
-      catch (Throwable e) {
-        logger.error("EntityImages.getImage resetting transaction mode failed: " + e.toString());
-        e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
-      }
-
-      try {
-        storageObject.freeConnection(con, stmt);
-      }
-      catch (Throwable e) {
-        logger.error("EntityImages.getImage freeing connection failed: " +e.toString());
-      }
-
-      throw new StorageObjectFailure(t);
+    catch (SQLException e) {
+      throw new StorageObjectFailure(e);
     }
-
-    return img_in;
   }
 
-  public void setImage(InputStream in, String type) throws StorageObjectFailure {
-    // todo: failures should be treated in a better way: exception -> rollback instead
+  /**
+   * Processes and saves image data
+   */
+  public void setImage(InputStream anInputStream, String type) throws StorageObjectFailure {
+    // todo: failures should be treated anInputStream a better way: exception -> rollback instead
     //  of commit
-    if (in != null) {
-
-      Connection con = null;
-      PreparedStatement pstmt = null;
-      File f = null;
+    if (anInputStream != null) {
       try {
-        logger.debug("EntityImages.settimage :: making internal representation of image");
+        ByteArrayOutputStream inputData = new ByteArrayOutputStream();
+        StreamCopier.copy(anInputStream, inputData);
 
-        f = File.createTempFile("mir", ".tmp",
-                new File(MirPropertiesConfiguration.instance().getString("TempDir")));
-        FileUtil.write(f, in);
-        ImageProcessor processor = new ImageProcessor(f);
-
-        con = storageObject.getPooledCon();
-        con.setAutoCommit(false);
-        LargeObjectManager lom;
-        java.sql.Connection connection;
-        connection = ((com.codestudio.sql.PoolManConnectionHandle)con).getNativeConnection();
+        ImageProcessor processor = new ImageProcessor(inputData.toByteArray());
+        processor.descaleImage(maxImageSize, minDescaleRatio, minDescaleReduction);
 
-        lom = ((org.postgresql.Connection) connection).getLargeObjectAPI();
+        ByteArrayOutputStream imageData = new ByteArrayOutputStream();
+        processor.writeScaledData(imageData, type);
+        storageObject.setBinaryField("update images set image_data = ? where id = "+getId(), imageData.toByteArray());
 
-        int oidImage = lom.create();
-        LargeObject lobImage = lom.open(oidImage);
-        processor.descaleImage(maxImageSize, minDescaleRatio, minDescaleReduction);
-        processor.writeScaledData(lobImage.getOutputStream(), type);
-        lobImage.close();
         setFieldValue("img_height", new Integer(processor.getScaledHeight()).toString());
         setFieldValue("img_width", new Integer(processor.getScaledWidth()).toString());
 
-        int oidIcon = lom.create();
-        LargeObject lobIcon = lom.open(oidIcon);
+        imageData.reset();
         processor.descaleImage(maxIconSize, minDescaleRatio, minDescaleReduction);
-        processor.writeScaledData(lobIcon.getOutputStream(), type);
-        lobIcon.close();
+        processor.writeScaledData(imageData, type);
+        storageObject.setBinaryField("update images set icon_data = ? where id = "+getId(), imageData.toByteArray());
 
         setFieldValue("icon_height", new Integer(processor.getScaledHeight()).toString());
         setFieldValue("icon_width", new Integer(processor.getScaledWidth()).toString());
 
-        setFieldValue("image_data", new Integer(oidImage).toString());
-        setFieldValue("icon_data", new Integer(oidIcon).toString());
         update();
       }
       catch (Exception e) {
         throw new StorageObjectFailure(e);
       }
-      finally {
-        try {
-          if (con!=null)
-            con.setAutoCommit(true);
-          // get rid of the temp. file
-        }
-        catch (Throwable e) {
-          logger.error("EntityImages.setImage: unable to reset the connection to auto-commit:" + e.toString());
-        }
-        try {
-          f.delete();
-        }
-        catch (Throwable t) {
-          logger.error("EntityImages.setImage: unable to delete the temporary file:" + t.toString());
-        }
-
-        try {
-          if (con!=null)
-            storageObject.freeConnection(con,pstmt);
-        }
-        catch (Throwable t) {
-          logger.error("EntityImages.setImage: unable to free the connection:" + t.toString());
-        }
-      }
     }
   }
 
@@ -215,85 +123,11 @@ public class EntityImages extends EntityUploadedMedia
    * It will also take care of closing the OutputStream.
    */
   public InputStream getIcon() throws StorageObjectFailure {
-    Connection con=null;
-    Statement stmt=null;
-    BlobInputStream in=null;
-    ImageInputStream img_in=null;
-
     try {
-      con = storageObject.getPooledCon();
-      con.setAutoCommit(false);
-      LargeObjectManager lom;
-      java.sql.Connection jCon;
-      stmt = con.createStatement();
-      ResultSet rs = storageObject.executeSql(stmt, "select icon_data from images where id="+getId());
-      jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
-           .getNativeConnection();
-      lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
-      if(rs!=null) {
-        if (rs.next()) {
-          LargeObject lob = lom.open(rs.getInt(1));
-          in = (BlobInputStream)lob.getInputStream();
-          img_in = new ImageInputStream( in, con ,stmt);
-          //img_data = rs.getBytes(1);
-        }
-        rs.close();
-      }
+      return storageObject.getBinaryField("select icon_data from images where id="+getId());
     }
-    catch (Throwable t) {
-      logger.error("EntityImages.getIcon failed: "+t.toString());
-      t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
-
-      try {
-        con.setAutoCommit(true);
-      }
-      catch (SQLException e) {
-        logger.error("EntityImages.getIcon resetting transaction mode failed: " + e.toString());
-        e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
-      }
-      try {
-        storageObject.freeConnection(con, stmt);
-      }
-      catch (Throwable e) {
-       logger.error("EntityImages -- freeing connection failed: " + e.getMessage());
-      }
-
-      throw new StorageObjectFailure(t);
-    }
-
-    return img_in;
-  }
-
-  /**
-   * a small wrapper class that allows us to store the DB connection resources
-   * that the BlobInputStream is using and free them upon closing of the stream
-   */
-  private class ImageInputStream extends InputStream {
-
-    InputStream _in;
-    Connection _con;
-    Statement _stmt;
-
-    public ImageInputStream(BlobInputStream in, Connection con,
-                            Statement stmt ) {
-      _in = in;
-      _con = con;
-      _stmt = stmt;
-    }
-
-    public void close () throws IOException {
-      _in.close();
-      try {
-        _con.setAutoCommit(true);
-        storageObject.freeConnection(_con,_stmt);
-      }
-      catch (Exception e) {
-        throw new IOException("close(): "+e.toString());
-      }
-    }
-
-    public int read() throws IOException {
-      return _in.read();
+    catch (SQLException e) {
+      throw new StorageObjectFailure(e);
     }
   }
 }