IMetabaseUpdateUserEvents.OnBeforeApplyUpdate

Syntax

OnBeforeApplyUpdate(Update: IMetabaseUpdate);

Parameters

Update. Update applied to the current repository.

Description

The OnBeforeApplyUpdate method implements an event that occurs after preparation of objects for updating.

Comments

This event can be used to check if objects are ready for update and to solve any conflicts.

Example

Public Class CUpdateEvents: UpdateEvents
    Public Sub OnBeginUpdate(Update: IMetabaseUpdate);
    Begin
        Debug.WriteLine("Repository objects update start");
    End Sub OnBeginUpdate;

    Public Sub OnBeforeApplyUpdate(Update: IMetabaseUpdate);
    Var
        MB: IMetabase;
        Root: IMetabaseUpdateFolderNode;
        Node: IMetabaseUpdateNode;
        ObjectNode: IMetabaseUpdateObjectNode;
        State: IMetabaseUpdateObjectApplyState;
        CrInfo: IMetabaseObjectCreateInfo;
        MObj: IMetabaseObject;
        i: Integer;
    Begin
        MB := MetabaseClass.Active;
        Root := Update.RootFolder;
        For i := 0 To Root.Count - 1 Do
            Node := Root.Item(i);
            If Node.NodeType = MetabaseUpdateNodeType.Object Then
                ObjectNode := Node As IMetabaseUpdateObjectNode;
                State := ObjectNode.ApplyState;
                Select Case State.State
                    Case MetabaseUpdateObjectApplyState.Conflict, MetabaseUpdateObjectApplyState.ConflictObjectNotFound:
                        CrInfo := MB.CreateCreateInfo;
                        CrInfo.ClassID := ObjectNode.Object.ClassId;
                        CrInfo.Id := MB.GenerateId(ObjectNode.Object.Id);
                        CrInfo.Name := ObjectNode.Object.Name;
                        CrInfo.Parent := MB.Root;
                        CrInfo.SkipIdFormatCheck := True;
                        MObj := MB.CreateObject(CrInfo).Edit;
                        MObj.Save;
                        State.UpdateObject := MObj;
                        State.State := MetabaseUpdateObjectApplyState.EditExisting;
                End Select;
            End If;
        End For;
    End Sub OnBeforeApplyUpdate;

    Public Sub OnEndUpdate(Update: IMetabaseUpdate);
    Begin
        Debug.WriteLine("Object update is finished");
    End Sub OnEndUpdate;
End Class CUpdateEvents;

This example is a template for update unit. On applying the update that contains this unit, it is checked if the objects that correspond to repository objects are ready. Particularly, a conflict will be solved if the repository does not have an object, for which the Update Only type was set in update.

A new empty object with the same class is created in the repository to solve the conflict. This object is set as an updated object. The state of readiness of update object will be changed to Update Existing Object (MetabaseUpdateObjectApplyState.EditExisting).

NOTE. Remember that not all conflicts can be solved. In this example a condition of update of the object by key is ignored. If an object does not exist in repository, it will be created. Keys of the object in repository and in update are different.

See also:

IMetabaseUpdateUserEvents