|
| 1 | +package org.folio.notes.domain.event; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 4 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 5 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 6 | +import java.util.UUID; |
| 7 | +import lombok.Data; |
| 8 | +import lombok.NoArgsConstructor; |
| 9 | + |
| 10 | +/** |
| 11 | + * Envelope for a mod-notes domain event. |
| 12 | + * |
| 13 | + * @param <T> the payload type carried in the {@code old} and {@code new} fields (a full entity snapshot). |
| 14 | + */ |
| 15 | +@Data |
| 16 | +@NoArgsConstructor |
| 17 | +@JsonInclude(JsonInclude.Include.NON_NULL) |
| 18 | +public class DomainEvent<T> { |
| 19 | + |
| 20 | + private UUID id; |
| 21 | + @JsonProperty("old") |
| 22 | + private T oldEntity; |
| 23 | + @JsonProperty("new") |
| 24 | + private T newEntity; |
| 25 | + private DomainEventType type; |
| 26 | + private String tenant; |
| 27 | + private String ts; |
| 28 | + |
| 29 | + @JsonCreator |
| 30 | + public DomainEvent(@JsonProperty("id") UUID id, |
| 31 | + @JsonProperty("old") T oldEntity, |
| 32 | + @JsonProperty("new") T newEntity, |
| 33 | + @JsonProperty("type") DomainEventType type, |
| 34 | + @JsonProperty("tenant") String tenant, |
| 35 | + @JsonProperty("ts") String ts) { |
| 36 | + this.id = id; |
| 37 | + this.oldEntity = oldEntity; |
| 38 | + this.newEntity = newEntity; |
| 39 | + this.type = type; |
| 40 | + this.tenant = tenant; |
| 41 | + this.ts = ts; |
| 42 | + } |
| 43 | + |
| 44 | + public static <T> DomainEvent<T> createEvent(UUID id, T newEntity, String tenant) { |
| 45 | + return new DomainEvent<>(id, null, newEntity, DomainEventType.CREATE, tenant, currentTs()); |
| 46 | + } |
| 47 | + |
| 48 | + public static <T> DomainEvent<T> updateEvent(UUID id, T oldEntity, T newEntity, String tenant) { |
| 49 | + return new DomainEvent<>(id, oldEntity, newEntity, DomainEventType.UPDATE, tenant, currentTs()); |
| 50 | + } |
| 51 | + |
| 52 | + public static <T> DomainEvent<T> deleteEvent(UUID id, T oldEntity, String tenant) { |
| 53 | + return new DomainEvent<>(id, oldEntity, null, DomainEventType.DELETE, tenant, currentTs()); |
| 54 | + } |
| 55 | + |
| 56 | + private static String currentTs() { |
| 57 | + return String.valueOf(System.currentTimeMillis()); |
| 58 | + } |
| 59 | +} |
0 commit comments