Author: jflesch
Date: 2006-10-22 21:20:48 +0000 (Sun, 22 Oct 2006)
New Revision: 10696

Modified:
   trunk/apps/Thaw/src/thaw/plugins/index/IndexTree.java
   trunk/apps/Thaw/src/thaw/plugins/index/LinkTable.java
Log:
Doesn't allow to add an already added index

Modified: trunk/apps/Thaw/src/thaw/plugins/index/IndexTree.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/IndexTree.java       2006-10-22 
20:53:29 UTC (rev 10695)
+++ trunk/apps/Thaw/src/thaw/plugins/index/IndexTree.java       2006-10-22 
21:20:48 UTC (rev 10696)
@@ -47,6 +47,10 @@
 import javax.swing.JTextField;
 import javax.swing.JLabel;

+
+import java.sql.*;
+
+
 import thaw.plugins.Hsqldb;
 import thaw.core.*;
 import thaw.fcp.*;
@@ -214,8 +218,8 @@

                tree.setCellRenderer(treeRenderer);

-               if (selectionOnly)
-                       
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
+               //if (selectionOnly)
+               
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

                toolBar = new JToolBar();

@@ -643,16 +647,67 @@
        }


-       void addToRoot(IndexTreeNode node) {
+       public boolean addToRoot(IndexTreeNode node) {
+               if (alreadyExistingIndex(node.getPublicKey())) {
+                       Logger.notice(this, "Index already added");
+                       return false;
+               }
+
                node.setParent(root);
                root.insert(node.getTreeNode(), root.getChildCount());
                reloadModel(root);
+
+               return true;
        }

+       
+
+       public boolean alreadyExistingIndex(String key) {
+               String realKey = key.substring(0, 60).toLowerCase();
+
+               try {
+                       Connection c = db.getConnection();
+                       PreparedStatement st;
+
+                       String query;
+
+                       query = "SELECT id FROM indexes WHERE LOWER(publicKey) 
LIKE ?";
+
+                       
+                       if (modifiables)
+                               query = query + " AND privateKey IS NOT NULL;";
+                       else
+                               query = query + " AND privateKey IS NULL;";
+                       
+
+                       Logger.info(this, query + " : " + realKey+"%");
+
+                       st = c.prepareStatement(query);
+
+                       st.setString(1, realKey+"%");
+
+                       if (st.execute()) {
+                               ResultSet results = st.getResultSet();
+
+                               if (results.next()) {
+                                       return true;
+                               }
+                       }
+
+
+               } catch(java.sql.SQLException e) {
+                       Logger.warning(this, "Exception while trying to check 
if '"+key+"' is already know: '"+e.toString()+"'");
+               }
+
+               return false;
+       }
+
+
+
        /**
         * @param node can be null
         */
-       void reloadModel(DefaultMutableTreeNode node) {
+       public void reloadModel(DefaultMutableTreeNode node) {
                treeModel.reload(node);
        }


Modified: trunk/apps/Thaw/src/thaw/plugins/index/LinkTable.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/LinkTable.java       2006-10-22 
20:53:29 UTC (rev 10695)
+++ trunk/apps/Thaw/src/thaw/plugins/index/LinkTable.java       2006-10-22 
21:20:48 UTC (rev 10696)
@@ -165,8 +165,8 @@
                                Index index = new Index(db, queueManager, -2, 
null, Index.getNameFromKey(link.getKey()),
                                                        
Index.getNameFromKey(link.getKey()), link.getKey(), null,
                                                        0, null, false);
-                               index.create();
-                               indexTree.addToRoot(index);
+                               if (indexTree.addToRoot(index))
+                                       index.create();
                        }

                        if (e.getSource() == copyKey) {


Reply via email to