Interface ChangeHook

  • All Known Implementing Classes:
    DefaultChangeHook

    public interface ChangeHook
    ChangeHooks allow to listen to changes of persistent data. They are registered via ModelBuilder.changeHooks when creating the model. Registering hooks later is not supported.

    Methods of hooks are called synchronously when the change actually occurs, by the thread that does the change. If a method of a hook fails with an exception, that exception is "thrown through" to the call causing the change. If you want to postpone your action until the current transaction commits, use either pre- or post-commit hooks.

    Note that any new change to persistent data (i.e. other than returning modified setValues) done in one of the methods will cause this hook to be called again. Without care you may end up with infinite recursion.

    Synchronization
    There is only one instance of the ChangeHook for each model, and calls to ChangeHooks are not synchronized. If multiple threads concurrently change persistent data, then methods of this hook are called concurrently.

    It is highly recommended to override toString with an informative message about the hook. This message is returned by Model.getChangeHookString().

    See Also:
    ChangeHooks
    • Method Detail

      • beforeNew

        default SetValue<?>[] beforeNew​(Type<?> type,
                                        SetValue<?>[] setValues)
        Is called before any item creation. You may change the values of the newly created item by returning changed setValues. The default implementation does nothing and returns setValues unmodified.
      • afterNew

        default void afterNew​(Item item)
        Is called after any item creation. The default implementation does nothing.
        See Also:
        Item.afterNewCopeItem()
      • beforeSet

        default SetValue<?>[] beforeSet​(Item item,
                                        SetValue<?>[] setValues)
        Is called before any item modification. The default implementation does nothing.
        Parameters:
        setValues - is never null and never empty
        Returns:
        must not return null
        See Also:
        Item.beforeSetCopeItem(SetValue[])
      • beforeDelete

        default void beforeDelete​(Item item)
        Is called before any item deletion. The default implementation does nothing.
        See Also:
        Item.beforeDeleteCopeItem()