From 5933e18c3cd0a65c5179752da2413e4eab3b897c Mon Sep 17 00:00:00 2001
From: ludovicp <ludovicp@localhost>
Date: Tue, 15 Jun 2010 14:55:08 +0000
Subject: [PATCH] Add RDN attributes if missing in entry during the import.

---
 opends/src/server/org/opends/server/util/LDIFReader.java |  104 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 101 insertions(+), 3 deletions(-)

diff --git a/opends/src/server/org/opends/server/util/LDIFReader.java b/opends/src/server/org/opends/server/util/LDIFReader.java
index afd6a2d..64fd6b6 100644
--- a/opends/src/server/org/opends/server/util/LDIFReader.java
+++ b/opends/src/server/org/opends/server/util/LDIFReader.java
@@ -411,6 +411,12 @@
       // appropriate to do so.
       if (checkSchema)
       {
+        //Add the RDN attributes.
+        addRDNAttributesIfNecessary(entryDN,userAttributes,
+                operationalAttributes);
+        //Add any superior objectclass(s) missing in the objectclass map.
+        addSuperiorObjectClasses(objectClasses);
+
         MessageBuilder invalidReason = new MessageBuilder();
         if (! entry.conformsToSchema(null, false, true, false, invalidReason))
         {
@@ -423,9 +429,6 @@
           suffix.removePending(entryDN);
           continue;
         }
-         //Add any superior objectclass(s) missing in an entries
-        //objectclass map.
-        addSuperiorObjectClasses(entry.getObjectClasses());
       }
       entryInfo.setEntryID(entryID);
       entryInfo.setSuffix(suffix);
@@ -2361,5 +2364,100 @@
       attrList.add(builder.toAttribute());
     }
   }
+
+
+
+  /**
+   * Adds any missing RDN attributes to the entry that is being imported.
+   */
+  private void addRDNAttributesIfNecessary(DN entryDN,
+          HashMap<AttributeType,List<Attribute>>userAttributes,
+          HashMap<AttributeType,List<Attribute>> operationalAttributes)
+  {
+    RDN rdn = entryDN.getRDN();
+    int numAVAs = rdn.getNumValues();
+    for (int i=0; i < numAVAs; i++)
+    {
+      AttributeType  t = rdn.getAttributeType(i);
+      AttributeValue v = rdn.getAttributeValue(i);
+      String         n = rdn.getAttributeName(i);
+      if (t.isOperational())
+      {
+        List<Attribute> attrList = operationalAttributes.get(t);
+        if (attrList == null)
+        {
+          attrList = new ArrayList<Attribute>();
+          attrList.add(Attributes.create(t, n, v));
+          operationalAttributes.put(t, attrList);
+        }
+        else
+        {
+          boolean found = false;
+          for (int j = 0; j < attrList.size(); j++)
+          {
+            Attribute a = attrList.get(j);
+
+            if (a.hasOptions())
+            {
+              continue;
+            }
+
+            if (!a.contains(v))
+            {
+              AttributeBuilder builder = new AttributeBuilder(a);
+              builder.add(v);
+              attrList.set(j, builder.toAttribute());
+            }
+
+            found = true;
+            break;
+          }
+
+          if (!found)
+          {
+            attrList.add(Attributes.create(t, n, v));
+          }
+        }
+      }
+      else
+      {
+        List<Attribute> attrList = userAttributes.get(t);
+        if (attrList == null)
+        {
+          attrList = new ArrayList<Attribute>();
+          attrList.add(Attributes.create(t, n, v));
+          userAttributes.put(t, attrList);
+        }
+        else
+        {
+          boolean found = false;
+          for (int j = 0; j < attrList.size(); j++)
+          {
+            Attribute a = attrList.get(j);
+
+            if (a.hasOptions())
+            {
+              continue;
+            }
+
+            if (!a.contains(v))
+            {
+              AttributeBuilder builder = new AttributeBuilder(a);
+              builder.add(v);
+              attrList.set(j, builder.toAttribute());
+            }
+
+            found = true;
+            break;
+          }
+
+          if (!found)
+          {
+            attrList.add(Attributes.create(t, n, v));
+          }
+        }
+      }
+    }
+  }
 }
 

--
Gitblit v1.10.0