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