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