some db code rewriting
[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.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 /**
44  * <b>implements abstract DB connection to the content_x_media SQL table
45  *
46  * @author RK, mir-coders group
47  * @version $Id: DatabaseContentToMedia.java,v 1.19.2.10 2005/03/26 11:26:28 zapata Exp $
48  *
49  */
50
51 public class DatabaseContentToMedia extends Database {
52
53   private static DatabaseContentToMedia instance;
54
55   public synchronized static DatabaseContentToMedia getInstance() {
56     if (instance == null) {
57       instance = new DatabaseContentToMedia();
58     }
59     return instance;
60   }
61
62   private DatabaseContentToMedia() {
63     super();
64
65     logger = new LoggerWrapper("Database.ContentToMedia");
66     mainTable = "content_x_media";
67     entityClass = mir.entity.GenericEntity.class;
68   }
69
70   public void addMedia(String contentId, String mediaId) throws
71       DatabaseFailure {
72     if (contentId == 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 + " (content_id,media_id) values ("
81         + contentId + "," + 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 DatabaseFailure("-- add media failed -- insert ", e);
91     }
92     finally {
93       freeConnection(con, stmt);
94     }
95   }
96
97   public void setMedia(String contentId, String mediaId) throws
98       DatabaseFailure {
99     if (contentId == null && mediaId == null) {
100       return;
101     }
102     //first delete all row with content_id=contentId
103     String sql = "delete from " + mainTable + " where content_id=" + contentId;
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 DatabaseFailure("-- set media failed -- delete ", e);
116     }
117     finally {
118       freeConnection(con, stmt);
119     }
120
121     //now insert
122     //first delete all row with content_id=contentId
123
124     sql = "insert into " + mainTable + " (content_id,media_id) values ("
125         + contentId + "," + 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 DatabaseFailure("-- set media failed -- insert ", e);
135     }
136     finally {
137       freeConnection(con, stmt);
138     }
139   }
140
141   public void deleteByContentId(String contentId) throws DatabaseFailure {
142     if (contentId == null) {
143       //theLog.printDebugInfo("-- delete topics failed -- no content id");
144       return;
145     }
146     //delete all row with content_id=contentId
147     String sql = "delete from " + mainTable + " where content_id=" + contentId;
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 contentId failed  ");
159       throw new DatabaseFailure(
160           "-- delete by content id failed -- delete ", e);
161     }
162     finally {
163       freeConnection(con, stmt);
164     }
165   }
166
167   public void deleteByMediaId(String mediaId) throws DatabaseFailure {
168
169     if (mediaId == null) {
170       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
171       return;
172     }
173     //delete all row with content_id=contentId
174     String sql = "delete from " + mainTable + " where media_id=" + mediaId;
175
176     Connection con = null;
177     Statement stmt = null;
178     try {
179       con = obtainConnection();
180       // should be a preparedStatement because is faster
181       stmt = con.createStatement();
182       int rs = executeUpdate(stmt, sql);
183       logger.debug("-- delete media success ");
184     }
185     catch (Exception e) {
186       logger.error("-- delete media failed ");
187       throw new DatabaseFailure("-- delete by media id failed -- ", e);
188     }
189     finally {
190       freeConnection(con, stmt);
191     }
192   }
193
194   public void delete(String contentId, String mediaId) throws
195       DatabaseFailure {
196     if (mediaId == null || contentId == null) {
197       logger.debug("-- delete media failed -- missing parameter");
198       return;
199     }
200     //delete all row with content_id=contentId and media_id=mediaId
201     String sql = "delete from " + mainTable + " where media_id=" + mediaId +
202         " and content_id= " + contentId;
203
204     Connection con = null;
205     Statement stmt = null;
206     try {
207       con = obtainConnection();
208       // should be a preparedStatement because is faster
209       stmt = con.createStatement();
210       int rs = executeUpdate(stmt, sql);
211       logger.debug("-- delete content_x_media success ");
212     }
213     catch (Exception e) {
214       logger.error("-- delete content_x_media failed ");
215       throw new DatabaseFailure("-- delete content_x_media failed -- ", e);
216     }
217     finally {
218       freeConnection(con, stmt);
219     }
220   }
221
222   public EntityList getContent(EntityUploadedMedia media) throws
223       DatabaseFailure {
224
225     EntityList returnList = null;
226     if (media != null) {
227
228       String id = media.getId();
229       ArrayList extraTables = new ArrayList();
230       extraTables.add(mainTable + " cxm");
231
232       String mediaSelect = "cxm.content_id=c.id and cxm.media_id="+id;
233       try {
234         returnList = DatabaseContent.getInstance().selectByWhereClause("c",
235           extraTables, mediaSelect, "c.id" );
236
237       }
238       catch (Exception e) {
239         logger.error("-- get content failed");
240         throw new DatabaseFailure("-- get content failed -- ", e);
241       }
242     }
243     return returnList;
244   }
245
246   /**
247    * Returns a EntityList with all content-objects having a relation to a media
248    */
249
250   public EntityList getContent() throws DatabaseFailure {
251
252     EntityList returnList = null;
253
254     ArrayList extraTables = new ArrayList();
255     extraTables.add(mainTable + " cxm");
256
257     String mediaSelect = "cxm.content_id=c.id";
258     try {
259       returnList = DatabaseContent.getInstance().selectByWhereClause("c",
260         extraTables, mediaSelect, "c.webdb_lastchange desc" );
261
262     }
263     catch (Exception e) {
264       logger.error("-- get content failed");
265       throw new DatabaseFailure("-- get content failed -- ", e);
266     }
267     return returnList;
268   }
269
270 }