Author: jflesch
Date: 2007-08-08 22:24:22 +0000 (Wed, 08 Aug 2007)
New Revision: 14546

Modified:
   trunk/apps/Thaw/src/thaw/plugins/index/DatabaseManager.java
Log:
Clean the category list at startup

Modified: trunk/apps/Thaw/src/thaw/plugins/index/DatabaseManager.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/DatabaseManager.java 2007-08-08 
21:59:50 UTC (rev 14545)
+++ trunk/apps/Thaw/src/thaw/plugins/index/DatabaseManager.java 2007-08-08 
22:24:22 UTC (rev 14546)
@@ -75,7 +75,7 @@
  */
 public class DatabaseManager {

-       public DatabaseManager() {
+       private DatabaseManager() {

        }

@@ -161,10 +161,47 @@

                createTables(db);

+               cleanUpCategories(db);
+
                return newDb;
        }


+       public static void cleanUpCategories(Hsqldb db) {
+               try {
+                       synchronized(db.dbLock) {
+                               PreparedStatement st;
+                               PreparedStatement countSt;
+                               PreparedStatement deleteSt;
+
+                               st = 
db.getConnection().prepareStatement("SELECT id FROM categories");
+                               countSt = 
db.getConnection().prepareStatement("SELECT count(id) FROM indexes "+
+                                                                             
"WHERE categoryId = ?");
+                               deleteSt = 
db.getConnection().prepareStatement("DELETE FROM categories "+
+                                                                              
"WHERE id = ?");
+
+                               ResultSet set = st.executeQuery();
+
+                               while(set.next()) {
+                                       int id = set.getInt("id");
+
+                                       countSt.setInt(1, id);
+
+                                       ResultSet aSet = countSt.executeQuery();
+                                       aSet.next();
+
+                                       if (aSet.getInt(1) == 0) {
+                                               deleteSt.setInt(1, id);
+                                               deleteSt.execute();
+                                       }
+                               }
+                       }
+               } catch(SQLException e) {
+                       Logger.error(new DatabaseManager(), "Can't cleanup the 
unused categories because: "+e.toString());
+               }
+       }
+
+
        /**
         * Can be safely called, even if the tables already exist.
         */


Reply via email to