d13ac5c5108e4c7305cf73b012ad1b67ca3f29ad
[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 import mir.entity.*;
9 import mir.misc.*;
10 import mir.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 EntityImages extends Entity
21 {
22         private static int instances;
23
24         public EntityImages()
25         {
26                 super();
27                 instances++;
28         }
29
30         public EntityImages(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, String imageType)
67         {
68         int type = 0;
69
70         //hack -mh
71         if (imageType.equals("1"))
72             type = 1;
73          //end hack
74
75                 if (uploadData!=null) {
76                         Connection con=null;PreparedStatement pstmt=null;
77                         try {
78
79                                 theLog.printDebugInfo("settimage :: making internal representation of image");
80                                 WebdbImage webdbImage= new WebdbImage(uploadData,type);
81                                 theLog.printDebugInfo("settimage :: made internal representation of image");
82                                 byte[] imageData = webdbImage.getImage();
83                                 theLog.printDebugInfo("settimage :: getImage");
84                                 byte[] iconData = webdbImage.getIcon();
85                                 theLog.printDebugInfo("settimage :: getIcon");
86
87                                 if (iconData!=null && imageData!=null) {
88                                         con = theStorageObject.getPooledCon();
89                                         con.setAutoCommit(false);
90                                         theLog.printDebugInfo("settimage :: trying to insert image");
91
92                                         // setting values
93                                         String sql = "update images set img_height='"+webdbImage.getImageHeight() +
94                                                 "',img_width='"   + webdbImage.getImageWidth() +
95                                                 "',icon_height='" + webdbImage.getIconHeight() +
96                                                 "',icon_width='"  + webdbImage.getIconWidth()  +  "', image_data=?, icon_data=? where id="+getId();
97                                         theLog.printDebugInfo("settimage :: updating sizes: "+ sql);
98                                         pstmt = con.prepareStatement(sql);
99                                         pstmt.setBytes(1, imageData);
100                                         pstmt.setBytes(2, iconData);
101                                         pstmt.executeUpdate();
102                                         sql="update content set is_produced='0' where to_media="+getId();
103                                         pstmt = con.prepareStatement(sql);
104                                         pstmt.executeUpdate();
105                                 }
106                         }
107                         catch (Exception e) {theLog.printDebugInfo("settimage :: setImage gescheitert: "+e.toString());}
108                         finally {
109                                 try {con.setAutoCommit(true); } catch (Exception e) {;}
110                                 theStorageObject.freeConnection(con,pstmt); }
111                 }
112         }
113
114         public void update() throws StorageObjectException {
115                 super.update();
116                 try {
117                         theStorageObject.executeUpdate("update content set is_produced='0' where to_media="+getId());
118                 } catch (SQLException e) {
119                         theLog.printError("EntityImages :: update :: failed!! "+ e.toString());
120                 }
121         }
122
123         public void setValues(HashMap theStringValues)
124         {
125                 if (theStringValues != null) {
126                         if (!theStringValues.containsKey("is_published"))
127                          theStringValues.put("is_published","0");
128                 }
129                 super.setValues(theStringValues);
130         }
131
132         public byte[] getIcon()
133         {
134                 Connection con=null;Statement stmt=null;
135                 byte[] img_data=null;
136
137                 try {
138                         con = theStorageObject.getPooledCon();
139                         con.setAutoCommit(false);
140                         stmt = con.createStatement();
141                         ResultSet rs = theStorageObject.executeSql(stmt,"select icon_data from images where id="+getId());
142                         if(rs!=null) {
143                                 if (rs.next()) {
144                                          img_data = rs.getBytes(1);
145                                 }
146                                 rs.close();
147                         }
148                 }
149                 catch (Exception e) {theLog.printDebugInfo("-- getIcon gescheitert: "+e.toString());}
150                 finally {
151                         try {con.setAutoCommit(true); } catch (Exception e) {;}
152                         theStorageObject.freeConnection(con,stmt); }
153
154                 return img_data;
155         }
156
157         public void finalize() {
158     instances--;
159     super.finalize();
160   }
161
162 }