1.1 restoration
[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;
34 import java.sql.ResultSet;
35 import java.sql.Statement;
36 import java.util.Vector;
37
38 import mir.entity.EntityList;
39 import mir.log.LoggerWrapper;
40 import mir.storage.Database;
41 import mir.storage.StorageObject;
42 import mir.storage.StorageObjectExc;
43 import mir.storage.StorageObjectFailure;
44 import mircoders.entity.EntityComment;
45 import mircoders.entity.EntityUploadedMedia;
46
47 /**
48  * <b>implements abstract DB connection to the comment_x_media SQL table
49  *
50  * @author RK, mir-coders group
51  * @version $Id: DatabaseCommentToMedia.java,v 1.3.2.7 2004/11/21 22:07:14 zapata Exp $
52  *
53  */
54
55 public class DatabaseCommentToMedia extends Database implements StorageObject{
56
57   private static DatabaseCommentToMedia instance;
58
59   public static synchronized DatabaseCommentToMedia getInstance() {
60     if (instance == null) {
61       instance = new DatabaseCommentToMedia();
62     }
63     return instance;
64   }
65
66   private DatabaseCommentToMedia() {
67     super();
68     logger = new LoggerWrapper("Database.CommentToMedia");
69     hasTimestamp = false;
70     mainTable = "comment_x_media";
71     theEntityClass = mir.entity.GenericEntity.class;
72   }
73
74   public boolean hasMedia(EntityComment comment) throws StorageObjectFailure,
75       StorageObjectExc {
76     if (comment != null) {
77       try {
78         return (getSize("comment_id=" + comment.getId()) == 0) ? false:true;
79       }
80       catch (Exception e) {
81         logger.error("DatabaseCommentToMedia.hasMedia: " + e.toString());
82         throw new StorageObjectFailure("DatabaseCommentToMedia.hasMedia: " +
83                                        e.toString(), e);
84       }
85     }
86     else {
87       logger.error("DatabaseCommentToMedia.hasMedia: comment == null");
88       throw new StorageObjectExc(
89           "DatabaseCommentToMedia.hasMedia: comment == null");
90     }
91   }
92
93   private EntityList getMultiMediaForComment(StorageObject store, EntityComment comment)
94       throws StorageObjectFailure {
95
96       EntityList returnList = null;
97       if (comment != null) {
98         String id = comment.getId();
99         try {
100           Vector extraTable = new Vector();
101           extraTable.add(mainTable+" cxm");
102           // media should stay in uploaded order. this is especially important
103           // for photo stories which require a specific chronologic order.
104           // this is why we have the the second parameter "id"
105           store.selectByWhereClause("m", extraTable,
106             "m.id=cxm.media_id and cxm.comment_id="+id, "id", -1, -1);
107
108         } catch (Exception e) {
109           logger.error("DatabaseCommenttToMedia.getMultiMediaForComment: " + e.toString());
110           throw new StorageObjectFailure("DatabaseContentToMedia.etMultiMediaForComment: " +
111                                        e.toString(), e);
112         }
113       }
114       return returnList;
115     }
116
117   /**
118    * get all the audio belonging to a comment entity
119    */
120   public EntityList getAudio(EntityComment comment) throws StorageObjectFailure {
121     return getMultiMediaForComment(DatabaseAudio.getInstance(), comment);
122   }
123
124   /**
125    * get all the video belonging to a comment entity
126    *
127    */
128   public EntityList getVideo(EntityComment comment) throws StorageObjectFailure {
129     return getMultiMediaForComment(DatabaseVideo.getInstance(), comment);
130   }
131
132   /**
133    * get all the images belonging to a comment entity
134    */
135   public EntityList getImages(EntityComment comment) throws StorageObjectFailure {
136     return getMultiMediaForComment(DatabaseImages.getInstance(), comment);
137   }
138
139   /**
140    * get all the uploaded/other Media belonging to a comment entity
141    */
142   public EntityList getOther(EntityComment comment) throws StorageObjectFailure {
143     return getMultiMediaForComment(DatabaseOther.getInstance(), comment);
144   }
145
146   /**
147    * get all the uploaded/other Media belonging to a comment entity
148    */
149   public EntityList getUploadedMedia(EntityComment comment) throws StorageObjectFailure {
150     return getMultiMediaForComment(DatabaseUploadedMedia.getInstance(), comment);
151   }
152
153   public void setMedia(String commentId, String[] mediaId) throws
154       StorageObjectFailure {
155     if (commentId == null) {
156       return;
157     }
158     if (mediaId == null || mediaId[0] == null) {
159       return;
160     }
161     //first delete all row with comment_id=commentId
162     String sql = "delete from " + mainTable + " where comment_id=" + commentId;
163
164     Connection con = null;
165     Statement stmt = null;
166     try {
167       con = obtainConnection();
168       // should be a preparedStatement because is faster
169       stmt = con.createStatement();
170       ResultSet rs = executeSql(stmt, sql);
171     }
172     catch (Exception e) {
173       logger.error("-- set media failed -- delete");
174       throw new StorageObjectFailure("-- set media failed -- delete", e);
175     }
176     finally {
177       freeConnection(con, stmt);
178     }
179
180     //now insert
181     //first delete all row with comment_id=commentId
182     for (int i = 0; i < mediaId.length; i++) {
183       sql = "insert into " + mainTable + " (comment_id,media_id) values ("
184           + commentId + "," + mediaId[i] + ")";
185       try {
186         con = obtainConnection();
187         // should be a preparedStatement because is faster
188         stmt = con.createStatement();
189         int rs = executeUpdate(stmt, sql);
190       }
191       catch (Exception e) {
192         logger.error("-- set topics failed -- insert");
193         throw new StorageObjectFailure("-- set topics failed -- insert ", e);
194       }
195       finally {
196         freeConnection(con, stmt);
197       }
198     }
199   }
200
201   public void addMedia(String commentId, String mediaId) throws
202       StorageObjectFailure {
203     if (commentId == null && mediaId == null) {
204       return;
205     }
206
207     Connection con = null;
208     Statement stmt = null;
209     //now insert
210
211     String sql = "insert into " + mainTable + " (comment_id,media_id) values ("
212         + commentId + "," + mediaId + ")";
213     try {
214       con = obtainConnection();
215       // should be a preparedStatement because is faster
216       stmt = con.createStatement();
217       int rs = executeUpdate(stmt, sql);
218     }
219     catch (Exception e) {
220       logger.error("-- add media failed -- insert");
221       throw new StorageObjectFailure("-- add media failed -- insert ", e);
222     }
223     finally {
224       freeConnection(con, stmt);
225     }
226   }
227
228   public void setMedia(String commentId, String mediaId) throws
229       StorageObjectFailure {
230     if (commentId == null && mediaId == null) {
231       return;
232     }
233     //first delete all row with comment_id=commentId
234     String sql = "delete from " + mainTable + " where comment_id=" + commentId;
235
236     Connection con = null;
237     Statement stmt = null;
238     try {
239       con = obtainConnection();
240       // should be a preparedStatement because is faster
241       stmt = con.createStatement();
242       int rs = executeUpdate(stmt, sql);
243     }
244     catch (Exception e) {
245       logger.error("-- set media failed -- delete");
246       throw new StorageObjectFailure("-- set media failed -- delete ", e);
247     }
248     finally {
249       freeConnection(con, stmt);
250     }
251
252     //now insert
253     //first delete all row with comment_id=commentId
254
255     sql = "insert into " + mainTable + " (comment_id,media_id) values ("
256         + commentId + "," + mediaId + ")";
257     try {
258       con = obtainConnection();
259       // should be a preparedStatement because is faster
260       stmt = con.createStatement();
261       int rs = executeUpdate(stmt, sql);
262     }
263     catch (Exception e) {
264       logger.error("-- set media failed -- insert");
265       throw new StorageObjectFailure("-- set media failed -- insert ", e);
266     }
267     finally {
268       freeConnection(con, stmt);
269     }
270   }
271
272   public void deleteByCommentId(String commentId) throws StorageObjectFailure {
273     if (commentId == null) {
274       //theLog.printDebugInfo("-- delete topics failed -- no comment id");
275       return;
276     }
277     //delete all row with comment_id=commentId
278     String sql = "delete from " + mainTable + " where comment_id=" + commentId;
279
280     Connection con = null;
281     Statement stmt = null;
282     try {
283       con = obtainConnection();
284       // should be a preparedStatement because is faster
285       stmt = con.createStatement();
286       int rs = executeUpdate(stmt, sql);
287     }
288     catch (Exception e) {
289       logger.error("-- delete by commentId failed  ");
290       throw new StorageObjectFailure(
291           "-- delete by comment id failed -- delete ", e);
292     }
293     finally {
294       freeConnection(con, stmt);
295     }
296   }
297
298   public void deleteByMediaId(String mediaId) throws StorageObjectFailure {
299     if (mediaId == null) {
300       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
301       return;
302     }
303     //delete all row with comment_id=commentId
304     String sql = "delete from " + mainTable + " where media_id=" + mediaId;
305
306     Connection con = null;
307     Statement stmt = null;
308     try {
309       con = obtainConnection();
310       // should be a preparedStatement because is faster
311       stmt = con.createStatement();
312       int rs = executeUpdate(stmt, sql);
313       logger.debug("-- delete media success ");
314     }
315     catch (Exception e) {
316       logger.error("-- delete media failed ");
317       throw new StorageObjectFailure("-- delete by media id failed -- ", e);
318     }
319     finally {
320       freeConnection(con, stmt);
321     }
322   }
323
324   public void delete(String commentId, String mediaId) throws
325       StorageObjectFailure {
326     if (mediaId == null || commentId == null) {
327       logger.debug("-- delete media failed -- missing parameter");
328       return;
329     }
330     //delete all row with comment_id=commentId and media_id=mediaId
331     String sql = "delete from " + mainTable + " where media_id=" + mediaId +
332         " and comment_id= " + commentId;
333
334     Connection con = null;
335     Statement stmt = null;
336     try {
337       con = obtainConnection();
338       // should be a preparedStatement because is faster
339       stmt = con.createStatement();
340       int rs = executeUpdate(stmt, sql);
341       logger.debug("-- delete comment_x_media success ");
342     }
343     catch (Exception e) {
344       logger.error("-- delete comment_x_media failed ");
345       throw new StorageObjectFailure("-- delete comment_x_media failed -- ", e);
346     }
347     finally {
348       freeConnection(con, stmt);
349     }
350   }
351
352   public EntityList getComment(EntityUploadedMedia media) throws
353       StorageObjectFailure {
354
355     EntityList returnList = null;
356     if (media != null) {
357
358       String id = media.getId();
359       Vector extraTables = new Vector();
360       extraTables.add(mainTable + " cxm");
361
362       String mediaSelect = "cxm.comment_id=c.id and cxm.media_id="+id;
363       try {
364         returnList = DatabaseComment.getInstance().selectByWhereClause("c",
365           extraTables, mediaSelect, "c.id" );
366
367       }
368       catch (Exception e) {
369         logger.error("-- get comment failed");
370         throw new StorageObjectFailure("-- get comment failed -- ", e);
371       }
372     }
373     return returnList;
374   }
375
376   /**
377    * Returns a EntityList with all comment-objects having
378    *  a relation to a media
379    */
380
381   public EntityList getComment() throws StorageObjectFailure {
382     EntityList returnList = null;
383
384     Vector extraTables = new Vector();
385     extraTables.add(mainTable + " cxm");
386
387     String mediaSelect = "cxm.comment_id=c.id";
388     try {
389       returnList = DatabaseComment.getInstance().selectByWhereClause("c",
390         extraTables, mediaSelect, "c.webdb_lastchange desc" );
391
392     }
393     catch (Exception e) {
394       logger.error("-- get comment failed");
395       throw new StorageObjectFailure("-- get comment failed -- ", e);
396     }
397     return returnList;
398
399   }
400
401 }