Synchronization with external storages
The mechanism in Creatio for synchronization with external data storages is the Sync Engine. This mechanism enables you to create, modify, and delete Entity in the system based on synchronization with external systems and export data to external systems.
Synchronization is performed by using the SyncAgent class implemented in the Terrasoft.Sync namespace of the application kernel.
Classes used in the synchronization mechanism
- Synchronization agent (SyncAgent) — a class with one public Synchronize method that triggers synchronization between storages.
- Synchronization context (SyncContext) — a class representing the aggregation of providers and metadata for SyncAgent.
- Storage — storage of synchronized data.
- Local storage (LocalProvider) — enables you to work with LocalItem in Creatio.
- External storage (RemoteProvider) — an external service or application from which data is synchronized with Creatio.
- Synchronization item (SyncItem) — a set of objects from external and local storage which are synchronized.
- External storage synchronization item (RemoteItem) — represents a set of data from external storage that syncs automatically. It can consist of one or more entities (records) from the external storage.
- Synchronization item (SyncEntity) — a wrapper of a specific Entity. SyncEntity is required to work with Entity as the synchronizing object (add, delete, change).
- Synchronization item (LocalItem) — one or more objects from Creatio that are synchronized with the external storage as a unit. The synchronization item from the external storage, converted in the LocalItem entity contains a set of instances of the SyncEntity class.
- SysSyncMetadata metadata table — contains service information of the synchronized elements and is essentially RemoteItem-LocalItem interchanges table.
General synchronization algorythm
Before starting synchronization, you must create an instance of SyncAgent and the SyncContext sync context, then update records in the metadata table with data from Creatio. For this, you need to call the CollectChangesInSyncedEntities class method that implements the IReplicaMetadata interface.
The algorithm for updating metadata records is the following:
- If any previously synchronized entity in Creatio has been modified since the last synchronization, then the corresponding record in the metadata changes its modification date, the LocalState property is set to “Modified”, and the source of the modification is set to the LocalStore ID.
- If a synchronized entity in Creatio has been deleted since the last synchronization — the corresponding record in the metadata LocalState is set to “Deleted".
- If there is no corresponding record in the metadata for the entity in Creatio — it is ignored.
The process of synchronizing storages then starts the following:
- All changes from the external storage are requested alternately.
- You need to obtain the metadata for each item in the external storage .
- If the metadata can not be obtained — this is a new item which should be converted to a Creatio element. To fill in a synchronization object, a FillLocalItem method is called from the specific RemoteItem instance. It is also recorded in the metadata (Id of the external storage, the element Id in the external storage, date of creation and modification is set as current, the source of creation and modification — external storage).
- If the metadata is received, so this item has already been synchronized with Creatio. You need to go to the version conflict resolution. By default, the last change in the application or external storage (RemoteProvider) has the priority.
- The metadata for the current pair of synchronization items is actualized.
After looking through all the modified items from the external storage, the elements that were changed in Creatio, but was not changed in the external storage remain in the metadata (in the interval between the last and the current synchronization).
- You need to get elements changed in Creatio in the interval between the last synchronization and the current synchronization.
- Save the changes in the external storage.
- You must update the modification date of the items in the metadata (Creatio is the change source).
After that, you need to add new, not synchronized records to the external storage, and add metadata for new items.
Synchronization description
An activity and participants are synchronized into one Google-calendar task. An activity (Activity) and participants (SyncEntity) are one element of the synchronization - LocalItem.
RemoteItem - Google task received outside Creatio. LocalItem - a set of objects (SyncEntity), to which the Google task is converted.
The synchronization schema:
Working with synchronization metadata
The auxiliary SysSyncMetaData metadata table is used for synchronization, which is the junction between the outer RemoteItem table (synchronizing element in external storage) and LocalItem (synchronization element in Creatio). Each table row is represented in the system as an instance of SysSyncMetaData class.
Only information about synchronized elements is stored in the metadata.
There can be multiple metadata table records for a single synchronization element - one for each application object included in a synchronization element.
Activity and participants — a single synchronization element. However, the metadata contains one record for each activity and one record for each participant.
Currently, only one object from external storage is transformed into several Creatio objects:
The metadata system for a single synchronization element is represented as the ItemMetadata object class (SysSyncMetaData element collection). Metadata management is carried out through the class that implements the IReplicaMetadata interface. An instance of the class that implements the IReplicaMetadata interface is created via the MetaDataStore factory class for a particular storage.
Namespace Terrasoft.Sync
A class representing the aggregation of providers and metadata for SyncAgent.
Properties
The current date and time synchronization in UTC. Set after the metadata update.
The date and time of the last synchronization in UTC.
Enables you to work with LocalItem.
The object that enables messages to be saved into the integration log.
Message logger for synchronization engine.
External service or application, data from which is synchronized with Creatio.
Works with metadata.
User connection.
Methods
Writes error message to the log.
operation | Log action for an object synchronization. |
direction | Synchronization direction. |
format | Format. |
exception | Exception that caused this error. |
args | Format parameters. |
Writes information message to the log.
operation | Log action for an object synchronization. |
direction | Synchronization direction. |
format | Format. |
args | Format parameters. |
Sets entity lookup column value.
entity | Instance of the Terrasoft.Core.Entities.Entity class. |
lookupColumnName | Lookup column name. |
lookupDisplayValue | Lookup column display value. |
lookupDisplayColumnName | Lookup display column name. |
Namespace Terrasoft.Sync
RemoteProvider — a basic class that enables you to work with an external storage. In fact, it is the only way to work with external systems.
Properties
ID of external storage that will be synchronized.
Date and time of the last synchronization in UTC.
External storage element schema.
Number of items processed from external storage.
Number of items processed from local storage.
Methods
Returns a collection of all types that implement the IRemoteItem interface. SyncAgent builds the SyncItemSchema instances that describe the entities that participate in synchronization.
Applies changes to external storage element.
context | Synchronization context. |
syncItem | Item of the synchronization. |
Called after processing changes in external and local storage. Intended for the implementation of necessary additional steps for the specific integration implementation.
Returns an enumeration of new and modified elements of external storage.
Fills in the IRemoteItem instance with data from external storage.
schema | Synchronization schema. |
id | Unique foreign key. |
itemMetadata | Synchronization metadata. |
Creates a new instance of IRemoteItem.
Returns an enumeration of new Creatio entities that will be synchronized with external storage.
Resolves conflicts between changed elements of local and external storages. By default, (RemoteProvider) priority is given to changes in Creatio.
Returns the sign that checks whether there is a need to update the metadata before starting synchronization.
Returns synchronization elements changed in the local store since the last synchronization.
context | Synchronization Context. |
modifiedItemsEsq | Request to the scheme object Terrasoft.Core.Configuration.SysSyncMetaData. |
Namespace Terrasoft.Sync
Interface of the external storage element.
The class that implements the IRemoteItem interface is an indivisible unit of synchronization and represents one element of the synchronization of the external data storage. This class is a container for data coming from an external system, and it knows how to convert the data to the Entity entity, and vice versa. The interface contains two methods: FillLocalItem and FillRemoteItem for converting external synchronization elements (IRemoteItem) to LocalItem, and vice versa.
Methods
Fills in properties of an element of the LocalItem local storage with values of external storage element. Used to apply changes in local storage.
Fills in properties of an element of external storage with element values from the LocalItem local storage. Used to apply changes in external storage.
MapAttribute class
Namespace Terrasoft.Sync
The Map attribute decorates the IRemoteItem interface implementations.
Properties
Object schema name.
The entity processing order for the synchronization element.
A flag that indicates that this schema is a key element of this synchronization element. It can be installed in one schema only.
The schema name of the main object. It can not be set in tandem with IsPrimarySchema.
The column name for the connection with the details of the main object It can not be set in tandem with IsPrimarySchema.
It specifies the synchronization direction for the objects of this type. By default - DownloadAndUpload.
If it contains the Download flag - the changes will not apply to Creatio.
If it does not contain the Upload flag - the new entities will not be selected from Creatio.
The names of the columns that will be loaded from the local storage.
Examples of using map attribute
SchemaName is the main parameter. This is the name of the EntitySchema that is included in the current synchronization element.
This class declaration task from Google Calendar will sync with the activity and its participants from Creatio.
The second parameter, Order, specifies in which order Entity will be stored in the local storage. Activityis indicated first, because ActivityParticipant stores a link to the created activity.
In most cases, SyncAgent can automatically generate a request for a sample of the new elements of synchronization with Creatio. To do this, you must specify the basic entity and method of communication with the details:
In this case, a request for new activities will be sent to the database along with a request for each selected activity to receive their participants.
Namespace Terrasoft.Sync.
Local Storage (LocalProvider) - encapsulates the work with data in internal storage (Creatio).
LocalProvider - basic class that implements work with the local storage. Enables you to work with LocalItem.
Properties
ID of Local storage that will be synchronized.
Maximum items in select count.
UserConnection instance.
Methods
Adds EntitySchemaQuery column specified in EntityConfig.
esqForFetching | Instance of the Terrasoft.Core.Entities.EntitySchemaQuery class. |
entityConfig | Instance of the Terrasoft.Sync.EntityConfig class. |
Applies changes to each SyncEntity in LocalItem.
context | Synchronization context. |
entities | Items of the synchronization. |
Loads a collection of entities associated with a particular synchronization element.
itemMetaData | Synchronization element. |
itemSchema | Synchronization schema. |
loadAllColumns | If true, loads all entities columns, loads only columns from entity EntityConfig instance. |
Namespace Terrasoft.Sync.
The SyncEntity class encapsulates Entity instance and all the necessary actions to perform the synchronization of the instance properties.
Properties
The name of the schema for which the wrapper is created.
Entity for which the wrapper is created.
The last action performed on the entity (0 - not changed, 1 - new, 2 - changed 3 - deleted).
Action to be done with the entity.
Additional information that can be bound to a specific entity by an external provider for a specific purpose.
Namespace Terrasoft.Sync
Synchronization element entity settings schema.
Properties
Entity type name.<
Entity type.
Synchronization element entity settings.
Synchronization element entity settings list.
Synchronization element detail entity settings list.
It specifies the synchronization direction for the objects of this type. By default - DownloadAndUpload.
If it does not contain the Download flag, changes will not be applied in Creatio.
If it does not contain the Upload flag, new entities will not be selected from Creatio.
Methods
It creates a configuration element synchronization entity with all the settings of the related entities.
The method checks that EntityConfig is well-formed.
If authentication fails, an exception is applied. If the EntityConfig schema name is specified twice, DublicateDataException is generated. If the name of the defunct schema is specified, InvalidSyncItemSchemaException is generated).
Namespace Terrasoft.Sync.
Synchronization element entity settings.
Properties
Object schema name.
The order of processing entities for a synchronization element. The lower the value, the sooner the entity will be processed in the processing of the synchronization element.
It specifies the synchronization direction for the objects of this type. By default - DownloadAndUpload.
If it does not contain the Download flag, changes will not be applied in Creatio.
If it does not contain the Upload flag, new entities will not be selected from Creatio.
The names of the columns that will be loaded from the local storage. If the value is not specified, it will load all object columns.
Namespace Terrasoft.Sync.
One or more objects from Creatio that are synchronized with external storage as a unit. It contains a set of SyncEntity class instances.
Properties
The SyncEntity collection that is set in accordance with a SyncItem. It contains a collection of "key-value" pairs, where the key is the schema name, and the value is the SyncEntity collection of the scheme.
The highest value of the date and time of the modification of all Entities in LocalItem.
Synchronization element entity settings schema.
The auxiliary SysSyncMetaData metadata table is used for synchronization, which is the junction between the outer RemoteItem table (synchronizing element in external storage) and LocalItem (synchronization element in Creatio). Each table row is represented in the system as an instance of SysSyncMetaData class.
Properties
Element ID in external storage.
Synchronized element schema name.
Element ID in local storage.
It indicates whether an item has been removed from the local storage since the last synchronization. The parameter is updated before the synchronization and application of changes in the local storage. On the basis of its value, when selecting modified elements from local storage, the SyncEntity state is set. Obsolete, left for compatibility. LocalState is currently used.
It indicates whether an item has been removed from the external storage since the last synchronization. Obsolete, left for compatibility. RemoteState is currently used.
Date of the last element modification.
Id of storage in which the last modification was performed.
Id of storage in which the synchronization element was created.
Id of external storage with which the element was synchronized.
Additional element parameters.
Element state in local storage (0 - not modified, 1 - new, 2 - modified, 3 - deleted).
Element state in external storage (0 - not modified, 1 - new, 2 - modified, 3 - deleted).
Namespace Terrasoft.Sync.
Creates the specific class instance that implements the IReplicaMetadata interface for a storage.
Namespace Terrasoft.Sync.
Class implementing the IReplicaMetadata interface, encapsulates the synchronization metadata and works with ItemMetadata objects.
Methods
Finds and returns the ItemMetadata synchronization metadata object by an ID in the remoteItemId external storage.
Updates metadata after synchronization.
oldItemMetaDatas | Metadata instance from previous synchronization. |
remoteItem | Remote item instance. |
localItem | Local item instance. |
changesToBpm | Current synchronization direction. |
Returns a collection of ItemMetadata objects that have been modified since the last synchronization and not processed during the current synchronization session.
Updates metadata for synchronization elements modified in Creatio. If an element has been modified since the last SysMetadata synchronization, the Version column will be filled in with the date of element modification. The ActualizeSysSyncMetaData procedure is used to update metadata.
userConnection | Terrasoft.Core.UserConnection Instance. |
schemaName | Sync entity name. |
lastSyncVersion | Last synchronization date. |
Creates new records in the SysSyncMetaData table for the synchronization element details.
userConnection | User connection. |
detailEntityConfig | Instance of the Terrasoft.Sync.DetailEntityConfig class. |
remoteItemName | Remote storage item name. |
lastSyncVersion | Last synchronization date. |
Returns the element ID in the external remoteId storage from metadata by a unique localId synchronization element in Creatio. If an element is marked as deleted, the remoteId wil not be returned, and the method will return false.
Returns additional extraParameters parameters for the synchronization element by localId. If extraParameters are not found, the method returns false.