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