From 06799c3035e21a1cb7b684a8b6b28d296e1e4824 Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Sat, 04 Jul 2009 01:34:52 +0000
Subject: [PATCH] Fix for issue 4090 (Control panel: User shown in the wrong place on the Manage entries panel tree)

---
 opends/src/guitools/org/opends/guitools/controlpanel/task/NewEntryTask.java |  133 ++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 121 insertions(+), 12 deletions(-)

diff --git a/opends/src/guitools/org/opends/guitools/controlpanel/task/NewEntryTask.java b/opends/src/guitools/org/opends/guitools/controlpanel/task/NewEntryTask.java
index bd388f6..9f44ab8 100644
--- a/opends/src/guitools/org/opends/guitools/controlpanel/task/NewEntryTask.java
+++ b/opends/src/guitools/org/opends/guitools/controlpanel/task/NewEntryTask.java
@@ -250,23 +250,47 @@
         {
           getProgressDialog().appendProgressHtml(
               Utilities.getProgressDone(ColorAndFontConstants.progressFont));
+          boolean entryInserted = false;
           if (parentNode != null)
           {
-            TreePath parentPath =
-              new TreePath(controller.getTreeModel().getPathToRoot(parentNode));
-            if (parentPath != null)
+            boolean isReallyParentNode = false;
+            try
             {
-              BrowserNodeInfo nodeInfo =
-                controller.getNodeInfoFromPath(parentPath);
-              if (nodeInfo != null)
+              DN parentDN = DN.decode(parentNode.getDN());
+              isReallyParentNode =
+                parentDN.equals(newEntry.getDN().getParent());
+            }
+            catch (Throwable t)
+            {
+              // Bug
+              t.printStackTrace();
+              isReallyParentNode = false;
+            }
+            if (isReallyParentNode)
+            {
+              insertNode(parentNode, newEntry.getDN(),
+                  isBaseDN(newEntry.getDN()));
+              entryInserted = true;
+            }
+          }
+          if (!entryInserted)
+          {
+            BasicNode root = (BasicNode)controller.getTreeModel().getRoot();
+            BasicNode realParentNode = findParentNode(newEntry.getDN(), root);
+            if (realParentNode != null)
+            {
+              insertNode(realParentNode, newEntry.getDN(), false);
+            }
+            else
+            {
+              if (isBaseDN(newEntry.getDN()))
               {
-                TreePath newPath = controller.notifyEntryAdded(
-                    controller.getNodeInfoFromPath(parentPath),
-                    newEntry.getDN().toString());
-                if (newPath != null)
+                int nRootChildren = controller.getTreeModel().getChildCount(
+                  controller.getTreeModel().getRoot());
+                if (nRootChildren > 1)
                 {
-                  controller.getTree().setSelectionPath(newPath);
-                  controller.getTree().scrollPathToVisible(newPath);
+                  // Insert in the root.
+                  insertNode(root, newEntry.getDN(), true);
                 }
               }
             }
@@ -311,5 +335,90 @@
         sb.toString()+"</b><br><br>",
         ColorAndFontConstants.progressFont));
   }
+
+  private BasicNode findParentNode(DN dn, BasicNode root)
+  {
+    BasicNode parentNode = null;
+    int nRootChildren = controller.getTreeModel().getChildCount(root);
+    for (int i=0; i<nRootChildren; i++)
+    {
+      BasicNode node =
+        (BasicNode)controller.getTreeModel().getChild(root, i);
+      try
+      {
+        DN nodeDN = DN.decode(node.getDN());
+        if (dn.isDescendantOf(nodeDN))
+        {
+          if (dn.getNumComponents() == nodeDN.getNumComponents() + 1)
+          {
+            parentNode = node;
+            break;
+          }
+          else
+          {
+            parentNode = findParentNode(dn, node);
+            break;
+          }
+        }
+      }
+      catch (Throwable t)
+      {
+        // Bug
+        throw new IllegalStateException("Unexpected error: "+t, t);
+      }
+    }
+    return parentNode;
+  }
+
+  private void insertNode(BasicNode parentNode, DN dn, boolean isSuffix)
+  {
+    TreePath parentPath =
+      new TreePath(controller.getTreeModel().getPathToRoot(parentNode));
+    if (parentPath != null)
+    {
+      BrowserNodeInfo nodeInfo =
+        controller.getNodeInfoFromPath(parentPath);
+      if (nodeInfo != null)
+      {
+        TreePath newPath;
+        if (isSuffix)
+        {
+          newPath = controller.addSuffix(dn.toString(), parentNode.getDN());
+        }
+        else
+        {
+          newPath = controller.notifyEntryAdded(
+            controller.getNodeInfoFromPath(parentPath), dn.toString());
+        }
+        if (newPath != null)
+        {
+          controller.getTree().setSelectionPath(newPath);
+          controller.getTree().scrollPathToVisible(newPath);
+        }
+      }
+    }
+  }
+
+  private boolean isBaseDN(DN dn)
+  {
+    boolean isBaseDN = false;
+    for (BackendDescriptor backend :
+      getInfo().getServerDescriptor().getBackends())
+    {
+      for (BaseDNDescriptor baseDN : backend.getBaseDns())
+      {
+        if (baseDN.getDn().equals(dn))
+        {
+          isBaseDN = true;
+          break;
+        }
+      }
+      if (isBaseDN)
+      {
+        break;
+      }
+    }
+    return isBaseDN;
+  }
 }
 

--
Gitblit v1.10.0