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