mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
ALF-7260: schema comparator
* replace path/push/pop mechanism as it doesn't work well at reporting where differences/validation errors occur. * add getParent() to DbObject - so that a path-style identifier can be deduced for a DbObject when needed * add DbProperty to specify a specific DbObject's property and value -- acts as a schema location pointer * refactored Result code (need difference result and validation error result) git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@31527 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -19,10 +19,11 @@
|
||||
|
||||
package org.alfresco.util.schemacomp;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.alfresco.util.schemacomp.Result.Strength;
|
||||
import org.alfresco.util.schemacomp.Result.Where;
|
||||
import org.alfresco.util.schemacomp.Difference.Where;
|
||||
import org.alfresco.util.schemacomp.model.DbObject;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.collections.Predicate;
|
||||
@@ -54,42 +55,60 @@ public class DefaultComparisonUtils implements ComparisonUtils
|
||||
|
||||
|
||||
@Override
|
||||
public void compareSimpleCollections(Collection<? extends Object> leftCollection,
|
||||
Collection<? extends Object> rightCollection, DiffContext ctx, Strength strength)
|
||||
public void compareSimpleCollections(DbProperty leftProp,
|
||||
DbProperty rightProp, DiffContext ctx, Strength strength)
|
||||
{
|
||||
Differences differences = ctx.getDifferences();
|
||||
for (Object leftObj : leftCollection)
|
||||
@SuppressWarnings("unchecked")
|
||||
Collection<? extends Object> leftCollection = (Collection<? extends Object>) leftProp.getPropertyValue();
|
||||
@SuppressWarnings("unchecked")
|
||||
Collection<? extends Object> rightCollection = (Collection<? extends Object>) rightProp.getPropertyValue();
|
||||
|
||||
// TODO: Temporary code during refactoring
|
||||
ArrayList<? extends Object> leftList = new ArrayList<Object>(leftCollection);
|
||||
ArrayList<? extends Object> rightList = new ArrayList<Object>(rightCollection);
|
||||
|
||||
Results differences = ctx.getDifferences();
|
||||
|
||||
for (int leftIndex = 0; leftIndex < leftList.size(); leftIndex++)
|
||||
{
|
||||
if (rightCollection.contains(leftObj))
|
||||
Object leftObj = leftList.get(leftIndex);
|
||||
DbProperty leftIndexedProp = new DbProperty(leftProp.getDbObject(), leftProp.getPropertyName(), leftIndex);
|
||||
|
||||
int rightIndex;
|
||||
if ((rightIndex = rightList.indexOf(leftObj)) != -1)
|
||||
{
|
||||
// The same valued object in the right hand collection as in the left.
|
||||
// Note: it isn't possible to determine a result of Where.IN_BOTH_BUT_DIFFERENCE
|
||||
// with a 'simple' value — as there is no way of knowing if the term represents the same value
|
||||
// (e.g. two strings {red_value, green_value}, are these meant to be the same or different?)
|
||||
differences.add(Where.IN_BOTH_NO_DIFFERENCE, leftObj, leftObj, strength);
|
||||
DbProperty rightIndexedProp = new DbProperty(rightProp.getDbObject(), rightProp.getPropertyName(), rightIndex);
|
||||
differences.add(Where.IN_BOTH_NO_DIFFERENCE, leftIndexedProp, rightIndexedProp, strength);
|
||||
}
|
||||
else
|
||||
{
|
||||
// No equivalent object in the right hand collection.
|
||||
differences.add(Where.ONLY_IN_LEFT, leftObj, null, strength);
|
||||
// Using rightIndexedProperty would result in index out of bounds error.
|
||||
differences.add(Where.ONLY_IN_LEFT, leftIndexedProp, rightProp, strength);
|
||||
}
|
||||
}
|
||||
|
||||
// Identify objects in the right collection but not the left
|
||||
for (Object rightObj : rightCollection)
|
||||
for (int rightIndex = 0; rightIndex < rightList.size(); rightIndex++)
|
||||
{
|
||||
Object rightObj = rightList.get(rightIndex);
|
||||
if (!leftCollection.contains(rightObj))
|
||||
{
|
||||
DbProperty rightIndexedProp = new DbProperty(rightProp.getDbObject(), rightProp.getPropertyName(), rightIndex);
|
||||
// No equivalent object in the left hand collection.
|
||||
differences.add(Where.ONLY_IN_RIGHT, null, rightObj, strength);
|
||||
differences.add(Where.ONLY_IN_RIGHT, leftProp, rightIndexedProp, strength);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare collections, reporting differences using the default {@link Result.Strength}
|
||||
* Compare collections, reporting differences using the default {@link Difference.Strength}
|
||||
*
|
||||
* @see #compareCollections(Collection, Collection, Differences, Strength)
|
||||
* @see #compareCollections(Collection, Collection, Results, Strength)
|
||||
*/
|
||||
@Override
|
||||
public void compareCollections(Collection<? extends DbObject> leftCollection,
|
||||
@@ -99,8 +118,8 @@ public class DefaultComparisonUtils implements ComparisonUtils
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare collections of {@link DbObject}s using their {@link DbObject#diff(DbObject, Differences)} method.
|
||||
* Differences are reported using the specified {@link Result.Strength}.
|
||||
* Compare collections of {@link DbObject}s using their {@link DbObject#diff(DbObject, Results)} method.
|
||||
* Differences are reported using the specified {@link Difference.Strength}.
|
||||
*
|
||||
* @param leftCollection
|
||||
* @param rightCollection
|
||||
@@ -111,7 +130,7 @@ public class DefaultComparisonUtils implements ComparisonUtils
|
||||
public void compareCollections(Collection<? extends DbObject> leftCollection,
|
||||
Collection<? extends DbObject> rightCollection, DiffContext ctx, Strength strength)
|
||||
{
|
||||
Differences differences = ctx.getDifferences();
|
||||
Results differences = ctx.getDifferences();
|
||||
for (DbObject leftObj : leftCollection)
|
||||
{
|
||||
DbObject rightObj = findSameObjectAs(rightCollection, leftObj);
|
||||
@@ -125,17 +144,19 @@ public class DefaultComparisonUtils implements ComparisonUtils
|
||||
else
|
||||
{
|
||||
// No equivalent object in the right hand collection.
|
||||
differences.add(Where.ONLY_IN_LEFT, leftObj, null, strength);
|
||||
differences.add(Where.ONLY_IN_LEFT, new DbProperty(leftObj, null), null, strength);
|
||||
}
|
||||
}
|
||||
|
||||
// Identify objects in the right collection but not the left
|
||||
for (DbObject rightObj : rightCollection)
|
||||
{
|
||||
if (!leftCollection.contains(rightObj))
|
||||
DbObject leftObj = findSameObjectAs(leftCollection, rightObj);
|
||||
|
||||
if (leftObj == null)
|
||||
{
|
||||
// No equivalent object in the left hand collection.
|
||||
differences.add(Where.ONLY_IN_RIGHT, null, rightObj, strength);
|
||||
differences.add(Where.ONLY_IN_RIGHT, null, new DbProperty(rightObj, null), strength);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -143,10 +164,10 @@ public class DefaultComparisonUtils implements ComparisonUtils
|
||||
/**
|
||||
* Compare two simple objects. Differences are reported using the default Result.Strength.
|
||||
*
|
||||
* @see #compareSimple(Object, Object, Differences, Strength)
|
||||
* @see #compareSimple(Object, Object, Results, Strength)
|
||||
*/
|
||||
@Override
|
||||
public void compareSimple(Object left, Object right, DiffContext ctx)
|
||||
public void compareSimple(DbProperty left, DbProperty right, DiffContext ctx)
|
||||
{
|
||||
compareSimple(left, right, ctx, null);
|
||||
}
|
||||
@@ -161,11 +182,16 @@ public class DefaultComparisonUtils implements ComparisonUtils
|
||||
* @param strength
|
||||
*/
|
||||
@Override
|
||||
public void compareSimple(Object left, Object right, DiffContext ctx, Strength strength)
|
||||
public void compareSimple(DbProperty leftProperty, DbProperty rightProperty, DiffContext ctx, Strength strength)
|
||||
{
|
||||
|
||||
Where where = null;
|
||||
|
||||
Object left = leftProperty.getPropertyValue();
|
||||
checkNotDbObject(left);
|
||||
Object right = rightProperty.getPropertyValue();
|
||||
checkNotDbObject(right);
|
||||
|
||||
if (left == right)
|
||||
{
|
||||
// Same object, or both nulls
|
||||
@@ -194,6 +220,19 @@ public class DefaultComparisonUtils implements ComparisonUtils
|
||||
}
|
||||
}
|
||||
|
||||
ctx.getDifferences().add(where, left, right, strength);
|
||||
ctx.getDifferences().add(where, leftProperty, rightProperty, strength);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param obj
|
||||
*/
|
||||
private void checkNotDbObject(Object obj)
|
||||
{
|
||||
if (obj != null && DbObject.class.isAssignableFrom(obj.getClass()))
|
||||
{
|
||||
throw new IllegalArgumentException(
|
||||
"Property value is a DbObject - this method shouldn't be used to compare this type: " + obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user