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