9f8807c4a921d22ee72ced10876cd2b20acdad2e
[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.Statement;
35 import java.util.ArrayList;
36
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;
42
43 /**
44  * <b>implements abstract DB connection to the comment_x_media SQL table
45  *
46  * @author RK, mir-coders group
47  * @version $Id: DatabaseCommentToMedia.java,v 1.3.2.10 2005/02/10 16:22:22 rhindes Exp $
48  *
49  */
50
51 public class DatabaseCommentToMedia extends Database {
52
53   private static DatabaseCommentToMedia instance;
54
55   public static synchronized DatabaseCommentToMedia getInstance() {
56     if (instance == null) {
57       instance = new DatabaseCommentToMedia();
58     }
59     return instance;
60   }
61
62   private DatabaseCommentToMedia() {
63     super();
64
65     logger = new LoggerWrapper("Database.CommentToMedia");
66     mainTable = "comment_x_media";
67     entityClass = mir.entity.GenericEntity.class;
68   }
69
70   public void addMedia(String commentId, String mediaId) throws
71       StorageObjectFailure {
72     if (commentId == null && mediaId == null) {
73       return;
74     }
75
76     Connection con = null;
77     Statement stmt = null;
78     //now insert
79
80     String sql = "insert into " + mainTable + " (comment_id,media_id) values ("
81         + commentId + "," + mediaId + ")";
82     try {
83       con = obtainConnection();
84       // should be a preparedStatement because is faster
85       stmt = con.createStatement();
86       int rs = executeUpdate(stmt, sql);
87     }
88     catch (Exception e) {
89       logger.error("-- add media failed -- insert");
90       throw new StorageObjectFailure("-- add media failed -- insert ", e);
91     }
92     finally {
93       freeConnection(con, stmt);
94     }
95   }
96
97   public void setMedia(String commentId, String mediaId) throws
98       StorageObjectFailure {
99     if (commentId == null && mediaId == null) {
100       return;
101     }
102     //first delete all row with comment_id=commentId
103     String sql = "delete from " + mainTable + " where comment_id=" + commentId;
104
105     Connection con = null;
106     Statement stmt = null;
107     try {
108       con = obtainConnection();
109       // should be a preparedStatement because is faster
110       stmt = con.createStatement();
111       int rs = executeUpdate(stmt, sql);
112     }
113     catch (Exception e) {
114       logger.error("-- set media failed -- delete");
115       throw new StorageObjectFailure("-- set media failed -- delete ", e);
116     }
117     finally {
118       freeConnection(con, stmt);
119     }
120
121     //now insert
122     //first delete all row with comment_id=commentId
123
124     sql = "insert into " + mainTable + " (comment_id,media_id) values ("
125         + commentId + "," + mediaId + ")";
126     try {
127       con = obtainConnection();
128       // should be a preparedStatement because is faster
129       stmt = con.createStatement();
130       int rs = executeUpdate(stmt, sql);
131     }
132     catch (Exception e) {
133       logger.error("-- set media failed -- insert");
134       throw new StorageObjectFailure("-- set media failed -- insert ", e);
135     }
136     finally {
137       freeConnection(con, stmt);
138     }
139   }
140
141   public void deleteByCommentId(String commentId) throws StorageObjectFailure {
142     if (commentId == null) {
143       //theLog.printDebugInfo("-- delete topics failed -- no comment id");
144       return;
145     }
146     //delete all row with comment_id=commentId
147     String sql = "delete from " + mainTable + " where comment_id=" + commentId;
148
149     Connection con = null;
150     Statement stmt = null;
151     try {
152       con = obtainConnection();
153       // should be a preparedStatement because is faster
154       stmt = con.createStatement();
155       int rs = executeUpdate(stmt, sql);
156     }
157     catch (Exception e) {
158       logger.error("-- delete by commentId failed  ");
159       throw new StorageObjectFailure(
160           "-- delete by comment id failed -- delete ", e);
161     }
162     finally {
163       freeConnection(con, stmt);
164     }
165   }
166
167   public void deleteByMediaId(String mediaId) throws StorageObjectFailure {
168     if (mediaId == null) {
169       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
170       return;
171     }
172     //delete all row with comment_id=commentId
173     String sql = "delete from " + mainTable + " where media_id=" + mediaId;
174
175     Connection con = null;
176     Statement stmt = null;
177     try {
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 ");
183     }
184     catch (Exception e) {
185       logger.error("-- delete media failed ");
186       throw new StorageObjectFailure("-- delete by media id failed -- ", e);
187     }
188     finally {
189       freeConnection(con, stmt);
190     }
191   }
192
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");
197       return;
198     }
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;
202
203     Connection con = null;
204     Statement stmt = null;
205     try {
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 ");
211     }
212     catch (Exception e) {
213       logger.error("-- delete comment_x_media failed ");
214       throw new StorageObjectFailure("-- delete comment_x_media failed -- ", e);
215     }
216     finally {
217       freeConnection(con, stmt);
218     }
219   }
220
221   public EntityList getComment(EntityUploadedMedia media) throws
222       StorageObjectFailure {
223
224     EntityList returnList = null;
225     if (media != null) {
226
227       String id = media.getId();
228       ArrayList extraTables = new ArrayList();
229       extraTables.add(mainTable + " cxm");
230
231       String mediaSelect = "cxm.comment_id=c.id and cxm.media_id="+id;
232       try {
233         returnList = DatabaseComment.getInstance().selectByWhereClause("c",
234           extraTables, mediaSelect, "c.id" );
235
236       }
237       catch (Exception e) {
238         logger.error("-- get comment failed");
239         throw new StorageObjectFailure("-- get comment failed -- ", e);
240       }
241     }
242     return returnList;
243   }
244
245   /**
246    * Returns a EntityList with all comment-objects having
247    *  a relation to a media
248    */
249
250   public EntityList getComment() throws StorageObjectFailure {
251     EntityList returnList = null;
252
253     ArrayList extraTables = new ArrayList();
254     extraTables.add(mainTable + " cxm");
255
256     String mediaSelect = "cxm.comment_id=c.id";
257     try {
258       returnList = DatabaseComment.getInstance().selectByWhereClause("c",
259         extraTables, mediaSelect, "c.webdb_lastchange desc" );
260
261     }
262     catch (Exception e) {
263       logger.error("-- get comment failed");
264       throw new StorageObjectFailure("-- get comment failed -- ", e);
265     }
266     return returnList;
267
268   }
269
270 }