better way to create temp. files
[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.6.4.4 2002/11/27 21:16:54 mh 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                 } catch (Exception e) {
104       e.printStackTrace();
105       theLog.printError("EntityImages -- getImage failed"+e.toString()); 
106       try {
107         con.setAutoCommit(true);
108       } catch (Exception e2) {
109         e.printStackTrace();
110         theLog.printError(
111           "EntityImages -- getImage reseting transaction mode failed"
112             +e2.toString()); 
113       }
114       theStorageObject.freeConnection(con,stmt);
115       throwStorageObjectException(e, "EntityImages -- getImage failed: ");
116     }
117     //}
118     return img_in;
119         }
120
121         public void setImage(InputStream in, String type)
122             throws StorageObjectException {
123
124                 if (in!=null) {
125                         java.sql.Connection con=null;PreparedStatement pstmt=null;
126       File f = null;
127                         try {
128
129                                 theLog.printDebugInfo("settimage :: making internal representation of image");
130
131         File tempDir = new File(MirConfig.getProp("TempDir"));
132         f = File.createTempFile("mir", ".tmp", tempDir);
133         FileUtil.write(f, in);
134                                 WebdbImage webdbImage= new WebdbImage(f, type);
135                                 theLog.printDebugInfo("settimage :: made internal representation of image");
136
137         con = theStorageObject.getPooledCon();
138         con.setAutoCommit(false);
139         theLog.printDebugInfo("settimage :: trying to insert image");
140
141         // setting values
142         LargeObjectManager lom;
143         java.sql.Connection jCon;
144         jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
145                           .getNativeConnection();
146         lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
147         int oidImage = lom.create();
148         int oidIcon = lom.create();
149         LargeObject lobImage = lom.open(oidImage);
150         LargeObject lobIcon = lom.open(oidIcon);
151         webdbImage.setImage(lobImage.getOutputStream());
152         webdbImage.setIcon(lobIcon.getOutputStream());
153         lobImage.close();
154         lobIcon.close();
155         String sql = "update images set img_height='"
156                       +webdbImage.getImageHeight() +
157                       "',img_width='"   + webdbImage.getImageWidth() +
158                       "',icon_height='" + webdbImage.getIconHeight() +
159                       "',icon_width='"  + webdbImage.getIconWidth()
160                       +  "', image_data="+oidImage+", icon_data="+oidIcon
161                       +" where id="+getId();
162         theLog.printDebugInfo("settimage :: updating sizes: "+ sql);
163         pstmt = con.prepareStatement(sql);
164         //pstmt.setBytes(1, imageData);
165         //pstmt.setBytes(2, iconData);
166         pstmt.executeUpdate();
167         sql="update content set is_produced='0' where to_media="+getId();
168         pstmt = con.prepareStatement(sql);
169         pstmt.executeUpdate();
170                         }
171                         catch (Exception e) {throwStorageObjectException(e, "settimage :: setImage gescheitert: ");}
172                         finally {
173                                 try {
174           if (con!=null)
175             con.setAutoCommit(true);
176           // get rid of the temp. file
177           f.delete();
178         } catch (Exception e) {;}
179                                 theStorageObject.freeConnection(con,pstmt);
180       }
181                 }
182         }
183
184         public void update() throws StorageObjectException {
185                 super.update();
186                 try {
187                         theStorageObject.executeUpdate("update content set is_produced='0' where to_media="+getId());
188                 } catch (SQLException e) {
189                         throwStorageObjectException(e, "EntityImages :: update :: failed!! ");
190                 }
191         }
192
193         public void setValues(HashMap theStringValues)
194         {
195                 if (theStringValues != null) {
196                         if (!theStringValues.containsKey("is_published"))
197                          theStringValues.put("is_published","0");
198                 }
199                 super.setValues(theStringValues);
200         }
201
202   /**
203    * Takes an OutputStream as an argument and reads in the data
204    * from the DB and writes it to the OutputStream.
205    *
206    * It will also take care of closing the OutputStream.
207    */
208         public InputStream getIcon() throws StorageObjectException
209         {
210                 java.sql.Connection con=null;Statement stmt=null;
211     BlobInputStream in=null;ImageInputStream img_in=null;
212
213                 try {
214                         con = theStorageObject.getPooledCon();
215                         con.setAutoCommit(false);
216       LargeObjectManager lom;
217       java.sql.Connection jCon;
218                         stmt = con.createStatement();
219                         ResultSet rs = theStorageObject.executeSql(stmt,
220                             "select icon_data from images where id="+getId());
221       jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
222                     .getNativeConnection();
223       lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
224                         if(rs!=null) {
225                                 if (rs.next()) {
226           LargeObject lob = lom.open(rs.getInt(1));
227           in = (BlobInputStream)lob.getInputStream();
228           img_in = new ImageInputStream( in, con ,stmt);
229           //img_data = rs.getBytes(1);
230                                 }
231         rs.close();
232                         }
233                 } catch (Exception e) {
234       e.printStackTrace();
235       theLog.printError("EntityImages -- getIcon failed"+e.toString()); 
236       try {
237         con.setAutoCommit(true);
238       } catch (Exception e2) {
239         e.printStackTrace();
240         theLog.printError(
241           "EntityImages -- getIcon reseting transaction mode failed"
242             +e2.toString()); 
243       }
244       theStorageObject.freeConnection(con,stmt);
245       throwStorageObjectException(e, "EntityImages -- getIcon failed:");
246                 }
247
248     return img_in;
249         }
250
251   /**
252    * a small wrapper class that allows us to store the DB connection resources
253    * that the BlobInputStream is using and free them upon closing of the stream
254    */
255   private class ImageInputStream extends InputStream {
256
257     InputStream _in;
258     java.sql.Connection _con;
259     Statement _stmt;
260
261     public ImageInputStream(BlobInputStream in, java.sql.Connection con,
262                             Statement stmt )
263     {
264       _in = in;
265       _con = con;
266       _stmt = stmt;
267     }
268
269     public void close () throws IOException {
270       _in.close();
271
272       try {
273         _con.setAutoCommit(true);
274         theStorageObject.freeConnection(_con,_stmt);
275       } catch (Exception e) {
276         throw new IOException("close(): "+e.toString());
277       }
278     }
279
280     public int read() throws IOException {
281       return _in.read();
282     }
283
284   }
285
286 }