Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 90918 invoked from network); 28 Sep 2009 19:57:00 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 28 Sep 2009 19:57:00 -0000 Received: (qmail 90584 invoked by uid 500); 28 Sep 2009 19:57:00 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 90523 invoked by uid 500); 28 Sep 2009 19:57:00 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 90514 invoked by uid 99); 28 Sep 2009 19:57:00 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 28 Sep 2009 19:57:00 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 28 Sep 2009 19:56:58 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 9EE05238897D; Mon, 28 Sep 2009 19:56:38 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@directory.apache.org From: seelmann@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090928195638.9EE05238897D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 attributesToDelAdd = new HashSet(); + Set attributesToReplace = new HashSet(); - // 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 newValues = new HashSet( 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 oldValues = new HashSet(); + if ( oldAttribute != null ) + { + oldValues.addAll( Arrays.asList( oldAttribute.getStringValues() ) ); + } + Set newValues = new HashSet(); + 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 oldValues = new HashSet(); + if ( oldAttribute != null ) { - // check later: replace + oldValues.addAll( Arrays.asList( oldAttribute.getStringValues() ) ); } - else + Set newValues = new HashSet(); + if ( newAttribute != null ) + { + newValues.addAll( Arrays.asList( newAttribute.getStringValues() ) ); + } + + if ( !newValues.equals( oldValues ) ) { - Set oldValues = new HashSet( 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 ); }