X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmircoders%2Fstorage%2FDatabaseContentToTopics.java;h=eca6c293dcd3eade7f3e82517e0f5185b135e791;hb=4451d4c8c25d46e9c405e966ff6bd016a1512f4f;hp=d01861ca2aef16891a15bb7f31cdd1067978ffa4;hpb=1ba06c565ec314b8e189a25e7aead8b3a4ce3ad5;p=mir.git diff --git a/source/mircoders/storage/DatabaseContentToTopics.java b/source/mircoders/storage/DatabaseContentToTopics.java index d01861ca..eca6c293 100755 --- a/source/mircoders/storage/DatabaseContentToTopics.java +++ b/source/mircoders/storage/DatabaseContentToTopics.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2001, 2002 The Mir-coders group + * Copyright (C) 2001, 2002 The Mir-coders group * * This file is part of Mir. * @@ -18,15 +18,14 @@ * 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 the com.oreilly.servlet library, 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. + * 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; @@ -35,8 +34,10 @@ import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; -import java.util.Collection; +import java.util.Arrays; import java.util.Iterator; +import java.util.List; +import java.util.Vector; import mir.entity.EntityList; import mir.log.LoggerWrapper; @@ -56,28 +57,21 @@ public class DatabaseContentToTopics extends Database implements StorageObject{ private static DatabaseContentToTopics instance; - // 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 DatabaseContentToTopics getInstance() - throws StorageObjectFailure { + public synchronized static DatabaseContentToTopics getInstance() { if (instance == null) { instance = new DatabaseContentToTopics(); - instance.myselfDatabase = instance; } return instance; } - private DatabaseContentToTopics() throws StorageObjectFailure { + private DatabaseContentToTopics() { super(); logger = new LoggerWrapper("Database.ContentToTopics"); hasTimestamp = false; theTable="content_x_topic"; - try { this.theEntityClass = Class.forName("mir.entity.GenericEntity"); } - catch (Exception e) { throw new StorageObjectFailure(e); } - + theEntityClass = mir.entity.GenericEntity.class; } /** @@ -88,12 +82,14 @@ public class DatabaseContentToTopics extends Database implements StorageObject{ 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); + Vector extraTables = new Vector(); + extraTables.add(theTable+" 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()); @@ -106,7 +102,7 @@ public class DatabaseContentToTopics extends Database implements StorageObject{ * Returns a ArrayList of Integer-Objects from a content-id. * @returns ArrayList */ - public ArrayList getTopicsOfContent(String contentId) + public List getTopicsOfContent(String contentId) throws StorageObjectFailure { ArrayList returnList = new ArrayList(); @@ -134,104 +130,95 @@ public class DatabaseContentToTopics extends Database implements StorageObject{ return returnList; } - /** - * Set new topics - */ - public void setTopics(String contentId, String[] topicId) - throws StorageObjectFailure { - if (contentId == null){ - return; + private String getIdListExpression(List aList) { + String result = ""; + + Iterator i = aList.iterator(); + + while (i.hasNext()) { + result = result + i.next().toString(); + if (i.hasNext()) + result = result + ", "; } - if (topicId==null || topicId[0]==null) { - return; + return result; + } + + public void setTopics(String anArticleId, String [] aTopics) throws StorageObjectFailure { + 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(); + if (aTopics!=null) { + Iterator i = aTopics.iterator(); + + while (i.hasNext()) { + newTopics.add(new Integer(Integer.parseInt((String) i.next()))); + } } - //first check which topics this article has - Collection hasTopics = getTopicsOfContent(contentId); - Collection toSet = new ArrayList(); - Collection toDelete = new ArrayList(); - if(hasTopics!=null && hasTopics.size()>0){ - //now we check if there are new topics and copy them to an array. - for(int i = 0; i< topicId.length;i++){ - boolean set=false; - int whichTopic = 0; - for(Iterator it=hasTopics.iterator();it.hasNext();){ - Integer topic = (Integer)it.next(); - if(topicId[i].equals(topic.toString())){ - set=true; - } else { - whichTopic = i; - } - } - if(set==false){ - toSet.add(topicId[i]); - logger.debug("to set: "+ topicId[i]); - } + List currentTopics = getTopicsOfContent(anArticleId); + logger.debug("New topics = " + newTopics.toString()); + logger.debug("Current topics = " + currentTopics.toString()); + List topicsToDelete = new Vector(currentTopics); + topicsToDelete.removeAll(newTopics); + List topicsToAdd = new Vector(newTopics); + topicsToAdd.removeAll(currentTopics); + logger.debug("to delete = " + topicsToDelete.toString()); + logger.debug("to add = " + topicsToAdd.toString()); + + + if (!topicsToDelete.isEmpty()) { + String sql = + "delete from " + theTable + " " + + "where content_id=" + anArticleId + + " and topic_id in (" + getIdListExpression(topicsToDelete) + ")"; + + Connection connection=null; + Statement statement=null; + try { + connection = getPooledCon(); + statement = connection.createStatement(); + int rs = executeUpdate(statement, sql); } - //now we check if we have to delete topics - for(Iterator it=hasTopics.iterator();it.hasNext();){ - boolean delete=true; - int whichTopic = 0; - Integer topic = (Integer)it.next(); - for(int i = 0; i< topicId.length;i++){ - if(topicId[i].equals(topic.toString())){ - delete=false; - } else { - whichTopic = i; - } + catch (Exception e) { + logger.error("-- deleting topics failed"); + } + finally { + try { + freeConnection(connection, statement); } - if(delete==true){ - toDelete.add(topic.toString()); - logger.debug("to delete: "+ topic.toString()); + catch (Throwable t) { } } - } else { - //all the topics has to be set, so we copy all to the array - for (int i = 0; i < topicId.length; i++){ - toSet.add(topicId[i]); - } - } - - //first delete all row with content_id=contentId - String sql = "delete from "+ theTable +" where content_id=" + contentId - + " and topic_id in ("; - boolean first=false; - for(Iterator it = toDelete.iterator(); it.hasNext();){ - if(first==false){ - first=true; - } else { - sql+=","; - } - sql+= (String)it.next(); - } - sql+=")"; - Connection con=null;Statement stmt=null; - try { - con = getPooledCon(); - // should be a preparedStatement because is faster - stmt = con.createStatement(); - int rs = executeUpdate(stmt,sql); - } catch (Exception e) { - logger.error("-- deleting topics failed"); - } finally { - freeConnection(con,stmt); } - //now insert - //first delete all row with content_id=contentId - for (Iterator it = toSet.iterator(); it.hasNext();) { - sql = "insert into "+ theTable +" (content_id,topic_id) values (" - + contentId + "," + (String)it.next() + ")"; + Iterator i = topicsToAdd.iterator(); + while (i.hasNext()) { + Integer topicId = (Integer) i.next(); + String sql = + "insert into " + theTable + " (content_id, topic_id) "+ + "values (" + anArticleId + "," + topicId + ")"; + Connection connection=null; + Statement statement=null; try { - con = getPooledCon(); + connection = getPooledCon(); // should be a preparedStatement because is faster - stmt = con.createStatement(); - int rs = executeUpdate(stmt,sql); + statement = connection.createStatement(); + int rs = executeUpdate(statement, sql); } catch (Exception e) { - logger.error("-- set topics failed -- insert laenge topicId" + topicId.length); - } finally { - freeConnection(con,stmt); + logger.error("-- adding topics failed"); + } + finally { + try { + freeConnection(connection, statement); + } + catch (Throwable t) { + } } } } @@ -281,39 +268,28 @@ public class DatabaseContentToTopics extends Database implements StorageObject{ freeConnection(con,stmt); } } - - + +/** + * Returns list of Content for a specific topic + * @param topic + * @return EntityList + * @throws StorageObjectFailure + */ public EntityList getContent(EntityTopics topic) throws StorageObjectFailure { 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); - } + Vector extraTables = new Vector(); + extraTables.add(theTable+" 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; }