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());
68 //img_data = rs.getBytes(1);
72 } catch (Exception e) {
74 theLog.printError("EntityImages -- getImage failed"+e.toString());
75 throwStorageObjectException(e, "EntityImages -- getImage failed: ");
79 con.setAutoCommit(true);
80 } catch (Exception e) {
83 "EntityImages -- getImage reseting transaction mode failed"
86 theStorageObject.freeConnection(con,stmt);
92 public void setImage(byte[] uploadData)
93 throws StorageObjectException {
95 if (uploadData!=null) {
96 java.sql.Connection con=null;PreparedStatement pstmt=null;
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");
108 if (iconData!=null && imageData!=null) {
109 con = theStorageObject.getPooledCon();
110 con.setAutoCommit(false);
111 theLog.printDebugInfo("settimage :: trying to insert image");
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);
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();
144 catch (Exception e) {throwStorageObjectException(e, "settimage :: setImage gescheitert: ");}
146 try { if (con!=null) con.setAutoCommit(true); } catch (Exception e) {;}
147 theStorageObject.freeConnection(con,pstmt); }
151 public void update() throws StorageObjectException {
154 theStorageObject.executeUpdate("update content set is_produced='0' where to_media="+getId());
155 } catch (SQLException e) {
156 throwStorageObjectException(e, "EntityImages :: update :: failed!! ");
160 public void setValues(HashMap theStringValues)
162 if (theStringValues != null) {
163 if (!theStringValues.containsKey("is_published"))
164 theStringValues.put("is_published","0");
166 super.setValues(theStringValues);
169 public byte[] getIcon() throws StorageObjectException
171 java.sql.Connection con=null;Statement stmt=null;
172 byte[] img_data=null;
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();
187 LargeObject lob = lom.open(rs.getInt(1));
188 img_data = lob.read(lob.size());
190 //img_data = rs.getBytes(1);
194 } catch (Exception e) {
196 theLog.printError("EntityImages -- getIcon failed"+e.toString());
197 throwStorageObjectException(e, "EntityImages -- getIcon failed:");
200 con.setAutoCommit(true);
201 } catch (Exception e) {
204 "EntityImages -- getIcon reseting transaction mode failed"
207 theStorageObject.freeConnection(con,stmt);