How to create an adapter for the TFS Integration Platform - Part VII: WIT Conflict Handling

Submitted by Robert MacLean on Thu, 06/03/2010 - 09:40

Note: This post is part of a series and you can find the rest of the parts in the series index.

The WIT adapter needs a custom conflict type and a custom conflict handler, really for no reason other than the platform expects it.

Conflict Handler

If you have no reason for a custom conflict handler, a simple implementation which allows for manual resolution can be created, which is what I have below.

public class SharePointWITGeneralConflictHandler : IConflictHandler
{
    public bool CanResolve(MigrationConflict conflict, ConflictResolutionRule rule)
    {
        return ConflictTypeHandled.ScopeInterpreter.IsInScope(conflict.ScopeHint, rule.ApplicabilityScope);
    }

    public ConflictResolutionResult Resolve(MigrationConflict conflict, ConflictResolutionRule rule, out List<MigrationAction> actions)
    {
        actions = null;

        if (rule.ActionRefNameGuid.Equals(new ManualConflictResolutionAction().ReferenceName))
        {
            return ManualResolve(out actions);
        }

        return new ConflictResolutionResult(false, ConflictResolutionType.Other);
    }

    public ConflictType ConflictTypeHandled
    {
        get;
        set;
    }

    private static ConflictResolutionResult ManualResolve(out List<MigrationAction> actions)
    {
        actions = null;
        return new ConflictResolutionResult(true, ConflictResolutionType.Other);
    }
}

Conflict Type

If you have no reason for a custom conflict type, you can do what I did which is to re-implement the generic one with even less features namely only supporting ManualConflictResolution and a very simple scope hint.

public class SharePointWITGeneralConflictType : ConflictType
{
    public static MigrationConflict CreateConflict(Exception exception)
    {
        return new MigrationConflict(
            new SharePointWITGeneralConflictType(),
            MigrationConflict.Status.Unresolved,
            exception.ToString(),
            CreateScopeHint(Guid.NewGuid().ToString()));
    }

    public static MigrationConflict CreateConflict(Exception exception, IMigrationAction conflictedAction)
    {
        return new SharePointWITGeneralConflictType().CreateConflict(exception.ToString(), CreateScopeHint(Guid.NewGuid().ToString()), conflictedAction);
    }

    public override Guid ReferenceName
    {
        get
        {
            return s_conflictTypeReferenceName;
        }
    }

    public override string FriendlyName
    {
        get
        {
            return s_conflictTypeFriendlyName;
        }
    }

    public override string Description
    {
        get
        {
            return s_conflictTypeDescription;
        }
    }

    public SharePointWITGeneralConflictType()
        : base(new SharePointWITGeneralConflictHandler())
    { }

    public static string CreateScopeHint(string sourceItemId)
    {
        return string.Format(CultureInfo.CurrentCulture, "/{0}/{1}", sourceItemId, Guid.NewGuid().ToString());
    }

    protected override void RegisterDefaultSupportedResolutionActions()
    {
        AddSupportedResolutionAction(new ManualConflictResolutionAction());
    }

    private static readonly Guid s_conflictTypeReferenceName = new Guid("{606531DF-231A-496B-9996-50F239481988}");
    private const string s_conflictTypeFriendlyName = "TFS WIT general conflict type";
    private const string s_conflictTypeDescription =
        "This conflict is detected when an unknown exception is thrown during Work Item data submission.";
}