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.EntityComment;
+import mir.storage.DatabaseFailure;
import mircoders.entity.EntityUploadedMedia;
-/**
- * <b>implements abstract DB connection to the comment_x_media SQL table
- *
- * @author RK, mir-coders group
- * @version $Id: DatabaseCommentToMedia.java,v 1.3.2.5 2003/11/26 19:23:16 rk Exp $
- *
- */
-
-public class DatabaseCommentToMedia extends Database implements StorageObject{
+public class DatabaseCommentToMedia extends Database {
private static DatabaseCommentToMedia instance;
private DatabaseCommentToMedia() {
super();
- logger = new LoggerWrapper("Database.CommentToMedia");
- hasTimestamp = false;
- theTable = "comment_x_media";
- theEntityClass = mir.entity.GenericEntity.class;
- }
-
- public boolean hasMedia(EntityComment comment) throws StorageObjectFailure,
- StorageObjectExc {
- if (comment != null) {
- try {
- return (getSize("comment_id=" + comment.getId()) == 0) ? false:true;
- }
- catch (Exception e) {
- logger.error("DatabaseCommentToMedia.hasMedia: " + e.toString());
- throw new StorageObjectFailure("DatabaseCommentToMedia.hasMedia: " +
- e.toString(), e);
- }
- }
- else {
- logger.error("DatabaseCommentToMedia.hasMedia: comment == null");
- throw new StorageObjectExc(
- "DatabaseCommentToMedia.hasMedia: comment == null");
- }
- }
-
- private EntityList getMultiMediaForComment(StorageObject store, EntityComment comment)
- throws StorageObjectFailure {
-
- EntityList returnList = null;
- if (comment != null) {
- String id = comment.getId();
- try {
- Vector extraTable = new Vector();
- extraTable.add(theTable+" 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.comment_id="+id, "id", -1, -1);
-
- } catch (Exception e) {
- logger.error("DatabaseCommenttToMedia.getMultiMediaForComment: " + e.toString());
- throw new StorageObjectFailure("DatabaseContentToMedia.etMultiMediaForComment: " +
- e.toString(), e);
- }
- }
- return returnList;
- }
-
- /**
- * get all the audio belonging to a comment entity
- */
- public EntityList getAudio(EntityComment comment) throws StorageObjectFailure {
- return getMultiMediaForComment(DatabaseAudio.getInstance(), comment);
- }
-
- /**
- * get all the video belonging to a comment entity
- *
- */
- public EntityList getVideo(EntityComment comment) throws StorageObjectFailure {
- return getMultiMediaForComment(DatabaseVideo.getInstance(), comment);
- }
-
- /**
- * get all the images belonging to a comment entity
- */
- public EntityList getImages(EntityComment comment) throws StorageObjectFailure {
- return getMultiMediaForComment(DatabaseImages.getInstance(), comment);
- }
-
- /**
- * get all the uploaded/other Media belonging to a comment entity
- */
- public EntityList getOther(EntityComment comment) throws StorageObjectFailure {
- return getMultiMediaForComment(DatabaseOther.getInstance(), comment);
- }
-
- /**
- * get all the uploaded/other Media belonging to a comment entity
- */
- public EntityList getUploadedMedia(EntityComment comment) throws StorageObjectFailure {
- return getMultiMediaForComment(DatabaseUploadedMedia.getInstance(), comment);
- }
-
- public void setMedia(String commentId, String[] mediaId) throws
- StorageObjectFailure {
- if (commentId == null) {
- return;
- }
- if (mediaId == null || mediaId[0] == null) {
- return;
- }
- //first delete all row with comment_id=commentId
- String sql = "delete from " + theTable + " where comment_id=" + commentId;
-
- Connection con = null;
- Statement stmt = null;
- try {
- con = getPooledCon();
- // 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 comment_id=commentId
- for (int i = 0; i < mediaId.length; i++) {
- sql = "insert into " + theTable + " (comment_id,media_id) values ("
- + commentId + "," + mediaId[i] + ")";
- try {
- con = getPooledCon();
- // 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);
- }
- }
+ logger = new LoggerWrapper("Database.CommentToMedia");
+ mainTable = "comment_x_media";
+ entityClass = mir.entity.GenericEntity.class;
}
public void addMedia(String commentId, String mediaId) throws
- StorageObjectFailure {
+ DatabaseFailure {
if (commentId == null && mediaId == null) {
return;
}
Statement stmt = null;
//now insert
- String sql = "insert into " + theTable + " (comment_id,media_id) values ("
+ String sql = "insert into " + mainTable + " (comment_id,media_id) values ("
+ commentId + "," + mediaId + ")";
try {
- con = getPooledCon();
+ con = obtainConnection();
// should be a preparedStatement because is faster
stmt = con.createStatement();
int rs = executeUpdate(stmt, sql);
}
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);
}
public void setMedia(String commentId, String mediaId) throws
- StorageObjectFailure {
+ DatabaseFailure {
if (commentId == null && mediaId == null) {
return;
}
//first delete all row with comment_id=commentId
- String sql = "delete from " + theTable + " where comment_id=" + commentId;
+ String sql = "delete from " + mainTable + " where comment_id=" + commentId;
Connection con = null;
Statement stmt = null;
try {
- con = getPooledCon();
+ con = obtainConnection();
// should be a preparedStatement because is faster
stmt = con.createStatement();
int rs = executeUpdate(stmt, sql);
}
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);
//now insert
//first delete all row with comment_id=commentId
- sql = "insert into " + theTable + " (comment_id,media_id) values ("
+ sql = "insert into " + mainTable + " (comment_id,media_id) values ("
+ commentId + "," + mediaId + ")";
try {
- con = getPooledCon();
+ con = obtainConnection();
// should be a preparedStatement because is faster
stmt = con.createStatement();
int rs = executeUpdate(stmt, sql);
}
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 deleteByCommentId(String commentId) throws StorageObjectFailure {
+ public void deleteByCommentId(String commentId) throws DatabaseFailure {
if (commentId == null) {
//theLog.printDebugInfo("-- delete topics failed -- no comment id");
return;
}
//delete all row with comment_id=commentId
- String sql = "delete from " + theTable + " where comment_id=" + commentId;
+ String sql = "delete from " + mainTable + " where comment_id=" + commentId;
Connection con = null;
Statement stmt = null;
try {
- con = getPooledCon();
+ con = obtainConnection();
// should be a preparedStatement because is faster
stmt = con.createStatement();
int rs = executeUpdate(stmt, sql);
}
catch (Exception e) {
logger.error("-- delete by commentId failed ");
- throw new StorageObjectFailure(
+ throw new DatabaseFailure(
"-- delete by comment id failed -- delete ", e);
}
finally {
}
}
- 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");
return;
}
//delete all row with comment_id=commentId
- String sql = "delete from " + theTable + " where media_id=" + mediaId;
+ String sql = "delete from " + mainTable + " where media_id=" + mediaId;
Connection con = null;
Statement stmt = null;
try {
- con = getPooledCon();
+ con = obtainConnection();
// should be a preparedStatement because is faster
stmt = con.createStatement();
int rs = executeUpdate(stmt, sql);
}
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);
}
public void delete(String commentId, String mediaId) throws
- StorageObjectFailure {
+ DatabaseFailure {
if (mediaId == null || commentId == null) {
logger.debug("-- delete media failed -- missing parameter");
return;
}
//delete all row with comment_id=commentId and media_id=mediaId
- String sql = "delete from " + theTable + " where media_id=" + mediaId +
+ String sql = "delete from " + mainTable + " where media_id=" + mediaId +
" and comment_id= " + commentId;
Connection con = null;
Statement stmt = null;
try {
- con = getPooledCon();
+ con = obtainConnection();
// should be a preparedStatement because is faster
stmt = con.createStatement();
int rs = executeUpdate(stmt, sql);
}
catch (Exception e) {
logger.error("-- delete comment_x_media failed ");
- throw new StorageObjectFailure("-- delete comment_x_media failed -- ", e);
+ throw new DatabaseFailure("-- delete comment_x_media failed -- ", e);
}
finally {
freeConnection(con, stmt);
}
public EntityList getComment(EntityUploadedMedia media) throws
- StorageObjectFailure {
-
+ DatabaseFailure {
+
EntityList returnList = null;
if (media != null) {
String id = media.getId();
- Vector extraTables = new Vector();
- extraTables.add(theTable + " cxm");
-
+ ArrayList extraTables = new ArrayList();
+ extraTables.add(mainTable + " cxm");
+
String mediaSelect = "cxm.comment_id=c.id and cxm.media_id="+id;
try {
returnList = DatabaseComment.getInstance().selectByWhereClause("c",
extraTables, mediaSelect, "c.id" );
-
+
}
catch (Exception e) {
logger.error("-- get comment failed");
- throw new StorageObjectFailure("-- get comment failed -- ", e);
+ throw new DatabaseFailure("-- get comment failed -- ", e);
}
}
return returnList;
}
/**
- * Returns a EntityList with all comment-objects having
+ * Returns a EntityList with all comment-objects having
* a relation to a media
*/
- public EntityList getComment() throws StorageObjectFailure {
+ public EntityList getComment() throws DatabaseFailure {
EntityList returnList = null;
-
- Vector extraTables = new Vector();
- extraTables.add(theTable + " cxm");
-
+
+ ArrayList extraTables = new ArrayList();
+ extraTables.add(mainTable + " cxm");
+
String mediaSelect = "cxm.comment_id=c.id";
try {
returnList = DatabaseComment.getInstance().selectByWhereClause("c",
extraTables, mediaSelect, "c.webdb_lastchange desc" );
-
+
}
catch (Exception e) {
logger.error("-- get comment failed");
- throw new StorageObjectFailure("-- get comment failed -- ", e);
+ throw new DatabaseFailure("-- get comment failed -- ", e);
}
return returnList;