first cut of merge of STABLE-pre1_0 into HEAD. I won't even guarantee that it
[mir.git] / source / mircoders / storage / DatabaseContentToTopics.java
index fadadf8..1eb4137 100755 (executable)
@@ -7,211 +7,266 @@ import java.util.*;
 
 import freemarker.template.*;
 
-import webdb.storage.*;
-import webdb.entity.*;
-import webdb.misc.*;
-
+import mir.storage.*;
 import mir.entity.*;
+import mir.misc.*;
+
+import mircoders.entity.*;
 
 /**
- * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
- *
+ * <b>This class implements the 1-n-relation between
+ * content and topic
  *
  */
 
 public class DatabaseContentToTopics extends Database implements StorageObject{
 
-       private static DatabaseContentToTopics instance;
-
-       public static DatabaseContentToTopics getInstance()
-               throws StorageObjectException {
-               if (instance == null) {
-                       instance = new DatabaseContentToTopics();
-                       instance.myselfDatabase = instance;
-               }
-               return instance;
-       }
-
-       private DatabaseContentToTopics()
-               throws StorageObjectException {
-
-               super();
-               this.hasTimestamp = false;
-               this.theTable="content_x_topic";
-               try {
-                       // this.theEntityClass = Class.forName("mir.entity.EntityGruppen");
-               } catch (Exception e) {
-                       throw new StorageObjectException(e.toString());
-               }
-       }
-
-
-       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+")";
-
-                       try {
-                               returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1);
-                       } catch (Exception e) {
-                               theLog.printDebugInfo("-- get topics failed " + e.toString());
-                       }
-               }
-               return returnList;
-       }
-
-
-       public void setTopics(EntityContent content, ArrayList topicId) {
-               if (content == null && topicId == null) {
-                       return;
-               }
-               String contentId = content.getId();
-               //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();
-                       ResultSet rs = executeSql(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
-
-               for (Iterator i = topicId.listIterator(); i.hasNext();) {
-                       sql = "insert into "+ theTable +" (content_id,topic_id) values ("
-                                               + contentId + "," + i.next().toString() + ")";
-                       try {
-                               con = getPooledCon();
-                               // should be a preparedStatement because is faster
-                               stmt = con.createStatement();
-                               ResultSet rs = executeSql(stmt,sql);
-                       } catch (Exception e) {
-                               theLog.printDebugInfo("-- set topics failed -- insert");
-                       } finally {
-                               freeConnection(con,stmt);
-                       }
-               }
-       }
-
-
-       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");
-                       return;
-               }
-               //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();
-                       ResultSet rs = executeSql(stmt,sql);
-               } catch (Exception e) {
-                       //theLog.printDebugInfo("-- delete topics failed  ");
-               } finally {
-                       freeConnection(con,stmt);
-               }
-       }
-
-       public void deleteByTopicId(String topicId) {
-               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;
-
-               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) {
-                       theLog.printDebugInfo("-- delete topics failed ");
-               } finally {
-                       freeConnection(con,stmt);
-               }
-       }
-
-
-       public EntityList getContent(EntityTopics topic) {
-               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);
-                               }
+  private static DatabaseContentToTopics instance;
+
+  public static DatabaseContentToTopics getInstance()
+    throws StorageObjectException {
+    if (instance == null) {
+      instance = new DatabaseContentToTopics();
+      instance.myselfDatabase = instance;
+    }
+    return instance;
+  }
+
+  private DatabaseContentToTopics()
+    throws StorageObjectException {
+
+    super();
+    this.hasTimestamp = false;
+    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) {
+      // 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+")";
+
+      try {
+        returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1);
+      } catch (Exception e) {
+        theLog.printDebugInfo("-- get topics failed " + e.toString());
+      }
+    }
+    return returnList;
+  }
+  
+  /**
+   * Returns a ArrayList of Integer-Objects from a content-id.
+   * @returns ArrayList
+   */
+  public ArrayList getTopicsOfContent(String contentId)
+    throws StorageObjectException {
+    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)
+    throws StorageObjectException {
+    if (contentId == null){
+      return;
+    }
+    if (topicId==null || topicId[0]==null) {
+      return;
+    }
+    //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]);
+          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
+                       for (int i = 0; i < topicId.length; i++){
+                               toSet.add(topicId[i]);
                        }
-                       catch (Exception e) {theLog.printDebugInfo("-- get contetn failed");}
-                       finally { freeConnection(con,stmt);}
-               }
-               return returnList;
-       }
+    }
+    
+    //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) {
+      theLog.printDebugInfo("-- 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() + ")";
+      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 laenge topicId" + topicId.length);
+      } finally {
+        freeConnection(con,stmt);
+      }
+    }
+  }
+
+  public void deleteByContentId(String contentId)
+    throws StorageObjectException {
+    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;
+
+    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) {
+      //theLog.printDebugInfo("-- delete topics failed  ");
+    } finally {
+      freeConnection(con,stmt);
+    }
+  }
+
+  public void deleteByTopicId(String topicId)
+    throws StorageObjectException {
+    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;
+
+    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) {
+      theLog.printDebugInfo("-- delete topics failed ");
+    } finally {
+      freeConnection(con,stmt);
+    }
+  }
+
+
+  public EntityList getContent(EntityTopics topic)
+    throws StorageObjectException {
+    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);
+        }
+      }
+      catch (Exception e) {theLog.printDebugInfo("-- get contetn failed");}
+      finally { freeConnection(con,stmt);}
+    }
+    return returnList;
+  }
 }