Organizing import, refresh the license (zapata deleted cos.jar)
[mir.git] / source / mircoders / entity / EntityImages.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with  any library licensed under the Apache Software License, 
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library 
23  * (or with modified versions of the above that use the same license as the above), 
24  * and distribute linked combinations including the two.  You must obey the 
25  * GNU General Public License in all respects for all of the code used other than 
26  * the above mentioned libraries.  If you modify this file, you may extend this 
27  * exception to your version of the file, but you are not obligated to do so.  
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30
31 package mircoders.entity;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.sql.Connection;
37 import java.sql.PreparedStatement;
38 import java.sql.ResultSet;
39 import java.sql.SQLException;
40 import java.sql.Statement;
41
42 import mir.config.MirPropertiesConfiguration;
43 import mir.log.LoggerWrapper;
44 import mir.misc.FileUtil;
45 import mir.misc.WebdbImage;
46 import mir.storage.StorageObject;
47 import mir.storage.StorageObjectFailure;
48
49 import org.postgresql.largeobject.BlobInputStream;
50 import org.postgresql.largeobject.LargeObject;
51 import org.postgresql.largeobject.LargeObjectManager;
52
53 /**
54  * Diese Klasse enth?lt die Daten eines MetaObjekts
55  *
56  * @author RK, mh, mir-coders
57  * @version $Id: EntityImages.java,v 1.20 2003/04/21 12:42:53 idfx Exp $
58  */
59
60
61 public class EntityImages extends EntityUploadedMedia
62 {
63
64   public EntityImages()
65   {
66     super();
67
68     logger = new LoggerWrapper("Entity.UploadedMedia.Images");
69   }
70
71   public EntityImages(StorageObject theStorage) {
72     this();
73     setStorage(theStorage);
74   }
75
76   //
77   // methods
78
79
80   public InputStream getImage() throws StorageObjectFailure {
81     logger.debug("EntityImages.getimage started");
82     java.sql.Connection con=null;
83     Statement stmt=null;
84     BlobInputStream in;
85     InputStream img_in = null;
86     try {
87       con = theStorageObject.getPooledCon();
88       con.setAutoCommit(false);
89       LargeObjectManager lom;
90       java.sql.Connection jCon;
91       stmt = con.createStatement();
92       ResultSet rs = theStorageObject.executeSql(stmt,
93           "select image_data from images where id="+getId());
94       jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
95            .getNativeConnection();
96       lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
97       if(rs!=null) {
98         if (rs.next()) {
99           LargeObject lob = lom.open(rs.getInt(1));
100           in = (BlobInputStream)lob.getInputStream();
101           img_in = new ImageInputStream(in, con, stmt);
102         }
103         rs.close();
104       }
105     }
106     catch (Throwable t) {
107       logger.error("EntityImages.getImage failed: " + t.toString());
108       t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
109
110       try {
111         con.setAutoCommit(true);
112       }
113       catch (Throwable e) {
114         logger.error("EntityImages.getImage resetting transaction mode failed: " + e.toString());
115         e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
116       }
117
118       try {
119         theStorageObject.freeConnection(con, stmt);
120       }
121       catch (Throwable e) {
122         logger.error("EntityImages.getImage freeing connection failed: " +e.toString());
123       }
124
125       throwStorageObjectFailure(t, "EntityImages -- getImage failed: ");
126     }
127     return img_in;
128   }
129
130   public void setImage(InputStream in, String type)
131         throws StorageObjectFailure {
132
133     if (in != null) {
134       Connection con = null;
135       PreparedStatement pstmt = null;
136       File f = null;
137       try {
138         logger.debug("EntityImages.settimage :: making internal representation of image");
139
140         File tempDir = new File(MirPropertiesConfiguration.instance().getString("TempDir"));
141         f = File.createTempFile("mir", ".tmp", tempDir);
142         FileUtil.write(f, in);
143         WebdbImage webdbImage= new WebdbImage(f, type);
144         logger.debug("EntityImages.settimage :: made internal representation of image");
145
146         con = theStorageObject.getPooledCon();
147         con.setAutoCommit(false);
148         logger.debug("EntityImages.settimage :: trying to insert image");
149
150         // setting values
151         LargeObjectManager lom;
152         java.sql.Connection jCon;
153         jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
154              .getNativeConnection();
155
156         lom = ((org.postgresql.Connection) jCon).getLargeObjectAPI();
157
158         int oidImage = lom.create();
159         int oidIcon = lom.create();
160         LargeObject lobImage = lom.open(oidImage);
161         LargeObject lobIcon = lom.open(oidIcon);
162         webdbImage.setImage(lobImage.getOutputStream());
163         webdbImage.setIcon(lobIcon.getOutputStream());
164         lobImage.close();
165         lobIcon.close();
166
167         setValueForProperty("img_height", new Integer(webdbImage.getImageHeight()).toString());
168         setValueForProperty("img_width", new Integer(webdbImage.getImageWidth()).toString());
169         setValueForProperty("icon_height", new Integer(webdbImage.getIconHeight()).toString());
170         setValueForProperty("icon_width", new Integer(webdbImage.getIconWidth()).toString());
171         setValueForProperty("image_data", new Integer(oidImage).toString());
172         setValueForProperty("icon_data", new Integer(oidIcon).toString());
173         update();
174       } catch (Exception e) {
175         throwStorageObjectFailure(e, "settimage :: setImage gescheitert: ");
176       } finally {
177         try {
178           if (con!=null)
179             con.setAutoCommit(true);
180           // get rid of the temp. file
181           f.delete();
182         } catch (SQLException e) {
183           throwStorageObjectFailure(e,"Resetting transaction-mode failed");
184         }
185         if (con!=null)
186           theStorageObject.freeConnection(con,pstmt);
187       }
188     }
189   }
190
191   /**
192    * Takes an OutputStream as an argument and reads in the data
193    * from the DB and writes it to the OutputStream.
194    *
195    * It will also take care of closing the OutputStream.
196    */
197   public InputStream getIcon() throws StorageObjectFailure {
198     Connection con=null;
199     Statement stmt=null;
200     BlobInputStream in=null;
201     ImageInputStream img_in=null;
202
203     try {
204       con = theStorageObject.getPooledCon();
205       con.setAutoCommit(false);
206       LargeObjectManager lom;
207       java.sql.Connection jCon;
208       stmt = con.createStatement();
209       ResultSet rs = theStorageObject.executeSql(stmt, "select icon_data from images where id="+getId());
210       jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
211            .getNativeConnection();
212       lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
213       if(rs!=null) {
214         if (rs.next()) {
215           LargeObject lob = lom.open(rs.getInt(1));
216           in = (BlobInputStream)lob.getInputStream();
217           img_in = new ImageInputStream( in, con ,stmt);
218           //img_data = rs.getBytes(1);
219         }
220         rs.close();
221       }
222     }
223     catch (Throwable t) {
224       logger.error("EntityImages.getIcon failed: "+t.toString());
225       t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
226
227       try {
228         con.setAutoCommit(true);
229       }
230       catch (SQLException e) {
231         logger.error("EntityImages.getIcon resetting transaction mode failed: " + e.toString());
232         e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
233       }
234       try {
235         theStorageObject.freeConnection(con, stmt);
236       }
237       catch (Throwable e) {
238        logger.error("EntityImages -- freeing connection failed: " + e.getMessage());
239       }
240
241       throwStorageObjectFailure(t, "EntityImages -- getIcon failed:");
242     }
243
244     return img_in;
245   }
246
247   /**
248    * a small wrapper class that allows us to store the DB connection resources
249    * that the BlobInputStream is using and free them upon closing of the stream
250    */
251   private class ImageInputStream extends InputStream {
252
253     InputStream _in;
254     Connection _con;
255     Statement _stmt;
256
257     public ImageInputStream(BlobInputStream in, Connection con,
258                             Statement stmt ) {
259       _in = in;
260       _con = con;
261       _stmt = stmt;
262     }
263
264     public void close () throws IOException {
265       _in.close();
266       try {
267         _con.setAutoCommit(true);
268         theStorageObject.freeConnection(_con,_stmt);
269       } catch (Exception e) {
270         throw new IOException("close(): "+e.toString());
271       }
272     }
273
274     public int read() throws IOException {
275       return _in.read();
276     }
277   }
278 }