update the getContent method so it uses the newer EntityUploadedMedia and not the...
[mir.git] / source / mircoders / storage / DatabaseContentToMedia.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mircoders.storage;
33
34 import java.lang.*;
35 import java.sql.*;
36 import java.io.*;
37 import java.util.*;
38
39 import freemarker.template.*;
40
41 import mir.storage.*;
42 import mir.entity.*;
43 import mir.misc.*;
44
45 import mircoders.entity.*;
46
47 /**
48  * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
49  *
50  *
51  */
52
53 public class DatabaseContentToMedia extends Database implements StorageObject{
54
55   private static DatabaseContentToMedia instance;
56
57   // the following *has* to be sychronized cause this static method
58   // could get preemted and we could end up with 2 instances of DatabaseFoo.
59   // see the "Singletons with needles and thread" article at JavaWorld -mh
60   public synchronized static DatabaseContentToMedia getInstance()
61     throws StorageObjectException {
62     if (instance == null) {
63       instance = new DatabaseContentToMedia();
64       instance.myselfDatabase = instance;
65     }
66     return instance;
67   }
68
69   private DatabaseContentToMedia()
70     throws StorageObjectException {
71
72     super();
73     this.hasTimestamp = false;
74     this.theTable="content_x_media";
75     try { this.theEntityClass = Class.forName("mir.entity.GenericEntity"); }
76     catch (Exception e) { throw new StorageObjectException(e.toString()); }
77   }
78
79   /**
80    * get all the media-files belonging to a content entity
81    *
82    */
83   public EntityList getMedia(EntityContent content)
84     throws StorageObjectException {
85     EntityList returnList=null;
86     if (content != null) {
87       // get all to_topic from media_x_topic
88       String id = content.getId();
89       //this is not supported by mysql
90       String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
91
92       try {
93         // media should stay in uploaded order. this is especially important
94         // for photo stories which require a specific chronologic order.
95         // this is why we have the the second parameter "id"
96         returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,"id",-1);
97       } catch (Exception e) {
98         theLog.printDebugInfo("-- get media failed " + e.toString());
99         throw new StorageObjectException("-- get media failed " + e.toString());
100       }
101     }
102     return returnList;
103   }
104
105   public boolean hasMedia(EntityContent content)
106     throws StorageObjectException {
107     String wc = "content_id="+content.getId();
108     if( content != null) {
109       try {
110         if(selectByWhereClause(wc,-1).size() == 0)
111           return false;
112         else
113           return true;
114       } catch (Exception e) {
115         theLog.printError("-- hasMedia failed " + e.toString());
116         throw new StorageObjectException("-- hasMedia failed " + e.toString());
117       }
118     } else {
119       theLog.printError("-- hasMedia failed: content is NULL");
120       throw new StorageObjectException("-- hasMedia failed: content is NULL");
121     }
122   }
123
124
125
126
127   /**
128    * get all the audio belonging to a content entity
129    *
130    */
131   public EntityList getAudio(EntityContent content)
132     throws StorageObjectException {
133     EntityList returnList=null;
134     if (content != null) {
135       // get all to_topic from media_x_topic
136       String id = content.getId();
137       //this is not supported by mysql
138       String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
139
140       try {
141         // media should stay in uploaded order. this is especially important
142         // for photo stories which require a specific chronologic order.
143         // this is why we have the the second parameter "id"
144         returnList = DatabaseAudio.getInstance().selectByWhereClause(subselect,"id",-1);
145       } catch (Exception e) {
146         theLog.printDebugInfo("-- get audio failed " + e.toString());
147         throw new StorageObjectException("-- get audio failed " + e.toString());
148       }
149     }
150     return returnList;
151   }
152
153   /**
154    * get all the video belonging to a content entity
155    *
156    */
157   public EntityList getVideo(EntityContent content)
158     throws StorageObjectException {
159     EntityList returnList=null;
160     if (content != null) {
161       // get all to_topic from media_x_topic
162       String id = content.getId();
163       //this is not supported by mysql
164       String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
165
166       try {
167         // media should stay in uploaded order. this is especially important
168         // for photo stories which require a specific chronologic order.
169         // this is why we have the the second parameter "id"
170         returnList = DatabaseVideo.getInstance().selectByWhereClause(subselect,"id",-1);
171       } catch (Exception e) {
172         theLog.printDebugInfo("-- get video failed " + e.toString());
173         throw new StorageObjectException("-- get video failed " + e.toString());
174       }
175     }
176     return returnList;
177   }
178
179   /**
180    * get all the images belonging to a content entity
181    *
182    */
183   public EntityList getImages(EntityContent content)
184     throws StorageObjectException {
185     EntityList returnList=null;
186     if (content != null) {
187       // get all to_topic from media_x_topic
188       String id = content.getId();
189       //this is not supported by mysql
190       String subselect = "id in (select media_id from " + theTable + " 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 = DatabaseImages.getInstance().selectByWhereClause(subselect,"id",-1);
197       } catch (Exception e) {
198         theLog.printDebugInfo("-- get images failed " + e.toString());
199         throw new StorageObjectException("-- get images failed " + e.toString());
200       }
201     }
202     return returnList;
203   }
204
205
206   /**
207    * get all the uploaded/other Media belonging to a content entity
208    *
209    */
210   public EntityList getOther(EntityContent content)
211     throws StorageObjectException
212   {
213     /** @todo this should only fetch published media / rk */
214
215     EntityList returnList=null;
216     if (content != null) {
217       // get all to_topic from media_x_topic
218       String id = content.getId();
219       //this is not supported by mysql
220       String subselect = "id in (select media_id from " + theTable +
221                                 " where content_id=" + id+")";
222
223       try {
224         // media should stay in uploaded order. this is especially important
225         // for photo stories which require a specific chronologic order.
226         // this is why we have the the second parameter "id"
227         returnList = DatabaseOther.getInstance().selectByWhereClause(subselect,
228                                                                     "id");
229       } catch (Exception e) {
230         e.printStackTrace();
231         theLog.printDebugInfo("-- get Other failed " + e.toString());
232         throw new StorageObjectException("-- get Other failed "
233                                         + e.toString());
234       }
235     }
236     return returnList;
237   }
238
239   /**
240    * get all the uploaded/other Media belonging to a content entity
241    *
242    */
243   public EntityList getUploadedMedia(EntityContent content)
244     throws StorageObjectException
245   {
246     /** @todo this should only fetch published media / rk */
247
248     EntityList returnList=null;
249     if (content != null) {
250       // get all to_topic from media_x_topic
251       String id = content.getId();
252       //this is not supported by mysql
253       String subselect = "id in (select media_id from " + theTable +
254                                 " where content_id=" + id+")";
255
256       try {
257         returnList = DatabaseUploadedMedia.getInstance().selectByWhereClause(subselect,
258                                                                     "id");
259       } catch (Exception e) {
260         e.printStackTrace();
261         theLog.printDebugInfo("-- get uploadedMedia failed " + e.toString());
262         throw new StorageObjectException("-- get uploadedMedia failed "
263                                         + e.toString());
264       }
265     }
266     return returnList;
267   }
268
269
270   public void setMedia(String contentId, String[] mediaId)
271     throws StorageObjectException {
272     if (contentId == null){
273       return;
274     }
275     if (mediaId==null || mediaId[0]==null) {
276       return;
277     }
278     //first delete all row with content_id=contentId
279     String sql = "delete from "+ theTable +" where content_id=" + contentId;
280
281     Connection con=null;Statement stmt=null;
282     try {
283       con = getPooledCon();
284       // should be a preparedStatement because is faster
285       stmt = con.createStatement();
286       ResultSet rs = executeSql(stmt,sql);
287     } catch (Exception e) {
288       theLog.printDebugInfo("-- set media failed -- delete");
289       throw new StorageObjectException("-- set media failed -- delete"+e.toString());
290     } finally {
291       freeConnection(con,stmt);
292     }
293
294     //now insert
295     //first delete all row with content_id=contentId
296     for (int i=0;i<mediaId.length;i++) {
297       sql = "insert into "+ theTable +" (content_id,media_id) values ("
298             + contentId + "," + mediaId[i] + ")";
299       try {
300         con = getPooledCon();
301         // should be a preparedStatement because is faster
302         stmt = con.createStatement();
303         int rs = executeUpdate(stmt,sql);
304       } catch (Exception e) {
305         theLog.printDebugInfo("-- set topics failed -- insert");
306         throw new StorageObjectException("-- set topics failed -- insert "+e.toString());
307       } finally {
308         freeConnection(con,stmt);
309       }
310     }
311   }
312
313   public void addMedia(String contentId, String mediaId)
314     throws StorageObjectException {
315     if (contentId == null && mediaId == null) {
316       return;
317     }
318
319     Connection con=null;Statement stmt=null;
320     //now insert
321
322     String sql = "insert into "+ theTable +" (content_id,media_id) values ("
323           + contentId + "," + mediaId + ")";
324     try {
325       con = getPooledCon();
326       // should be a preparedStatement because is faster
327       stmt = con.createStatement();
328       int rs = executeUpdate(stmt,sql);
329     } catch (Exception e) {
330       theLog.printDebugInfo("-- add media failed -- insert");
331       throw new StorageObjectException("-- add media failed -- insert "
332         +e.toString());
333     } finally {
334       freeConnection(con,stmt);
335     }
336   }
337
338   public void setMedia(String contentId, String mediaId)
339     throws StorageObjectException {
340     if (contentId == null && mediaId == null) {
341       return;
342     }
343     //first delete all row with content_id=contentId
344     String sql = "delete from "+ theTable +" where content_id=" + contentId;
345
346     Connection con=null;Statement stmt=null;
347     try {
348       con = getPooledCon();
349       // should be a preparedStatement because is faster
350       stmt = con.createStatement();
351       int rs = executeUpdate(stmt,sql);
352     } catch (Exception e) {
353       theLog.printDebugInfo("-- set media failed -- delete");
354       throw new StorageObjectException("-- set media failed -- delete "
355         +e.toString());
356     } finally {
357       freeConnection(con,stmt);
358     }
359
360     //now insert
361     //first delete all row with content_id=contentId
362
363     sql = "insert into "+ theTable +" (content_id,media_id) values ("
364           + contentId + "," + mediaId + ")";
365     try {
366       con = getPooledCon();
367       // should be a preparedStatement because is faster
368       stmt = con.createStatement();
369       int rs = executeUpdate(stmt,sql);
370     } catch (Exception e) {
371       theLog.printDebugInfo("-- set media failed -- insert");
372       throw new StorageObjectException("-- set media failed -- insert "
373         +e.toString());
374     } finally {
375       freeConnection(con,stmt);
376     }
377   }
378
379   public void deleteByContentId(String contentId)
380     throws StorageObjectException {
381     if (contentId == null) {
382       //theLog.printDebugInfo("-- delete topics failed -- no content id");
383       return;
384     }
385     //delete all row with content_id=contentId
386     String sql = "delete from "+ theTable +" where 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     } catch (Exception e) {
395       theLog.printDebugInfo("-- delete by contentId failed  ");
396       throw new StorageObjectException("-- delete by content id failed -- delete "
397         +e.toString());
398     } finally {
399       freeConnection(con,stmt);
400     }
401   }
402
403   public void deleteByMediaId(String mediaId)
404     throws StorageObjectException {
405     if (mediaId == null) {
406       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
407       return;
408     }
409     //delete all row with content_id=contentId
410     String sql = "delete from "+ theTable +" where media_id=" + mediaId;
411
412     Connection con=null;Statement stmt=null;
413     try {
414       con = getPooledCon();
415       // should be a preparedStatement because is faster
416       stmt = con.createStatement();
417       int rs = executeUpdate(stmt,sql);
418       theLog.printDebugInfo("-- delete media success ");
419     } catch (Exception e) {
420       theLog.printDebugInfo("-- delete media failed ");
421       throw new StorageObjectException("-- delete by media id failed -- "
422         +e.toString());
423     } finally {
424       freeConnection(con,stmt);
425     }
426   }
427
428   public void delete(String contentId, String mediaId)
429     throws StorageObjectException {
430     if (mediaId == null || contentId==null) {
431       theLog.printDebugInfo("-- delete media failed -- missing parameter");
432       return;
433     }
434     //delete all row with content_id=contentId and media_id=mediaId
435     String sql = "delete from "+ theTable +" where media_id=" + mediaId + " and content_id= "+contentId;
436
437     Connection con=null;Statement stmt=null;
438     try {
439       con = getPooledCon();
440       // should be a preparedStatement because is faster
441       stmt = con.createStatement();
442       int rs = executeUpdate(stmt,sql);
443       theLog.printDebugInfo("-- delete content_x_media success ");
444     } catch (Exception e) {
445       theLog.printDebugInfo("-- delete content_x_media failed ");
446       throw new StorageObjectException("-- delete content_x_media failed -- "
447         +e.toString());
448     } finally {
449       freeConnection(con,stmt);
450     }
451   }
452
453
454   public EntityList getContent(EntityUploadedMedia media)
455     throws StorageObjectException {
456     EntityList returnList=null;
457     if (media != null) {
458       String id = media.getId();
459       String select = "select content_id from " + theTable + " where media_id=" + id;
460
461       // execute select statement
462       Connection con=null;Statement stmt=null;
463       try {
464         con = getPooledCon();
465         // should be a preparedStatement because is faster
466         stmt = con.createStatement();
467         ResultSet rs = executeSql(stmt,select);
468         if (rs!=null) {
469           String mediaSelect= "id IN (";
470           boolean first=true;
471           while (rs.next()) {
472             if (first==false) mediaSelect+=",";
473             mediaSelect += rs.getString(1);
474             first=false;
475           }
476           mediaSelect+=")";
477           if (first==false)
478             returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,-1);
479         }
480       }
481       catch (Exception e) {
482         theLog.printDebugInfo("-- get content failed");
483         throw new StorageObjectException("-- get content failed -- "
484         +e.toString());
485       }
486       finally { freeConnection(con,stmt);}
487     }
488     return returnList;
489   }
490
491 /**
492  * Returns a EntityList with all content-objects having a relation to a media
493  */
494
495 public EntityList getContent()
496     throws StorageObjectException {
497     EntityList returnList=null;
498
499     String select = "select distinct content_id from " + theTable;
500     // execute select statement
501     Connection con=null;Statement stmt=null;
502     try {
503       con = getPooledCon();
504       // should be a preparedStatement because is faster
505       stmt = con.createStatement();
506       ResultSet rs = executeSql(stmt,select);
507       if (rs!=null) {
508         String mediaSelect= "id IN (";
509         boolean first=true;
510         while (rs.next()) {
511           if (first==false) mediaSelect+=",";
512           mediaSelect += rs.getString(1);
513           first=false;
514         }
515         mediaSelect+=")";
516         if (first==false)
517           returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,"webdb_lastchange desc");
518       }
519     }
520     catch (Exception e) {
521         theLog.printDebugInfo("-- get content failed");
522         throw new StorageObjectException("-- get content failed -- "
523         +e.toString());
524     }
525     finally { freeConnection(con,stmt);}
526
527     return returnList;
528   }
529
530 }