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.ArrayList;
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.8 2005/01/09 20:37:15 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() {
69 logger = new LoggerWrapper("Database.CommentToMedia");
70 mainTable = "comment_x_media";
71 entityClass = mir.entity.GenericEntity.class;
74 public void addMedia(String commentId, String mediaId) throws
75 StorageObjectFailure {
76 if (commentId == null && mediaId == null) {
80 Connection con = null;
81 Statement stmt = null;
84 String sql = "insert into " + mainTable + " (comment_id,media_id) values ("
85 + commentId + "," + mediaId + ")";
87 con = obtainConnection();
88 // should be a preparedStatement because is faster
89 stmt = con.createStatement();
90 int rs = executeUpdate(stmt, sql);
93 logger.error("-- add media failed -- insert");
94 throw new StorageObjectFailure("-- add media failed -- insert ", e);
97 freeConnection(con, stmt);
101 public void setMedia(String commentId, String mediaId) throws
102 StorageObjectFailure {
103 if (commentId == null && mediaId == null) {
106 //first delete all row with comment_id=commentId
107 String sql = "delete from " + mainTable + " where comment_id=" + commentId;
109 Connection con = null;
110 Statement stmt = null;
112 con = obtainConnection();
113 // should be a preparedStatement because is faster
114 stmt = con.createStatement();
115 int rs = executeUpdate(stmt, sql);
117 catch (Exception e) {
118 logger.error("-- set media failed -- delete");
119 throw new StorageObjectFailure("-- set media failed -- delete ", e);
122 freeConnection(con, stmt);
126 //first delete all row with comment_id=commentId
128 sql = "insert into " + mainTable + " (comment_id,media_id) values ("
129 + commentId + "," + mediaId + ")";
131 con = obtainConnection();
132 // should be a preparedStatement because is faster
133 stmt = con.createStatement();
134 int rs = executeUpdate(stmt, sql);
136 catch (Exception e) {
137 logger.error("-- set media failed -- insert");
138 throw new StorageObjectFailure("-- set media failed -- insert ", e);
141 freeConnection(con, stmt);
145 public void deleteByCommentId(String commentId) throws StorageObjectFailure {
146 if (commentId == null) {
147 //theLog.printDebugInfo("-- delete topics failed -- no comment id");
150 //delete all row with comment_id=commentId
151 String sql = "delete from " + mainTable + " where comment_id=" + commentId;
153 Connection con = null;
154 Statement stmt = null;
156 con = obtainConnection();
157 // should be a preparedStatement because is faster
158 stmt = con.createStatement();
159 int rs = executeUpdate(stmt, sql);
161 catch (Exception e) {
162 logger.error("-- delete by commentId failed ");
163 throw new StorageObjectFailure(
164 "-- delete by comment id failed -- delete ", e);
167 freeConnection(con, stmt);
171 public void deleteByMediaId(String mediaId) throws StorageObjectFailure {
172 if (mediaId == null) {
173 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
176 //delete all row with comment_id=commentId
177 String sql = "delete from " + mainTable + " where media_id=" + mediaId;
179 Connection con = null;
180 Statement stmt = null;
182 con = obtainConnection();
183 // should be a preparedStatement because is faster
184 stmt = con.createStatement();
185 int rs = executeUpdate(stmt, sql);
186 logger.debug("-- delete media success ");
188 catch (Exception e) {
189 logger.error("-- delete media failed ");
190 throw new StorageObjectFailure("-- delete by media id failed -- ", e);
193 freeConnection(con, stmt);
197 public void delete(String commentId, String mediaId) throws
198 StorageObjectFailure {
199 if (mediaId == null || commentId == null) {
200 logger.debug("-- delete media failed -- missing parameter");
203 //delete all row with comment_id=commentId and media_id=mediaId
204 String sql = "delete from " + mainTable + " where media_id=" + mediaId +
205 " and comment_id= " + commentId;
207 Connection con = null;
208 Statement stmt = null;
210 con = obtainConnection();
211 // should be a preparedStatement because is faster
212 stmt = con.createStatement();
213 int rs = executeUpdate(stmt, sql);
214 logger.debug("-- delete comment_x_media success ");
216 catch (Exception e) {
217 logger.error("-- delete comment_x_media failed ");
218 throw new StorageObjectFailure("-- delete comment_x_media failed -- ", e);
221 freeConnection(con, stmt);
225 public EntityList getComment(EntityUploadedMedia media) throws
226 StorageObjectFailure {
228 EntityList returnList = null;
231 String id = media.getId();
232 ArrayList extraTables = new ArrayList();
233 extraTables.add(mainTable + " cxm");
235 String mediaSelect = "cxm.comment_id=c.id and cxm.media_id="+id;
237 returnList = DatabaseComment.getInstance().selectByWhereClause("c",
238 extraTables, mediaSelect, "c.id" );
241 catch (Exception e) {
242 logger.error("-- get comment failed");
243 throw new StorageObjectFailure("-- get comment failed -- ", e);
250 * Returns a EntityList with all comment-objects having
251 * a relation to a media
254 public EntityList getComment() throws StorageObjectFailure {
255 EntityList returnList = null;
257 ArrayList extraTables = new ArrayList();
258 extraTables.add(mainTable + " cxm");
260 String mediaSelect = "cxm.comment_id=c.id";
262 returnList = DatabaseComment.getInstance().selectByWhereClause("c",
263 extraTables, mediaSelect, "c.webdb_lastchange desc" );
266 catch (Exception e) {
267 logger.error("-- get comment failed");
268 throw new StorageObjectFailure("-- get comment failed -- ", e);