bugfixes mainly...
[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   // the following *has* to be sychronized cause this static method
27   // could get preemted and we could end up with 2 instances of DatabaseFoo.
28   // see the "Singletons with needles and thread" article at JavaWorld -mh
29   public synchronized static DatabaseContentToMedia getInstance()
30     throws StorageObjectException {
31     if (instance == null) {
32       instance = new DatabaseContentToMedia();
33       instance.myselfDatabase = instance;
34     }
35     return instance;
36   }
37
38   private DatabaseContentToMedia()
39     throws StorageObjectException {
40
41     super();
42     this.hasTimestamp = false;
43     this.theTable="content_x_media";
44     try { this.theEntityClass = Class.forName("mir.entity.GenericEntity"); }
45     catch (Exception e) { throw new StorageObjectException(e.toString()); }
46   }
47
48   /**
49    * get all the media-files belonging to a content entity
50    *
51    */
52   public EntityList getMedia(EntityContent content)
53     throws StorageObjectException {
54     EntityList returnList=null;
55     if (content != null) {
56       // get all to_topic from media_x_topic
57       String id = content.getId();
58       //this is not supported by mysql
59       String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
60
61       try {
62         returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,-1);
63       } catch (Exception e) {
64         theLog.printDebugInfo("-- get media failed " + e.toString());
65         throw new StorageObjectException("-- get media failed " + e.toString());
66       }
67     }
68     return returnList;
69   }
70
71   public boolean hasMedia(EntityContent content)
72     throws StorageObjectException {
73     String wc = "content_id="+content.getId();
74     if( content != null) {
75       try {
76         if(selectByWhereClause(wc,-1).size() == 0)
77           return false;
78         else
79           return true;
80       } catch (Exception e) {
81         theLog.printError("-- hasMedia failed " + e.toString());
82         throw new StorageObjectException("-- hasMedia failed " + e.toString());
83       }
84     } else {
85       theLog.printError("-- hasMedia failed: content is NULL");
86       throw new StorageObjectException("-- hasMedia failed: content is NULL");
87     }
88   }
89
90
91
92
93   /**
94    * get all the audio belonging to a content entity
95    *
96    */
97   public EntityList getAudio(EntityContent content)
98     throws StorageObjectException {
99     EntityList returnList=null;
100     if (content != null) {
101       // get all to_topic from media_x_topic
102       String id = content.getId();
103       //this is not supported by mysql
104       String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
105
106       try {
107         returnList = DatabaseAudio.getInstance().selectByWhereClause(subselect,-1);
108       } catch (Exception e) {
109         theLog.printDebugInfo("-- get audio failed " + e.toString());
110         throw new StorageObjectException("-- get audio failed " + e.toString());
111       }
112     }
113     return returnList;
114   }
115
116   /**
117    * get all the video belonging to a content entity
118    *
119    */
120   public EntityList getVideo(EntityContent content)
121     throws StorageObjectException {
122     EntityList returnList=null;
123     if (content != null) {
124       // get all to_topic from media_x_topic
125       String id = content.getId();
126       //this is not supported by mysql
127       String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
128
129       try {
130         returnList = DatabaseVideo.getInstance().selectByWhereClause(subselect,-1);
131       } catch (Exception e) {
132         theLog.printDebugInfo("-- get video failed " + e.toString());
133         throw new StorageObjectException("-- get video failed " + e.toString());
134       }
135     }
136     return returnList;
137   }
138
139   /**
140    * get all the images belonging to a content entity
141    *
142    */
143   public EntityList getImages(EntityContent content)
144     throws StorageObjectException {
145     EntityList returnList=null;
146     if (content != null) {
147       // get all to_topic from media_x_topic
148       String id = content.getId();
149       //this is not supported by mysql
150       String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
151
152       try {
153         returnList = DatabaseImages.getInstance().selectByWhereClause(subselect,-1);
154       } catch (Exception e) {
155         theLog.printDebugInfo("-- get images failed " + e.toString());
156         throw new StorageObjectException("-- get images failed " + e.toString());
157       }
158     }
159     return returnList;
160   }
161
162
163   /**
164    * get all the uploaded/other Media belonging to a content entity
165    *
166    */
167   public EntityList getOther(EntityContent content)
168     throws StorageObjectException
169   {
170     /** @todo this should only fetch published media / rk */
171
172     EntityList returnList=null;
173     if (content != null) {
174       // get all to_topic from media_x_topic
175       String id = content.getId();
176       //this is not supported by mysql
177       String subselect = "id in (select media_id from " + theTable +
178                                 " where content_id=" + id+")";
179
180       try {
181         returnList = DatabaseOther.getInstance().selectByWhereClause(subselect,
182                                                                     "id");
183       } catch (Exception e) {
184         e.printStackTrace();
185         theLog.printDebugInfo("-- get Other failed " + e.toString());
186         throw new StorageObjectException("-- get Other failed "
187                                         + e.toString());
188       }
189     }
190     return returnList;
191   }
192
193   /**
194    * get all the uploaded/other Media belonging to a content entity
195    *
196    */
197   public EntityList getUploadedMedia(EntityContent content)
198     throws StorageObjectException
199   {
200     /** @todo this should only fetch published media / rk */
201
202     EntityList returnList=null;
203     if (content != null) {
204       // get all to_topic from media_x_topic
205       String id = content.getId();
206       //this is not supported by mysql
207       String subselect = "id in (select media_id from " + theTable +
208                                 " where content_id=" + id+")";
209
210       try {
211         returnList = DatabaseUploadedMedia.getInstance().selectByWhereClause(subselect,
212                                                                     "id");
213       } catch (Exception e) {
214         e.printStackTrace();
215         theLog.printDebugInfo("-- get uploadedMedia failed " + e.toString());
216         throw new StorageObjectException("-- get uploadedMedia failed "
217                                         + e.toString());
218       }
219     }
220     return returnList;
221   }
222
223
224   public void setMedia(String contentId, String[] mediaId)
225     throws StorageObjectException {
226     if (contentId == null){
227       return;
228     }
229     if (mediaId==null || mediaId[0]==null) {
230       return;
231     }
232     //first delete all row with content_id=contentId
233     String sql = "delete from "+ theTable +" where content_id=" + contentId;
234
235     Connection con=null;Statement stmt=null;
236     try {
237       con = getPooledCon();
238       // should be a preparedStatement because is faster
239       stmt = con.createStatement();
240       ResultSet rs = executeSql(stmt,sql);
241     } catch (Exception e) {
242       theLog.printDebugInfo("-- set media failed -- delete");
243       throw new StorageObjectException("-- set media failed -- delete"+e.toString());
244     } finally {
245       freeConnection(con,stmt);
246     }
247
248     //now insert
249     //first delete all row with content_id=contentId
250     for (int i=0;i<mediaId.length;i++) {
251       sql = "insert into "+ theTable +" (content_id,media_id) values ("
252             + contentId + "," + mediaId[i] + ")";
253       try {
254         con = getPooledCon();
255         // should be a preparedStatement because is faster
256         stmt = con.createStatement();
257         int rs = executeUpdate(stmt,sql);
258       } catch (Exception e) {
259         theLog.printDebugInfo("-- set topics failed -- insert");
260         throw new StorageObjectException("-- set topics failed -- insert "+e.toString());
261       } finally {
262         freeConnection(con,stmt);
263       }
264     }
265   }
266
267   public void addMedia(String contentId, String mediaId)
268     throws StorageObjectException {
269     if (contentId == null && mediaId == null) {
270       return;
271     }
272
273     Connection con=null;Statement stmt=null;
274     //now insert
275
276     String sql = "insert into "+ theTable +" (content_id,media_id) values ("
277           + contentId + "," + mediaId + ")";
278     try {
279       con = getPooledCon();
280       // should be a preparedStatement because is faster
281       stmt = con.createStatement();
282       int rs = executeUpdate(stmt,sql);
283     } catch (Exception e) {
284       theLog.printDebugInfo("-- add media failed -- insert");
285       throw new StorageObjectException("-- add media failed -- insert "
286         +e.toString());
287     } finally {
288       freeConnection(con,stmt);
289     }
290   }
291
292   public void setMedia(String contentId, String mediaId)
293     throws StorageObjectException {
294     if (contentId == null && mediaId == null) {
295       return;
296     }
297     //first delete all row with content_id=contentId
298     String sql = "delete from "+ theTable +" where content_id=" + contentId;
299
300     Connection con=null;Statement stmt=null;
301     try {
302       con = getPooledCon();
303       // should be a preparedStatement because is faster
304       stmt = con.createStatement();
305       int rs = executeUpdate(stmt,sql);
306     } catch (Exception e) {
307       theLog.printDebugInfo("-- set media failed -- delete");
308       throw new StorageObjectException("-- set media failed -- delete "
309         +e.toString());
310     } finally {
311       freeConnection(con,stmt);
312     }
313
314     //now insert
315     //first delete all row with content_id=contentId
316
317     sql = "insert into "+ theTable +" (content_id,media_id) values ("
318           + contentId + "," + mediaId + ")";
319     try {
320       con = getPooledCon();
321       // should be a preparedStatement because is faster
322       stmt = con.createStatement();
323       int rs = executeUpdate(stmt,sql);
324     } catch (Exception e) {
325       theLog.printDebugInfo("-- set media failed -- insert");
326       throw new StorageObjectException("-- set media failed -- insert "
327         +e.toString());
328     } finally {
329       freeConnection(con,stmt);
330     }
331   }
332
333   public void deleteByContentId(String contentId)
334     throws StorageObjectException {
335     if (contentId == null) {
336       //theLog.printDebugInfo("-- delete topics failed -- no content id");
337       return;
338     }
339     //delete all row with content_id=contentId
340     String sql = "delete from "+ theTable +" where content_id=" + contentId;
341
342     Connection con=null;Statement stmt=null;
343     try {
344       con = getPooledCon();
345       // should be a preparedStatement because is faster
346       stmt = con.createStatement();
347       int rs = executeUpdate(stmt,sql);
348     } catch (Exception e) {
349       theLog.printDebugInfo("-- delete by contentId failed  ");
350       throw new StorageObjectException("-- delete by content id failed -- delete "
351         +e.toString());
352     } finally {
353       freeConnection(con,stmt);
354     }
355   }
356
357   public void deleteByMediaId(String mediaId)
358     throws StorageObjectException {
359     if (mediaId == null) {
360       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
361       return;
362     }
363     //delete all row with content_id=contentId
364     String sql = "delete from "+ theTable +" where media_id=" + mediaId;
365
366     Connection con=null;Statement stmt=null;
367     try {
368       con = getPooledCon();
369       // should be a preparedStatement because is faster
370       stmt = con.createStatement();
371       int rs = executeUpdate(stmt,sql);
372       theLog.printDebugInfo("-- delete media success ");
373     } catch (Exception e) {
374       theLog.printDebugInfo("-- delete media failed ");
375       throw new StorageObjectException("-- delete by media id failed -- "
376         +e.toString());
377     } finally {
378       freeConnection(con,stmt);
379     }
380   }
381
382   public void delete(String contentId, String mediaId)
383     throws StorageObjectException {
384     if (mediaId == null || contentId==null) {
385       theLog.printDebugInfo("-- delete media failed -- missing parameter");
386       return;
387     }
388     //delete all row with content_id=contentId and media_id=mediaId
389     String sql = "delete from "+ theTable +" where media_id=" + mediaId + " and content_id= "+contentId;
390
391     Connection con=null;Statement stmt=null;
392     try {
393       con = getPooledCon();
394       // should be a preparedStatement because is faster
395       stmt = con.createStatement();
396       int rs = executeUpdate(stmt,sql);
397       theLog.printDebugInfo("-- delete content_x_media success ");
398     } catch (Exception e) {
399       theLog.printDebugInfo("-- delete content_x_media failed ");
400       throw new StorageObjectException("-- delete content_x_media failed -- "
401         +e.toString());
402     } finally {
403       freeConnection(con,stmt);
404     }
405   }
406
407
408   public EntityList getContent(EntityMedia media)
409     throws StorageObjectException {
410     EntityList returnList=null;
411     if (media != null) {
412       String id = media.getId();
413       String select = "select content_id from " + theTable + " where media_id=" + id;
414
415       // execute select statement
416       Connection con=null;Statement stmt=null;
417       try {
418         con = getPooledCon();
419         // should be a preparedStatement because is faster
420         stmt = con.createStatement();
421         ResultSet rs = executeSql(stmt,select);
422         if (rs!=null) {
423           String mediaSelect= "id IN (";
424           boolean first=true;
425           while (rs.next()) {
426             if (first==false) mediaSelect+=",";
427             mediaSelect += rs.getString(1);
428             first=false;
429           }
430           mediaSelect+=")";
431           if (first==false)
432             returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,-1);
433         }
434       }
435       catch (Exception e) {
436         theLog.printDebugInfo("-- get content failed");
437         throw new StorageObjectException("-- get content failed -- "
438         +e.toString());
439       }
440       finally { freeConnection(con,stmt);}
441     }
442     return returnList;
443   }
444
445 /**
446  * Returns a EntityList with all content-objects having a relation to a media
447  */
448
449 public EntityList getContent()
450     throws StorageObjectException {
451     EntityList returnList=null;
452
453     String select = "select distinct content_id from " + theTable;
454     // execute select statement
455     Connection con=null;Statement stmt=null;
456     try {
457       con = getPooledCon();
458       // should be a preparedStatement because is faster
459       stmt = con.createStatement();
460       ResultSet rs = executeSql(stmt,select);
461       if (rs!=null) {
462         String mediaSelect= "id IN (";
463         boolean first=true;
464         while (rs.next()) {
465           if (first==false) mediaSelect+=",";
466           mediaSelect += rs.getString(1);
467           first=false;
468         }
469         mediaSelect+=")";
470         if (first==false)
471           returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,"webdb_lastchange desc");
472       }
473     }
474     catch (Exception e) {
475         theLog.printDebugInfo("-- get content failed");
476         throw new StorageObjectException("-- get content failed -- "
477         +e.toString());
478     }
479     finally { freeConnection(con,stmt);}
480
481     return returnList;
482   }
483
484 }