Matt Ward 29dadc748a ALF-11518 and ALF-11519: unify separate validation/differences lists and move text generation to Result classes.
ALF-11518: Unify differences and validation results lists
ALF-11519: Move validation/diff result text generation from SchemaBootstrap to result classes.



git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@32110 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2011-11-18 18:20:53 +00:00

256 lines
6.9 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.DbProperty;
import org.alfresco.util.schemacomp.DefaultComparisonUtils;
import org.alfresco.util.schemacomp.DiffContext;
import org.alfresco.util.schemacomp.Result.Strength;
import org.alfresco.util.schemacomp.Results;
import org.alfresco.util.schemacomp.validator.DbValidator;
/**
* Useful base class for many, if not all the {@link DbObject} implementations.
*
* @author Matt Ward
*/
public abstract class AbstractDbObject implements DbObject
{
private DbObject parent;
private String name;
/** How differences in the name field should be reported */
private Strength nameStrength = Strength.ERROR;
protected ComparisonUtils comparisonUtils = new DefaultComparisonUtils();
private List<DbValidator> validators = new ArrayList<DbValidator>();
/**
* Instantiate, giving the object a parent and a name.
*
* @param parent
* @param name
*/
public AbstractDbObject(DbObject parent, String name)
{
this.parent = parent;
this.name = name;
}
/**
* @return the name
*/
public String getName()
{
if (this.name == null)
{
return "";
}
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 (other == null)
{
return false;
}
if (this == other)
{
return true;
}
if (!this.getClass().equals(other.getClass()))
{
// Objects are not the same type, even if they have the same name and parent
return false;
}
if (getName() != null && other != null && other.getName() != null)
{
boolean sameParent = false;
if (getParent() == null && other.getParent() == null)
{
sameParent = true;
}
else if (getParent() != null && getParent().sameAs(other.getParent()))
{
sameParent = true;
}
return sameParent && getName().equals(other.getName());
}
return false;
}
@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, Results)}. The template
* method {@link #doDiff(DbObject, Results)} 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)
{
DbProperty leftNameProp = new DbProperty(this, "name");
DbProperty rightNameProp = new DbProperty(right, "name");
comparisonUtils.compareSimple(leftNameProp, rightNameProp, ctx, getNameStrength());
doDiff(right, ctx, strength);
}
@Override
public DbObject getParent()
{
return parent;
}
@Override
public void setParent(DbObject parent)
{
this.parent = parent;
}
/**
* 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;
}
@Override
public List<DbValidator> getValidators()
{
return validators;
}
/**
* @param validators the validators to set
*/
@Override
public void setValidators(List<DbValidator> validators)
{
if (validators == null)
{
this.validators = Collections.emptyList();
}
else
{
this.validators = validators;
}
}
}