2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
31 package mircoders.storage;
33 import java.sql.Connection;
34 import java.sql.Statement;
35 import java.util.ArrayList;
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;
44 * <b>implements abstract DB connection to the content_x_media SQL table
46 * @author RK, mir-coders group
47 * @version $Id: DatabaseContentToMedia.java,v 1.22 2007/04/08 21:46:36 idfx Exp $
51 public class DatabaseContentToMedia extends Database {
53 private static DatabaseContentToMedia instance;
55 public synchronized static DatabaseContentToMedia getInstance() {
56 if (instance == null) {
57 instance = new DatabaseContentToMedia();
62 private DatabaseContentToMedia() {
65 logger = new LoggerWrapper("Database.ContentToMedia");
66 mainTable = "content_x_media";
67 entityClass = mir.entity.GenericEntity.class;
70 public void addMedia(String contentId, String mediaId) throws
72 if (contentId == null && mediaId == null) {
76 Connection con = null;
77 Statement stmt = null;
80 String sql = "insert into " + mainTable + " (content_id,media_id) values ("
81 + contentId + "," + mediaId + ")";
83 con = obtainConnection();
84 // should be a preparedStatement because is faster
85 stmt = con.createStatement();
86 int rs = executeUpdate(stmt, sql);
89 logger.error("-- add media failed -- insert");
90 throw new DatabaseFailure("-- add media failed -- insert ", e);
93 freeConnection(con, stmt);
97 public void setMedia(String contentId, String mediaId) throws
99 if (contentId == null && mediaId == null) {
102 //first delete all row with content_id=contentId
103 String sql = "delete from " + mainTable + " where content_id=" + contentId;
105 Connection con = null;
106 Statement stmt = null;
108 con = obtainConnection();
109 // should be a preparedStatement because is faster
110 stmt = con.createStatement();
111 int rs = executeUpdate(stmt, sql);
113 catch (Exception e) {
114 logger.error("-- set media failed -- delete");
115 throw new DatabaseFailure("-- set media failed -- delete ", e);
118 freeConnection(con, stmt);
122 //first delete all row with content_id=contentId
124 sql = "insert into " + mainTable + " (content_id,media_id) values ("
125 + contentId + "," + mediaId + ")";
127 con = obtainConnection();
128 // should be a preparedStatement because is faster
129 stmt = con.createStatement();
130 int rs = executeUpdate(stmt, sql);
132 catch (Exception e) {
133 logger.error("-- set media failed -- insert");
134 throw new DatabaseFailure("-- set media failed -- insert ", e);
137 freeConnection(con, stmt);
141 public void deleteByContentId(String contentId) throws DatabaseFailure {
142 if (contentId == null) {
143 //theLog.printDebugInfo("-- delete topics failed -- no content id");
146 //delete all row with content_id=contentId
147 String sql = "delete from " + mainTable + " where content_id=" + contentId;
149 Connection con = null;
150 Statement stmt = null;
152 con = obtainConnection();
153 // should be a preparedStatement because is faster
154 stmt = con.createStatement();
155 int rs = executeUpdate(stmt, sql);
157 catch (Exception e) {
158 logger.error("-- delete by contentId failed ");
159 throw new DatabaseFailure(
160 "-- delete by content id failed -- delete ", e);
163 freeConnection(con, stmt);
167 public void deleteByMediaId(String mediaId) throws DatabaseFailure {
169 if (mediaId == null) {
170 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
173 //delete all row with content_id=contentId
174 String sql = "delete from " + mainTable + " where media_id=" + mediaId;
176 Connection con = null;
177 Statement stmt = null;
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 ");
185 catch (Exception e) {
186 logger.error("-- delete media failed ");
187 throw new DatabaseFailure("-- delete by media id failed -- ", e);
190 freeConnection(con, stmt);
194 public void delete(String contentId, String mediaId) throws
196 if (mediaId == null || contentId == null) {
197 logger.debug("-- delete media failed -- missing parameter");
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;
204 Connection con = null;
205 Statement stmt = null;
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 ");
213 catch (Exception e) {
214 logger.error("-- delete content_x_media failed ");
215 throw new DatabaseFailure("-- delete content_x_media failed -- ", e);
218 freeConnection(con, stmt);
222 public EntityList getContent(EntityUploadedMedia media) throws
225 EntityList returnList = null;
228 String id = media.getId();
229 ArrayList extraTables = new ArrayList();
230 extraTables.add(mainTable + " cxm");
232 String mediaSelect = "cxm.content_id=c.id and cxm.media_id="+id;
234 returnList = DatabaseContent.getInstance().selectByWhereClause("c",
235 extraTables, mediaSelect, "c.id" );
238 catch (Exception e) {
239 logger.error("-- get content failed");
240 throw new DatabaseFailure("-- get content failed -- ", e);
247 * Returns a EntityList with all content-objects having a relation to a media
250 public EntityList getContent() throws DatabaseFailure {
252 EntityList returnList = null;
254 ArrayList extraTables = new ArrayList();
255 extraTables.add(mainTable + " cxm");
257 String mediaSelect = "cxm.content_id=c.id";
259 returnList = DatabaseContent.getInstance().selectByWhereClause("c",
260 extraTables, mediaSelect, "c.webdb_lastchange desc" );
263 catch (Exception e) {
264 logger.error("-- get content failed");
265 throw new DatabaseFailure("-- get content failed -- ", e);