X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmircoders%2Fstorage%2FDatabaseContentToMedia.java;h=11a2b357449314d057a4a661987db1a6cf553673;hb=d06e23e638f2538f263af76bd32da6b140f20ac6;hp=14a4a53214726b72033519ec2e2e53a9a7f8b4f5;hpb=63e0ee1fb8038eb6d8f0190cf38c3b3ab2727216;p=mir.git diff --git a/source/mircoders/storage/DatabaseContentToMedia.java b/source/mircoders/storage/DatabaseContentToMedia.java index 14a4a532..11a2b357 100755 --- a/source/mircoders/storage/DatabaseContentToMedia.java +++ b/source/mircoders/storage/DatabaseContentToMedia.java @@ -31,28 +31,24 @@ package mircoders.storage; import java.sql.Connection; -import java.sql.ResultSet; import java.sql.Statement; -import java.util.Vector; +import java.util.ArrayList; import mir.entity.EntityList; import mir.log.LoggerWrapper; import mir.storage.Database; -import mir.storage.StorageObject; -import mir.storage.StorageObjectExc; -import mir.storage.StorageObjectFailure; -import mircoders.entity.EntityContent; +import mir.storage.DatabaseFailure; import mircoders.entity.EntityUploadedMedia; /** * implements abstract DB connection to the content_x_media SQL table * * @author RK, mir-coders group - * @version $Id: DatabaseContentToMedia.java,v 1.19.2.6 2004/11/21 22:07:14 zapata Exp $ + * @version $Id: DatabaseContentToMedia.java,v 1.19.2.10 2005/03/26 11:26:28 zapata Exp $ * */ -public class DatabaseContentToMedia extends Database implements StorageObject{ +public class DatabaseContentToMedia extends Database { private static DatabaseContentToMedia instance; @@ -67,136 +63,12 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ super(); logger = new LoggerWrapper("Database.ContentToMedia"); - hasTimestamp = false; mainTable = "content_x_media"; - theEntityClass = mir.entity.GenericEntity.class; - } - - public boolean hasMedia(EntityContent content) throws StorageObjectFailure, - StorageObjectExc { - if (content != null) { - try { - return (getSize("content_id=" + content.getId()) == 0) ? false:true; - } - catch (Exception e) { - logger.error("DatabaseContentToMedia.hasMedia: " + e.toString()); - throw new StorageObjectFailure("DatabaseContentToMedia.hasMedia: " + - e.toString(), e); - } - } - else { - logger.error("DatabaseContentToMedia.hasMedia: content == null"); - throw new StorageObjectExc( - "DatabaseContentToMedia.hasMedia: content == null"); - } - } - - private EntityList getMultiMediaForContent(StorageObject store, EntityContent content) - throws StorageObjectFailure { - - EntityList returnList = null; - if (content != null) { - String id = content.getId(); - try { - Vector extraTable = new Vector(); - extraTable.add(mainTable+" cxm"); - // media should stay in uploaded order. this is especially important - // for photo stories which require a specific chronologic order. - // this is why we have the the second parameter "id" - store.selectByWhereClause("m", extraTable, - "m.id=cxm.media_id and cxm.content_id="+id, "id", -1, -1); - - } catch (Exception e) { - logger.error("DatabaseContentToMedia.getMultiMediaForContent: " + e.toString()); - throw new StorageObjectFailure("DatabaseContentToMedia.etMultiMediaForContent: " + - e.toString(), e); - } - } - return returnList; - } - - /** - * get all the audio belonging to a content entity - */ - public EntityList getAudio(EntityContent content) throws StorageObjectFailure { - return getMultiMediaForContent(DatabaseAudio.getInstance(), content); - } - - /** - * get all the video belonging to a content entity - */ - public EntityList getVideo(EntityContent content) throws StorageObjectFailure { - return getMultiMediaForContent(DatabaseVideo.getInstance(), content); - } - - /** - * get all the images belonging to a content entity - */ - public EntityList getImages(EntityContent content) throws StorageObjectFailure { - return getMultiMediaForContent(DatabaseImages.getInstance(), content); - } - - /** - * get all the uploaded/other Media belonging to a content entity - */ - public EntityList getOther(EntityContent content) throws StorageObjectFailure { - return getMultiMediaForContent(DatabaseOther.getInstance(), content); - } - - /** - * get all the uploaded/other Media belonging to a content entity - */ - public EntityList getUploadedMedia(EntityContent content) throws StorageObjectFailure { - return getMultiMediaForContent(DatabaseUploadedMedia.getInstance(), content); - } - - - public void setMedia(String contentId, String[] mediaId) throws StorageObjectFailure { - if (contentId == null || mediaId == null || mediaId[0] == null) { - return; - } - //first delete all row with content_id=contentId - String sql = "delete from " + mainTable + " where content_id=" + contentId; - - Connection con = null; - Statement stmt = null; - try { - con = obtainConnection(); - // should be a preparedStatement because is faster - stmt = con.createStatement(); - ResultSet rs = executeSql(stmt, sql); - } - catch (Exception e) { - logger.error("-- set media failed -- delete"); - throw new StorageObjectFailure("-- set media failed -- delete", e); - } - finally { - freeConnection(con, stmt); - } - - //now insert - //first delete all row with content_id=contentId - for (int i = 0; i < mediaId.length; i++) { - sql = "insert into " + mainTable + " (content_id,media_id) values (" - + contentId + "," + mediaId[i] + ")"; - try { - con = obtainConnection(); - // should be a preparedStatement because is faster - stmt = con.createStatement(); - int rs = executeUpdate(stmt, sql); - } - catch (Exception e) { - logger.error("-- set topics failed -- insert"); - throw new StorageObjectFailure("-- set topics failed -- insert ", e); - } - finally { - freeConnection(con, stmt); - } - } + entityClass = mir.entity.GenericEntity.class; } public void addMedia(String contentId, String mediaId) throws - StorageObjectFailure { + DatabaseFailure { if (contentId == null && mediaId == null) { return; } @@ -215,7 +87,7 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ } catch (Exception e) { logger.error("-- add media failed -- insert"); - throw new StorageObjectFailure("-- add media failed -- insert ", e); + throw new DatabaseFailure("-- add media failed -- insert ", e); } finally { freeConnection(con, stmt); @@ -223,7 +95,7 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ } public void setMedia(String contentId, String mediaId) throws - StorageObjectFailure { + DatabaseFailure { if (contentId == null && mediaId == null) { return; } @@ -240,7 +112,7 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ } catch (Exception e) { logger.error("-- set media failed -- delete"); - throw new StorageObjectFailure("-- set media failed -- delete ", e); + throw new DatabaseFailure("-- set media failed -- delete ", e); } finally { freeConnection(con, stmt); @@ -259,14 +131,14 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ } catch (Exception e) { logger.error("-- set media failed -- insert"); - throw new StorageObjectFailure("-- set media failed -- insert ", e); + throw new DatabaseFailure("-- set media failed -- insert ", e); } finally { freeConnection(con, stmt); } } - public void deleteByContentId(String contentId) throws StorageObjectFailure { + public void deleteByContentId(String contentId) throws DatabaseFailure { if (contentId == null) { //theLog.printDebugInfo("-- delete topics failed -- no content id"); return; @@ -284,7 +156,7 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ } catch (Exception e) { logger.error("-- delete by contentId failed "); - throw new StorageObjectFailure( + throw new DatabaseFailure( "-- delete by content id failed -- delete ", e); } finally { @@ -292,7 +164,7 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ } } - public void deleteByMediaId(String mediaId) throws StorageObjectFailure { + public void deleteByMediaId(String mediaId) throws DatabaseFailure { if (mediaId == null) { //theLog.printDebugInfo("-- delete topics failed -- no topic id"); @@ -312,7 +184,7 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ } catch (Exception e) { logger.error("-- delete media failed "); - throw new StorageObjectFailure("-- delete by media id failed -- ", e); + throw new DatabaseFailure("-- delete by media id failed -- ", e); } finally { freeConnection(con, stmt); @@ -320,7 +192,7 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ } public void delete(String contentId, String mediaId) throws - StorageObjectFailure { + DatabaseFailure { if (mediaId == null || contentId == null) { logger.debug("-- delete media failed -- missing parameter"); return; @@ -340,7 +212,7 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ } catch (Exception e) { logger.error("-- delete content_x_media failed "); - throw new StorageObjectFailure("-- delete content_x_media failed -- ", e); + throw new DatabaseFailure("-- delete content_x_media failed -- ", e); } finally { freeConnection(con, stmt); @@ -348,13 +220,13 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ } public EntityList getContent(EntityUploadedMedia media) throws - StorageObjectFailure { + DatabaseFailure { EntityList returnList = null; if (media != null) { String id = media.getId(); - Vector extraTables = new Vector(); + ArrayList extraTables = new ArrayList(); extraTables.add(mainTable + " cxm"); String mediaSelect = "cxm.content_id=c.id and cxm.media_id="+id; @@ -365,7 +237,7 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ } catch (Exception e) { logger.error("-- get content failed"); - throw new StorageObjectFailure("-- get content failed -- ", e); + throw new DatabaseFailure("-- get content failed -- ", e); } } return returnList; @@ -375,11 +247,11 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ * Returns a EntityList with all content-objects having a relation to a media */ - public EntityList getContent() throws StorageObjectFailure { + public EntityList getContent() throws DatabaseFailure { EntityList returnList = null; - Vector extraTables = new Vector(); + ArrayList extraTables = new ArrayList(); extraTables.add(mainTable + " cxm"); String mediaSelect = "cxm.content_id=c.id"; @@ -390,7 +262,7 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ } catch (Exception e) { logger.error("-- get content failed"); - throw new StorageObjectFailure("-- get content failed -- ", e); + throw new DatabaseFailure("-- get content failed -- ", e); } return returnList; }