RS Solution Pseudo Code

To help you understand the flow of the TIBCO Scribe® RS Solution, refer to the following pseudo code. This example illustrates the flow of the RS process, and the interaction between the RS Source and Target Connectors:

void MainProcess()
{
    // connect
    SourceConnector.Connect();
    TargetConnector.Connect();
 
    // inform source that we are starting replication
    SourceConnector.InitReplication(null);
 
    // get a list of the object types to be replicated
    var objectDefList = SourceConnector.GetObjectDefinitionList(null);
    foreach(var objectDef in objectDefList)
    {
        // make sure target replication object has been created and is current
        var sourceFullObjectDef = SourceConnector.GetObjectDefinition(objectDef);
        var targetFullObjectDef = TargetConnector.GetObjectDefinition(objectDef);
        if (targetFullObjectDef == null) // target replication object not found
        {
            // inform source that we are replicating this object
            SourceConnector.InitReplication(objectDef);
            // create target replication object 
            TargetConnector.CreateOrUpdateObjectForReplication(sourceFullObjectDef);
        }
        else if (targetFullObjectDef != sourceFullObjectDef)
        {
            // update target replication object
            TargetConnector.CreateOrUpdateObjectForReplication(sourceFullObjectDef);
        }
 
        // call next process to replicate the data for the current object
        ReplicateData(objectDef);
    }
 
    // disconnect
    SourceConnector.Disconnect();
    TargetConnector.Disconnect();
}
 
void ReplicateData(object objectDef)
{
    // get the timestamp of the last entity synced from the target
    var lastReplicationDate = TargetConnector.GetLastReplicationSyncDate(objectDef);
 
    // query the source for all data changed since the lastReplicationDate
    var dataEntities = SourceConnector.GetReplicationData(lastReplicationDate);
 
    // walk the query results replicating the data
    foreach(var dataEntity in dataEntities)
    {
        // attempt to insert the data into the target 
        var success = TargetConnector.Create(dataEntity);
 
        if(!success) // insert failed, attempt to replace the row
        {
            // remove
            TargetConnector.Delete(dataEntity);
 
            // replace
            success = TargetConnector.Create(dataEntity);
 
            // log an error on failure
            if (!success)
            {
                LogRowError(objectDef);
            }
        }
    }
 
    // call next process to soft delete any records deleted since last execution
    ProcessDeletes(objectDef, lastReplicationDate);
}
 
void ProcessDeletes(object objectDef,DateTime lastReplicationDate)
{
    if (lastReplicationDate != DateTime.MinValue) // we have valid replication data
    {
        // query the source for all deleted data since the lastReplicationDate
        var deletedEntities = SourceConnector.GetChangeHistoryData(objectDef, lastReplicationDate);
 
        // walk the query results marking the deleted records as deleted in the target
        foreach (var deletedEntity in deletedEntities)
        {
               TargetConnector.Update(deletedEntity);
        }
    }
    else // either an initial sync or an object with no last modified timestamp field
    {
        // update all records not updated by the current execution as 
        // deleted in the target
        TargetConnector.Update(objectDef, lastReplicationDate);
    }
}