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