=== Domain events

:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
:page-partial:

Whenever a domain object (or list of domain objects) is to be rendered, the framework fires off multiple domain events for every property, collection and action of the domain object.
In the cases of the domain object's collections, the event that is fired is:

* hide phase: to check that the collection is visible (has not been hidden)

[NOTE]
====
Unlike actions and properties, collections are immutable and so there are no events emitted for disable, validate, pre-execute or post-execute.
====

Subscribers (which must be domain services) subscribe to events posted through the xref:refguide:applib:index/services/eventbus/EventBusService.adoc[EventBusService], and can influence each of these phases.


By default the event raised is `CollectionDomainEvent.Default`.
For example:

[source,java]
----
import lombok.Getter;
import lombok.Setter;

public class ToDoItem {

    @Collection()
    @Getter @Setter
    private SortedSet<ToDoItem> dependencies = ...

    // ...
}
----

The xref:refguide:applib:index/annotation/Collection.adoc#domainEvent[domainEvent()] element allows a custom subclass to be emitted allowing more precise subscriptions (to those subclasses) to be defined instead.

For example:

[source,java]
----
public class ToDoItem {

    public static class DependenciesCollectionEvent
            extends CollectionDomainEvent<ToDoItem, ToDoItem> { } // <.>
    @Collection(
        domainEvent=DependenciesCollectionEvent.class
    )
    @Getter @Setter
    private SortedSet<ToDoItem> dependencies = ...

    // ...
}
----

<.> inherit from `CollectionDomainEvent<T,E>` where `T` is the type of the domain object being interacted with, and `E` is the type of the element in the collection (both `ToDoItem` in this example)


The benefit is that subscribers can be more targetted as to the events that they subscribe to.

[NOTE]
====
The framework provides a no-arg constructor and will initialize the domain event using (non-API) setters rather than through the constructor.
This substantially reduces the boilerplate in the subclasses because no explicit constructor is required..
====

==== Subscribers

Subscribers (which must be domain services) subscribe to events posted through the xref:refguide:applib:index/services/eventbus/EventBusService.adoc[EventBusService].

Subscribers can be either coarse-grained (if they subscribe to the top-level event type):

[source,java]
----
import org.springframework.stereotype.Service;
import org.springframework.context.event.EventListener;

@Service
public class SomeSubscriber {
    @EventListener(CollectionDomainEvent.class)
    public void on(CollectionDomainEvent ev) {
        // ...
    }
}
----

or can be fine-grained (by subscribing to specific event subtypes):

[source,java]
----
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

@Service
public class SomeSubscriber {
    @EventListener(ToDoItem.DependenciesCollectionEvent.class)
    public void on(ToDoItem.DependenciesCollectionEvent ev) {
        // ...
    }
}
----

The subscriber's method is called (up to) 5 times:

* whether to veto visibility (hide)
* whether to veto usability (disable)
* whether to veto execution (validate) the element being added to/removed from the collection
* steps to perform prior to the collection being added to/removed from
* steps to perform after the collection has been added to/removed from

The subscriber can distinguish these by calling `ev.getEventPhase()`.
Thus the general form is:

[source,java]
----
 import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

@Service
public class SomeSubscriber {

    @EventListener(CollectionDomainEvent.class)
    public void on(CollectionDomainEvent ev) {
        switch(ev.getEventPhase()) {

            case HIDE:                      // <.>
                break;
            case DISABLE:                   // <.>
            case VALIDATE:
            case EXECUTING:
            case EXECUTED:
                break;
        }
    }
}
----

<.> call `ev.hide()` or `ev.veto("")` to hide the collection
<.> collections are immutable and so domain events are never called for these phases.


==== Default, Doop and Noop events

If the xref:refguide:applib:index/annotation/Collection.adoc#domainEvent[`domainEvent()] element is not explicitly specified (is left as its default value, `CollectionDomainEvent.Default`), then the framework will, by default, post an event.

If this is not required, then the `causeway.reflector.facet.collectionAnnotation.domainEvent.postForDefault` configuration collection can be set to "false"; this will disable posting.

On the other hand, if the `domainEvent` has been explicitly specified to some subclass, then an event will be posted.
The framework provides `CollectionDomainEvent.Doop` as such a subclass, so setting the `domainEvent` attribute to this class will ensure that the event to be posted, irrespective of the configuration collection setting.

And, conversely, the framework also provides `CollectionDomainEvent.Noop`; if `domainEvent` attribute is set to this class, then no event will be posted.

==== Raising events programmatically

Normally events are only raised for interactions through the UI.
However, events can be raised programmatically either by calling the xref:refguide:applib:index/services/eventbus/EventBusService.adoc[EventBusService] API directly, or by emulating the UI by wrapping the target object using the xref:refguide:applib:index/services/wrapper/WrapperFactory.adoc[WrapperFactory] domain service.

