Some small things like, reorganizing imports, accessing static variables
[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.PreparedStatement;\r
38 import java.sql.ResultSet;\r
39 import java.sql.Statement;\r
40 \r
41 import org.postgresql.largeobject.BlobInputStream;\r
42 import org.postgresql.largeobject.LargeObject;\r
43 import org.postgresql.largeobject.LargeObjectManager;\r
44 \r
45 import mir.config.MirPropertiesConfiguration;\r
46 import mir.misc.FileUtil;\r
47 import mir.misc.WebdbImage;\r
48 import mir.log.LoggerWrapper;\r
49 import mir.storage.StorageObject;\r
50 import mir.storage.StorageObjectFailure;\r
51 \r
52 /**\r
53  * Diese Klasse enth?lt die Daten eines MetaObjekts\r
54  *\r
55  * @author RK, mh, mir-coders\r
56  * @version $Id: EntityImages.java,v 1.16 2003/02/28 18:27:08 idfx Exp $\r
57  */\r
58 \r
59 \r
60 public class EntityImages extends EntityUploadedMedia\r
61 {\r
62 \r
63   public EntityImages()\r
64   {\r
65     super();\r
66 \r
67     logger = new LoggerWrapper("Entity.UploadedMedia.Images");\r
68   }\r
69 \r
70   public EntityImages(StorageObject theStorage) {\r
71     this();\r
72     setStorage(theStorage);\r
73   }\r
74 \r
75   //\r
76   // methods\r
77 \r
78 \r
79   public InputStream getImage() throws StorageObjectFailure\r
80   {\r
81     logger.debug("EntityImages.getimage started");\r
82     java.sql.Connection con=null;Statement stmt=null;\r
83     BlobInputStream in; InputStream img_in = null;\r
84 \r
85     try {\r
86       con = theStorageObject.getPooledCon();\r
87       con.setAutoCommit(false);\r
88       LargeObjectManager lom;\r
89       java.sql.Connection jCon;\r
90       stmt = con.createStatement();\r
91       ResultSet rs = theStorageObject.executeSql(stmt,\r
92           "select image_data from images where id="+getId());\r
93       jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)\r
94            .getNativeConnection();\r
95       lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();\r
96       if(rs!=null) {\r
97         if (rs.next()) {\r
98           LargeObject lob = lom.open(rs.getInt(1));\r
99           in = (BlobInputStream)lob.getInputStream();\r
100           img_in = new ImageInputStream(in, con, stmt);\r
101         }\r
102         rs.close();\r
103       }\r
104     }\r
105     catch (Exception e) {\r
106       logger.error("EntityImages.getImage failed: "+e.toString());\r
107       e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));\r
108       try {\r
109         con.setAutoCommit(true);\r
110       }\r
111       catch (Exception e2) {\r
112         logger.error("EntityImages.getImage reseting transaction mode failed: " + e2.toString());\r
113         e2.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));\r
114       }\r
115 \r
116       try {\r
117         theStorageObject.freeConnection(con, stmt);\r
118       }\r
119       catch (Throwable t) {\r
120       }\r
121 \r
122       throwStorageObjectFailure(e, "EntityImages -- getImage failed: ");\r
123     }\r
124     //}\r
125     return img_in;\r
126   }\r
127 \r
128   public void setImage(InputStream in, String type)\r
129       throws StorageObjectFailure {\r
130 \r
131     if (in!=null) {\r
132       java.sql.Connection con=null;PreparedStatement pstmt=null;\r
133       File f = null;\r
134       try {\r
135         logger.debug("EntityImages.settimage :: making internal representation of image");\r
136 \r
137         File tempDir = new File(MirPropertiesConfiguration.instance().getString("TempDir"));\r
138         f = File.createTempFile("mir", ".tmp", tempDir);\r
139         FileUtil.write(f, in);\r
140         WebdbImage webdbImage= new WebdbImage(f, type);\r
141         logger.debug("EntityImages.settimage :: made internal representation of image");\r
142 \r
143         con = theStorageObject.getPooledCon();\r
144         con.setAutoCommit(false);\r
145         logger.debug("EntityImages.settimage :: trying to insert image");\r
146 \r
147         // setting values\r
148         LargeObjectManager lom;\r
149         java.sql.Connection jCon;\r
150         jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)\r
151              .getNativeConnection();\r
152 \r
153         lom = ((org.postgresql.Connection) jCon).getLargeObjectAPI();\r
154 \r
155         int oidImage = lom.create();\r
156         int oidIcon = lom.create();\r
157         LargeObject lobImage = lom.open(oidImage);\r
158         LargeObject lobIcon = lom.open(oidIcon);\r
159         webdbImage.setImage(lobImage.getOutputStream());\r
160         webdbImage.setIcon(lobIcon.getOutputStream());\r
161         lobImage.close();\r
162         lobIcon.close();\r
163 \r
164         setValueForProperty("img_height", new Integer(webdbImage.getImageHeight()).toString());\r
165         setValueForProperty("img_width", new Integer(webdbImage.getImageWidth()).toString());\r
166         setValueForProperty("icon_height", new Integer(webdbImage.getIconHeight()).toString());\r
167         setValueForProperty("icon_width", new Integer(webdbImage.getIconWidth()).toString());\r
168         setValueForProperty("image_data", new Integer(oidImage).toString());\r
169         setValueForProperty("icon_data", new Integer(oidIcon).toString());\r
170         update();\r
171       }\r
172       catch (Exception e) {\r
173         throwStorageObjectFailure(e, "settimage :: setImage gescheitert: ");\r
174       }\r
175       finally {\r
176         try {\r
177           if (con!=null)\r
178             con.setAutoCommit(true);\r
179           // get rid of the temp. file\r
180           f.delete();\r
181         }\r
182         catch (Exception e) {\r
183         }\r
184 \r
185         if (con!=null)\r
186           theStorageObject.freeConnection(con,pstmt);\r
187       }\r
188     }\r
189   }\r
190 \r
191   /**\r
192    * Takes an OutputStream as an argument and reads in the data\r
193    * from the DB and writes it to the OutputStream.\r
194    *\r
195    * It will also take care of closing the OutputStream.\r
196    */\r
197   public InputStream getIcon() throws StorageObjectFailure\r
198   {\r
199     java.sql.Connection con=null;Statement stmt=null;\r
200     BlobInputStream in=null;ImageInputStream img_in=null;\r
201 \r
202     try {\r
203       con = theStorageObject.getPooledCon();\r
204       con.setAutoCommit(false);\r
205       LargeObjectManager lom;\r
206       java.sql.Connection jCon;\r
207       stmt = con.createStatement();\r
208       ResultSet rs = theStorageObject.executeSql(stmt, "select icon_data from images where id="+getId());\r
209       jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)\r
210            .getNativeConnection();\r
211       lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();\r
212       if(rs!=null) {\r
213         if (rs.next()) {\r
214           LargeObject lob = lom.open(rs.getInt(1));\r
215           in = (BlobInputStream)lob.getInputStream();\r
216           img_in = new ImageInputStream( in, con ,stmt);\r
217           //img_data = rs.getBytes(1);\r
218         }\r
219         rs.close();\r
220       }\r
221     }\r
222     catch (Throwable e) {\r
223       logger.error("EntityImages.getIcon failed: "+e.toString());\r
224       e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));\r
225       try {\r
226         con.setAutoCommit(true);\r
227       }\r
228       catch (Throwable e2) {\r
229         logger.error("EntityImages.getIcon reseting transaction mode failed: " + e2.toString());\r
230         e2.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));\r
231       }\r
232 \r
233       try {\r
234         theStorageObject.freeConnection(con, stmt);\r
235       }\r
236       catch (Throwable t) {\r
237       }\r
238 \r
239       throwStorageObjectFailure(e, "EntityImages -- getIcon failed:");\r
240     }\r
241 \r
242     return img_in;\r
243   }\r
244 \r
245   /**\r
246    * a small wrapper class that allows us to store the DB connection resources\r
247    * that the BlobInputStream is using and free them upon closing of the stream\r
248    */\r
249   private class ImageInputStream extends InputStream {\r
250 \r
251     InputStream _in;\r
252     java.sql.Connection _con;\r
253     Statement _stmt;\r
254 \r
255     public ImageInputStream(BlobInputStream in, java.sql.Connection con,\r
256                             Statement stmt )\r
257     {\r
258       _in = in;\r
259       _con = con;\r
260       _stmt = stmt;\r
261     }\r
262 \r
263     public void close () throws IOException {\r
264       _in.close();\r
265 \r
266       try {\r
267         _con.setAutoCommit(true);\r
268         theStorageObject.freeConnection(_con,_stmt);\r
269       }\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
280 }\r