6cd505a3e1df5916efd28b5fb4d8a4e8cf8b0c6c
[mir.git] / source / mircoders / entity / EntityOther.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, getOther
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  * This class handles storage of other data and meta data
23  *
24  * @author mh
25  * @version 11.11.2000
26  */
27
28
29 public class EntityOther extends EntityUploadedMedia
30 {
31         public EntityOther()
32         {
33                 super();
34         }
35
36         public EntityOther(StorageObject theStorage) {
37                 this();
38                 setStorage(theStorage);
39         }
40
41         //
42         // methods
43
44
45
46         public byte[] getOther() throws StorageObjectException
47         {
48                 theLog.printDebugInfo("--getother 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 other_data from other 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                 lob.close();
68                 //img_data = rs.getBytes(1);
69               }
70             rs.close();
71                         }
72                 } catch (Exception e) {
73           e.printStackTrace();
74           theLog.printError("EntityOther -- getOther failed"+e.toString()); 
75           throwStorageObjectException(e, "EntityOther -- getOther failed: ");
76         }
77         finally {
78           try {
79             con.setAutoCommit(true);
80           } catch (Exception e) {
81             e.printStackTrace();
82             theLog.printError(
83                     "EntityOther -- getOther reseting transaction mode failed"
84                     +e.toString()); 
85           }
86           theStorageObject.freeConnection(con,stmt);
87         }
88
89                 return img_data;
90         }
91
92         public void setOther(byte[] otherData, String otherType)
93             throws StorageObjectException {
94
95                 if (otherData!=null) {
96                         java.sql.Connection con=null;PreparedStatement pstmt=null;
97                         try {
98
99                                 theLog.printDebugInfo("settother :: making internal representation of other");
100                                 theLog.printDebugInfo("settother :: made internal representation of other");
101                                 theLog.printDebugInfo("settother :: getOther");
102
103                                 if ( otherData!=null) {
104                                         con = theStorageObject.getPooledCon();
105                                         con.setAutoCommit(false);
106                                         theLog.printDebugInfo("settother :: trying to insert other");
107
108                                         // setting values
109                                         pstmt.setBytes(1, otherData);
110                                         String sql="update content set is_produced='0' where to_media="+getId();
111                                         pstmt = con.prepareStatement(sql);
112                                         pstmt.executeUpdate();
113                                 }
114                         }
115                         catch (Exception e) {throwStorageObjectException(e, "settother :: setOther gescheitert: ");}
116                         finally {
117                                 try { if (con!=null) con.setAutoCommit(true); } catch (Exception e) {;}
118                                 theStorageObject.freeConnection(con,pstmt); }
119                 }
120         }
121
122         public void update() throws StorageObjectException {
123                 super.update();
124                 try {
125                         theStorageObject.executeUpdate("update content set is_produced='0' where to_media="+getId());
126                 } catch (SQLException e) {
127                         throwStorageObjectException(e, "EntityOther :: update :: failed!! ");
128                 }
129         }
130
131         public void setValues(HashMap theStringValues)
132         {
133                 if (theStringValues != null) {
134                         if (!theStringValues.containsKey("is_published"))
135                          theStringValues.put("is_published","0");
136                 }
137                 super.setValues(theStringValues);
138         }
139
140 }