|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HtmlFieldSyncSubscriber.cs" company="">
//
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace HtmlFieldsInReports
{
#region
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Common;
using Microsoft.TeamFoundation.Framework.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Server;
#endregion
public class HtmlFieldSyncSubscriber : ISubscriber
{
// ISubscriber - requested event types
// ISubscriber - name of the plugin
#region Public Properties
public string Name
{
get
{
return "BinaryDigit.Subscribers.HtmlFieldsInReports";
}
}
public SubscriberPriority Priority
{
get
{
return SubscriberPriority.Low;
}
}
#endregion
// ISubscriber - event handler
#region Public Methods and Operators
public EventNotificationStatus ProcessEvent(
TeamFoundationRequestContext requestContext, NotificationType notificationType, object notificationEventArgs, out int statusCode, out string statusMessage, out ExceptionPropertyCollection properties)
{
this.WriteInfo("In the EventNotificationStatus ProcessEvent method now", EventLogEntryType.Information);
string strDump = string.Empty;
statusCode = 0;
properties = null;
statusMessage = string.Empty;
try
{
if (notificationType == NotificationType.Notification && notificationEventArgs is WorkItemChangedEvent)
{
// change this object to be a type we can easily get into
var workItemEvent = notificationEventArgs as WorkItemChangedEvent;
string currentField = string.Empty;
IntegerField id = null;
foreach (IntegerField item in workItemEvent.CoreFields.IntegerFields)
{
if (string.Compare(item.Name, "ID", true) == 0)
{
id = item;
break;
}
}
if (id != null)
{
try
{
Uri uri = TFSFunctions.GetTFSUri(requestContext);
if (workItemEvent.TextFields != null)
{
var affectedWorkItems = new List<WorkItem>();
using (TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(uri))
{
var wit = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
foreach (TextField item in workItemEvent.TextFields)
{
if (item.Name.ToLower().EndsWith("_copy") && item.ReferenceName.ToLower().EndsWith("_copy"))
{
currentField = item.Name;
this.WriteInfo("In Copy area : " + item.Name, EventLogEntryType.Warning);
WorkItem wi = this.SetFieldValue(wit, id, item, uri);
if (wi != null)
{
affectedWorkItems.Add(wi);
}
}
}
if (affectedWorkItems.Count > 0)
{
wit.BatchSave(affectedWorkItems.ToArray());
}
}
}
else
{
return EventNotificationStatus.ActionPermitted;
}
}
catch (Exception ex)
{
this.WriteInfo("Failed to update field '" + currentField + "'.\n\n" + ex, EventLogEntryType.Error);
if (!string.IsNullOrEmpty(currentField))
{
statusMessage = "Failed to update field '" + currentField + "'.";
return EventNotificationStatus.ActionDenied;
}
}
}
}
}
catch (Exception ex)
{
// diagnostics
strDump = string.Format("There was a unhandled exception: {0} \n {1}", ex, ex.StackTrace);
this.WriteInfo(strDump, EventLogEntryType.Error);
}
return EventNotificationStatus.ActionPermitted;
}
public Type[] SubscribedTypes()
{
return new[] { typeof(WorkItemChangedEvent) };
}
// helper - dumping diagnostics
public void WriteInfo(string strDump, EventLogEntryType entryType)
{
var log = new EventLog();
log.Source = "TFS Services";
if (!EventLog.SourceExists(log.Source))
{
EventLog.CreateEventSource(log.Source, "Application");
}
string strMessage = string.Format("The TFS server plugin {0} provides the following logging information:\n\n{1}", Assembly.GetCallingAssembly().GetName().Name, strDump);
log.WriteEntry(strMessage, entryType);
}
#endregion
#region Methods
private WorkItem SetFieldValue(WorkItemStore wit, IntegerField id, TextField currentHtmlField, Uri uri)
{
WorkItem affectedWorkItem = null;
WorkItemCollection result = wit.Query("SELECT [System.Id] FROM WorkItems WHERE [System.Id] = " + id.NewValue);
foreach (WorkItem wi in result)
{
string fieldLookingFor = currentHtmlField.Name.Remove(currentHtmlField.Name.ToLower().IndexOf("_copy")).Trim();
foreach (Field item in wi.Fields)
{
if (string.Compare(item.Name, fieldLookingFor, true) == 0)
{
if (item.Value.ToString() != currentHtmlField.Value)
{
wi.Open();
item.Value = currentHtmlField.Value;
affectedWorkItem = wi;
}
break;
}
}
}
return affectedWorkItem;
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.