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.ResultSet;
35 import java.sql.Statement;
36 import java.util.ArrayList;
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;
48 * <b>implements abstract DB connection to the content_x_media SQL table
50 * @author RK, mir-coders group
51 * @version $Id: DatabaseContentToMedia.java,v 1.19.2.7 2005/01/09 20:37:15 zapata Exp $
55 public class DatabaseContentToMedia extends Database implements StorageObject{
57 private static DatabaseContentToMedia instance;
59 public synchronized static DatabaseContentToMedia getInstance() {
60 if (instance == null) {
61 instance = new DatabaseContentToMedia();
66 private DatabaseContentToMedia() {
69 logger = new LoggerWrapper("Database.ContentToMedia");
70 mainTable = "content_x_media";
71 entityClass = mir.entity.GenericEntity.class;
74 public void addMedia(String contentId, String mediaId) throws
75 StorageObjectFailure {
76 if (contentId == null && mediaId == null) {
80 Connection con = null;
81 Statement stmt = null;
84 String sql = "insert into " + mainTable + " (content_id,media_id) values ("
85 + contentId + "," + mediaId + ")";
87 con = obtainConnection();
88 // should be a preparedStatement because is faster
89 stmt = con.createStatement();
90 int rs = executeUpdate(stmt, sql);
93 logger.error("-- add media failed -- insert");
94 throw new StorageObjectFailure("-- add media failed -- insert ", e);
97 freeConnection(con, stmt);
101 public void setMedia(String contentId, String mediaId) throws
102 StorageObjectFailure {
103 if (contentId == null && mediaId == null) {
106 //first delete all row with content_id=contentId
107 String sql = "delete from " + mainTable + " where content_id=" + contentId;
109 Connection con = null;
110 Statement stmt = null;
112 con = obtainConnection();
113 // should be a preparedStatement because is faster
114 stmt = con.createStatement();
115 int rs = executeUpdate(stmt, sql);
117 catch (Exception e) {
118 logger.error("-- set media failed -- delete");
119 throw new StorageObjectFailure("-- set media failed -- delete ", e);
122 freeConnection(con, stmt);
126 //first delete all row with content_id=contentId
128 sql = "insert into " + mainTable + " (content_id,media_id) values ("
129 + contentId + "," + mediaId + ")";
131 con = obtainConnection();
132 // should be a preparedStatement because is faster
133 stmt = con.createStatement();
134 int rs = executeUpdate(stmt, sql);
136 catch (Exception e) {
137 logger.error("-- set media failed -- insert");
138 throw new StorageObjectFailure("-- set media failed -- insert ", e);
141 freeConnection(con, stmt);
145 public void deleteByContentId(String contentId) throws StorageObjectFailure {
146 if (contentId == null) {
147 //theLog.printDebugInfo("-- delete topics failed -- no content id");
150 //delete all row with content_id=contentId
151 String sql = "delete from " + mainTable + " where content_id=" + contentId;
153 Connection con = null;
154 Statement stmt = null;
156 con = obtainConnection();
157 // should be a preparedStatement because is faster
158 stmt = con.createStatement();
159 int rs = executeUpdate(stmt, sql);
161 catch (Exception e) {
162 logger.error("-- delete by contentId failed ");
163 throw new StorageObjectFailure(
164 "-- delete by content id failed -- delete ", e);
167 freeConnection(con, stmt);
171 public void deleteByMediaId(String mediaId) throws StorageObjectFailure {
173 if (mediaId == null) {
174 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
177 //delete all row with content_id=contentId
178 String sql = "delete from " + mainTable + " where media_id=" + mediaId;
180 Connection con = null;
181 Statement stmt = null;
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 ");
189 catch (Exception e) {
190 logger.error("-- delete media failed ");
191 throw new StorageObjectFailure("-- delete by media id failed -- ", e);
194 freeConnection(con, stmt);
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");
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;
208 Connection con = null;
209 Statement stmt = null;
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 ");
217 catch (Exception e) {
218 logger.error("-- delete content_x_media failed ");
219 throw new StorageObjectFailure("-- delete content_x_media failed -- ", e);
222 freeConnection(con, stmt);
226 public EntityList getContent(EntityUploadedMedia media) throws
227 StorageObjectFailure {
229 EntityList returnList = null;
232 String id = media.getId();
233 ArrayList extraTables = new ArrayList();
234 extraTables.add(mainTable + " cxm");
236 String mediaSelect = "cxm.content_id=c.id and cxm.media_id="+id;
238 returnList = DatabaseContent.getInstance().selectByWhereClause("c",
239 extraTables, mediaSelect, "c.id" );
242 catch (Exception e) {
243 logger.error("-- get content failed");
244 throw new StorageObjectFailure("-- get content failed -- ", e);
251 * Returns a EntityList with all content-objects having a relation to a media
254 public EntityList getContent() throws StorageObjectFailure {
256 EntityList returnList = null;
258 ArrayList extraTables = new ArrayList();
259 extraTables.add(mainTable + " cxm");
261 String mediaSelect = "cxm.content_id=c.id";
263 returnList = DatabaseContent.getInstance().selectByWhereClause("c",
264 extraTables, mediaSelect, "c.webdb_lastchange desc" );
267 catch (Exception e) {
268 logger.error("-- get content failed");
269 throw new StorageObjectFailure("-- get content failed -- ", e);