1 package com.reallifedeveloper.common.application.notification;
2
3 import java.time.ZonedDateTime;
4
5 import org.checkerframework.checker.nullness.qual.Nullable;
6
7 import com.reallifedeveloper.common.domain.ErrorHandling;
8 import com.reallifedeveloper.common.domain.event.DomainEvent;
9
10 /**
11 * A notification is information about a domain event that has occurred, and that has been stored in an
12 * {@link com.reallifedeveloper.common.application.eventstore.EventStore}. The notification can be sent to external systems that need to be
13 * informed about the event.
14 *
15 * @param eventType the type of the event
16 * @param storedEventId the stored event ID, must not be {@code null}
17 * @param occurredOn the timestamp the event occurred
18 * @param event the domain event that occurred, must not be {@code null}
19 *
20 * @author RealLifeDeveloper
21 */
22 public record Notification(@Nullable String eventType, Long storedEventId, @Nullable ZonedDateTime occurredOn, DomainEvent event) {
23
24 /**
25 * Creates a new {@code Notification}.
26 *
27 * @throws IllegalArgumentException if {@code event} or {@code storedEventId} is {@code null}
28 */
29 public Notification {
30 ErrorHandling.checkNull("Arguments must not be null: event=%s, storedEventId=%s", event, storedEventId);
31 }
32
33 /**
34 * A factory method that sets the {@code eventType} to the name of the event class and {@code occurredOn} to the date and time when the
35 * event occurred.
36 *
37 * @param event the event to store and use to set {@code eventType} and {@code occurredOn}
38 * @param storedEventId the stored event ID to store
39 *
40 * @return a new {@code Notification}
41 *
42 * @throws IllegalArgumentException if {@code event} or {@code storedEventId} is {@code null}
43 */
44 public static Notification create(DomainEvent event, Long storedEventId) {
45 ErrorHandling.checkNull("Arguments must not be null: event=%s, storedEventId=%s", event, storedEventId);
46 return new Notification(event.getClass().getName(), storedEventId, event.eventOccurredOn(), event);
47 }
48
49 }