From c62997c0408c0caf3ee4824bbf6bdb3ea147d964 Mon Sep 17 00:00:00 2001
From: neil_a_wilson <neil_a_wilson@localhost>
Date: Mon, 12 Feb 2007 20:43:08 +0000
Subject: [PATCH] Update the ldif-diff tool so that it provides an option that allows differences between entries to be split into multiple modifications each of which has only a single value (as opposed to one modification containing all changes to the entry).  This can be useful when attempting to apply configuration changes during an upgrade.

---
 opendj-sdk/opends/src/server/org/opends/server/tools/LDIFDiff.java |   60 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 53 insertions(+), 7 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/LDIFDiff.java b/opendj-sdk/opends/src/server/org/opends/server/tools/LDIFDiff.java
index e7f6f04..bac6ef9 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/LDIFDiff.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/LDIFDiff.java
@@ -126,6 +126,7 @@
   {
     BooleanArgument overwriteExisting;
     BooleanArgument showUsage;
+    BooleanArgument singleValueChanges;
     StringArgument  configClass;
     StringArgument  configFile;
     StringArgument  outputLDIF;
@@ -159,6 +160,11 @@
                                MSGID_LDIFDIFF_DESCRIPTION_OVERWRITE_EXISTING);
       argParser.addArgument(overwriteExisting);
 
+      singleValueChanges =
+           new BooleanArgument("singlevaluechanges", 'S', "singleValueChanges",
+                               MSGID_LDIFDIFF_DESCRIPTION_SINGLE_VALUE_CHANGES);
+      argParser.addArgument(singleValueChanges);
+
       configFile = new StringArgument("configfile", 'c', "configFile", false,
                                       false, true, "{configFile}", null, null,
                                       MSGID_LDIFDIFF_DESCRIPTION_CONFIG_FILE);
@@ -478,7 +484,8 @@
           {
             // The DNs are the same, so check to see if the entries are the
             // same or have been modified.
-            if (writeModify(writer, sourceEntry, targetEntry))
+            if (writeModify(writer, sourceEntry, targetEntry,
+                            singleValueChanges.isPresent()))
             {
               differenceFound = true;
             }
@@ -602,10 +609,13 @@
    * user attributes.  It will ignore differences in the DN and operational
    * attributes.
    *
-   * @param  writer       The writer to which the modify record should be
-   *                      written.
-   * @param  sourceEntry  The source form of the entry.
-   * @param  targetEntry  The target form of the entry.
+   * @param  writer              The writer to which the modify record should be
+   *                             written.
+   * @param  sourceEntry         The source form of the entry.
+   * @param  targetEntry         The target form of the entry.
+   * @param  singleValueChanges  Indicates whether each attribute-level change
+   *                             should be written in a separate modification
+   *                             per attribute value.
    *
    * @return  <CODE>true</CODE> if there were any differences found between the
    *          source and target entries, or <CODE>false</CODE> if not.
@@ -614,7 +624,8 @@
    *                       change record.
    */
   private static boolean writeModify(LDIFWriter writer, Entry sourceEntry,
-                                     Entry targetEntry)
+                                     Entry targetEntry,
+                                     boolean singleValueChanges)
           throws IOException
   {
     // Create a list to hold the modifications that are found.
@@ -786,7 +797,42 @@
     }
     else
     {
-      writer.writeModifyChangeRecord(sourceEntry.getDN(), modifications);
+      if (singleValueChanges)
+      {
+        for (Modification m : modifications)
+        {
+          Attribute a = m.getAttribute();
+          LinkedHashSet<AttributeValue> values = a.getValues();
+          if (values.isEmpty())
+          {
+            LinkedList<Modification> attrMods = new LinkedList<Modification>();
+            attrMods.add(m);
+            writer.writeModifyChangeRecord(sourceEntry.getDN(), attrMods);
+          }
+          else
+          {
+            LinkedList<Modification> attrMods = new LinkedList<Modification>();
+            LinkedHashSet<AttributeValue> valueSet =
+                 new LinkedHashSet<AttributeValue>();
+            for (AttributeValue v : values)
+            {
+              valueSet.clear();
+              valueSet.add(v);
+              Attribute attr = new Attribute(a.getAttributeType(),
+                                             a.getName(), valueSet);
+
+              attrMods.clear();
+              attrMods.add(new Modification(m.getModificationType(), attr));
+              writer.writeModifyChangeRecord(sourceEntry.getDN(), attrMods);
+            }
+          }
+        }
+      }
+      else
+      {
+        writer.writeModifyChangeRecord(sourceEntry.getDN(), modifications);
+      }
+
       return true;
     }
   }

--
Gitblit v1.10.0