2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with the com.oreilly.servlet library, any library
\r
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
\r
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
\r
24 * the above that use the same license as the above), and distribute linked
\r
25 * combinations including the two. You must obey the GNU General Public
\r
26 * License in all respects for all of the code used other than the above
\r
27 * mentioned libraries. If you modify this file, you may extend this exception
\r
28 * to your version of the file, but you are not obligated to do so. If you do
\r
29 * not wish to do so, delete this exception statement from your version.
\r
32 package mircoders.storage;
\r
34 import java.sql.Connection;
\r
35 import java.sql.ResultSet;
\r
36 import java.sql.Statement;
\r
38 import mir.entity.EntityList;
\r
39 import mir.log.LoggerWrapper;
\r
40 import mir.storage.Database;
\r
41 import mir.storage.StorageObject;
\r
42 import mir.storage.StorageObjectExc;
\r
43 import mir.storage.StorageObjectFailure;
\r
44 import mircoders.entity.EntityContent;
\r
45 import mircoders.entity.EntityUploadedMedia;
\r
48 * <b>implements abstract DB connection to the content_x_media SQL table
\r
50 * @author RK, mir-coders group
\r
51 * @version $Id: DatabaseContentToMedia.java,v 1.16 2003/03/09 03:53:12 zapata Exp $
\r
55 public class DatabaseContentToMedia extends Database implements StorageObject{
\r
57 private static DatabaseContentToMedia instance;
\r
59 public static DatabaseContentToMedia getInstance() {
\r
60 if (instance == null) {
\r
61 synchronized (DatabaseContentToMedia.class) {
\r
62 if (instance == null) {
\r
63 instance = new DatabaseContentToMedia();
\r
64 instance.myselfDatabase = instance;
\r
71 private DatabaseContentToMedia() {
\r
74 logger = new LoggerWrapper("Database.ContentToMedia");
\r
76 hasTimestamp = false;
\r
77 theTable = "content_x_media";
\r
78 theEntityClass = mir.entity.GenericEntity.class;
\r
82 * get all the media-files belonging to a content entity
\r
85 public EntityList getMedia(EntityContent content) throws StorageObjectFailure {
\r
86 EntityList returnList = null;
\r
87 if (content != null) {
\r
88 // get all to_topic from media_x_topic
\r
89 String id = content.getId();
\r
90 String subselect = "id in (select media_id from " + theTable +
\r
91 " where content_id=" + id + ")";
\r
94 // media should stay in uploaded order. this is especially important
\r
95 // for photo stories which require a specific chronologic order.
\r
96 // this is why we have the the second parameter "id"
\r
97 returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,
\r
100 catch (Throwable e) {
\r
101 logger.debug("-- get media failed " + e.toString());
\r
102 throw new StorageObjectFailure("-- get media failed ", e);
\r
108 public boolean hasMedia(EntityContent content) throws StorageObjectFailure,
\r
110 if (content != null) {
\r
112 if (selectByWhereClause("content_id=" + content.getId(), -1).size() ==
\r
118 catch (Exception e) {
\r
119 logger.error("DatabaseContentToMedia.hasMedia: " + e.toString());
\r
120 throw new StorageObjectFailure("DatabaseContentToMedia.hasMedia: " +
\r
125 logger.error("DatabaseContentToMedia.hasMedia: content == null");
\r
126 throw new StorageObjectExc(
\r
127 "DatabaseContentToMedia.hasMedia: content == null");
\r
132 * get all the audio belonging to a content entity
\r
135 public EntityList getAudio(EntityContent content) throws StorageObjectFailure {
\r
136 EntityList returnList = null;
\r
137 if (content != null) {
\r
138 // get all to_topic from media_x_topic
\r
139 String id = content.getId();
\r
140 //this is not supported by mysql
\r
141 String subselect = "id in (select media_id from " + theTable +
\r
142 " where content_id=" + id + ")";
\r
145 // media should stay in uploaded order. this is especially important
\r
146 // for photo stories which require a specific chronologic order.
\r
147 // this is why we have the the second parameter "id"
\r
148 returnList = DatabaseAudio.getInstance().selectByWhereClause(subselect,
\r
151 catch (Exception e) {
\r
152 logger.error("DatabaseContentToMedia.getAudio: " + e.toString());
\r
153 throw new StorageObjectFailure("DatabaseContentToMedia.getAudio: " +
\r
161 * get all the video belonging to a content entity
\r
164 public EntityList getVideo(EntityContent content) throws StorageObjectFailure {
\r
165 EntityList returnList = null;
\r
166 if (content != null) {
\r
167 // get all to_topic from media_x_topic
\r
168 String id = content.getId();
\r
169 //this is not supported by mysql
\r
170 String subselect = "id in (select media_id from " + theTable +
\r
171 " where content_id=" + id + ")";
\r
174 // media should stay in uploaded order. this is especially important
\r
175 // for photo stories which require a specific chronologic order.
\r
176 // this is why we have the the second parameter "id"
\r
177 returnList = DatabaseVideo.getInstance().selectByWhereClause(subselect,
\r
180 catch (Exception e) {
\r
181 logger.error("DatabaseContentToMedia.getVideo: " + e.toString());
\r
182 throw new StorageObjectFailure("DatabaseContentToMedia.getVideo: " +
\r
190 * get all the images belonging to a content entity
\r
193 public EntityList getImages(EntityContent content) throws
\r
194 StorageObjectFailure {
\r
195 EntityList returnList = null;
\r
196 if (content != null) {
\r
197 // get all to_topic from media_x_topic
\r
198 String id = content.getId();
\r
199 //this is not supported by mysql
\r
200 String subselect = "id in (select media_id from " + theTable +
\r
201 " where content_id=" + id + ")";
\r
204 // media should stay in uploaded order. this is especially important
\r
205 // for photo stories which require a specific chronologic order.
\r
206 // this is why we have the the second parameter "id"
\r
207 returnList = DatabaseImages.getInstance().selectByWhereClause(subselect,
\r
210 catch (Exception e) {
\r
211 logger.error("DatabaseContentToMedia.getImages: " + e.toString());
\r
212 throw new StorageObjectFailure("DatabaseContentToMedia.getImages: " +
\r
220 * get all the uploaded/other Media belonging to a content entity
\r
223 public EntityList getOther(EntityContent content) throws StorageObjectFailure {
\r
224 /** @todo this should only fetch published media / rk */
\r
226 EntityList returnList = null;
\r
227 if (content != null) {
\r
228 // get all to_topic from media_x_topic
\r
229 String id = content.getId();
\r
230 //this is not supported by mysql
\r
231 String subselect = "id in (select media_id from " + theTable +
\r
232 " where content_id=" + id + ")";
\r
235 // media should stay in uploaded order. this is especially important
\r
236 // for photo stories which require a specific chronologic order.
\r
237 // this is why we have the the second parameter "id"
\r
238 returnList = DatabaseOther.getInstance().selectByWhereClause(subselect,
\r
241 catch (Exception e) {
\r
242 logger.error("DatabaseContentToMedia.getOther: " + e.toString());
\r
243 throw new StorageObjectFailure("DatabaseContentToMedia.getOther: " + e.toString(), e);
\r
250 * get all the uploaded/other Media belonging to a content entity
\r
253 public EntityList getUploadedMedia(EntityContent content) throws
\r
254 StorageObjectFailure {
\r
255 /** @todo this should only fetch published media / rk */
\r
257 EntityList returnList = null;
\r
258 if (content != null) {
\r
259 // get all to_topic from media_x_topic
\r
260 String id = content.getId();
\r
261 //this is not supported by mysql
\r
262 String subselect = "id in (select media_id from " + theTable +
\r
263 " where content_id=" + id + ")";
\r
266 returnList = DatabaseUploadedMedia.getInstance().selectByWhereClause(
\r
270 catch (Exception e) {
\r
271 logger.error("DatabaseContentToMedia.getUploadedMedia: " + e.toString());
\r
272 throw new StorageObjectFailure(
\r
273 "DatabaseContentToMedia.getUploadedMedia: " + e.toString(), e);
\r
279 public void setMedia(String contentId, String[] mediaId) throws
\r
280 StorageObjectFailure {
\r
281 if (contentId == null) {
\r
284 if (mediaId == null || mediaId[0] == null) {
\r
287 //first delete all row with content_id=contentId
\r
288 String sql = "delete from " + theTable + " where content_id=" + contentId;
\r
290 Connection con = null;
\r
291 Statement stmt = null;
\r
293 con = getPooledCon();
\r
294 // should be a preparedStatement because is faster
\r
295 stmt = con.createStatement();
\r
296 ResultSet rs = executeSql(stmt, sql);
\r
298 catch (Exception e) {
\r
299 logger.error("-- set media failed -- delete");
\r
300 throw new StorageObjectFailure("-- set media failed -- delete", e);
\r
303 freeConnection(con, stmt);
\r
307 //first delete all row with content_id=contentId
\r
308 for (int i = 0; i < mediaId.length; i++) {
\r
309 sql = "insert into " + theTable + " (content_id,media_id) values ("
\r
310 + contentId + "," + mediaId[i] + ")";
\r
312 con = getPooledCon();
\r
313 // should be a preparedStatement because is faster
\r
314 stmt = con.createStatement();
\r
315 int rs = executeUpdate(stmt, sql);
\r
317 catch (Exception e) {
\r
318 logger.error("-- set topics failed -- insert");
\r
319 throw new StorageObjectFailure("-- set topics failed -- insert ", e);
\r
322 freeConnection(con, stmt);
\r
327 public void addMedia(String contentId, String mediaId) throws
\r
328 StorageObjectFailure {
\r
329 if (contentId == null && mediaId == null) {
\r
333 Connection con = null;
\r
334 Statement stmt = null;
\r
337 String sql = "insert into " + theTable + " (content_id,media_id) values ("
\r
338 + contentId + "," + mediaId + ")";
\r
340 con = getPooledCon();
\r
341 // should be a preparedStatement because is faster
\r
342 stmt = con.createStatement();
\r
343 int rs = executeUpdate(stmt, sql);
\r
345 catch (Exception e) {
\r
346 logger.error("-- add media failed -- insert");
\r
347 throw new StorageObjectFailure("-- add media failed -- insert ", e);
\r
350 freeConnection(con, stmt);
\r
354 public void setMedia(String contentId, String mediaId) throws
\r
355 StorageObjectFailure {
\r
356 if (contentId == null && mediaId == null) {
\r
359 //first delete all row with content_id=contentId
\r
360 String sql = "delete from " + theTable + " where content_id=" + contentId;
\r
362 Connection con = null;
\r
363 Statement stmt = null;
\r
365 con = getPooledCon();
\r
366 // should be a preparedStatement because is faster
\r
367 stmt = con.createStatement();
\r
368 int rs = executeUpdate(stmt, sql);
\r
370 catch (Exception e) {
\r
371 logger.error("-- set media failed -- delete");
\r
372 throw new StorageObjectFailure("-- set media failed -- delete ", e);
\r
375 freeConnection(con, stmt);
\r
379 //first delete all row with content_id=contentId
\r
381 sql = "insert into " + theTable + " (content_id,media_id) values ("
\r
382 + contentId + "," + mediaId + ")";
\r
384 con = getPooledCon();
\r
385 // should be a preparedStatement because is faster
\r
386 stmt = con.createStatement();
\r
387 int rs = executeUpdate(stmt, sql);
\r
389 catch (Exception e) {
\r
390 logger.error("-- set media failed -- insert");
\r
391 throw new StorageObjectFailure("-- set media failed -- insert ", e);
\r
394 freeConnection(con, stmt);
\r
398 public void deleteByContentId(String contentId) throws StorageObjectFailure {
\r
399 if (contentId == null) {
\r
400 //theLog.printDebugInfo("-- delete topics failed -- no content id");
\r
403 //delete all row with content_id=contentId
\r
404 String sql = "delete from " + theTable + " where content_id=" + contentId;
\r
406 Connection con = null;
\r
407 Statement stmt = null;
\r
409 con = getPooledCon();
\r
410 // should be a preparedStatement because is faster
\r
411 stmt = con.createStatement();
\r
412 int rs = executeUpdate(stmt, sql);
\r
414 catch (Exception e) {
\r
415 logger.error("-- delete by contentId failed ");
\r
416 throw new StorageObjectFailure(
\r
417 "-- delete by content id failed -- delete ", e);
\r
420 freeConnection(con, stmt);
\r
424 public void deleteByMediaId(String mediaId) throws StorageObjectFailure {
\r
425 if (mediaId == null) {
\r
426 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
\r
429 //delete all row with content_id=contentId
\r
430 String sql = "delete from " + theTable + " where media_id=" + mediaId;
\r
432 Connection con = null;
\r
433 Statement stmt = null;
\r
435 con = getPooledCon();
\r
436 // should be a preparedStatement because is faster
\r
437 stmt = con.createStatement();
\r
438 int rs = executeUpdate(stmt, sql);
\r
439 logger.debug("-- delete media success ");
\r
441 catch (Exception e) {
\r
442 logger.error("-- delete media failed ");
\r
443 throw new StorageObjectFailure("-- delete by media id failed -- ", e);
\r
446 freeConnection(con, stmt);
\r
450 public void delete(String contentId, String mediaId) throws
\r
451 StorageObjectFailure {
\r
452 if (mediaId == null || contentId == null) {
\r
453 logger.debug("-- delete media failed -- missing parameter");
\r
456 //delete all row with content_id=contentId and media_id=mediaId
\r
457 String sql = "delete from " + theTable + " where media_id=" + mediaId +
\r
458 " and content_id= " + contentId;
\r
460 Connection con = null;
\r
461 Statement stmt = null;
\r
463 con = getPooledCon();
\r
464 // should be a preparedStatement because is faster
\r
465 stmt = con.createStatement();
\r
466 int rs = executeUpdate(stmt, sql);
\r
467 logger.debug("-- delete content_x_media success ");
\r
469 catch (Exception e) {
\r
470 logger.error("-- delete content_x_media failed ");
\r
471 throw new StorageObjectFailure("-- delete content_x_media failed -- ", e);
\r
474 freeConnection(con, stmt);
\r
478 public EntityList getContent(EntityUploadedMedia media) throws
\r
479 StorageObjectFailure {
\r
480 EntityList returnList = null;
\r
481 if (media != null) {
\r
482 String id = media.getId();
\r
483 String select = "select content_id from " + theTable + " where media_id=" +
\r
486 // execute select statement
\r
487 Connection con = null;
\r
488 Statement stmt = null;
\r
490 con = getPooledCon();
\r
491 // should be a preparedStatement because is faster
\r
492 stmt = con.createStatement();
\r
493 ResultSet rs = executeSql(stmt, select);
\r
495 String mediaSelect = "id IN (";
\r
496 boolean first = true;
\r
497 while (rs.next()) {
\r
498 if (first == false)
\r
499 mediaSelect += ",";
\r
500 mediaSelect += rs.getString(1);
\r
503 mediaSelect += ")";
\r
504 if (first == false)
\r
505 returnList = DatabaseContent.getInstance().selectByWhereClause(
\r
509 catch (Exception e) {
\r
510 logger.error("-- get content failed");
\r
511 throw new StorageObjectFailure("-- get content failed -- ", e);
\r
514 freeConnection(con, stmt);
\r
521 * Returns a EntityList with all content-objects having a relation to a media
\r
524 public EntityList getContent() throws StorageObjectFailure {
\r
525 EntityList returnList = null;
\r
527 String select = "select distinct content_id from " + theTable;
\r
528 // execute select statement
\r
529 Connection con = null;
\r
530 Statement stmt = null;
\r
532 con = getPooledCon();
\r
533 // should be a preparedStatement because is faster
\r
534 stmt = con.createStatement();
\r
535 ResultSet rs = executeSql(stmt, select);
\r
537 String mediaSelect = "id IN (";
\r
538 boolean first = true;
\r
539 while (rs.next()) {
\r
540 if (first == false)
\r
541 mediaSelect += ",";
\r
542 mediaSelect += rs.getString(1);
\r
545 mediaSelect += ")";
\r
546 if (first == false)
\r
547 returnList = DatabaseContent.getInstance().selectByWhereClause(
\r
548 mediaSelect, "webdb_lastchange desc");
\r
551 catch (Exception e) {
\r
552 logger.error("-- get content failed");
\r
553 throw new StorageObjectFailure("-- get content failed -- ", e);
\r
556 freeConnection(con, stmt);
\r