From 2ec0f0d13403d64d8b7c64a6b6cb21a4537158f1 Mon Sep 17 00:00:00 2001 From: idfx Date: Fri, 26 Oct 2001 22:13:25 +0000 Subject: [PATCH] Cleaned up DatabaseContentToTopics.java. The set-method now only deletes the entries that have to be deleted and sets only the entries have to be set. So it is the first step to a more intelligent ProducerTopic-Method. --- .../mircoders/storage/DatabaseContentToTopics.java | 150 ++++++++++++++------- 1 file changed, 101 insertions(+), 49 deletions(-) diff --git a/source/mircoders/storage/DatabaseContentToTopics.java b/source/mircoders/storage/DatabaseContentToTopics.java index 7201f947..d00fa861 100755 --- a/source/mircoders/storage/DatabaseContentToTopics.java +++ b/source/mircoders/storage/DatabaseContentToTopics.java @@ -14,8 +14,8 @@ import mir.misc.*; import mircoders.entity.*; /** - * Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle - * + * This class implements the 1-n-relation between + * content and topic * */ @@ -40,7 +40,11 @@ public class DatabaseContentToTopics extends Database implements StorageObject{ this.theTable="content_x_topic"; } - + /** + * This class return an EntityList of Topics + * @param EntityContent content + * @returns EntityList + */ public EntityList getTopics(EntityContent content) { EntityList returnList=null; if (content != null) { @@ -56,8 +60,39 @@ public class DatabaseContentToTopics extends Database implements StorageObject{ } return returnList; } + + /** + * Returns a ArrayList of Integer-Objects from a content-id. + * @returns ArrayList + */ + public ArrayList getTopicsOfContent(String contentId) { + ArrayList returnList = new ArrayList(); + if (contentId != null) { + String sql = "select topic_id from " + theTable + " where content_id=" + contentId; + Connection con=null;Statement stmt=null; + try { + con = getPooledCon(); + // 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"))); + } + } + } catch (Exception e) { + theLog.printError(e.toString()); + theLog.printError("-- get topicsofcontent failed"); + } finally { + freeConnection(con,stmt); + } + } + return returnList; + } - + /** + * Set new topics + */ public void setTopics(String contentId, String[] topicId) { if (contentId == null){ return; @@ -65,31 +100,86 @@ public class DatabaseContentToTopics extends Database implements StorageObject{ if (topicId==null || topicId[0]==null) { return; } + //first check which topics this article has + ArrayList hasTopics = getTopicsOfContent(contentId); + ArrayList toSet = new ArrayList(); + ArrayList 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]); + theLog.printDebugInfo("to set: "+ topicId[i]); + } + } + //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; + } + } + if(delete==true){ + toDelete.add(topic.toString()); + theLog.printDebugInfo("to delete: "+ topic.toString()); + } + } + } else { + //all the topics has to be set, so we copy all to the array + toSet=(ArrayList)Arrays.asList(topicId); + } + //first delete all row with content_id=contentId - String sql = "delete from "+ theTable +" where 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(); - ResultSet rs = executeSql(stmt,sql); + int rs = executeUpdate(stmt,sql); } catch (Exception e) { - theLog.printDebugInfo("-- set topics failed -- delete"); + theLog.printDebugInfo("-- deleting topics failed"); } finally { freeConnection(con,stmt); } //now insert //first delete all row with content_id=contentId - for (int i=0;i