first cut of merge of STABLE-pre1_0 into HEAD. I won't even guarantee that it
[mir.git] / source / mircoders / entity / EntityAudio.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, getAudio
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 audio data and meta data
23  *
24  * @author mh
25  * @version 11.11.2000
26  */
27
28
29 public class EntityAudio extends EntityUploadedMedia
30 {
31         public EntityAudio()
32         {
33                 super();
34         }
35
36         public EntityAudio(StorageObject theStorage) {
37                 this();
38                 setStorage(theStorage);
39         }
40
41         //
42         // methods
43
44
45
46         public byte[] getAudio() throws StorageObjectException
47         {
48                 theLog.printDebugInfo("--getaudio started");
49                 java.sql.Connection con=null;Statement stmt=null;
50                 byte[] 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 audio_data from audio 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                 data = lob.read(lob.size());
67                 lob.close();
68                 //data = rs.getBytes(1);
69               }
70             rs.close();
71                         }
72                 } catch (Exception e) {
73           e.printStackTrace();
74           theLog.printError("EntityAudio -- getAudio failed"+e.toString()); 
75           throwStorageObjectException(e, "EntityAudio -- getAudio failed: ");
76         }
77         finally {
78           try {
79             con.setAutoCommit(true);
80           } catch (Exception e) {
81             e.printStackTrace();
82             theLog.printError(
83                     "EntityAudio -- getAudio reseting transaction mode failed"
84                     +e.toString()); 
85           }
86           theStorageObject.freeConnection(con,stmt);
87         }
88
89                 return data;
90         }
91
92         public void setAudio(byte[] uploadData)
93             throws StorageObjectException {
94
95                 if (uploadData!=null) {
96                         java.sql.Connection con=null;PreparedStatement pstmt=null;
97                         try {
98
99                                 if (uploadData!=null) {
100                                         con = theStorageObject.getPooledCon();
101                                         con.setAutoCommit(false);
102                                         theLog.printDebugInfo("setaudio :: trying to insert audio");
103
104                                         // setting values
105                     LargeObjectManager lom;
106                     java.sql.Connection jCon;
107                     jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
108                             .getNativeConnection();
109                     lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
110                     int oid = lom.create();
111                     LargeObject lob = lom.open(oid);
112                     lob.write(uploadData);
113                     lob.close();
114                     String sql = "update images set"
115                                  +" audio_data="+oid
116                                  +" where id="+getId();
117                                         theLog.printDebugInfo("setaudio :: updating: "+ sql);
118                                         pstmt = con.prepareStatement(sql);
119                                         //pstmt.setBytes(1, imageData);
120                                         //pstmt.setBytes(2, iconData);
121                                         pstmt.executeUpdate();
122                                         sql="update content set is_produced='0' where to_media="+getId();
123                                         pstmt = con.prepareStatement(sql);
124                                         pstmt.executeUpdate();
125                                 }
126                         }
127                         catch (Exception e) {
128                 throwStorageObjectException(e,"setaudio ::failed: ");
129             }
130                         finally {
131                                 try { if (con!=null) con.setAutoCommit(true); } catch (Exception e) {;}
132                                 theStorageObject.freeConnection(con,pstmt); }
133                 }
134         }
135
136
137         public void update() throws StorageObjectException {
138                 super.update();
139                 try {
140                         theStorageObject.executeUpdate("update content set is_produced='0' where to_media="+getId());
141                 } catch (SQLException e) {
142                         throwStorageObjectException(e, "EntityAudio :: update :: failed!! ");
143                 }
144         }
145
146         public void setValues(HashMap theStringValues)
147         {
148                 if (theStringValues != null) {
149                         if (!theStringValues.containsKey("is_published"))
150                          theStringValues.put("is_published","0");
151                 }
152                 super.setValues(theStringValues);
153         }
154
155 }