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.Statement;
35 import java.util.ArrayList;
37 import mir.entity.EntityList;
38 import mir.log.LoggerWrapper;
39 import mir.storage.Database;
40 import mir.storage.StorageObjectFailure;
41 import mircoders.entity.EntityUploadedMedia;
44 * <b>implements abstract DB connection to the comment_x_media SQL table
46 * @author RK, mir-coders group
47 * @version $Id: DatabaseCommentToMedia.java,v 1.3.2.10 2005/02/10 16:22:22 rhindes Exp $
51 public class DatabaseCommentToMedia extends Database {
53 private static DatabaseCommentToMedia instance;
55 public static synchronized DatabaseCommentToMedia getInstance() {
56 if (instance == null) {
57 instance = new DatabaseCommentToMedia();
62 private DatabaseCommentToMedia() {
65 logger = new LoggerWrapper("Database.CommentToMedia");
66 mainTable = "comment_x_media";
67 entityClass = mir.entity.GenericEntity.class;
70 public void addMedia(String commentId, String mediaId) throws
71 StorageObjectFailure {
72 if (commentId == null && mediaId == null) {
76 Connection con = null;
77 Statement stmt = null;
80 String sql = "insert into " + mainTable + " (comment_id,media_id) values ("
81 + commentId + "," + mediaId + ")";
83 con = obtainConnection();
84 // should be a preparedStatement because is faster
85 stmt = con.createStatement();
86 int rs = executeUpdate(stmt, sql);
89 logger.error("-- add media failed -- insert");
90 throw new StorageObjectFailure("-- add media failed -- insert ", e);
93 freeConnection(con, stmt);
97 public void setMedia(String commentId, String mediaId) throws
98 StorageObjectFailure {
99 if (commentId == null && mediaId == null) {
102 //first delete all row with comment_id=commentId
103 String sql = "delete from " + mainTable + " where comment_id=" + commentId;
105 Connection con = null;
106 Statement stmt = null;
108 con = obtainConnection();
109 // should be a preparedStatement because is faster
110 stmt = con.createStatement();
111 int rs = executeUpdate(stmt, sql);
113 catch (Exception e) {
114 logger.error("-- set media failed -- delete");
115 throw new StorageObjectFailure("-- set media failed -- delete ", e);
118 freeConnection(con, stmt);
122 //first delete all row with comment_id=commentId
124 sql = "insert into " + mainTable + " (comment_id,media_id) values ("
125 + commentId + "," + mediaId + ")";
127 con = obtainConnection();
128 // should be a preparedStatement because is faster
129 stmt = con.createStatement();
130 int rs = executeUpdate(stmt, sql);
132 catch (Exception e) {
133 logger.error("-- set media failed -- insert");
134 throw new StorageObjectFailure("-- set media failed -- insert ", e);
137 freeConnection(con, stmt);
141 public void deleteByCommentId(String commentId) throws StorageObjectFailure {
142 if (commentId == null) {
143 //theLog.printDebugInfo("-- delete topics failed -- no comment id");
146 //delete all row with comment_id=commentId
147 String sql = "delete from " + mainTable + " where comment_id=" + commentId;
149 Connection con = null;
150 Statement stmt = null;
152 con = obtainConnection();
153 // should be a preparedStatement because is faster
154 stmt = con.createStatement();
155 int rs = executeUpdate(stmt, sql);
157 catch (Exception e) {
158 logger.error("-- delete by commentId failed ");
159 throw new StorageObjectFailure(
160 "-- delete by comment id failed -- delete ", e);
163 freeConnection(con, stmt);
167 public void deleteByMediaId(String mediaId) throws StorageObjectFailure {
168 if (mediaId == null) {
169 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
172 //delete all row with comment_id=commentId
173 String sql = "delete from " + mainTable + " where media_id=" + mediaId;
175 Connection con = null;
176 Statement stmt = null;
178 con = obtainConnection();
179 // should be a preparedStatement because is faster
180 stmt = con.createStatement();
181 int rs = executeUpdate(stmt, sql);
182 logger.debug("-- delete media success ");
184 catch (Exception e) {
185 logger.error("-- delete media failed ");
186 throw new StorageObjectFailure("-- delete by media id failed -- ", e);
189 freeConnection(con, stmt);
193 public void delete(String commentId, String mediaId) throws
194 StorageObjectFailure {
195 if (mediaId == null || commentId == null) {
196 logger.debug("-- delete media failed -- missing parameter");
199 //delete all row with comment_id=commentId and media_id=mediaId
200 String sql = "delete from " + mainTable + " where media_id=" + mediaId +
201 " and comment_id= " + commentId;
203 Connection con = null;
204 Statement stmt = null;
206 con = obtainConnection();
207 // should be a preparedStatement because is faster
208 stmt = con.createStatement();
209 int rs = executeUpdate(stmt, sql);
210 logger.debug("-- delete comment_x_media success ");
212 catch (Exception e) {
213 logger.error("-- delete comment_x_media failed ");
214 throw new StorageObjectFailure("-- delete comment_x_media failed -- ", e);
217 freeConnection(con, stmt);
221 public EntityList getComment(EntityUploadedMedia media) throws
222 StorageObjectFailure {
224 EntityList returnList = null;
227 String id = media.getId();
228 ArrayList extraTables = new ArrayList();
229 extraTables.add(mainTable + " cxm");
231 String mediaSelect = "cxm.comment_id=c.id and cxm.media_id="+id;
233 returnList = DatabaseComment.getInstance().selectByWhereClause("c",
234 extraTables, mediaSelect, "c.id" );
237 catch (Exception e) {
238 logger.error("-- get comment failed");
239 throw new StorageObjectFailure("-- get comment failed -- ", e);
246 * Returns a EntityList with all comment-objects having
247 * a relation to a media
250 public EntityList getComment() throws StorageObjectFailure {
251 EntityList returnList = null;
253 ArrayList extraTables = new ArrayList();
254 extraTables.add(mainTable + " cxm");
256 String mediaSelect = "cxm.comment_id=c.id";
258 returnList = DatabaseComment.getInstance().selectByWhereClause("c",
259 extraTables, mediaSelect, "c.webdb_lastchange desc" );
262 catch (Exception e) {
263 logger.error("-- get comment failed");
264 throw new StorageObjectFailure("-- get comment failed -- ", e);