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