1 package mircoders.entity;
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.
13 import org.postgresql.Connection;
14 import org.postgresql.largeobject.LargeObject;
15 import org.postgresql.largeobject.LargeObjectManager;
22 * Diese Klasse enthält die Daten eines MetaObjekts
29 public class EntityImages extends EntityUploadedMedia
36 public EntityImages(StorageObject theStorage) {
38 setStorage(theStorage);
46 public byte[] getImage() throws StorageObjectException
48 theLog.printDebugInfo("--getimage started");
49 java.sql.Connection con=null;Statement stmt=null;
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();
65 LargeObject lob = lom.open(rs.getInt(1));
66 img_data = lob.read(lob.size());
67 System.err.println("LOB IMG SIZE: "+lob.size());
69 System.err.println("res set img NOT NULL2");
70 //img_data = rs.getBytes(1);
74 } catch (Exception e) {
76 theLog.printError("EntityImages -- getImage failed"+e.toString());
77 throwStorageObjectException(e, "EntityImages -- getImage failed: ");
81 con.setAutoCommit(true);
82 } catch (Exception e) {
85 "EntityImages -- getImage reseting transaction mode failed"
88 theStorageObject.freeConnection(con,stmt);
94 public void setImage(byte[] uploadData)
95 throws StorageObjectException {
97 if (uploadData!=null) {
98 java.sql.Connection con=null;PreparedStatement pstmt=null;
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");
109 if (iconData!=null && imageData!=null) {
110 con = theStorageObject.getPooledCon();
111 con.setAutoCommit(false);
112 theLog.printDebugInfo("settimage :: trying to insert image");
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);
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();
145 catch (Exception e) {throwStorageObjectException(e, "settimage :: setImage gescheitert: ");}
147 try { if (con!=null) con.setAutoCommit(true); } catch (Exception e) {;}
148 theStorageObject.freeConnection(con,pstmt); }
152 public void update() throws StorageObjectException {
155 theStorageObject.executeUpdate("update content set is_produced='0' where to_media="+getId());
156 } catch (SQLException e) {
157 throwStorageObjectException(e, "EntityImages :: update :: failed!! ");
161 public void setValues(HashMap theStringValues)
163 if (theStringValues != null) {
164 if (!theStringValues.containsKey("is_published"))
165 theStringValues.put("is_published","0");
167 super.setValues(theStringValues);
170 public byte[] getIcon() throws StorageObjectException
172 java.sql.Connection con=null;Statement stmt=null;
173 byte[] img_data=null;
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();
187 System.err.println("res set NOT NULL");
189 LargeObject lob = lom.open(rs.getInt(1));
190 img_data = lob.read(lob.size());
191 System.err.println("LOB SIZE: "+lob.size());
193 System.err.println("res set NOT NULL2");
194 //img_data = rs.getBytes(1);
195 System.err.println("res set NOT NULL3");
199 } catch (Exception e) {
201 theLog.printError("EntityImages -- getIcon failed"+e.toString());
202 throwStorageObjectException(e, "EntityImages -- getIcon failed:");
205 con.setAutoCommit(true);
206 } catch (Exception e) {
209 "EntityImages -- getIcon reseting transaction mode failed"
212 theStorageObject.freeConnection(con,stmt);