2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
32 package mircoders.entity;
40 * kind of hack for postgres non-standard LargeObjects that Poolman
41 * doesn't know about. see all the casting, LargeObj stuff in getIcon, getImage
42 * at some point when postgres has normal BLOB support, this should go.
44 import org.postgresql.Connection;
45 import org.postgresql.largeobject.LargeObject;
46 import org.postgresql.largeobject.LargeObjectManager;
47 import org.postgresql.largeobject.BlobInputStream;
54 * Diese Klasse enthält die Daten eines MetaObjekts
56 * @author RK, mh, mir-coders
57 * @version $Id: EntityImages.java,v 1.13 2002/12/17 19:20:31 zapata Exp $
61 public class EntityImages extends EntityUploadedMedia
69 public EntityImages(StorageObject theStorage) {
71 setStorage(theStorage);
78 public InputStream getImage() throws StorageObjectException
80 theLog.printDebugInfo("--getimage started");
81 java.sql.Connection con=null;Statement stmt=null;
82 BlobInputStream in; InputStream img_in = null;
85 con = theStorageObject.getPooledCon();
86 con.setAutoCommit(false);
87 LargeObjectManager lom;
88 java.sql.Connection jCon;
89 stmt = con.createStatement();
90 ResultSet rs = theStorageObject.executeSql(stmt,
91 "select image_data from images where id="+getId());
92 jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
93 .getNativeConnection();
94 lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
97 LargeObject lob = lom.open(rs.getInt(1));
98 in = (BlobInputStream)lob.getInputStream();
99 img_in = new ImageInputStream(in, con, stmt);
104 catch (Exception e) {
106 theLog.printError("EntityImages -- getImage failed"+e.toString());
108 con.setAutoCommit(true);
109 } catch (Exception e2) {
112 "EntityImages -- getImage reseting transaction mode failed"
115 theStorageObject.freeConnection(con,stmt);
116 throwStorageObjectException(e, "EntityImages -- getImage failed: ");
122 public void setImage(InputStream in, String type)
123 throws StorageObjectException {
126 java.sql.Connection con=null;PreparedStatement pstmt=null;
130 theLog.printDebugInfo("settimage :: making internal representation of image");
132 File tempDir = new File(MirConfig.getProp("TempDir"));
133 f = File.createTempFile("mir", ".tmp", tempDir);
134 FileUtil.write(f, in);
135 WebdbImage webdbImage= new WebdbImage(f, type);
136 theLog.printDebugInfo("settimage :: made internal representation of image");
138 con = theStorageObject.getPooledCon();
139 con.setAutoCommit(false);
140 theLog.printDebugInfo("settimage :: trying to insert image");
143 LargeObjectManager lom;
144 java.sql.Connection jCon;
145 jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
146 .getNativeConnection();
148 lom = ((org.postgresql.Connection) jCon).getLargeObjectAPI();
150 int oidImage = lom.create();
151 int oidIcon = lom.create();
152 LargeObject lobImage = lom.open(oidImage);
153 LargeObject lobIcon = lom.open(oidIcon);
154 webdbImage.setImage(lobImage.getOutputStream());
155 webdbImage.setIcon(lobIcon.getOutputStream());
159 setValueForProperty("img_height",
160 new Integer(webdbImage.getImageHeight()).toString());
161 setValueForProperty("img_width",
162 new Integer(webdbImage.getImageWidth()).toString());
163 setValueForProperty("icon_height",
164 new Integer(webdbImage.getIconHeight()).toString());
165 setValueForProperty("icon_width",
166 new Integer(webdbImage.getIconWidth()).toString());
167 setValueForProperty("image_data", new Integer(oidImage).toString());
168 setValueForProperty("icon_data", new Integer(oidIcon).toString());
171 catch (Exception e) {
172 throwStorageObjectException(e, "settimage :: setImage gescheitert: ");
177 con.setAutoCommit(true);
178 // get rid of the temp. file
181 catch (Exception e) {
185 theStorageObject.freeConnection(con,pstmt);
191 * Takes an OutputStream as an argument and reads in the data
192 * from the DB and writes it to the OutputStream.
194 * It will also take care of closing the OutputStream.
196 public InputStream getIcon() throws StorageObjectException
198 java.sql.Connection con=null;Statement stmt=null;
199 BlobInputStream in=null;ImageInputStream img_in=null;
202 con = theStorageObject.getPooledCon();
203 con.setAutoCommit(false);
204 LargeObjectManager lom;
205 java.sql.Connection jCon;
206 stmt = con.createStatement();
207 ResultSet rs = theStorageObject.executeSql(stmt,
208 "select icon_data from images where id="+getId());
209 jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
210 .getNativeConnection();
211 lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
214 LargeObject lob = lom.open(rs.getInt(1));
215 in = (BlobInputStream)lob.getInputStream();
216 img_in = new ImageInputStream( in, con ,stmt);
217 //img_data = rs.getBytes(1);
222 catch (Exception e) {
224 theLog.printError("EntityImages -- getIcon failed"+e.toString());
226 con.setAutoCommit(true);
228 catch (Exception e2) {
231 "EntityImages -- getIcon reseting transaction mode failed"
234 theStorageObject.freeConnection(con,stmt);
235 throwStorageObjectException(e, "EntityImages -- getIcon failed:");
242 * a small wrapper class that allows us to store the DB connection resources
243 * that the BlobInputStream is using and free them upon closing of the stream
245 private class ImageInputStream extends InputStream {
248 java.sql.Connection _con;
251 public ImageInputStream(BlobInputStream in, java.sql.Connection con,
259 public void close () throws IOException {
263 _con.setAutoCommit(true);
264 theStorageObject.freeConnection(_con,_stmt);
266 catch (Exception e) {
267 throw new IOException("close(): "+e.toString());
271 public int read() throws IOException {