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