2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
31 package mircoders.storage;
33 import java.sql.Connection;
34 import java.sql.ResultSet;
35 import java.sql.Statement;
36 import java.util.Vector;
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;
48 * <b>implements abstract DB connection to the comment_x_media SQL table
50 * @author RK, mir-coders group
51 * @version $Id: DatabaseCommentToMedia.java,v 1.3.2.7 2004/11/21 22:07:14 zapata Exp $
55 public class DatabaseCommentToMedia extends Database implements StorageObject{
57 private static DatabaseCommentToMedia instance;
59 public static synchronized DatabaseCommentToMedia getInstance() {
60 if (instance == null) {
61 instance = new DatabaseCommentToMedia();
66 private DatabaseCommentToMedia() {
68 logger = new LoggerWrapper("Database.CommentToMedia");
70 mainTable = "comment_x_media";
71 theEntityClass = mir.entity.GenericEntity.class;
74 public boolean hasMedia(EntityComment comment) throws StorageObjectFailure,
76 if (comment != null) {
78 return (getSize("comment_id=" + comment.getId()) == 0) ? false:true;
81 logger.error("DatabaseCommentToMedia.hasMedia: " + e.toString());
82 throw new StorageObjectFailure("DatabaseCommentToMedia.hasMedia: " +
87 logger.error("DatabaseCommentToMedia.hasMedia: comment == null");
88 throw new StorageObjectExc(
89 "DatabaseCommentToMedia.hasMedia: comment == null");
93 private EntityList getMultiMediaForComment(StorageObject store, EntityComment comment)
94 throws StorageObjectFailure {
96 EntityList returnList = null;
97 if (comment != null) {
98 String id = comment.getId();
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);
108 } catch (Exception e) {
109 logger.error("DatabaseCommenttToMedia.getMultiMediaForComment: " + e.toString());
110 throw new StorageObjectFailure("DatabaseContentToMedia.etMultiMediaForComment: " +
118 * get all the audio belonging to a comment entity
120 public EntityList getAudio(EntityComment comment) throws StorageObjectFailure {
121 return getMultiMediaForComment(DatabaseAudio.getInstance(), comment);
125 * get all the video belonging to a comment entity
128 public EntityList getVideo(EntityComment comment) throws StorageObjectFailure {
129 return getMultiMediaForComment(DatabaseVideo.getInstance(), comment);
133 * get all the images belonging to a comment entity
135 public EntityList getImages(EntityComment comment) throws StorageObjectFailure {
136 return getMultiMediaForComment(DatabaseImages.getInstance(), comment);
140 * get all the uploaded/other Media belonging to a comment entity
142 public EntityList getOther(EntityComment comment) throws StorageObjectFailure {
143 return getMultiMediaForComment(DatabaseOther.getInstance(), comment);
147 * get all the uploaded/other Media belonging to a comment entity
149 public EntityList getUploadedMedia(EntityComment comment) throws StorageObjectFailure {
150 return getMultiMediaForComment(DatabaseUploadedMedia.getInstance(), comment);
153 public void setMedia(String commentId, String[] mediaId) throws
154 StorageObjectFailure {
155 if (commentId == null) {
158 if (mediaId == null || mediaId[0] == null) {
161 //first delete all row with comment_id=commentId
162 String sql = "delete from " + mainTable + " where comment_id=" + commentId;
164 Connection con = null;
165 Statement stmt = null;
167 con = obtainConnection();
168 // should be a preparedStatement because is faster
169 stmt = con.createStatement();
170 ResultSet rs = executeSql(stmt, sql);
172 catch (Exception e) {
173 logger.error("-- set media failed -- delete");
174 throw new StorageObjectFailure("-- set media failed -- delete", e);
177 freeConnection(con, stmt);
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] + ")";
186 con = obtainConnection();
187 // should be a preparedStatement because is faster
188 stmt = con.createStatement();
189 int rs = executeUpdate(stmt, sql);
191 catch (Exception e) {
192 logger.error("-- set topics failed -- insert");
193 throw new StorageObjectFailure("-- set topics failed -- insert ", e);
196 freeConnection(con, stmt);
201 public void addMedia(String commentId, String mediaId) throws
202 StorageObjectFailure {
203 if (commentId == null && mediaId == null) {
207 Connection con = null;
208 Statement stmt = null;
211 String sql = "insert into " + mainTable + " (comment_id,media_id) values ("
212 + commentId + "," + mediaId + ")";
214 con = obtainConnection();
215 // should be a preparedStatement because is faster
216 stmt = con.createStatement();
217 int rs = executeUpdate(stmt, sql);
219 catch (Exception e) {
220 logger.error("-- add media failed -- insert");
221 throw new StorageObjectFailure("-- add media failed -- insert ", e);
224 freeConnection(con, stmt);
228 public void setMedia(String commentId, String mediaId) throws
229 StorageObjectFailure {
230 if (commentId == null && mediaId == null) {
233 //first delete all row with comment_id=commentId
234 String sql = "delete from " + mainTable + " where comment_id=" + commentId;
236 Connection con = null;
237 Statement stmt = null;
239 con = obtainConnection();
240 // should be a preparedStatement because is faster
241 stmt = con.createStatement();
242 int rs = executeUpdate(stmt, sql);
244 catch (Exception e) {
245 logger.error("-- set media failed -- delete");
246 throw new StorageObjectFailure("-- set media failed -- delete ", e);
249 freeConnection(con, stmt);
253 //first delete all row with comment_id=commentId
255 sql = "insert into " + mainTable + " (comment_id,media_id) values ("
256 + commentId + "," + mediaId + ")";
258 con = obtainConnection();
259 // should be a preparedStatement because is faster
260 stmt = con.createStatement();
261 int rs = executeUpdate(stmt, sql);
263 catch (Exception e) {
264 logger.error("-- set media failed -- insert");
265 throw new StorageObjectFailure("-- set media failed -- insert ", e);
268 freeConnection(con, stmt);
272 public void deleteByCommentId(String commentId) throws StorageObjectFailure {
273 if (commentId == null) {
274 //theLog.printDebugInfo("-- delete topics failed -- no comment id");
277 //delete all row with comment_id=commentId
278 String sql = "delete from " + mainTable + " where comment_id=" + commentId;
280 Connection con = null;
281 Statement stmt = null;
283 con = obtainConnection();
284 // should be a preparedStatement because is faster
285 stmt = con.createStatement();
286 int rs = executeUpdate(stmt, sql);
288 catch (Exception e) {
289 logger.error("-- delete by commentId failed ");
290 throw new StorageObjectFailure(
291 "-- delete by comment id failed -- delete ", e);
294 freeConnection(con, stmt);
298 public void deleteByMediaId(String mediaId) throws StorageObjectFailure {
299 if (mediaId == null) {
300 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
303 //delete all row with comment_id=commentId
304 String sql = "delete from " + mainTable + " where media_id=" + mediaId;
306 Connection con = null;
307 Statement stmt = null;
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 ");
315 catch (Exception e) {
316 logger.error("-- delete media failed ");
317 throw new StorageObjectFailure("-- delete by media id failed -- ", e);
320 freeConnection(con, stmt);
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");
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;
334 Connection con = null;
335 Statement stmt = null;
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 ");
343 catch (Exception e) {
344 logger.error("-- delete comment_x_media failed ");
345 throw new StorageObjectFailure("-- delete comment_x_media failed -- ", e);
348 freeConnection(con, stmt);
352 public EntityList getComment(EntityUploadedMedia media) throws
353 StorageObjectFailure {
355 EntityList returnList = null;
358 String id = media.getId();
359 Vector extraTables = new Vector();
360 extraTables.add(mainTable + " cxm");
362 String mediaSelect = "cxm.comment_id=c.id and cxm.media_id="+id;
364 returnList = DatabaseComment.getInstance().selectByWhereClause("c",
365 extraTables, mediaSelect, "c.id" );
368 catch (Exception e) {
369 logger.error("-- get comment failed");
370 throw new StorageObjectFailure("-- get comment failed -- ", e);
377 * Returns a EntityList with all comment-objects having
378 * a relation to a media
381 public EntityList getComment() throws StorageObjectFailure {
382 EntityList returnList = null;
384 Vector extraTables = new Vector();
385 extraTables.add(mainTable + " cxm");
387 String mediaSelect = "cxm.comment_id=c.id";
389 returnList = DatabaseComment.getInstance().selectByWhereClause("c",
390 extraTables, mediaSelect, "c.webdb_lastchange desc" );
393 catch (Exception e) {
394 logger.error("-- get comment failed");
395 throw new StorageObjectFailure("-- get comment failed -- ", e);