Initial revision
[mir.git] / source / mircoders / entity / EntityImage.java
1 package mircoders.entity;
2
3 import java.lang.*;
4 import java.io.*;
5 import java.util.*;
6 import java.sql.*;
7
8 import webdb.entity.*;
9 import webdb.misc.*;
10 import webdb.storage.*;
11
12 /**
13  * Diese Klasse enthält die Daten eines MetaObjekts
14  *
15  * @author RK
16  * @version 11.11.2000
17  */
18
19
20 public class EntityImage extends AbstractEntity implements Entity
21 {
22         private static int instances;
23
24         public EntityImage()
25         {
26                 super();
27                 instances++;
28         }
29
30         public EntityImage(StorageObject theStorage) {
31                 this();
32                 setStorage(theStorage);
33         }
34
35         //
36         // methods
37
38
39
40         public byte[] getImage()
41         {
42                 theLog.printDebugInfo("--getimage started");
43                 Connection con=null;Statement stmt=null;
44                 byte[] img_data=null;
45
46                 try {
47                         con = theStorageObject.getPooledCon();
48                         con.setAutoCommit(false);
49                         stmt = con.createStatement();
50                         ResultSet rs = theStorageObject.executeSql(stmt,"select image_data from images where id="+getId());
51                         if(rs!=null) {
52                                 if (rs.next()) {
53                                          img_data = rs.getBytes(1);
54                                 }
55                                 rs.close();
56                         }
57                 }
58                 catch (Exception e) {theLog.printDebugInfo("-- getImage gescheitert: "+e.toString());}
59                 finally {
60                         try {con.setAutoCommit(true); } catch (Exception e) {;}
61                         theStorageObject.freeConnection(con,stmt); }
62
63                 return img_data;
64         }
65
66         public void setImage(byte[] uploadData, int imageType)
67         {
68                 if (uploadData!=null) {
69                         Connection con=null;PreparedStatement pstmt=null;
70                         try {
71
72                                 theLog.printDebugInfo("settimage :: making internal representation of image");
73                                 WebdbImage webdbImage= new WebdbImage(uploadData,imageType);
74                                 theLog.printDebugInfo("settimage :: made internal representation of image");
75                                 byte[] imageData = webdbImage.getImage();
76                                 theLog.printDebugInfo("settimage :: getImage");
77                                 byte[] iconData = webdbImage.getIcon();
78                                 theLog.printDebugInfo("settimage :: getIcon");
79
80                                 if (iconData!=null && imageData!=null) {
81                                         con = theStorageObject.getPooledCon();
82                                         con.setAutoCommit(false);
83                                         theLog.printDebugInfo("settimage :: trying to insert image");
84
85                                         // setting values
86                                         String sql = "update images set img_height='"+webdbImage.getImageHeight() +
87                                                 "',img_width='"   + webdbImage.getImageWidth() +
88                                                 "',icon_height='" + webdbImage.getIconHeight() +
89                                                 "',icon_width='"  + webdbImage.getIconWidth()  +  "', image_data=?, icon_data=? where id="+getId();
90                                         theLog.printDebugInfo("settimage :: updating sizes: "+ sql);
91                                         pstmt = con.prepareStatement(sql);
92                                         pstmt.setBytes(1, imageData);
93                                         pstmt.setBytes(2, iconData);
94                                         pstmt.executeUpdate();
95                                         sql="update content set is_produced='0' where to_media="+getId();
96                                         pstmt = con.prepareStatement(sql);
97                                         pstmt.executeUpdate();
98                                 }
99                         }
100                         catch (Exception e) {theLog.printDebugInfo("settimage :: setImage gescheitert: "+e.toString());}
101                         finally {
102                                 try {con.setAutoCommit(true); } catch (Exception e) {;}
103                                 theStorageObject.freeConnection(con,pstmt); }
104                 }
105         }
106
107         public void update() throws StorageObjectException {
108                 super.update();
109                 try {
110                         theStorageObject.executeUpdate("update content set is_produced='0' where to_media="+getId());
111                 } catch (SQLException e) {
112                         theLog.printError("EntityImage :: update :: failed!! "+ e.toString());
113                 }
114         }
115
116         public void setValues(HashMap theStringValues)
117         {
118                 if (theStringValues != null) {
119                         if (!theStringValues.containsKey("is_published"))
120                          theStringValues.put("is_published","0");
121                 }
122                 super.setValues(theStringValues);
123         }
124
125         public byte[] getIcon()
126         {
127                 Connection con=null;Statement stmt=null;
128                 byte[] img_data=null;
129
130                 try {
131                         con = theStorageObject.getPooledCon();
132                         con.setAutoCommit(false);
133                         stmt = con.createStatement();
134                         ResultSet rs = theStorageObject.executeSql(stmt,"select icon_data from images where id="+getId());
135                         if(rs!=null) {
136                                 if (rs.next()) {
137                                          img_data = rs.getBytes(1);
138                                 }
139                                 rs.close();
140                         }
141                 }
142                 catch (Exception e) {theLog.printDebugInfo("-- getIcon gescheitert: "+e.toString());}
143                 finally {
144                         try {con.setAutoCommit(true); } catch (Exception e) {;}
145                         theStorageObject.freeConnection(con,stmt); }
146
147                 return img_data;
148         }
149
150         public void finalize() {
151     instances--;
152     super.finalize();
153   }
154
155 }