directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From seelm...@apache.org
Subject svn commit: r819696 - /directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/utils/Utils.java
Date Mon, 28 Sep 2009 19:56:38 GMT
Author: seelmann
Date: Mon Sep 28 19:56:38 2009
New Revision: 819696

URL: http://svn.apache.org/viewvc?rev=819696&view=rev
Log:
DIRSTUDIO-513: made compute diff schema aware

Modified:
    directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/utils/Utils.java

Modified: directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/utils/Utils.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/utils/Utils.java?rev=819696&r1=819695&r2=819696&view=diff
==============================================================================
--- directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/utils/Utils.java
(original)
+++ directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/utils/Utils.java
Mon Sep 28 19:56:38 2009
@@ -36,6 +36,7 @@
 import org.apache.directory.shared.ldap.name.AttributeTypeAndValue;
 import org.apache.directory.shared.ldap.name.LdapDN;
 import org.apache.directory.shared.ldap.name.Rdn;
+import org.apache.directory.shared.ldap.schema.parsers.AttributeTypeDescription;
 import org.apache.directory.shared.ldap.util.LdapURL;
 import org.apache.directory.studio.connection.core.ConnectionParameter.EncryptionMethod;
 import org.apache.directory.studio.ldapbrowser.core.BrowserCoreConstants;
@@ -46,6 +47,7 @@
 import org.apache.directory.studio.ldapbrowser.core.model.ISearch;
 import org.apache.directory.studio.ldapbrowser.core.model.IValue;
 import org.apache.directory.studio.ldapbrowser.core.model.schema.Schema;
+import org.apache.directory.studio.ldapbrowser.core.model.schema.SchemaUtils;
 import org.apache.directory.studio.ldifparser.LdifFormatParameters;
 import org.apache.directory.studio.ldifparser.LdifUtils;
 import org.apache.directory.studio.ldifparser.model.container.LdifChangeModifyRecord;
@@ -380,120 +382,169 @@
      */
     public static LdifChangeModifyRecord computeDiff( IEntry t0, IEntry t1 )
     {
-        LdifChangeModifyRecord record = LdifChangeModifyRecord.create( t0.getDn().getUpName()
);
+        Set<String> attributesToDelAdd = new HashSet<String>();
+        Set<String> attributesToReplace = new HashSet<String>();
 
-        // check which attributes/values must be deleted
+        // check attributes of old entry
         for ( IAttribute oldAttr : t0.getAttributes() )
         {
-            String oldAttrDesc = oldAttr.getDescription();
-            IAttribute newAttr = t1.getAttribute( oldAttrDesc );
-            if ( newAttr == null )
+            String attributeDescription = oldAttr.getDescription();
+
+            Schema schema = oldAttr.getEntry().getBrowserConnection().getSchema();
+            AttributeTypeDescription atd = schema.getAttributeTypeDescription( oldAttr.getType()
);
+            String emr = SchemaUtils.getEqualityMatchingRuleNameOrNumericOidTransitive( atd,
schema );
+            boolean hasEMR = emr != null;
+            if ( hasEMR )
             {
-                // delete whole attribute
-                LdifModSpec modSpec = LdifModSpec.createDelete( oldAttrDesc );
-                modSpec.finish( LdifModSpecSepLine.create() );
-                record.addModSpec( modSpec );
+                attributesToDelAdd.add( attributeDescription );
             }
-            else if ( oldAttr.getValueSize() == 1 && newAttr.getValueSize() == 1
)
+            else
+            {
+                attributesToReplace.add( attributeDescription );
+            }
+        }
+
+        // check attributes of new entry
+        for ( IAttribute newAttr : t1.getAttributes() )
+        {
+            String attributeDescription = newAttr.getDescription();
+
+            Schema schema = newAttr.getEntry().getBrowserConnection().getSchema();
+            AttributeTypeDescription atd = schema.getAttributeTypeDescription( newAttr.getType()
);
+            String emr = SchemaUtils.getEqualityMatchingRuleNameOrNumericOidTransitive( atd,
schema );
+            boolean hasEMR = emr != null;
+
+            if ( hasEMR )
             {
-                // check later: replace
+                attributesToDelAdd.add( attributeDescription );
             }
             else
             {
-                Set<String> newValues = new HashSet<String>( Arrays.asList( newAttr.getStringValues()
) );
-                for ( IValue oldValue : oldAttr.getValues() )
+                attributesToReplace.add( attributeDescription );
+            }
+        }
+
+        LdifChangeModifyRecord record = LdifChangeModifyRecord.create( t0.getDn().getUpName()
);
+
+        for ( String attributeDescription : attributesToDelAdd )
+        {
+            IAttribute oldAttribute = t0.getAttribute( attributeDescription );
+            IAttribute newAttribute = t1.getAttribute( attributeDescription );
+
+            Set<String> oldValues = new HashSet<String>();
+            if ( oldAttribute != null )
+            {
+                oldValues.addAll( Arrays.asList( oldAttribute.getStringValues() ) );
+            }
+            Set<String> newValues = new HashSet<String>();
+            if ( newAttribute != null )
+            {
+                newValues.addAll( Arrays.asList( newAttribute.getStringValues() ) );
+            }
+
+            if ( oldAttribute != null )
+            {
+                LdifModSpec modSpec = LdifModSpec.createDelete( attributeDescription );
+                for ( IValue oldValue : oldAttribute.getValues() )
                 {
-                    if ( !newValues.contains( oldValue.getStringValue() ) && !oldValue.isEmpty()
)
+                    if ( oldValue.isEmpty() )
                     {
-                        LdifModSpec modSpec = LdifModSpec.createDelete( oldAttrDesc );
-                        if ( oldAttr.isBinary() )
+                        return null;
+                    }
+
+                    if ( !newValues.contains( oldValue.getStringValue() ) )
+                    {
+                        if ( oldAttribute.isBinary() )
                         {
-                            modSpec.addAttrVal( LdifAttrValLine.create( oldAttrDesc, oldValue.getBinaryValue()
) );
+                            modSpec.addAttrVal( LdifAttrValLine
+                                .create( attributeDescription, oldValue.getBinaryValue()
) );
                         }
                         else
                         {
-                            modSpec.addAttrVal( LdifAttrValLine.create( oldAttrDesc, oldValue.getStringValue()
) );
+                            modSpec.addAttrVal( LdifAttrValLine
+                                .create( attributeDescription, oldValue.getStringValue()
) );
                         }
-                        modSpec.finish( LdifModSpecSepLine.create() );
-                        record.addModSpec( modSpec );
                     }
                 }
+                modSpec.finish( LdifModSpecSepLine.create() );
+                if ( modSpec.getAttrVals().length > 0 )
+                {
+                    record.addModSpec( modSpec );
+                }
             }
-        }
 
-        // check which attributes/values must be added
-        for ( IAttribute newAttr : t1.getAttributes() )
-        {
-            String newAttrDesc = newAttr.getDescription();
-            IAttribute oldAttr = t0.getAttribute( newAttrDesc );
-            if ( oldAttr == null )
-            {
-                // add whole attribute
-                LdifModSpec modSpec = LdifModSpec.createAdd( newAttrDesc );
-                for ( IValue newValue : newAttr.getValues() )
+            if ( newAttribute != null )
+            {
+                LdifModSpec modSpec = LdifModSpec.createAdd( attributeDescription );
+                for ( IValue newValue : newAttribute.getValues() )
                 {
-                    if ( !newValue.isEmpty() )
+                    if ( newValue.isEmpty() )
                     {
-                        if ( newAttr.isBinary() )
+                        return null;
+                    }
+
+                    if ( !oldValues.contains( newValue.getStringValue() ) )
+                    {
+                        if ( newAttribute.isBinary() )
                         {
-                            modSpec.addAttrVal( LdifAttrValLine.create( newAttrDesc, newValue.getBinaryValue()
) );
+                            modSpec.addAttrVal( LdifAttrValLine
+                                .create( attributeDescription, newValue.getBinaryValue()
) );
                         }
                         else
                         {
-                            modSpec.addAttrVal( LdifAttrValLine.create( newAttrDesc, newValue.getStringValue()
) );
+                            modSpec.addAttrVal( LdifAttrValLine
+                                .create( attributeDescription, newValue.getStringValue()
) );
                         }
                     }
                 }
                 modSpec.finish( LdifModSpecSepLine.create() );
-                if ( modSpec.isValid() )
+                if ( modSpec.getAttrVals().length > 0 )
                 {
                     record.addModSpec( modSpec );
                 }
             }
-            else if ( oldAttr.getValueSize() == 1 && newAttr.getValueSize() == 1
)
+        }
+
+        for ( String attributeDescription : attributesToReplace )
+        {
+            IAttribute oldAttribute = t0.getAttribute( attributeDescription );
+            IAttribute newAttribute = t1.getAttribute( attributeDescription );
+
+            Set<String> oldValues = new HashSet<String>();
+            if ( oldAttribute != null )
             {
-                // check later: replace
+                oldValues.addAll( Arrays.asList( oldAttribute.getStringValues() ) );
             }
-            else
+            Set<String> newValues = new HashSet<String>();
+            if ( newAttribute != null )
+            {
+                newValues.addAll( Arrays.asList( newAttribute.getStringValues() ) );
+            }
+
+            if ( !newValues.equals( oldValues ) )
             {
-                Set<String> oldValues = new HashSet<String>( Arrays.asList( oldAttr.getStringValues()
) );
-                for ( IValue newValue : newAttr.getValues() )
+                LdifModSpec modSpec = LdifModSpec.createReplace( attributeDescription );
+                if ( newAttribute != null )
                 {
-                    if ( !oldValues.contains( newValue.getStringValue() ) && !newValue.isEmpty()
)
+                    for ( IValue newValue : newAttribute.getValues() )
                     {
-                        LdifModSpec modSpec = LdifModSpec.createAdd( newAttrDesc );
-                        if ( newAttr.isBinary() )
+                        if ( newValue.isEmpty() )
+                        {
+                            return null;
+                        }
+
+                        if ( newAttribute.isBinary() )
                         {
-                            modSpec.addAttrVal( LdifAttrValLine.create( newAttrDesc, newValue.getBinaryValue()
) );
+                            modSpec.addAttrVal( LdifAttrValLine
+                                .create( attributeDescription, newValue.getBinaryValue()
) );
                         }
                         else
                         {
-                            modSpec.addAttrVal( LdifAttrValLine.create( newAttrDesc, newValue.getStringValue()
) );
+                            modSpec.addAttrVal( LdifAttrValLine
+                                .create( attributeDescription, newValue.getStringValue()
) );
                         }
-                        modSpec.finish( LdifModSpecSepLine.create() );
-                        record.addModSpec( modSpec );
                     }
                 }
-            }
-        }
-
-        // check which attributes/values must be replaced
-        for ( IAttribute newAttr : t1.getAttributes() )
-        {
-            String newAttrDesc = newAttr.getDescription();
-            IAttribute oldAttr = t0.getAttribute( newAttrDesc );
-            if ( oldAttr != null && oldAttr.getValueSize() == 1 && newAttr.getValueSize()
== 1
-                && !oldAttr.getValues()[0].getStringValue().equals( newAttr.getValues()[0].getStringValue()
) )
-            {
-                LdifModSpec modSpec = LdifModSpec.createReplace( newAttrDesc );
-                if ( newAttr.isBinary() )
-                {
-                    modSpec.addAttrVal( LdifAttrValLine.create( newAttrDesc, newAttr.getValues()[0].getBinaryValue()
) );
-                }
-                else
-                {
-                    modSpec.addAttrVal( LdifAttrValLine.create( newAttrDesc, newAttr.getValues()[0].getStringValue()
) );
-                }
                 modSpec.finish( LdifModSpecSepLine.create() );
                 record.addModSpec( modSpec );
             }



Mime
View raw message