* anti-abuse upgrade: filters now stored in the database (experimental)
[mir.git] / source / mircoders / storage / DatabaseContentToMedia.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.EntityContent;
45 import mircoders.entity.EntityUploadedMedia;
46
47 /**
48  * <b>implements abstract DB connection to the content_x_media SQL table
49  *
50  * @author RK, mir-coders group
51  * @version $Id: DatabaseContentToMedia.java,v 1.19.2.7 2005/01/09 20:37:15 zapata Exp $
52  *
53  */
54
55 public class DatabaseContentToMedia extends Database implements StorageObject{
56
57   private static DatabaseContentToMedia instance;
58
59   public synchronized static DatabaseContentToMedia getInstance() {
60     if (instance == null) {
61       instance = new DatabaseContentToMedia();
62     }
63     return instance;
64   }
65
66   private DatabaseContentToMedia() {
67     super();
68
69     logger = new LoggerWrapper("Database.ContentToMedia");
70     mainTable = "content_x_media";
71     entityClass = mir.entity.GenericEntity.class;
72   }
73
74   public void addMedia(String contentId, String mediaId) throws
75       StorageObjectFailure {
76     if (contentId == 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 + " (content_id,media_id) values ("
85         + contentId + "," + 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 contentId, String mediaId) throws
102       StorageObjectFailure {
103     if (contentId == null && mediaId == null) {
104       return;
105     }
106     //first delete all row with content_id=contentId
107     String sql = "delete from " + mainTable + " where content_id=" + contentId;
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 content_id=contentId
127
128     sql = "insert into " + mainTable + " (content_id,media_id) values ("
129         + contentId + "," + 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 deleteByContentId(String contentId) throws StorageObjectFailure {
146     if (contentId == null) {
147       //theLog.printDebugInfo("-- delete topics failed -- no content id");
148       return;
149     }
150     //delete all row with content_id=contentId
151     String sql = "delete from " + mainTable + " where content_id=" + contentId;
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 contentId failed  ");
163       throw new StorageObjectFailure(
164           "-- delete by content id failed -- delete ", e);
165     }
166     finally {
167       freeConnection(con, stmt);
168     }
169   }
170
171   public void deleteByMediaId(String mediaId) throws StorageObjectFailure {
172
173     if (mediaId == null) {
174       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
175       return;
176     }
177     //delete all row with content_id=contentId
178     String sql = "delete from " + mainTable + " where media_id=" + mediaId;
179
180     Connection con = null;
181     Statement stmt = null;
182     try {
183       con = obtainConnection();
184       // should be a preparedStatement because is faster
185       stmt = con.createStatement();
186       int rs = executeUpdate(stmt, sql);
187       logger.debug("-- delete media success ");
188     }
189     catch (Exception e) {
190       logger.error("-- delete media failed ");
191       throw new StorageObjectFailure("-- delete by media id failed -- ", e);
192     }
193     finally {
194       freeConnection(con, stmt);
195     }
196   }
197
198   public void delete(String contentId, String mediaId) throws
199       StorageObjectFailure {
200     if (mediaId == null || contentId == null) {
201       logger.debug("-- delete media failed -- missing parameter");
202       return;
203     }
204     //delete all row with content_id=contentId and media_id=mediaId
205     String sql = "delete from " + mainTable + " where media_id=" + mediaId +
206         " and content_id= " + contentId;
207
208     Connection con = null;
209     Statement stmt = null;
210     try {
211       con = obtainConnection();
212       // should be a preparedStatement because is faster
213       stmt = con.createStatement();
214       int rs = executeUpdate(stmt, sql);
215       logger.debug("-- delete content_x_media success ");
216     }
217     catch (Exception e) {
218       logger.error("-- delete content_x_media failed ");
219       throw new StorageObjectFailure("-- delete content_x_media failed -- ", e);
220     }
221     finally {
222       freeConnection(con, stmt);
223     }
224   }
225
226   public EntityList getContent(EntityUploadedMedia media) throws
227       StorageObjectFailure {
228
229     EntityList returnList = null;
230     if (media != null) {
231
232       String id = media.getId();
233       ArrayList extraTables = new ArrayList();
234       extraTables.add(mainTable + " cxm");
235
236       String mediaSelect = "cxm.content_id=c.id and cxm.media_id="+id;
237       try {
238         returnList = DatabaseContent.getInstance().selectByWhereClause("c",
239           extraTables, mediaSelect, "c.id" );
240
241       }
242       catch (Exception e) {
243         logger.error("-- get content failed");
244         throw new StorageObjectFailure("-- get content failed -- ", e);
245       }
246     }
247     return returnList;
248   }
249
250   /**
251    * Returns a EntityList with all content-objects having a relation to a media
252    */
253
254   public EntityList getContent() throws StorageObjectFailure {
255
256     EntityList returnList = null;
257
258     ArrayList extraTables = new ArrayList();
259     extraTables.add(mainTable + " cxm");
260
261     String mediaSelect = "cxm.content_id=c.id";
262     try {
263       returnList = DatabaseContent.getInstance().selectByWhereClause("c",
264         extraTables, mediaSelect, "c.webdb_lastchange desc" );
265
266     }
267     catch (Exception e) {
268       logger.error("-- get content failed");
269       throw new StorageObjectFailure("-- get content failed -- ", e);
270     }
271     return returnList;
272   }
273
274 }