. . .

Data Model Change Watcher

The data change notification watcher is a new function, which allows you to develop a native module that watches for changes on registered classes of another module (or the module itself).

Therefore you can react on CRUD actions without having a used-module dependency.

Adding a watcher

You can register a watcher by calling:

Register a listener
MyModule.AOM.watchDataModel( "OtherModule", "OtherModel", r, EventType.values( ) );

This call would register a data model watcher from "MyModule" (in the given version) on the "OtherModel" class within the "OtherModule". The example will register for all available EventTypes, but you can also register a subset of types (f.i. only for CREATE and/or DELETE).

Note that the registration needs to be done for each app backend separately. A call to watchDataModel will only register the watcher for the app backend given in the request object.

Get Notifications

To implement the logic for the watcher, add this method to your module main class:

Receiving notifications
@Override
public void onDataModelChange( final ChangeEvent event, final Request r )
{
// put some logic here
// you'll get informed about the changes of "OtherModel" in "OtherModule"
}

The ChangeEvent object will contain the information, which Module and Model was changed, a string list of affected IDs (or foreignIds in case of transient classes) and the type of the event. In case of CREATE, READ and DELETE, the id list will only contain the ID of the affected object.

The application where this change was applied to is contained in the request object.

The notification method will be called according to the following behavior:
CREATE, READ, READ_ALL, UPDATE, DELETE for the according calls to the model, over the REST interface, as well for native module calls. Note that some actions within the native module trigger additional actions by default. For example: a save() call to update an object will also trigger a READ internally.
Adding and removing a reference to an object will be an UPDATE on the object (which holds the reference). Getting references of an object will trigger a READ_ALL event for the reference class.

A notification will only be called, if the executed action was successful.

Removing a watcher and automatic deregistration

You can also deregister this watcher again by calling:

Register a listener
MyModule.AOM.unwatchDataModel( "OtherModule", "OtherModel", r, EventType.values( ) );

The registrations are not persisted and will be removed if the whole server cluster gets offline. Please re-register existing registrations for example within the onDeployForAppBackend-Hook.
If the module which watches for changes gets undeployed (or redeployed) (this is the module which did the registration), the existing registrations are removed.
If the module which is watched for changes (the "OtherModule" in the above example) gets undeployed (or redeployed), the registrations will stay, as there is no possibility to get informed about these changes in the watching module.
Multiple registration calls for the same module and class and from the same module and version will only lead to a single registration and therefore also notify one time.