Cleaned up DatabaseContentToTopics.java. The set-method now only deletes the entries...
authoridfx <idfx>
Fri, 26 Oct 2001 22:13:25 +0000 (22:13 +0000)
committeridfx <idfx>
Fri, 26 Oct 2001 22:13:25 +0000 (22:13 +0000)
source/mircoders/storage/DatabaseContentToTopics.java

index 7201f94..d00fa86 100755 (executable)
@@ -14,8 +14,8 @@ import mir.misc.*;
 import mircoders.entity.*;
 
 /**
 import mircoders.entity.*;
 
 /**
- * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
- *
+ * <b>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.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) {
   public EntityList getTopics(EntityContent content) {
     EntityList returnList=null;
     if (content != null) {
@@ -56,8 +60,39 @@ public class DatabaseContentToTopics extends Database implements StorageObject{
     }
     return returnList;
   }
     }
     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;
   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;
     }
     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
     //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();
     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) {
     } 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
     } finally {
       freeConnection(con,stmt);
     }
 
     //now insert
     //first delete all row with content_id=contentId
-    for (int i=0;i<topicId.length;i++) {
+    for (Iterator it = toSet.iterator(); it.hasNext();) {
       sql = "insert into "+ theTable +" (content_id,topic_id) values ("
       sql = "insert into "+ theTable +" (content_id,topic_id) values ("
-            + contentId + "," + topicId[i] + ")";
+            + contentId + "," + (String)it.next() + ")";
       try {
         con = getPooledCon();
         // should be a preparedStatement because is faster
         stmt = con.createStatement();
       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 -- insert laenge topicId" + topicId.length);
       } finally {
       } catch (Exception e) {
         theLog.printDebugInfo("-- set topics failed -- insert laenge topicId" + topicId.length);
       } finally {
@@ -98,43 +188,6 @@ public class DatabaseContentToTopics extends Database implements StorageObject{
     }
   }
 
     }
   }
 
-
-  public void setTopics(String contentId, String topicId) {
-    if (contentId == null && topicId == null) {
-      return;
-    }
-    //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();
-      // should be a preparedStatement because is faster
-      stmt = con.createStatement();
-      int rs = executeUpdate(stmt,sql);
-    } catch (Exception e) {
-      theLog.printDebugInfo("-- set topics failed -- delete");
-    } finally {
-      freeConnection(con,stmt);
-    }
-
-    //now insert
-    //first delete all row with content_id=contentId
-
-    sql = "insert into "+ theTable +" (content_id,topic_id) values ("
-          + contentId + "," + topicId + ")";
-    try {
-      con = getPooledCon();
-      // should be a preparedStatement because is faster
-      stmt = con.createStatement();
-      int rs = executeUpdate(stmt,sql);
-    } catch (Exception e) {
-      theLog.printDebugInfo("-- set topics failed -- insert");
-    } finally {
-      freeConnection(con,stmt);
-    }
-  }
-
   public void deleteByContentId(String contentId) {
     if (contentId == null) {
       //theLog.printDebugInfo("-- delete topics failed -- no content id");
   public void deleteByContentId(String contentId) {
     if (contentId == null) {
       //theLog.printDebugInfo("-- delete topics failed -- no content id");
@@ -209,5 +262,4 @@ public class DatabaseContentToTopics extends Database implements StorageObject{
     }
     return returnList;
   }
     }
     return returnList;
   }
-
 }
 }