proper handling of exceptions all around. especially for the media stuff.
[mir.git] / source / mircoders / storage / DatabaseContentToMedia.java
1 package mircoders.storage;
2
3 import java.lang.*;
4 import java.sql.*;
5 import java.io.*;
6 import java.util.*;
7
8 import freemarker.template.*;
9
10 import mir.storage.*;
11 import mir.entity.*;
12 import mir.misc.*;
13
14 import mircoders.entity.*;
15
16 /**
17  * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
18  *
19  *
20  */
21
22 public class DatabaseContentToMedia extends Database implements StorageObject{
23
24   private static DatabaseContentToMedia instance;
25
26   public static DatabaseContentToMedia getInstance()
27     throws StorageObjectException {
28     if (instance == null) {
29       instance = new DatabaseContentToMedia();
30       instance.myselfDatabase = instance;
31     }
32     return instance;
33   }
34
35   private DatabaseContentToMedia()
36     throws StorageObjectException {
37
38     super();
39     this.hasTimestamp = false;
40     this.theTable="content_x_media";
41   }
42   
43   /**
44    * get all the media-files belonging to a content entity
45    *
46    */
47   public EntityList getMedia(EntityContent content) {
48     EntityList returnList=null;
49     if (content != null) {
50       // get all to_topic from media_x_topic
51       String id = content.getId();
52       //this is not supported by mysql
53       String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
54
55       try {
56         returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,-1);
57       } catch (Exception e) {
58         theLog.printDebugInfo("-- get media failed " + e.toString());
59       }
60     }
61     return returnList;
62   }
63   
64   /**
65    * get all the images belonging to a content entity
66    *
67    */
68   public EntityList getImages(EntityContent content) {
69     EntityList returnList=null;
70     if (content != null) {
71       // get all to_topic from media_x_topic
72       String id = content.getId();
73       //this is not supported by mysql
74       String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
75
76       try {
77         returnList = DatabaseImages.getInstance().selectByWhereClause(subselect,-1);
78       } catch (Exception e) {
79         theLog.printDebugInfo("-- get images failed " + e.toString());
80       }
81     }
82     return returnList;
83   }
84
85
86   /**
87    * get all the uploaded Media belonging to a content entity
88    *
89    */
90   public EntityList getUploadedMedia(EntityContent content) {
91     EntityList returnList=null;
92     if (content != null) {
93       // get all to_topic from media_x_topic
94       String id = content.getId();
95       //this is not supported by mysql
96       String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
97
98       try {
99         returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,-1);
100       } catch (Exception e) {
101         theLog.printDebugInfo("-- get uploadedMedia failed " + e.toString());
102       }
103     }
104     return returnList;
105   }
106
107
108   public void setMedia(String contentId, String[] mediaId)
109     throws StorageObjectException {
110     if (contentId == null){
111       return;
112     }
113     if (mediaId==null || mediaId[0]==null) {
114       return;
115     }
116     //first delete all row with content_id=contentId
117     String sql = "delete from "+ theTable +" where content_id=" + contentId;
118   
119     Connection con=null;Statement stmt=null;
120     try {
121       con = getPooledCon();
122       // should be a preparedStatement because is faster
123       stmt = con.createStatement();
124       ResultSet rs = executeSql(stmt,sql);
125     } catch (Exception e) {
126       theLog.printDebugInfo("-- set media failed -- delete");
127     } finally {
128       freeConnection(con,stmt);
129     }
130   
131     //now insert
132     //first delete all row with content_id=contentId
133     for (int i=0;i<mediaId.length;i++) {
134       sql = "insert into "+ theTable +" (content_id,media_id) values ("
135             + contentId + "," + mediaId[i] + ")";
136       try {
137         con = getPooledCon();
138         // should be a preparedStatement because is faster
139         stmt = con.createStatement();
140         int rs = executeUpdate(stmt,sql);
141       } catch (Exception e) {
142         theLog.printDebugInfo("-- set topics failed -- insert");
143       } finally {
144         freeConnection(con,stmt);
145       }
146     }
147   }
148
149   public void addMedia(String contentId, String mediaId)
150     throws StorageObjectException {
151     if (contentId == null && mediaId == null) {
152       return;
153     }
154     
155     Connection con=null;Statement stmt=null;
156     //now insert
157     
158     String sql = "insert into "+ theTable +" (content_id,media_id) values ("
159           + contentId + "," + mediaId + ")";
160     try {
161       con = getPooledCon();
162       // should be a preparedStatement because is faster
163       stmt = con.createStatement();
164       int rs = executeUpdate(stmt,sql);
165     } catch (Exception e) {
166       theLog.printDebugInfo("-- add media failed -- insert");
167     } finally {
168       freeConnection(con,stmt);
169     }
170   }
171   
172   public void setMedia(String contentId, String mediaId)
173     throws StorageObjectException {
174     if (contentId == null && mediaId == null) {
175       return;
176     }
177     //first delete all row with content_id=contentId
178     String sql = "delete from "+ theTable +" where content_id=" + contentId;
179
180     Connection con=null;Statement stmt=null;
181     try {
182       con = getPooledCon();
183       // should be a preparedStatement because is faster
184       stmt = con.createStatement();
185       int rs = executeUpdate(stmt,sql);
186     } catch (Exception e) {
187       theLog.printDebugInfo("-- set media failed -- delete");
188     } finally {
189       freeConnection(con,stmt);
190     }
191
192     //now insert
193     //first delete all row with content_id=contentId
194
195     sql = "insert into "+ theTable +" (content_id,media_id) values ("
196           + contentId + "," + mediaId + ")";
197     try {
198       con = getPooledCon();
199       // should be a preparedStatement because is faster
200       stmt = con.createStatement();
201       int rs = executeUpdate(stmt,sql);
202     } catch (Exception e) {
203       theLog.printDebugInfo("-- set media failed -- insert");
204     } finally {
205       freeConnection(con,stmt);
206     }
207   }
208
209   public void deleteByContentId(String contentId)
210     throws StorageObjectException {
211     if (contentId == null) {
212       //theLog.printDebugInfo("-- delete topics failed -- no content id");
213       return;
214     }
215     //delete all row with content_id=contentId
216     String sql = "delete from "+ theTable +" where content_id=" + contentId;
217
218     Connection con=null;Statement stmt=null;
219     try {
220       con = getPooledCon();
221       // should be a preparedStatement because is faster
222       stmt = con.createStatement();
223       int rs = executeUpdate(stmt,sql);
224     } catch (Exception e) {
225       //theLog.printDebugInfo("-- delete topics failed  ");
226     } finally {
227       freeConnection(con,stmt);
228     }
229   }
230
231   public void deleteByMediaId(String mediaId)
232     throws StorageObjectException {
233     if (mediaId == null) {
234       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
235       return;
236     }
237     //delete all row with content_id=contentId
238     String sql = "delete from "+ theTable +" where media_id=" + mediaId;
239
240     Connection con=null;Statement stmt=null;
241     try {
242       con = getPooledCon();
243       // should be a preparedStatement because is faster
244       stmt = con.createStatement();
245       int rs = executeUpdate(stmt,sql);
246       theLog.printDebugInfo("-- delete media success ");
247     } catch (Exception e) {
248       theLog.printDebugInfo("-- delete media failed ");
249     } finally {
250       freeConnection(con,stmt);
251     }
252   }
253   
254   public void delete(String contentId, String mediaId)
255     throws StorageObjectException {
256     if (mediaId == null || contentId==null) {
257       theLog.printDebugInfo("-- delete media failed -- missing parameter");
258       return;
259     }
260     //delete all row with content_id=contentId and media_id=mediaId
261     String sql = "delete from "+ theTable +" where media_id=" + mediaId + " and content_id= "+contentId;
262
263     Connection con=null;Statement stmt=null;
264     try {
265       con = getPooledCon();
266       // should be a preparedStatement because is faster
267       stmt = con.createStatement();
268       int rs = executeUpdate(stmt,sql);
269       theLog.printDebugInfo("-- delete content_x_media success ");
270     } catch (Exception e) {
271       theLog.printDebugInfo("-- delete content_x_media failed ");
272     } finally {
273       freeConnection(con,stmt);
274     }
275   }
276
277
278   public EntityList getContent(EntityMedia media)
279     throws StorageObjectException {
280     EntityList returnList=null;
281     if (media != null) {
282       String id = media.getId();
283       String select = "select content_id from " + theTable + " where media_id=" + id;
284
285       // execute select statement
286       Connection con=null;Statement stmt=null;
287       try {
288         con = getPooledCon();
289         // should be a preparedStatement because is faster
290         stmt = con.createStatement();
291         ResultSet rs = executeSql(stmt,select);
292         if (rs!=null) {
293           String mediaSelect= "id IN (";
294           boolean first=true;
295           while (rs.next()) {
296             if (first==false) mediaSelect+=",";
297             mediaSelect += rs.getString(1);
298             first=false;
299           }
300           mediaSelect+=")";
301           if (first==false)
302             returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,-1);
303         }
304       }
305       catch (Exception e) {theLog.printDebugInfo("-- get content failed");}
306       finally { freeConnection(con,stmt);}
307     }
308     return returnList;
309   }
310   
311 /**
312  * Returns a EntityList with all content-objects having a relation to a media
313  */
314   
315 public EntityList getContent()
316     throws StorageObjectException {
317     EntityList returnList=null;
318     
319     String select = "select distinct content_id from " + theTable;
320     // execute select statement
321     Connection con=null;Statement stmt=null;
322     try {
323       con = getPooledCon();
324       // should be a preparedStatement because is faster
325       stmt = con.createStatement();
326       ResultSet rs = executeSql(stmt,select);
327       if (rs!=null) {
328         String mediaSelect= "id IN (";
329         boolean first=true;
330         while (rs.next()) {
331           if (first==false) mediaSelect+=",";
332           mediaSelect += rs.getString(1);
333           first=false;
334         }
335         mediaSelect+=")";
336         if (first==false)
337           returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,"webdb_lastchange desc");
338       }
339     }
340     catch (Exception e) {theLog.printDebugInfo("-- get content failed");}
341     finally { freeConnection(con,stmt);}
342
343     return returnList;
344   }
345
346 }