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