977902ea0a183a1749bd3ee3bf50fd57ccd34e4b
[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                 lob.close();
68                 //img_data = rs.getBytes(1);
69               }
70             rs.close();
71                         }
72                 } catch (Exception e) {
73           e.printStackTrace();
74           theLog.printError("EntityImages -- getImage failed"+e.toString()); 
75           throwStorageObjectException(e, "EntityImages -- getImage failed: ");
76         }
77         finally {
78           try {
79             con.setAutoCommit(true);
80           } catch (Exception e) {
81             e.printStackTrace();
82             theLog.printError(
83                     "EntityImages -- getImage reseting transaction mode failed"
84                     +e.toString()); 
85           }
86           theStorageObject.freeConnection(con,stmt);
87         }
88
89                 return img_data;
90         }
91
92         public void setImage(byte[] uploadData)
93             throws StorageObjectException {
94
95                 if (uploadData!=null) {
96                         java.sql.Connection con=null;PreparedStatement pstmt=null;
97                         try {
98
99                                 theLog.printDebugInfo("settimage :: making internal representation of image");
100                                 WebdbImage webdbImage= new WebdbImage(uploadData);
101                                 theLog.printDebugInfo("settimage :: made internal representation of image");
102                                 byte[] imageData = webdbImage.getImage();
103                                 theLog.printDebugInfo("settimage :: getImage");
104                                 byte[] iconData = webdbImage.getIcon();
105                                 theLog.printDebugInfo("settimage :: getIcon");
106
107
108                                 if (iconData!=null && imageData!=null) {
109                                         con = theStorageObject.getPooledCon();
110                                         con.setAutoCommit(false);
111                                         theLog.printDebugInfo("settimage :: trying to insert image");
112
113                                         // setting values
114                     LargeObjectManager lom;
115                     java.sql.Connection jCon;
116                     jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
117                             .getNativeConnection();
118                     lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
119                     int oidImage = lom.create();
120                     int oidIcon = lom.create();
121                     LargeObject lobImage = lom.open(oidImage);
122                     LargeObject lobIcon = lom.open(oidIcon);
123                     lobImage.write(imageData);
124                     lobIcon.write(iconData);
125                     lobImage.close();
126                     lobIcon.close();
127                     String sql = "update images set img_height='"
128                         +webdbImage.getImageHeight() +
129                                                 "',img_width='"   + webdbImage.getImageWidth() +
130                                                 "',icon_height='" + webdbImage.getIconHeight() +
131                                                 "',icon_width='"  + webdbImage.getIconWidth()
132                         +  "', image_data="+oidImage+", icon_data="+oidIcon
133                         +" where id="+getId();
134                                         theLog.printDebugInfo("settimage :: updating sizes: "+ sql);
135                                         pstmt = con.prepareStatement(sql);
136                                         //pstmt.setBytes(1, imageData);
137                                         //pstmt.setBytes(2, iconData);
138                                         pstmt.executeUpdate();
139                                         sql="update content set is_produced='0' where to_media="+getId();
140                                         pstmt = con.prepareStatement(sql);
141                                         pstmt.executeUpdate();
142                                 }
143                         }
144                         catch (Exception e) {throwStorageObjectException(e, "settimage :: setImage gescheitert: ");}
145                         finally {
146                                 try { if (con!=null) con.setAutoCommit(true); } catch (Exception e) {;}
147                                 theStorageObject.freeConnection(con,pstmt); }
148                 }
149         }
150
151         public void update() throws StorageObjectException {
152                 super.update();
153                 try {
154                         theStorageObject.executeUpdate("update content set is_produced='0' where to_media="+getId());
155                 } catch (SQLException e) {
156                         throwStorageObjectException(e, "EntityImages :: update :: failed!! ");
157                 }
158         }
159
160         public void setValues(HashMap theStringValues)
161         {
162                 if (theStringValues != null) {
163                         if (!theStringValues.containsKey("is_published"))
164                          theStringValues.put("is_published","0");
165                 }
166                 super.setValues(theStringValues);
167         }
168
169         public byte[] getIcon() throws StorageObjectException
170         {
171                 java.sql.Connection con=null;Statement stmt=null;
172                 byte[] img_data=null;
173
174                 try {
175                         con = theStorageObject.getPooledCon();
176                         con.setAutoCommit(false);
177             LargeObjectManager lom;
178             java.sql.Connection jCon;
179                         stmt = con.createStatement();
180                         ResultSet rs = theStorageObject.executeSql(stmt,
181                             "select icon_data from images where id="+getId());
182             jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
183                     .getNativeConnection();
184             lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
185                         if(rs!=null) {
186                                 if (rs.next()) {
187                   LargeObject lob = lom.open(rs.getInt(1));
188                   img_data = lob.read(lob.size());
189                   lob.close();
190                   //img_data = rs.getBytes(1);
191                                 }
192                 rs.close();
193                         }
194                 } catch (Exception e) {
195           e.printStackTrace();
196           theLog.printError("EntityImages -- getIcon failed"+e.toString()); 
197           throwStorageObjectException(e, "EntityImages -- getIcon failed:");
198                 } finally {
199           try {
200             con.setAutoCommit(true);
201           } catch (Exception e) {
202             e.printStackTrace();
203             theLog.printError(
204                     "EntityImages -- getIcon reseting transaction mode failed"
205                     +e.toString()); 
206           }
207           theStorageObject.freeConnection(con,stmt);
208        }
209
210        return img_data;
211         }
212
213 }