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