organizing imports
[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.14 2003/03/05 19:23:17 idfx 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   // the following *has* to be sychronized cause this static method\r
60   // could get preemted and we could end up with 2 instances of DatabaseFoo.\r
61   // see the "Singletons with needles and thread" article at JavaWorld -mh\r
62   public synchronized static DatabaseContentToMedia getInstance() throws\r
63       StorageObjectFailure {\r
64     if (instance == null) {\r
65       instance = new DatabaseContentToMedia();\r
66       instance.myselfDatabase = instance;\r
67     }\r
68     return instance;\r
69   }\r
70 \r
71   private DatabaseContentToMedia() throws StorageObjectFailure {\r
72     super();\r
73     logger = new LoggerWrapper("Database.ContentToMedia");\r
74 \r
75     hasTimestamp = false;\r
76     theTable = "content_x_media";\r
77 \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         e.printStackTrace();\r
243         logger.error("DatabaseContentToMedia.getOther: " + e.toString());\r
244         throw new StorageObjectFailure("DatabaseContentToMedia.getOther: " +\r
245                                        e.toString(), e);\r
246       }\r
247     }\r
248     return returnList;\r
249   }\r
250 \r
251   /**\r
252    * get all the uploaded/other Media belonging to a content entity\r
253    *\r
254    */\r
255   public EntityList getUploadedMedia(EntityContent content) throws\r
256       StorageObjectFailure {\r
257     /** @todo this should only fetch published media / rk */\r
258 \r
259     EntityList returnList = null;\r
260     if (content != null) {\r
261       // get all to_topic from media_x_topic\r
262       String id = content.getId();\r
263       //this is not supported by mysql\r
264       String subselect = "id in (select media_id from " + theTable +\r
265           " where content_id=" + id + ")";\r
266 \r
267       try {\r
268         returnList = DatabaseUploadedMedia.getInstance().selectByWhereClause(\r
269             subselect,\r
270             "id");\r
271       }\r
272       catch (Exception e) {\r
273         e.printStackTrace();\r
274         logger.error("DatabaseContentToMedia.getUploadedMedia: " + e.toString());\r
275         throw new StorageObjectFailure(\r
276             "DatabaseContentToMedia.getUploadedMedia: " + e.toString(), e);\r
277       }\r
278     }\r
279     return returnList;\r
280   }\r
281 \r
282   public void setMedia(String contentId, String[] mediaId) throws\r
283       StorageObjectFailure {\r
284     if (contentId == null) {\r
285       return;\r
286     }\r
287     if (mediaId == null || mediaId[0] == null) {\r
288       return;\r
289     }\r
290     //first delete all row with content_id=contentId\r
291     String sql = "delete from " + theTable + " where content_id=" + contentId;\r
292 \r
293     Connection con = null;\r
294     Statement stmt = null;\r
295     try {\r
296       con = getPooledCon();\r
297       // should be a preparedStatement because is faster\r
298       stmt = con.createStatement();\r
299       ResultSet rs = executeSql(stmt, sql);\r
300     }\r
301     catch (Exception e) {\r
302       logger.error("-- set media failed -- delete");\r
303       throw new StorageObjectFailure("-- set media failed -- delete", e);\r
304     }\r
305     finally {\r
306       freeConnection(con, stmt);\r
307     }\r
308 \r
309     //now insert\r
310     //first delete all row with content_id=contentId\r
311     for (int i = 0; i < mediaId.length; i++) {\r
312       sql = "insert into " + theTable + " (content_id,media_id) values ("\r
313           + contentId + "," + mediaId[i] + ")";\r
314       try {\r
315         con = getPooledCon();\r
316         // should be a preparedStatement because is faster\r
317         stmt = con.createStatement();\r
318         int rs = executeUpdate(stmt, sql);\r
319       }\r
320       catch (Exception e) {\r
321         logger.error("-- set topics failed -- insert");\r
322         throw new StorageObjectFailure("-- set topics failed -- insert ", e);\r
323       }\r
324       finally {\r
325         freeConnection(con, stmt);\r
326       }\r
327     }\r
328   }\r
329 \r
330   public void addMedia(String contentId, String mediaId) throws\r
331       StorageObjectFailure {\r
332     if (contentId == null && mediaId == null) {\r
333       return;\r
334     }\r
335 \r
336     Connection con = null;\r
337     Statement stmt = null;\r
338     //now insert\r
339 \r
340     String sql = "insert into " + theTable + " (content_id,media_id) values ("\r
341         + contentId + "," + mediaId + ")";\r
342     try {\r
343       con = getPooledCon();\r
344       // should be a preparedStatement because is faster\r
345       stmt = con.createStatement();\r
346       int rs = executeUpdate(stmt, sql);\r
347     }\r
348     catch (Exception e) {\r
349       logger.error("-- add media failed -- insert");\r
350       throw new StorageObjectFailure("-- add media failed -- insert ", e);\r
351     }\r
352     finally {\r
353       freeConnection(con, stmt);\r
354     }\r
355   }\r
356 \r
357   public void setMedia(String contentId, String mediaId) throws\r
358       StorageObjectFailure {\r
359     if (contentId == null && mediaId == null) {\r
360       return;\r
361     }\r
362     //first delete all row with content_id=contentId\r
363     String sql = "delete from " + theTable + " where content_id=" + contentId;\r
364 \r
365     Connection con = null;\r
366     Statement stmt = null;\r
367     try {\r
368       con = getPooledCon();\r
369       // should be a preparedStatement because is faster\r
370       stmt = con.createStatement();\r
371       int rs = executeUpdate(stmt, sql);\r
372     }\r
373     catch (Exception e) {\r
374       logger.error("-- set media failed -- delete");\r
375       throw new StorageObjectFailure("-- set media failed -- delete ", e);\r
376     }\r
377     finally {\r
378       freeConnection(con, stmt);\r
379     }\r
380 \r
381     //now insert\r
382     //first delete all row with content_id=contentId\r
383 \r
384     sql = "insert into " + theTable + " (content_id,media_id) values ("\r
385         + contentId + "," + mediaId + ")";\r
386     try {\r
387       con = getPooledCon();\r
388       // should be a preparedStatement because is faster\r
389       stmt = con.createStatement();\r
390       int rs = executeUpdate(stmt, sql);\r
391     }\r
392     catch (Exception e) {\r
393       logger.error("-- set media failed -- insert");\r
394       throw new StorageObjectFailure("-- set media failed -- insert ", e);\r
395     }\r
396     finally {\r
397       freeConnection(con, stmt);\r
398     }\r
399   }\r
400 \r
401   public void deleteByContentId(String contentId) throws StorageObjectFailure {\r
402     if (contentId == null) {\r
403       //theLog.printDebugInfo("-- delete topics failed -- no content id");\r
404       return;\r
405     }\r
406     //delete all row with content_id=contentId\r
407     String sql = "delete from " + theTable + " where content_id=" + contentId;\r
408 \r
409     Connection con = null;\r
410     Statement stmt = null;\r
411     try {\r
412       con = getPooledCon();\r
413       // should be a preparedStatement because is faster\r
414       stmt = con.createStatement();\r
415       int rs = executeUpdate(stmt, sql);\r
416     }\r
417     catch (Exception e) {\r
418       logger.error("-- delete by contentId failed  ");\r
419       throw new StorageObjectFailure(\r
420           "-- delete by content id failed -- delete ", e);\r
421     }\r
422     finally {\r
423       freeConnection(con, stmt);\r
424     }\r
425   }\r
426 \r
427   public void deleteByMediaId(String mediaId) throws StorageObjectFailure {\r
428     if (mediaId == null) {\r
429       //theLog.printDebugInfo("-- delete topics failed -- no topic id");\r
430       return;\r
431     }\r
432     //delete all row with content_id=contentId\r
433     String sql = "delete from " + theTable + " where media_id=" + mediaId;\r
434 \r
435     Connection con = null;\r
436     Statement stmt = null;\r
437     try {\r
438       con = getPooledCon();\r
439       // should be a preparedStatement because is faster\r
440       stmt = con.createStatement();\r
441       int rs = executeUpdate(stmt, sql);\r
442       logger.debug("-- delete media success ");\r
443     }\r
444     catch (Exception e) {\r
445       logger.error("-- delete media failed ");\r
446       throw new StorageObjectFailure("-- delete by media id failed -- ", e);\r
447     }\r
448     finally {\r
449       freeConnection(con, stmt);\r
450     }\r
451   }\r
452 \r
453   public void delete(String contentId, String mediaId) throws\r
454       StorageObjectFailure {\r
455     if (mediaId == null || contentId == null) {\r
456       logger.debug("-- delete media failed -- missing parameter");\r
457       return;\r
458     }\r
459     //delete all row with content_id=contentId and media_id=mediaId\r
460     String sql = "delete from " + theTable + " where media_id=" + mediaId +\r
461         " and content_id= " + contentId;\r
462 \r
463     Connection con = null;\r
464     Statement stmt = null;\r
465     try {\r
466       con = getPooledCon();\r
467       // should be a preparedStatement because is faster\r
468       stmt = con.createStatement();\r
469       int rs = executeUpdate(stmt, sql);\r
470       logger.debug("-- delete content_x_media success ");\r
471     }\r
472     catch (Exception e) {\r
473       logger.error("-- delete content_x_media failed ");\r
474       throw new StorageObjectFailure("-- delete content_x_media failed -- ", e);\r
475     }\r
476     finally {\r
477       freeConnection(con, stmt);\r
478     }\r
479   }\r
480 \r
481   public EntityList getContent(EntityUploadedMedia media) throws\r
482       StorageObjectFailure {\r
483     EntityList returnList = null;\r
484     if (media != null) {\r
485       String id = media.getId();\r
486       String select = "select content_id from " + theTable + " where media_id=" +\r
487           id;\r
488 \r
489       // execute select statement\r
490       Connection con = null;\r
491       Statement stmt = null;\r
492       try {\r
493         con = getPooledCon();\r
494         // should be a preparedStatement because is faster\r
495         stmt = con.createStatement();\r
496         ResultSet rs = executeSql(stmt, select);\r
497         if (rs != null) {\r
498           String mediaSelect = "id IN (";\r
499           boolean first = true;\r
500           while (rs.next()) {\r
501             if (first == false)\r
502               mediaSelect += ",";\r
503             mediaSelect += rs.getString(1);\r
504             first = false;\r
505           }\r
506           mediaSelect += ")";\r
507           if (first == false)\r
508             returnList = DatabaseContent.getInstance().selectByWhereClause(\r
509                 mediaSelect, -1);\r
510         }\r
511       }\r
512       catch (Exception e) {\r
513         logger.error("-- get content failed");\r
514         throw new StorageObjectFailure("-- get content failed -- ", e);\r
515       }\r
516       finally {\r
517         freeConnection(con, stmt);\r
518       }\r
519     }\r
520     return returnList;\r
521   }\r
522 \r
523   /**\r
524    * Returns a EntityList with all content-objects having a relation to a media\r
525    */\r
526 \r
527   public EntityList getContent() throws StorageObjectFailure {\r
528     EntityList returnList = null;\r
529 \r
530     String select = "select distinct content_id from " + theTable;\r
531     // execute select statement\r
532     Connection con = null;\r
533     Statement stmt = null;\r
534     try {\r
535       con = getPooledCon();\r
536       // should be a preparedStatement because is faster\r
537       stmt = con.createStatement();\r
538       ResultSet rs = executeSql(stmt, select);\r
539       if (rs != null) {\r
540         String mediaSelect = "id IN (";\r
541         boolean first = true;\r
542         while (rs.next()) {\r
543           if (first == false)\r
544             mediaSelect += ",";\r
545           mediaSelect += rs.getString(1);\r
546           first = false;\r
547         }\r
548         mediaSelect += ")";\r
549         if (first == false)\r
550           returnList = DatabaseContent.getInstance().selectByWhereClause(\r
551               mediaSelect, "webdb_lastchange desc");\r
552       }\r
553     }\r
554     catch (Exception e) {\r
555       logger.error("-- get content failed");\r
556       throw new StorageObjectFailure("-- get content failed -- ", e);\r
557     }\r
558     finally {\r
559       freeConnection(con, stmt);\r
560     }\r
561 \r
562     return returnList;\r
563   }\r
564 \r
565 }\r