bufix media
[mir.git] / source / mircoders / storage / DatabaseContentToTopics.java
index fbf46e2..eca6c29 100755 (executable)
@@ -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.
  *
  * 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;
@@ -58,14 +57,9 @@ public class DatabaseContentToTopics extends Database implements StorageObject{
 
   private static DatabaseContentToTopics instance;
 
-  public static DatabaseContentToTopics getInstance() {
+  public synchronized static DatabaseContentToTopics getInstance() {
     if (instance == null) {
-      synchronized (DatabaseContentToTopics.class) {
-        if (instance == null) {
-          instance = new DatabaseContentToTopics();
-          instance.myselfDatabase = instance;
-        }
-      }
+      instance = new DatabaseContentToTopics();
     }
     return instance;
   }
@@ -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());
@@ -175,7 +171,7 @@ public class DatabaseContentToTopics extends Database implements StorageObject{
     logger.debug("to add = " + topicsToAdd.toString());
 
 
-    if (!topicsToDelete.isEmpty()) {
+    if (!topicsToDelete.isEmpty()) {      
       String sql =
           "delete from " + theTable + " " +
           "where content_id=" + anArticleId +
@@ -272,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;
   }