* @return the change modify record or null if there is no difference
* between the two entries
*/
public static LdifChangeModifyRecord computeDiff( IEntry t0, IEntry t1 )
{
LdifChangeModifyRecord record = LdifChangeModifyRecord.create( t0.getDn().getUpName() );
// check which attributes/values must be deleted
for ( IAttribute oldAttr : t0.getAttributes() )
{
String oldAttrDesc = oldAttr.getDescription();
IAttribute newAttr = t1.getAttribute( oldAttrDesc );
if ( newAttr == null )
{
// delete whole attribute
LdifModSpec modSpec = LdifModSpec.createDelete( oldAttrDesc );
modSpec.finish( LdifModSpecSepLine.create() );
record.addModSpec( modSpec );
}
else if ( oldAttr.getValueSize() == 1 && newAttr.getValueSize() == 1 )
{
// check later: replace
}
else
{
Set<String> newValues = new HashSet<String>( Arrays.asList( newAttr.getStringValues() ) );
for ( IValue oldValue : oldAttr.getValues() )
{
if ( !newValues.contains( oldValue.getStringValue() ) && !oldValue.isEmpty() )
{
LdifModSpec modSpec = LdifModSpec.createDelete( oldAttrDesc );
if ( oldAttr.isBinary() )
{
modSpec.addAttrVal( LdifAttrValLine.create( oldAttrDesc, oldValue.getBinaryValue() ) );
}
else
{
modSpec.addAttrVal( LdifAttrValLine.create( oldAttrDesc, oldValue.getStringValue() ) );
}
modSpec.finish( LdifModSpecSepLine.create() );
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 ( !newValue.isEmpty() )
{
if ( newAttr.isBinary() )
{
modSpec.addAttrVal( LdifAttrValLine.create( newAttrDesc, newValue.getBinaryValue() ) );
}
else
{
modSpec.addAttrVal( LdifAttrValLine.create( newAttrDesc, newValue.getStringValue() ) );
}
}
}
modSpec.finish( LdifModSpecSepLine.create() );
if ( modSpec.isValid() )
{
record.addModSpec( modSpec );
}
}
else if ( oldAttr.getValueSize() == 1 && newAttr.getValueSize() == 1 )
{
// check later: replace
}
else
{
Set<String> oldValues = new HashSet<String>( Arrays.asList( oldAttr.getStringValues() ) );
for ( IValue newValue : newAttr.getValues() )
{
if ( !oldValues.contains( newValue.getStringValue() ) && !newValue.isEmpty() )
{
LdifModSpec modSpec = LdifModSpec.createAdd( newAttrDesc );
if ( newAttr.isBinary() )
{
modSpec.addAttrVal( LdifAttrValLine.create( newAttrDesc, newValue.getBinaryValue() ) );
}
else
{
modSpec.addAttrVal( LdifAttrValLine.create( newAttrDesc, 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 );
}
}
record.finish( LdifSepLine.create() );
// check for changes: if there are no changes the record does not include
// any modifications and so it is not valid.
return record.isValid() ? record : null;
}