public abstract class XDataCloneOverrule<T> : XDataOverrule<T>
where T : DBObject
{
protected XDataCloneOverrule( string appname, Database database = null )
: base( appname, database )
{
}
public override DBObject DeepClone( DBObject dbObject, DBObject ownerObject, IdMapping idMap, bool isPrimary )
{
DBObject result = base.DeepClone( dbObject, ownerObject, idMap, isPrimary );
if( IsDatabaseApplicable( ownerObject.Database ) )
{
try
{
using( ResultBuffer xdata = GetXData( dbObject ) )
{
if( xdata != null )
{
T source = dbObject as T;
if( source != null )
{
T clone = result as T;
if( clone != null )
OnCloned( source, xdata, clone, ownerObject, idMap, isPrimary );
}
}
}
}
catch( System.Exception ex )
{
throw ex;
}
}
return result;
}
public override DBObject WblockClone( DBObject dbObject, RXObject ownerObject, IdMapping idMap, bool isPrimary )
{
DBObject result = base.WblockClone( dbObject, ownerObject, idMap, isPrimary );
if( IsDatabaseApplicable( ownerObject ) )
{
try
{
using( ResultBuffer xdata = GetXData( dbObject ) )
{
if( xdata != null )
{
T source = dbObject as T;
if( source != null )
{
T clone = result as T;
if( clone != null )
OnCloned( source, xdata, clone, ownerObject, idMap, isPrimary );
}
}
}
}
catch( System.Exception ex )
{
throw ex;
}
}
return result;
}
/// <summary>
///
/// You can override this to get control after the clone has
/// been created by either DeepClone() or WblockClone() (the
/// context indicating which can be deduced from the idMap),
/// and act on the clone and the xdata from the source object.
/// This will only be called if the source object has XData
/// for the given application name.
///
/// This is where you can resolve xdata on cloned objects in
/// those cases where the data has some special requirement,
/// such as having values that must be unique, or values in
/// xdata of copied objects that have some dependence on the
/// corresponding value in a source object.
///
/// </summary>
/// <param name="source">The source object that was cloned</param>
/// <param name="xdata">The source object's XData for the given application name</param>
/// <param name="clone">The clone of the source object</param>
/// <param name="owner">The owner object which the clone was appended to, or the owning Database</param>
/// <param name="idMap">The id map</param>
/// <param name="isPrimary">IsPrimary</param>
protected virtual void OnCloned( T source, ResultBuffer xdata, T clone, RXObject owner, IdMapping idMap, bool isPrimary )
{
}
}