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