TFS Warehouse Adapter
Folling code is to show how to add group name for a user http://social.msdn.microsoft.com/Forums/en-US/tfsreporting/thread/08dfdda9-1f3a-4411-a827-afaf48a945e5
TFSModel is defined in Get TFS Model and TFS Service Using TFS SDK
public sealed class AddGroupNameAdapter : IWarehouseAdapter
{
private bool m_stopRequested = false;
private IDataStore m_dataStore;
TFSModal tfs;
public AddGroupNameAdapter ()
{
}
public void Initialize(IDataStore ds)
{
m_dataStore = ds;
if (m_dataStore == null)
{
throw new Exception("Null data store.");
}
String url = Microsoft.TeamFoundation.Server.TeamFoundationApplication.TfsNameUrl;
tfs = new TFSModal(url);
if (tfs.TFServer == null)
{
throw new Exception("TF Server instance not obtained for TFS url: " + url);
}
}
// save schema changes to the data warehouse
public SchemaChangesResult MakeSchemaChanges()
{
SchemaChangesResult result = SchemaChangesResult.NoChanges;
// Check if the dimension attribute exists.
WarehouseConfig config = m_dataStore.GetWarehouseConfig();
Dimension personDim = config.GetDimension("Person");
// Check if the Changeset dimension exists.
// This should not happen, but another adapter may for some reason have deleted the Changeset dimension.
if (personDim == null)
{
throw new Exception("Person dimension does not exist!");
}
// Get dimension attribute.
Field dimensionAttribute = personDim.GetField("Group Name");
// Add attribute to schema.
if (dimensionAttribute == null)
{
// Check if adapter is requested to stop
if (m_stopRequested)
{
return SchemaChangesResult.StopRequested;
}
// Transactions are recommended when writing to the warehouse.
m_dataStore.BeginTransaction();
try
{
// Create the Policy Override Comment dimension attribute
Field field = new Field();
field.Name = "Group Name";
field.Type = "NVARCHAR";
field.Length = 128;
// Add this attribute to Changeset dimension
m_dataStore.AddDimensionField("Person", field);
m_dataStore.CommitTransaction();
}
catch
{
m_dataStore.RollbackTransaction();
throw;
}
result = SchemaChangesResult.ChangesComplete;
}
return result;
}
// save any new data changes in the operational store to the data warehouse
public DataChangesResult MakeDataChanges()
{
DataChangesResult result = DataChangesResult.NoChanges;
//A user in TFS may belong to multiple groups, so my suggestion is to get all users from your own method, each user contains "SID", and "GroupName".
var users=...
foreach(var user in users)
{
SaveGroupNameForUser(sid,groupname);
result = DataChangesResult.ChangesComplete;
}
m_dataStore.LogEvent(AdapterEventLevel.Informational, "Updated warehouse");
return result;
}
public void RequestStop()
{
m_stopRequested = true;
}
// Creates and saves a dimension member for Changeset.
private void SaveGroupNameForUser(string sid,string groupname)
{
DimensionMember dimMember = m_dataStore.CreateDimensionMember("Person");
//SID is the key field of Person
dimMember["SID"] = sid,
dimMember["Group Name"] = groupname
m_dataStore.BeginTransaction();
try
{
m_dataStore.SaveDimensionMember(dimMember);
m_dataStore.CommitTransaction();
}
catch
{
m_dataStore.RollbackTransaction();
throw;
}
}
}