first cut of merge of STABLE-pre1_0 into HEAD. I won't even guarantee that it
[mir.git] / source / mircoders / entity / EntityImages.java
1 package mircoders.entity;
2
3 import java.lang.*;
4 import java.io.*;
5 import java.util.*;
6 import java.sql.*;
7
8 /*
9  * kind of hack for postgres non-standard LargeObjects that Poolman
10  * doesn't know about. see all the casting, LargeObj stuff in getIcon, getImage
11  * at some point when postgres has normal BLOB support, this should go.
12  */
13 import org.postgresql.Connection;
14 import org.postgresql.largeobject.LargeObject;
15 import org.postgresql.largeobject.LargeObjectManager;
16
17 import mir.entity.*;
18 import mir.misc.*;
19 import mir.storage.*;
20
21 /**
22  * Diese Klasse enthält die Daten eines MetaObjekts
23  *
24  * @author RK
25  * @version 11.11.2000
26  */
27
28
29 public class EntityImages extends EntityUploadedMedia
30 {
31         public EntityImages()
32         {
33                 super();
34         }
35
36         public EntityImages(StorageObject theStorage) {
37                 this();
38                 setStorage(theStorage);
39         }
40
41         //
42         // methods
43
44
45
46         public byte[] getImage() throws StorageObjectException
47         {
48                 theLog.printDebugInfo("--getimage started");
49                 java.sql.Connection con=null;Statement stmt=null;
50                 byte[] img_data=null;
51
52                 try {
53                         con = theStorageObject.getPooledCon();
54                         con.setAutoCommit(false);
55                         LargeObjectManager lom;
56             java.sql.Connection jCon;
57             stmt = con.createStatement();
58                         ResultSet rs = theStorageObject.executeSql(stmt,
59                             "select image_data from images where id="+getId());
60             jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
61                     .getNativeConnection();
62             lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
63                         if(rs!=null) {
64               if (rs.next()) {
65                 LargeObject lob = lom.open(rs.getInt(1));
66                 img_data = lob.read(lob.size());
67                 System.err.println("LOB IMG SIZE: "+lob.size());
68                 lob.close();
69                 System.err.println("res set img NOT NULL2");
70                 //img_data = rs.getBytes(1);
71               }
72             rs.close();
73                         }
74                 } catch (Exception e) {
75           e.printStackTrace();
76           theLog.printError("EntityImages -- getImage failed"+e.toString()); 
77           throwStorageObjectException(e, "EntityImages -- getImage failed: ");
78         }
79         finally {
80           try {
81             con.setAutoCommit(true);
82           } catch (Exception e) {
83             e.printStackTrace();
84             theLog.printError(
85                     "EntityImages -- getImage reseting transaction mode failed"
86                     +e.toString()); 
87           }
88           theStorageObject.freeConnection(con,stmt);
89         }
90
91                 return img_data;
92         }
93
94         public void setImage(byte[] uploadData)
95             throws StorageObjectException {
96
97                 if (uploadData!=null) {
98                         java.sql.Connection con=null;PreparedStatement pstmt=null;
99                         try {
100
101                                 theLog.printDebugInfo("settimage :: making internal representation of image");
102                                 WebdbImage webdbImage= new WebdbImage(uploadData);
103                                 theLog.printDebugInfo("settimage :: made internal representation of image");
104                                 byte[] imageData = webdbImage.getImage();
105                                 theLog.printDebugInfo("settimage :: getImage");
106                                 byte[] iconData = webdbImage.getIcon();
107                                 theLog.printDebugInfo("settimage :: getIcon");
108
109                                 if (iconData!=null && imageData!=null) {
110                                         con = theStorageObject.getPooledCon();
111                                         con.setAutoCommit(false);
112                                         theLog.printDebugInfo("settimage :: trying to insert image");
113
114                                         // setting values
115                     LargeObjectManager lom;
116                     java.sql.Connection jCon;
117                     jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
118                             .getNativeConnection();
119                     lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
120                     int oidImage = lom.create();
121                     int oidIcon = lom.create();
122                     LargeObject lobImage = lom.open(oidImage);
123                     LargeObject lobIcon = lom.open(oidIcon);
124                     lobImage.write(imageData);
125                     lobIcon.write(iconData);
126                     lobImage.close();
127                     lobIcon.close();
128                     String sql = "update images set img_height='"
129                         +webdbImage.getImageHeight() +
130                                                 "',img_width='"   + webdbImage.getImageWidth() +
131                                                 "',icon_height='" + webdbImage.getIconHeight() +
132                                                 "',icon_width='"  + webdbImage.getIconWidth()
133                         +  "', image_data="+oidImage+", icon_data="+oidIcon
134                         +" where id="+getId();
135                                         theLog.printDebugInfo("settimage :: updating sizes: "+ sql);
136                                         pstmt = con.prepareStatement(sql);
137                                         //pstmt.setBytes(1, imageData);
138                                         //pstmt.setBytes(2, iconData);
139                                         pstmt.executeUpdate();
140                                         sql="update content set is_produced='0' where to_media="+getId();
141                                         pstmt = con.prepareStatement(sql);
142                                         pstmt.executeUpdate();
143                                 }
144                         }
145                         catch (Exception e) {throwStorageObjectException(e, "settimage :: setImage gescheitert: ");}
146                         finally {
147                                 try { if (con!=null) con.setAutoCommit(true); } catch (Exception e) {;}
148                                 theStorageObject.freeConnection(con,pstmt); }
149                 }
150         }
151
152         public void update() throws StorageObjectException {
153                 super.update();
154                 try {
155                         theStorageObject.executeUpdate("update content set is_produced='0' where to_media="+getId());
156                 } catch (SQLException e) {
157                         throwStorageObjectException(e, "EntityImages :: update :: failed!! ");
158                 }
159         }
160
161         public void setValues(HashMap theStringValues)
162         {
163                 if (theStringValues != null) {
164                         if (!theStringValues.containsKey("is_published"))
165                          theStringValues.put("is_published","0");
166                 }
167                 super.setValues(theStringValues);
168         }
169
170         public byte[] getIcon() throws StorageObjectException
171         {
172                 java.sql.Connection con=null;Statement stmt=null;
173                 byte[] img_data=null;
174
175                 try {
176                         con = theStorageObject.getPooledCon();
177                         con.setAutoCommit(false);
178             LargeObjectManager lom;
179             java.sql.Connection jCon;
180                         stmt = con.createStatement();
181                         ResultSet rs = theStorageObject.executeSql(stmt,
182                             "select icon_data from images where id="+getId());
183             jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
184                     .getNativeConnection();
185             lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
186                         if(rs!=null) {
187                 System.err.println("res set NOT NULL");
188                                 if (rs.next()) {
189                   LargeObject lob = lom.open(rs.getInt(1));
190                   img_data = lob.read(lob.size());
191                   System.err.println("LOB SIZE: "+lob.size());
192                   lob.close();
193                   System.err.println("res set NOT NULL2");
194                   //img_data = rs.getBytes(1);
195                   System.err.println("res set NOT NULL3");
196                                 }
197                 rs.close();
198                         }
199                 } catch (Exception e) {
200           e.printStackTrace();
201           theLog.printError("EntityImages -- getIcon failed"+e.toString()); 
202           throwStorageObjectException(e, "EntityImages -- getIcon failed:");
203                 } finally {
204           try {
205             con.setAutoCommit(true);
206           } catch (Exception e) {
207             e.printStackTrace();
208             theLog.printError(
209                     "EntityImages -- getIcon reseting transaction mode failed"
210                     +e.toString()); 
211           }
212           theStorageObject.freeConnection(con,stmt);
213        }
214
215        return img_data;
216         }
217
218 }