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 :-
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);
}
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");
}
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));
}
}
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());
}
Note: 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.
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 the Updated field in this example. In this case we need at least 2 columns to have changed to send the results to the server.
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 we use this to decide whether to disable the item for synchronisation.
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);
}
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);
}
}