tracked down a nasty error reporting bug
[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.lang.*;
35 import java.io.*;
36 import java.util.*;
37 import java.sql.*;
38
39 /*
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.
43  */
44 import org.postgresql.Connection;
45 import org.postgresql.largeobject.LargeObject;
46 import org.postgresql.largeobject.LargeObjectManager;
47 import org.postgresql.largeobject.BlobInputStream;
48
49 import mir.entity.*;
50 import mir.misc.*;
51 import mir.storage.*;
52
53 /**
54  * Diese Klasse enthält die Daten eines MetaObjekts
55  *
56  * @author RK, mh
57  * @version $Id: EntityImages.java,v 1.11 2002/12/01 15:05:51 zapata Exp $
58  */
59
60
61 public class EntityImages extends EntityUploadedMedia
62 {
63
64   public EntityImages()
65   {
66     super();
67   }
68
69   public EntityImages(StorageObject theStorage) {
70     this();
71     setStorage(theStorage);
72   }
73
74   //
75   // methods
76
77
78   public InputStream getImage() throws StorageObjectException
79   {
80     theLog.printDebugInfo("--getimage started");
81     java.sql.Connection con=null;Statement stmt=null;
82     BlobInputStream in; InputStream img_in = null;
83
84     try {
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();
95       if(rs!=null) {
96         if (rs.next()) {
97           LargeObject lob = lom.open(rs.getInt(1));
98           in = (BlobInputStream)lob.getInputStream();
99           img_in = new ImageInputStream(in, con, stmt);
100         }
101         rs.close();
102       }
103     }
104     catch (Exception e) {
105       e.printStackTrace();
106       theLog.printError("EntityImages -- getImage failed"+e.toString());
107       try {
108         con.setAutoCommit(true);
109       } catch (Exception e2) {
110         e.printStackTrace();
111         theLog.printError(
112             "EntityImages -- getImage reseting transaction mode failed"
113             +e2.toString());
114       }
115       theStorageObject.freeConnection(con,stmt);
116       throwStorageObjectException(e, "EntityImages -- getImage failed: ");
117     }
118     //}
119     return img_in;
120   }
121
122   public void setImage(InputStream in, String type)
123       throws StorageObjectException {
124
125     if (in!=null) {
126       java.sql.Connection con=null;PreparedStatement pstmt=null;
127       File f = null;
128       try {
129
130         theLog.printDebugInfo("settimage :: making internal representation of image");
131
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");
137
138         con = theStorageObject.getPooledCon();
139         con.setAutoCommit(false);
140         theLog.printDebugInfo("settimage :: trying to insert image");
141
142         // setting values
143         LargeObjectManager lom;
144         java.sql.Connection jCon;
145         jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
146              .getNativeConnection();
147         lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
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         String sql = "update images set img_height='"
157                    +webdbImage.getImageHeight() +
158                      "',img_width='"   + webdbImage.getImageWidth() +
159                      "',icon_height='" + webdbImage.getIconHeight() +
160                      "',icon_width='"  + webdbImage.getIconWidth()
161                    +  "', image_data="+oidImage+", icon_data="+oidIcon
162                    +" where id="+getId();
163         theLog.printDebugInfo("settimage :: updating sizes: "+ sql);
164         pstmt = con.prepareStatement(sql);
165         //pstmt.setBytes(1, imageData);
166         //pstmt.setBytes(2, iconData);
167         pstmt.executeUpdate();
168         sql="update content set is_produced='0' where to_media="+getId();
169         pstmt = con.prepareStatement(sql);
170         pstmt.executeUpdate();
171       }
172       catch (Exception e) {
173         throwStorageObjectException(e, "settimage :: setImage gescheitert: ");
174       }
175       finally {
176         try {
177           if (con!=null)
178             con.setAutoCommit(true);
179           // get rid of the temp. file
180           f.delete();
181         }
182         catch (Exception e) {
183         }
184
185         if (con!=null)
186           theStorageObject.freeConnection(con,pstmt);
187       }
188     }
189   }
190
191   public void update() throws StorageObjectException {
192     super.update();
193     try {
194       theStorageObject.executeUpdate("update content set is_produced='0' where to_media="+getId());
195     } catch (SQLException e) {
196       throwStorageObjectException(e, "EntityImages :: update :: failed!! ");
197     }
198   }
199
200   public void setValues(HashMap theStringValues)
201   {
202     if (theStringValues != null) {
203       if (!theStringValues.containsKey("is_published"))
204         theStringValues.put("is_published","0");
205     }
206     super.setValues(theStringValues);
207   }
208
209   /**
210    * Takes an OutputStream as an argument and reads in the data
211    * from the DB and writes it to the OutputStream.
212    *
213    * It will also take care of closing the OutputStream.
214    */
215   public InputStream getIcon() throws StorageObjectException
216   {
217     java.sql.Connection con=null;Statement stmt=null;
218     BlobInputStream in=null;ImageInputStream img_in=null;
219
220     try {
221       con = theStorageObject.getPooledCon();
222       con.setAutoCommit(false);
223       LargeObjectManager lom;
224       java.sql.Connection jCon;
225       stmt = con.createStatement();
226       ResultSet rs = theStorageObject.executeSql(stmt,
227           "select icon_data from images where id="+getId());
228       jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
229            .getNativeConnection();
230       lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
231       if(rs!=null) {
232         if (rs.next()) {
233           LargeObject lob = lom.open(rs.getInt(1));
234           in = (BlobInputStream)lob.getInputStream();
235           img_in = new ImageInputStream( in, con ,stmt);
236           //img_data = rs.getBytes(1);
237         }
238         rs.close();
239       }
240     }
241     catch (Exception e) {
242       e.printStackTrace();
243       theLog.printError("EntityImages -- getIcon failed"+e.toString());
244       try {
245         con.setAutoCommit(true);
246       }
247       catch (Exception e2) {
248         e.printStackTrace();
249         theLog.printError(
250             "EntityImages -- getIcon reseting transaction mode failed"
251             +e2.toString());
252       }
253       theStorageObject.freeConnection(con,stmt);
254       throwStorageObjectException(e, "EntityImages -- getIcon failed:");
255     }
256
257     return img_in;
258   }
259
260   /**
261    * a small wrapper class that allows us to store the DB connection resources
262    * that the BlobInputStream is using and free them upon closing of the stream
263    */
264   private class ImageInputStream extends InputStream {
265
266     InputStream _in;
267     java.sql.Connection _con;
268     Statement _stmt;
269
270     public ImageInputStream(BlobInputStream in, java.sql.Connection con,
271                             Statement stmt )
272     {
273       _in = in;
274       _con = con;
275       _stmt = stmt;
276     }
277
278     public void close () throws IOException {
279       _in.close();
280
281       try {
282         _con.setAutoCommit(true);
283         theStorageObject.freeConnection(_con,_stmt);
284       }
285       catch (Exception e) {
286         throw new IOException("close(): "+e.toString());
287       }
288     }
289
290     public int read() throws IOException {
291       return _in.read();
292     }
293
294   }
295 }