Matt Ward ec302df6ed ALF-10771: adding validation to schema compare tool
Added support to DbObjects to accept visitors
Added ValidatingVisitor to invoke suitable validator on each DbObject
Added NameValidator and NullValidator to operate on DbObject types
Added test suites



git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@31494 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2011-10-26 16:01:38 +00:00

204 lines
5.7 KiB
Java

/*
* Copyright (C) 2005-2011 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.util.schemacomp.model;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.alfresco.util.schemacomp.ComparisonUtils;
import org.alfresco.util.schemacomp.DbObjectVisitor;
import org.alfresco.util.schemacomp.DiffContext;
import org.alfresco.util.schemacomp.Differences;
import org.alfresco.util.schemacomp.Result.Strength;
import org.alfresco.util.schemacomp.DefaultComparisonUtils;
import org.springframework.util.StringUtils;
/**
* Useful base class for many, if not all the {@link DbObject} implementations.
*
* @author Matt Ward
*/
public abstract class AbstractDbObject implements DbObject
{
private String name;
/** How differences in the name field should be reported */
private Strength nameStrength = Strength.ERROR;
protected ComparisonUtils comparisonUtils = new DefaultComparisonUtils();
/**
* Default constructor
*/
public AbstractDbObject()
{
}
/**
* Instantiate, giving the object a name.
*
* @param name
*/
public AbstractDbObject(String name)
{
this.name = name;
}
/**
* @return the name
*/
public String getName()
{
return this.name;
}
/**
* @param name the name to set
*/
public void setName(String name)
{
this.name = name;
}
/**
* @return the nameStrength
*/
public Strength getNameStrength()
{
return this.nameStrength;
}
/**
* @param nameStrength the nameStrength to set
*/
public void setNameStrength(Strength nameStrength)
{
this.nameStrength = nameStrength;
}
@Override
public boolean sameAs(DbObject other)
{
if (getName() != null && other != null && other.getName() != null)
{
return getName().equals(other.getName());
}
else
{
// Only other way we can know if they are the same is if they are
// the exact same object reference.
return this == other;
}
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
AbstractDbObject other = (AbstractDbObject) obj;
if (this.name == null)
{
if (other.name != null) return false;
}
else if (!this.name.equals(other.name)) return false;
return true;
}
@Override
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append(getClass().getSimpleName());
sb.append("[name=");
if (getName() != null)
{
sb.append(getName());
}
else
{
sb.append("null");
}
sb.append("]");
return sb.toString();
}
/**
* Provides an implementation of {@link DbObject#diff(DbObject, Differences)}. The template
* method {@link #doDiff(DbObject, Differences)} provides the subclass specific diffing logic,
* whilst this method handles the workflow required in most cases: set the path's prefix that will be
* used to explain where differences occur; compare the name fields of the two objects; delegate to the
* subclass specific diffing (if any); remove the last path addition ready for the next object to perform
* its diff correctly.
*/
@Override
public void diff(DbObject right, DiffContext ctx, Strength strength)
{
Differences differences = ctx.getDifferences();
if (name != null && StringUtils.hasText(name))
{
differences.pushPath(name);
}
else
{
differences.pushPath("<" + getClass().getSimpleName() + ">");
}
comparisonUtils.compareSimple(name, right.getName(), ctx, getNameStrength());
doDiff(right, ctx, strength);
differences.popPath();
}
/**
* Override this method to provide subclass specific diffing logic.
*
* @param right
* @param differences
* @param strength
*/
protected void doDiff(DbObject right, DiffContext ctx, Strength strength)
{
}
/**
* If a ComparisonUtils other than the default is required, then this setter can be used.
* Useful for testing, where a mock can be injected.
*
* @param comparisonUtils the comparisonUtils to set
*/
public void setComparisonUtils(ComparisonUtils comparisonUtils)
{
this.comparisonUtils = comparisonUtils;
}
}