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.DatabaseFailure;
41 import mircoders.entity.EntityUploadedMedia;
43 public class DatabaseCommentToMedia extends Database {
45 private static DatabaseCommentToMedia instance;
47 public static synchronized DatabaseCommentToMedia getInstance() {
48 if (instance == null) {
49 instance = new DatabaseCommentToMedia();
54 private DatabaseCommentToMedia() {
57 logger = new LoggerWrapper("Database.CommentToMedia");
58 mainTable = "comment_x_media";
59 entityClass = mir.entity.GenericEntity.class;
62 public void addMedia(String commentId, String mediaId) throws
64 if (commentId == null && mediaId == null) {
68 Connection con = null;
69 Statement stmt = null;
72 String sql = "insert into " + mainTable + " (comment_id,media_id) values ("
73 + commentId + "," + mediaId + ")";
75 con = obtainConnection();
76 // should be a preparedStatement because is faster
77 stmt = con.createStatement();
78 int rs = executeUpdate(stmt, sql);
81 logger.error("-- add media failed -- insert");
82 throw new DatabaseFailure("-- add media failed -- insert ", e);
85 freeConnection(con, stmt);
89 public void setMedia(String commentId, String mediaId) throws
91 if (commentId == null && mediaId == null) {
94 //first delete all row with comment_id=commentId
95 String sql = "delete from " + mainTable + " where comment_id=" + commentId;
97 Connection con = null;
98 Statement stmt = null;
100 con = obtainConnection();
101 // should be a preparedStatement because is faster
102 stmt = con.createStatement();
103 int rs = executeUpdate(stmt, sql);
105 catch (Exception e) {
106 logger.error("-- set media failed -- delete");
107 throw new DatabaseFailure("-- set media failed -- delete ", e);
110 freeConnection(con, stmt);
114 //first delete all row with comment_id=commentId
116 sql = "insert into " + mainTable + " (comment_id,media_id) values ("
117 + commentId + "," + mediaId + ")";
119 con = obtainConnection();
120 // should be a preparedStatement because is faster
121 stmt = con.createStatement();
122 int rs = executeUpdate(stmt, sql);
124 catch (Exception e) {
125 logger.error("-- set media failed -- insert");
126 throw new DatabaseFailure("-- set media failed -- insert ", e);
129 freeConnection(con, stmt);
133 public void deleteByCommentId(String commentId) throws DatabaseFailure {
134 if (commentId == null) {
135 //theLog.printDebugInfo("-- delete topics failed -- no comment id");
138 //delete all row with comment_id=commentId
139 String sql = "delete from " + mainTable + " where comment_id=" + commentId;
141 Connection con = null;
142 Statement stmt = null;
144 con = obtainConnection();
145 // should be a preparedStatement because is faster
146 stmt = con.createStatement();
147 int rs = executeUpdate(stmt, sql);
149 catch (Exception e) {
150 logger.error("-- delete by commentId failed ");
151 throw new DatabaseFailure(
152 "-- delete by comment id failed -- delete ", e);
155 freeConnection(con, stmt);
159 public void deleteByMediaId(String mediaId) throws DatabaseFailure {
160 if (mediaId == null) {
161 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
164 //delete all row with comment_id=commentId
165 String sql = "delete from " + mainTable + " where media_id=" + mediaId;
167 Connection con = null;
168 Statement stmt = null;
170 con = obtainConnection();
171 // should be a preparedStatement because is faster
172 stmt = con.createStatement();
173 int rs = executeUpdate(stmt, sql);
174 logger.debug("-- delete media success ");
176 catch (Exception e) {
177 logger.error("-- delete media failed ");
178 throw new DatabaseFailure("-- delete by media id failed -- ", e);
181 freeConnection(con, stmt);
185 public void delete(String commentId, String mediaId) throws
187 if (mediaId == null || commentId == null) {
188 logger.debug("-- delete media failed -- missing parameter");
191 //delete all row with comment_id=commentId and media_id=mediaId
192 String sql = "delete from " + mainTable + " where media_id=" + mediaId +
193 " and comment_id= " + commentId;
195 Connection con = null;
196 Statement stmt = null;
198 con = obtainConnection();
199 // should be a preparedStatement because is faster
200 stmt = con.createStatement();
201 int rs = executeUpdate(stmt, sql);
202 logger.debug("-- delete comment_x_media success ");
204 catch (Exception e) {
205 logger.error("-- delete comment_x_media failed ");
206 throw new DatabaseFailure("-- delete comment_x_media failed -- ", e);
209 freeConnection(con, stmt);
213 public EntityList getComment(EntityUploadedMedia media) throws
216 EntityList returnList = null;
219 String id = media.getId();
220 ArrayList extraTables = new ArrayList();
221 extraTables.add(mainTable + " cxm");
223 String mediaSelect = "cxm.comment_id=c.id and cxm.media_id="+id;
225 returnList = DatabaseComment.getInstance().selectByWhereClause("c",
226 extraTables, mediaSelect, "c.id" );
229 catch (Exception e) {
230 logger.error("-- get comment failed");
231 throw new DatabaseFailure("-- get comment failed -- ", e);
238 * Returns a EntityList with all comment-objects having
239 * a relation to a media
242 public EntityList getComment() throws DatabaseFailure {
243 EntityList returnList = null;
245 ArrayList extraTables = new ArrayList();
246 extraTables.add(mainTable + " cxm");
248 String mediaSelect = "cxm.comment_id=c.id";
250 returnList = DatabaseComment.getInstance().selectByWhereClause("c",
251 extraTables, mediaSelect, "c.webdb_lastchange desc" );
254 catch (Exception e) {
255 logger.error("-- get comment failed");
256 throw new DatabaseFailure("-- get comment failed -- ", e);