Project Automation
The Project Automation feature allows you to run your own .NET C# code at certain points in the Data Sync process.
The Start() method allows you to adjust configuration settings on your source and target Data Connections before the project runs. This is useful to change a filter dynamically at runtime.
The End() Method allows you to run something when the process is complete like call a SQL Stored Procedure or Log the result somewhere else.
The standard events are as follows :-
- Start()
- BeforeCompare(ProjectAutomationLoadResult loadResult)
- AfterCompare(ProjectAutomationCompareResult compareResult)
- BeforeSynchronisation(ProjectAutomationCompareResult compareResult)
- End(ProjectAutomationResult result)
- Error(ProjectAutomationException e)
Set Data Source Properties
To set Data Source properties before the project completes the load process, write your code in the Start() method. You can access each side via the DataSourceA and DataSourceB properties.
For example to set the CommandWhere property on a SQL Connection.
public override void Start()
{
DataSourceA.CommandWhere = string.Format("[Updated] > '{0:yyyy-MM-dd HH:mm:ss}'", DateTime.Today);
}
Output Trace Messages
You can output a message to the log via the Trace object, WriteLine will include the message in the Log for Ouvvi whereas TraceError, TraceInformation and TraceWarning will only output to the Data Sync Output Window.
public override void Start()
{
Trace.WriteLine("Start");
}
Export Change Set
You can easily export the changes as a .NET Xml DataSet file via the GetChangeSetSerializer() method on the Compare Result object.
public override void BeforeSynchronisation(ProjectAutomationCompareResult compareResult)
{
if(compareResult.HasChanges)
{
compareResult.GetChangeSetSerializer().WriteXml(string.Format("C:\\Temp\\Project1\\Changes-{0:yyyyMMdd}{1}.xml", DateTime.UtcNow, Environment.TickCount));
}
}
Manually Modify Schema Map at Runtime
You can override the schema map at runtime to dynamically add/remove columns or replace the map completely. This allows you to support new columns at runtime without having to modify the project.
Add a method of override for the GetDataSchema(DataSchema schema)
method.
In this function you can adjust the schema, to bypass the current project schema you must return this schema or a new schema. For example using a simple helper function you can look at both systems and create a compatible schema where the columns match both sides by name.
public override DataSchema GetDataSchema(DataSchema schema)
{
return DataSourceA.GetDefaultMap().ToAutoMap(DataSourceB.GetDefaultMap());
}
If your data-sources do not return type information and unique key columns such as CSV files you will need to update the schema map to define the key column and any data types you need. Adjusting the schema map this way does not affect the project file.
Remove Items from the Compare Result
Using the Project Automation feature you can intercept the comparison results and disable items for synchronisation by setting the Sync property false. This has the same effect as un-checking items in the Data Sync UI manually.
With some basic C# code in Project Automation we can automatically remove items from the sync where there is only 1 change detected. Normally this would be a modified date column where the time might be different but no values have actually changed.
For this scenario we need at least 2 columns to have changed to send the results to the server e.g. Name column and Modified Date column.
The code below looks at the Updated items collection after the comparison phase and before the synchronisation. The row collection lists the changed columns for the row, and we use this to decide if the item for synchronisation should be disabled (unchecked).
public override void BeforeSynchronisation(ProjectAutomationCompareResult compareResult)
{
//Disable Update on all Items where there is only 1 change i.e. Date Modified
compareResult.Updated.Where(i => i.Row.Count == 1).ToList().ForEach(i => i.Sync = false);
}
Send an Email
You can send an email when a Data Sync project is finished by using Project Automation.
Simply implement the End method of the Automation Class and send an SMTP Email message like this.
public override void End(ProjectAutomationResult result)
{
if ( result.Success && result.HasChanges )
{
var smtpClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
smtpClient.EnableSsl = true;
smtpClient.Credentials = new System.Net.NetworkCredential("username", "password");
//Message to Send
string message = string.Format("New {0}, Updated {1}, Deleted {2}",
result.CompareResult.New.Count,
result.CompareResult.Updated.Count,
result.CompareResult.Deleted.Count);
//Send Message
smtpClient.Send("from@simego.com", "to@simego.com", "Synchronisation Result", message);
}
}