bugfixes mainly...
[mir.git] / source / mircoders / storage / DatabaseContentToTopics.java
index d00fa86..dc76e9d 100755 (executable)
@@ -23,7 +23,10 @@ public class DatabaseContentToTopics extends Database implements StorageObject{
 
   private static DatabaseContentToTopics instance;
 
-  public static DatabaseContentToTopics getInstance()
+  // 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 StorageObjectException {
     if (instance == null) {
       instance = new DatabaseContentToTopics();
@@ -38,6 +41,9 @@ public class DatabaseContentToTopics extends Database implements StorageObject{
     super();
     this.hasTimestamp = false;
     this.theTable="content_x_topic";
+    try { this.theEntityClass = Class.forName("mir.entity.GenericEntity"); }
+    catch (Exception e) { throw new StorageObjectException(e.toString()); }
+
   }
 
   /**
@@ -60,12 +66,13 @@ 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) {
+  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;
@@ -93,7 +100,8 @@ public class DatabaseContentToTopics extends Database implements StorageObject{
   /**
    * Set new topics
    */
-  public void setTopics(String contentId, String[] topicId) {
+  public void setTopics(String contentId, String[] topicId)
+    throws StorageObjectException {
     if (contentId == null){
       return;
     }
@@ -101,10 +109,10 @@ public class DatabaseContentToTopics extends Database implements StorageObject{
       return;
     }
     //first check which topics this article has
-    ArrayList hasTopics = getTopicsOfContent(contentId);
-    ArrayList toSet = new ArrayList();
-    ArrayList toDelete = new ArrayList();
-    
+    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++){
@@ -142,9 +150,11 @@ public class DatabaseContentToTopics extends Database implements StorageObject{
       }
     } else {
       //all the topics has to be set, so we copy all to the array
-      toSet=(ArrayList)Arrays.asList(topicId);
+                       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 (";
@@ -188,7 +198,8 @@ public class DatabaseContentToTopics extends Database implements StorageObject{
     }
   }
 
-  public void deleteByContentId(String contentId) {
+  public void deleteByContentId(String contentId)
+    throws StorageObjectException {
     if (contentId == null) {
       //theLog.printDebugInfo("-- delete topics failed -- no content id");
       return;
@@ -209,7 +220,8 @@ public class DatabaseContentToTopics extends Database implements StorageObject{
     }
   }
 
-  public void deleteByTopicId(String topicId) {
+  public void deleteByTopicId(String topicId)
+    throws StorageObjectException {
     if (topicId == null) {
       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
       return;
@@ -231,7 +243,8 @@ public class DatabaseContentToTopics extends Database implements StorageObject{
   }
 
 
-  public EntityList getContent(EntityTopics topic) {
+  public EntityList getContent(EntityTopics topic)
+    throws StorageObjectException {
     EntityList returnList=null;
     if (topic != null) {
       String id = topic.getId();