2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\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
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
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
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
32 package mircoders.entity;
\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
41 import org.postgresql.largeobject.BlobInputStream;
\r
42 import org.postgresql.largeobject.LargeObject;
\r
43 import org.postgresql.largeobject.LargeObjectManager;
\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
53 * Diese Klasse enth?lt die Daten eines MetaObjekts
\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
60 public class EntityImages extends EntityUploadedMedia
\r
63 public EntityImages()
\r
67 logger = new LoggerWrapper("Entity.UploadedMedia.Images");
\r
70 public EntityImages(StorageObject theStorage) {
\r
72 setStorage(theStorage);
\r
79 public InputStream getImage() throws StorageObjectFailure
\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
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
98 LargeObject lob = lom.open(rs.getInt(1));
\r
99 in = (BlobInputStream)lob.getInputStream();
\r
100 img_in = new ImageInputStream(in, con, stmt);
\r
105 catch (Exception e) {
\r
106 logger.error("EntityImages.getImage failed: "+e.toString());
\r
107 e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
\r
109 con.setAutoCommit(true);
\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
117 theStorageObject.freeConnection(con, stmt);
\r
119 catch (Throwable t) {
\r
122 throwStorageObjectFailure(e, "EntityImages -- getImage failed: ");
\r
128 public void setImage(InputStream in, String type)
\r
129 throws StorageObjectFailure {
\r
132 java.sql.Connection con=null;PreparedStatement pstmt=null;
\r
135 logger.debug("EntityImages.settimage :: making internal representation of image");
\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
143 con = theStorageObject.getPooledCon();
\r
144 con.setAutoCommit(false);
\r
145 logger.debug("EntityImages.settimage :: trying to insert image");
\r
148 LargeObjectManager lom;
\r
149 java.sql.Connection jCon;
\r
150 jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
\r
151 .getNativeConnection();
\r
153 lom = ((org.postgresql.Connection) jCon).getLargeObjectAPI();
\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
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
172 catch (Exception e) {
\r
173 throwStorageObjectFailure(e, "settimage :: setImage gescheitert: ");
\r
178 con.setAutoCommit(true);
\r
179 // get rid of the temp. file
\r
182 catch (Exception e) {
\r
186 theStorageObject.freeConnection(con,pstmt);
\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
195 * It will also take care of closing the OutputStream.
\r
197 public InputStream getIcon() throws StorageObjectFailure
\r
199 java.sql.Connection con=null;Statement stmt=null;
\r
200 BlobInputStream in=null;ImageInputStream img_in=null;
\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
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
222 catch (Throwable e) {
\r
223 logger.error("EntityImages.getIcon failed: "+e.toString());
\r
224 e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
\r
226 con.setAutoCommit(true);
\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
234 theStorageObject.freeConnection(con, stmt);
\r
236 catch (Throwable t) {
\r
239 throwStorageObjectFailure(e, "EntityImages -- getIcon failed:");
\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
249 private class ImageInputStream extends InputStream {
\r
252 java.sql.Connection _con;
\r
255 public ImageInputStream(BlobInputStream in, java.sql.Connection con,
\r
263 public void close () throws IOException {
\r
267 _con.setAutoCommit(true);
\r
268 theStorageObject.freeConnection(_con,_stmt);
\r
270 catch (Exception e) {
\r
271 throw new IOException("close(): "+e.toString());
\r
275 public int read() throws IOException {
\r