X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmircoders%2Fstorage%2FDatabaseContentToTopics.java;h=f1d4633ba035b91a154825c8937e1a12a0511065;hb=c9ac8fa71b679f8d967aac901bbef945c13b94c9;hp=52e6dbd27a20e27a29705db0235a36a46a9a3aea;hpb=a459f111d85598df56c6ab711cec11632676b39c;p=mir.git diff --git a/source/mircoders/storage/DatabaseContentToTopics.java b/source/mircoders/storage/DatabaseContentToTopics.java index 52e6dbd2..f1d4633b 100755 --- a/source/mircoders/storage/DatabaseContentToTopics.java +++ b/source/mircoders/storage/DatabaseContentToTopics.java @@ -18,18 +18,25 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * In addition, as a special exception, The Mir-coders gives permission to link - * the code of this program with any library licensed under the Apache Software License, - * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library - * (or with modified versions of the above that use the same license as the above), - * and distribute linked combinations including the two. You must obey the - * GNU General Public License in all respects for all of the code used other than - * the above mentioned libraries. If you modify this file, you may extend this - * exception to your version of the file, but you are not obligated to do so. + * the code of this program with any library licensed under the Apache Software License, + * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library + * (or with modified versions of the above that use the same license as the above), + * and distribute linked combinations including the two. You must obey the + * GNU General Public License in all respects for all of the code used other than + * the above mentioned libraries. If you modify this file, you may extend this + * exception to your version of the file, but you are not obligated to do so. * If you do not wish to do so, delete this exception statement from your version. */ package mircoders.storage; +import mir.entity.EntityList; +import mir.log.LoggerWrapper; +import mir.storage.Database; +import mir.storage.DatabaseFailure; +import mircoders.entity.EntityContent; +import mircoders.entity.EntityTopics; + import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; @@ -37,15 +44,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; -import java.util.Vector; - -import mir.entity.EntityList; -import mir.log.LoggerWrapper; -import mir.storage.Database; -import mir.storage.StorageObject; -import mir.storage.StorageObjectFailure; -import mircoders.entity.EntityContent; -import mircoders.entity.EntityTopics; /** * This class implements the 1-n-relation between @@ -53,18 +51,13 @@ import mircoders.entity.EntityTopics; * */ -public class DatabaseContentToTopics extends Database implements StorageObject{ +public class DatabaseContentToTopics extends Database { private static DatabaseContentToTopics instance; - public static DatabaseContentToTopics getInstance() { + public synchronized static DatabaseContentToTopics getInstance() { if (instance == null) { - synchronized (DatabaseContentToTopics.class) { - if (instance == null) { - instance = new DatabaseContentToTopics(); - instance.myselfDatabase = instance; - } - } + instance = new DatabaseContentToTopics(); } return instance; } @@ -73,26 +66,24 @@ public class DatabaseContentToTopics extends Database implements StorageObject{ super(); logger = new LoggerWrapper("Database.ContentToTopics"); - - hasTimestamp = false; - theTable="content_x_topic"; - theEntityClass = mir.entity.GenericEntity.class; + mainTable="content_x_topic"; + entityClass = mir.entity.GenericEntity.class; } /** * This class return an EntityList of Topics - * @param EntityContent content - * @returns EntityList */ public EntityList getTopics(EntityContent content) { EntityList returnList=null; if (content != null) { - // get all to_topic from content_x_topic - String id = content.getId(); - String subselect = "id in (select topic_id from " + theTable + " where content_id=" + id+")"; + String id = content.getId(); try { - returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1); + ArrayList extraTables = new ArrayList(); + extraTables.add(mainTable+" cxt"); + returnList = DatabaseTopics.getInstance() + .selectByWhereClauseWithExtraTables("t",extraTables, + "t.id=cxt.topic_id and cxt.content_id="+id ); } catch (Exception e) { logger.error("-- get topics failed " + e.toString()); @@ -102,24 +93,24 @@ public class DatabaseContentToTopics extends Database implements StorageObject{ } /** - * Returns a ArrayList of Integer-Objects from a content-id. - * @returns ArrayList + * Returns a List of String-Objects from a content-id. */ public List getTopicsOfContent(String contentId) - throws StorageObjectFailure { + throws DatabaseFailure { ArrayList returnList = new ArrayList(); if (contentId != null) { - String sql = "select topic_id from " + theTable + " where content_id=" + contentId; + String sql = "select topic_id from " + mainTable + " where content_id=" + contentId; Connection con=null;Statement stmt=null; try { - con = getPooledCon(); + con = obtainConnection(); + // should be a preparedStatement because is faster stmt = con.createStatement(); ResultSet rs = executeSql(stmt,sql); if(rs!=null){ while(rs.next()){ - returnList.add(new Integer(rs.getInt("topic_id"))); + returnList.add(Integer.toString(rs.getInt("topic_id"))); } } } @@ -146,15 +137,15 @@ public class DatabaseContentToTopics extends Database implements StorageObject{ return result; } - public void setTopics(String anArticleId, String [] aTopics) throws StorageObjectFailure { + public void setTopics(String anArticleId, String [] aTopics) throws DatabaseFailure { if (aTopics==null) setTopics(anArticleId, (List) null); else setTopics(anArticleId, Arrays.asList(aTopics)); } - public void setTopics(String anArticleId, List aTopics) throws StorageObjectFailure { - List newTopics = new Vector(); + public void setTopics(String anArticleId, List aTopics) throws DatabaseFailure { + List newTopics = new ArrayList(); if (aTopics!=null) { Iterator i = aTopics.iterator(); @@ -166,9 +157,9 @@ public class DatabaseContentToTopics extends Database implements StorageObject{ List currentTopics = getTopicsOfContent(anArticleId); logger.debug("New topics = " + newTopics.toString()); logger.debug("Current topics = " + currentTopics.toString()); - List topicsToDelete = new Vector(currentTopics); + List topicsToDelete = new ArrayList(currentTopics); topicsToDelete.removeAll(newTopics); - List topicsToAdd = new Vector(newTopics); + List topicsToAdd = new ArrayList(newTopics); topicsToAdd.removeAll(currentTopics); logger.debug("to delete = " + topicsToDelete.toString()); logger.debug("to add = " + topicsToAdd.toString()); @@ -176,16 +167,16 @@ public class DatabaseContentToTopics extends Database implements StorageObject{ if (!topicsToDelete.isEmpty()) { String sql = - "delete from " + theTable + " " + + "delete from " + mainTable + " " + "where content_id=" + anArticleId + " and topic_id in (" + getIdListExpression(topicsToDelete) + ")"; Connection connection=null; Statement statement=null; try { - connection = getPooledCon(); + connection = obtainConnection(); statement = connection.createStatement(); - int rs = executeUpdate(statement, sql); + executeUpdate(statement, sql); } catch (Exception e) { logger.error("-- deleting topics failed"); @@ -203,15 +194,15 @@ public class DatabaseContentToTopics extends Database implements StorageObject{ while (i.hasNext()) { Integer topicId = (Integer) i.next(); String sql = - "insert into " + theTable + " (content_id, topic_id) "+ + "insert into " + mainTable + " (content_id, topic_id) "+ "values (" + anArticleId + "," + topicId + ")"; Connection connection=null; Statement statement=null; try { - connection = getPooledCon(); + connection = obtainConnection(); // should be a preparedStatement because is faster statement = connection.createStatement(); - int rs = executeUpdate(statement, sql); + executeUpdate(statement, sql); } catch (Exception e) { logger.error("-- adding topics failed"); @@ -227,20 +218,20 @@ public class DatabaseContentToTopics extends Database implements StorageObject{ } public void deleteByContentId(String contentId) - throws StorageObjectFailure { + throws DatabaseFailure { if (contentId == null) { //theLog.printDebugInfo("-- delete topics failed -- no content id"); return; } //delete all row with content_id=contentId - String sql = "delete from "+ theTable +" where content_id=" + contentId; + String sql = "delete from "+ mainTable +" where content_id=" + contentId; Connection con=null;Statement stmt=null; try { - con = getPooledCon(); + con = obtainConnection(); // should be a preparedStatement because is faster stmt = con.createStatement(); - ResultSet rs = executeSql(stmt,sql); + executeSql(stmt,sql); } catch (Exception e) { //theLog.printDebugInfo("-- delete topics failed "); } finally { @@ -249,20 +240,20 @@ public class DatabaseContentToTopics extends Database implements StorageObject{ } public void deleteByTopicId(String topicId) - throws StorageObjectFailure { + throws DatabaseFailure { if (topicId == null) { //theLog.printDebugInfo("-- delete topics failed -- no topic id"); return; } //delete all row with content_id=contentId - String sql = "delete from "+ theTable +" where topic_id=" + topicId; + String sql = "delete from "+ mainTable +" where topic_id=" + topicId; Connection con=null;Statement stmt=null; try { - con = getPooledCon(); + con = obtainConnection(); // should be a preparedStatement because is faster stmt = con.createStatement(); - ResultSet rs = executeSql(stmt,sql); + executeSql(stmt,sql); } catch (Exception e) { logger.error("-- delete topics failed "); @@ -272,38 +263,27 @@ public class DatabaseContentToTopics extends Database implements StorageObject{ } } - +/** + * Returns list of Content for a specific topic + * @param topic + * @return EntityList + * @throws DatabaseFailure + */ public EntityList getContent(EntityTopics topic) - throws StorageObjectFailure { + throws DatabaseFailure { EntityList returnList=null; if (topic != null) { String id = topic.getId(); - String select = "select content_id from " + theTable + " where topic_id=" + id; - - // execute select statement - Connection con=null;Statement stmt=null; try { - con = getPooledCon(); - // should be a preparedStatement because is faster - stmt = con.createStatement(); - ResultSet rs = executeSql(stmt,select); - if (rs!=null) { - String topicSelect= "id IN ("; - boolean first=true; - while (rs.next()) { - if (first==false) topicSelect+=","; - topicSelect += rs.getString(1); - first=false; - } - topicSelect+=")"; - if (first==false) - returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1); - } + ArrayList extraTables = new ArrayList(); + extraTables.add(mainTable+" cxt"); + returnList = DatabaseContent.getInstance() + .selectByWhereClauseWithExtraTables("c",extraTables, + "c.id=cxt.content_id and cxt.topic_id="+id ); } catch (Exception e) { - logger.error("-- get contetn failed"); + logger.error("-- get content failed"); } - finally { freeConnection(con,stmt);} } return returnList; }