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.Vector;
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.5 2004/01/18 17:31:01 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");
71 mainTable = "content_x_media";
72 theEntityClass = mir.entity.GenericEntity.class;
75 public boolean hasMedia(EntityContent content) throws StorageObjectFailure,
77 if (content != null) {
79 return (getSize("content_id=" + content.getId()) == 0) ? false:true;
82 logger.error("DatabaseContentToMedia.hasMedia: " + e.toString());
83 throw new StorageObjectFailure("DatabaseContentToMedia.hasMedia: " +
88 logger.error("DatabaseContentToMedia.hasMedia: content == null");
89 throw new StorageObjectExc(
90 "DatabaseContentToMedia.hasMedia: content == null");
94 private EntityList getMultiMediaForContent(StorageObject store, EntityContent content)
95 throws StorageObjectFailure {
97 EntityList returnList = null;
98 if (content != null) {
99 String id = content.getId();
101 Vector extraTable = new Vector();
102 extraTable.add(mainTable+" cxm");
103 // media should stay in uploaded order. this is especially important
104 // for photo stories which require a specific chronologic order.
105 // this is why we have the the second parameter "id"
106 store.selectByWhereClause("m", extraTable,
107 "m.id=cxm.media_id and cxm.content_id="+id, "id", -1, -1);
109 } catch (Exception e) {
110 logger.error("DatabaseContentToMedia.getMultiMediaForContent: " + e.toString());
111 throw new StorageObjectFailure("DatabaseContentToMedia.etMultiMediaForContent: " +
119 * get all the audio belonging to a content entity
121 public EntityList getAudio(EntityContent content) throws StorageObjectFailure {
122 return getMultiMediaForContent(DatabaseAudio.getInstance(), content);
126 * get all the video belonging to a content entity
128 public EntityList getVideo(EntityContent content) throws StorageObjectFailure {
129 return getMultiMediaForContent(DatabaseVideo.getInstance(), content);
133 * get all the images belonging to a content entity
135 public EntityList getImages(EntityContent content) throws StorageObjectFailure {
136 return getMultiMediaForContent(DatabaseImages.getInstance(), content);
140 * get all the uploaded/other Media belonging to a content entity
142 public EntityList getOther(EntityContent content) throws StorageObjectFailure {
143 return getMultiMediaForContent(DatabaseOther.getInstance(), content);
147 * get all the uploaded/other Media belonging to a content entity
149 public EntityList getUploadedMedia(EntityContent content) throws StorageObjectFailure {
150 return getMultiMediaForContent(DatabaseUploadedMedia.getInstance(), content);
154 public void setMedia(String contentId, String[] mediaId) throws StorageObjectFailure {
155 if (contentId == null || mediaId == null || mediaId[0] == null) {
158 //first delete all row with content_id=contentId
159 String sql = "delete from " + mainTable + " where content_id=" + contentId;
161 Connection con = null;
162 Statement stmt = null;
164 con = getPooledCon();
165 // should be a preparedStatement because is faster
166 stmt = con.createStatement();
167 ResultSet rs = executeSql(stmt, sql);
169 catch (Exception e) {
170 logger.error("-- set media failed -- delete");
171 throw new StorageObjectFailure("-- set media failed -- delete", e);
174 freeConnection(con, stmt);
178 //first delete all row with content_id=contentId
179 for (int i = 0; i < mediaId.length; i++) {
180 sql = "insert into " + mainTable + " (content_id,media_id) values ("
181 + contentId + "," + mediaId[i] + ")";
183 con = getPooledCon();
184 // should be a preparedStatement because is faster
185 stmt = con.createStatement();
186 int rs = executeUpdate(stmt, sql);
188 catch (Exception e) {
189 logger.error("-- set topics failed -- insert");
190 throw new StorageObjectFailure("-- set topics failed -- insert ", e);
193 freeConnection(con, stmt);
198 public void addMedia(String contentId, String mediaId) throws
199 StorageObjectFailure {
200 if (contentId == null && mediaId == null) {
204 Connection con = null;
205 Statement stmt = null;
208 String sql = "insert into " + mainTable + " (content_id,media_id) values ("
209 + contentId + "," + mediaId + ")";
211 con = getPooledCon();
212 // should be a preparedStatement because is faster
213 stmt = con.createStatement();
214 int rs = executeUpdate(stmt, sql);
216 catch (Exception e) {
217 logger.error("-- add media failed -- insert");
218 throw new StorageObjectFailure("-- add media failed -- insert ", e);
221 freeConnection(con, stmt);
225 public void setMedia(String contentId, String mediaId) throws
226 StorageObjectFailure {
227 if (contentId == null && mediaId == null) {
230 //first delete all row with content_id=contentId
231 String sql = "delete from " + mainTable + " where content_id=" + contentId;
233 Connection con = null;
234 Statement stmt = null;
236 con = getPooledCon();
237 // should be a preparedStatement because is faster
238 stmt = con.createStatement();
239 int rs = executeUpdate(stmt, sql);
241 catch (Exception e) {
242 logger.error("-- set media failed -- delete");
243 throw new StorageObjectFailure("-- set media failed -- delete ", e);
246 freeConnection(con, stmt);
250 //first delete all row with content_id=contentId
252 sql = "insert into " + mainTable + " (content_id,media_id) values ("
253 + contentId + "," + mediaId + ")";
255 con = getPooledCon();
256 // should be a preparedStatement because is faster
257 stmt = con.createStatement();
258 int rs = executeUpdate(stmt, sql);
260 catch (Exception e) {
261 logger.error("-- set media failed -- insert");
262 throw new StorageObjectFailure("-- set media failed -- insert ", e);
265 freeConnection(con, stmt);
269 public void deleteByContentId(String contentId) throws StorageObjectFailure {
270 if (contentId == null) {
271 //theLog.printDebugInfo("-- delete topics failed -- no content id");
274 //delete all row with content_id=contentId
275 String sql = "delete from " + mainTable + " where content_id=" + contentId;
277 Connection con = null;
278 Statement stmt = null;
280 con = getPooledCon();
281 // should be a preparedStatement because is faster
282 stmt = con.createStatement();
283 int rs = executeUpdate(stmt, sql);
285 catch (Exception e) {
286 logger.error("-- delete by contentId failed ");
287 throw new StorageObjectFailure(
288 "-- delete by content id failed -- delete ", e);
291 freeConnection(con, stmt);
295 public void deleteByMediaId(String mediaId) throws StorageObjectFailure {
297 if (mediaId == null) {
298 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
301 //delete all row with content_id=contentId
302 String sql = "delete from " + mainTable + " where media_id=" + mediaId;
304 Connection con = null;
305 Statement stmt = null;
307 con = getPooledCon();
308 // should be a preparedStatement because is faster
309 stmt = con.createStatement();
310 int rs = executeUpdate(stmt, sql);
311 logger.debug("-- delete media success ");
313 catch (Exception e) {
314 logger.error("-- delete media failed ");
315 throw new StorageObjectFailure("-- delete by media id failed -- ", e);
318 freeConnection(con, stmt);
322 public void delete(String contentId, String mediaId) throws
323 StorageObjectFailure {
324 if (mediaId == null || contentId == null) {
325 logger.debug("-- delete media failed -- missing parameter");
328 //delete all row with content_id=contentId and media_id=mediaId
329 String sql = "delete from " + mainTable + " where media_id=" + mediaId +
330 " and content_id= " + contentId;
332 Connection con = null;
333 Statement stmt = null;
335 con = getPooledCon();
336 // should be a preparedStatement because is faster
337 stmt = con.createStatement();
338 int rs = executeUpdate(stmt, sql);
339 logger.debug("-- delete content_x_media success ");
341 catch (Exception e) {
342 logger.error("-- delete content_x_media failed ");
343 throw new StorageObjectFailure("-- delete content_x_media failed -- ", e);
346 freeConnection(con, stmt);
350 public EntityList getContent(EntityUploadedMedia media) throws
351 StorageObjectFailure {
353 EntityList returnList = null;
356 String id = media.getId();
357 Vector extraTables = new Vector();
358 extraTables.add(mainTable + " cxm");
360 String mediaSelect = "cxm.content_id=c.id and cxm.media_id="+id;
362 returnList = DatabaseContent.getInstance().selectByWhereClause("c",
363 extraTables, mediaSelect, "c.id" );
366 catch (Exception e) {
367 logger.error("-- get content failed");
368 throw new StorageObjectFailure("-- get content failed -- ", e);
375 * Returns a EntityList with all content-objects having a relation to a media
378 public EntityList getContent() throws StorageObjectFailure {
380 EntityList returnList = null;
382 Vector extraTables = new Vector();
383 extraTables.add(mainTable + " cxm");
385 String mediaSelect = "cxm.content_id=c.id";
387 returnList = DatabaseContent.getInstance().selectByWhereClause("c",
388 extraTables, mediaSelect, "c.webdb_lastchange desc" );
391 catch (Exception e) {
392 logger.error("-- get content failed");
393 throw new StorageObjectFailure("-- get content failed -- ", e);