X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmircoders%2Fstorage%2FDatabaseContentToMedia.java;h=5807ea5b88c05c3780f5752e951704376a3e1902;hb=5757f37a1bc4e2dcad3054a66611b1a02fb7d8b3;hp=2f5b3b57305bb195564788dee91c66c13b569cbb;hpb=07406c649982d8496617dfbf92097a27705303c2;p=mir.git diff --git a/source/mircoders/storage/DatabaseContentToMedia.java b/source/mircoders/storage/DatabaseContentToMedia.java index 2f5b3b57..5807ea5b 100755 --- a/source/mircoders/storage/DatabaseContentToMedia.java +++ b/source/mircoders/storage/DatabaseContentToMedia.java @@ -23,7 +23,10 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ private static DatabaseContentToMedia instance; - public static DatabaseContentToMedia getInstance() + // the following *has* to be sychronized cause this static method + // could get preemted and we could end up with 2 instances of DatabaseFoo. + // see the "Singletons with needles and thread" article at JavaWorld -mh + public synchronized static DatabaseContentToMedia getInstance() throws StorageObjectException { if (instance == null) { instance = new DatabaseContentToMedia(); @@ -38,13 +41,16 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ super(); this.hasTimestamp = false; this.theTable="content_x_media"; + try { this.theEntityClass = Class.forName("mir.entity.GenericEntity"); } + catch (Exception e) { throw new StorageObjectException(e.toString()); } } - + /** * get all the media-files belonging to a content entity * */ - public EntityList getMedia(EntityContent content) { + public EntityList getMedia(EntityContent content) + throws StorageObjectException { EntityList returnList=null; if (content != null) { // get all to_topic from media_x_topic @@ -56,16 +62,86 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,-1); } catch (Exception e) { theLog.printDebugInfo("-- get media failed " + e.toString()); + throw new StorageObjectException("-- get media failed " + e.toString()); + } + } + return returnList; + } + + public boolean hasMedia(EntityContent content) + throws StorageObjectException { + String wc = "content_id="+content.getId(); + if( content != null) { + try { + if(selectByWhereClause(wc,-1).size() == 0) + return false; + else + return true; + } catch (Exception e) { + theLog.printError("-- hasMedia failed " + e.toString()); + throw new StorageObjectException("-- hasMedia failed " + e.toString()); + } + } else { + theLog.printError("-- hasMedia failed: content is NULL"); + throw new StorageObjectException("-- hasMedia failed: content is NULL"); + } + } + + + + + /** + * get all the audio belonging to a content entity + * + */ + public EntityList getAudio(EntityContent content) + throws StorageObjectException { + EntityList returnList=null; + if (content != null) { + // get all to_topic from media_x_topic + String id = content.getId(); + //this is not supported by mysql + String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")"; + + try { + returnList = DatabaseAudio.getInstance().selectByWhereClause(subselect,-1); + } catch (Exception e) { + theLog.printDebugInfo("-- get audio failed " + e.toString()); + throw new StorageObjectException("-- get audio failed " + e.toString()); + } + } + return returnList; + } + + /** + * get all the video belonging to a content entity + * + */ + public EntityList getVideo(EntityContent content) + throws StorageObjectException { + EntityList returnList=null; + if (content != null) { + // get all to_topic from media_x_topic + String id = content.getId(); + //this is not supported by mysql + String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")"; + + try { + returnList = DatabaseVideo.getInstance().selectByWhereClause(subselect,-1); + } catch (Exception e) { + theLog.printDebugInfo("-- get video failed " + e.toString()); + throw new StorageObjectException("-- get video failed " + e.toString()); } } return returnList; } - + /** * get all the images belonging to a content entity * */ - public EntityList getImages(EntityContent content) { + public EntityList getImages(EntityContent content) + throws StorageObjectException { EntityList returnList=null; if (content != null) { // get all to_topic from media_x_topic @@ -77,13 +153,76 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ returnList = DatabaseImages.getInstance().selectByWhereClause(subselect,-1); } catch (Exception e) { theLog.printDebugInfo("-- get images failed " + e.toString()); + throw new StorageObjectException("-- get images failed " + e.toString()); + } + } + return returnList; + } + + + /** + * get all the uploaded/other Media belonging to a content entity + * + */ + public EntityList getOther(EntityContent content) + throws StorageObjectException + { + /** @todo this should only fetch published media / rk */ + + EntityList returnList=null; + if (content != null) { + // get all to_topic from media_x_topic + String id = content.getId(); + //this is not supported by mysql + String subselect = "id in (select media_id from " + theTable + + " where content_id=" + id+")"; + + try { + returnList = DatabaseOther.getInstance().selectByWhereClause(subselect, + "id"); + } catch (Exception e) { + e.printStackTrace(); + theLog.printDebugInfo("-- get Other failed " + e.toString()); + throw new StorageObjectException("-- get Other failed " + + e.toString()); + } + } + return returnList; + } + + /** + * get all the uploaded/other Media belonging to a content entity + * + */ + public EntityList getUploadedMedia(EntityContent content) + throws StorageObjectException + { + /** @todo this should only fetch published media / rk */ + + EntityList returnList=null; + if (content != null) { + // get all to_topic from media_x_topic + String id = content.getId(); + //this is not supported by mysql + String subselect = "id in (select media_id from " + theTable + + " where content_id=" + id+")"; + + try { + returnList = DatabaseUploadedMedia.getInstance().selectByWhereClause(subselect, + "id"); + } catch (Exception e) { + e.printStackTrace(); + theLog.printDebugInfo("-- get uploadedMedia failed " + e.toString()); + throw new StorageObjectException("-- get uploadedMedia failed " + + e.toString()); } } return returnList; } - public void setMedia(String contentId, String[] mediaId) { + public void setMedia(String contentId, String[] mediaId) + throws StorageObjectException { if (contentId == null){ return; } @@ -92,7 +231,7 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ } //first delete all row with content_id=contentId String sql = "delete from "+ theTable +" where content_id=" + contentId; - + Connection con=null;Statement stmt=null; try { con = getPooledCon(); @@ -101,10 +240,11 @@ public class DatabaseContentToMedia extends Database implements StorageObject{ ResultSet rs = executeSql(stmt,sql); } catch (Exception e) { theLog.printDebugInfo("-- set media failed -- delete"); + throw new StorageObjectException("-- set media failed -- delete"+e.toString()); } finally { freeConnection(con,stmt); } - + //now insert //first delete all row with content_id=contentId for (int i=0;i