All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rahulsom.grooves.api.OnSpec Maven / Gradle / Ivy

package com.github.rahulsom.grooves.api;

import com.github.rahulsom.grooves.api.events.BaseEvent;
import com.github.rahulsom.grooves.api.snapshots.Snapshot;
import com.github.rahulsom.grooves.queries.QuerySupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class OnSpec<
        AggregateIdT,
        AggregateT extends AggregateType,
        EventIdT,
        EventT extends BaseEvent,
        SnapshotIdT,
        SnapshotT extends Snapshot> {
    private AggregateT aggregate;
    private Consumer entityConsumer;
    private Supplier timestampSupplier;
    private Supplier userSupplier;
    private Supplier positionSupplier;

    private static final Logger log = LoggerFactory.getLogger(OnSpec.class);

    /**
     * Applies an event to an aggregate. This involves checking if any important fields are
     * missing and populating them based on the suppliers.
     *
     * @param event The event to be applied
     *
     * @return The event after persisting
     */
    public  T apply(T event) {
        event.setAggregate(aggregate);

        if (event.getCreatedBy() == null) {
            event.setCreatedBy(userSupplier.get());
        }
        if (event.getPosition() == null) {
            event.setPosition(positionSupplier.get());
        }
        if (event.getTimestamp() == null) {
            event.setTimestamp(timestampSupplier.get());
        }

        entityConsumer.accept(event);

        return event;
    }

    /**
     * Computes and persists a snapshot based on a QueryUtil on the aggregate that this
     * OnSpec applies on
     *
     * @param       The type of Query to be executed
     * @param query         The Query Util to compute the snapshot
     * @param beforePersist Code to execute before persisting the snapshot.
     *
     * @return The snapshot after persisting
     */
    public > SnapshotT snapshotWith(
                    QueryT query, Consumer beforePersist) {

        SnapshotT snapshotT = query
                .computeSnapshot(aggregate, Long.MAX_VALUE)
                .toBlocking()
                .single();

        beforePersist.accept(snapshotT);
        entityConsumer.accept(snapshotT);

        log.info("Persisted {}", snapshotT);

        return snapshotT;
    }

    /**
     * Computes and persists a snapshot based on a QueryUtil on the aggregate that this
     * OnSpec applies on.
     *
     * @param  The type of Query to be executed
     * @param query    The Query Util to compute the snapshot
     *
     * @return The snapshot after persisting
     */
    public > SnapshotT snapshotWith(QueryT query) {
        return snapshotWith(query, snapshotT -> {
        });
    }

    public AggregateT getAggregate() {
        return aggregate;
    }

    public void setAggregate(AggregateT aggregate) {
        this.aggregate = aggregate;
    }

    public void setEntityConsumer(Consumer entityConsumer) {
        this.entityConsumer = entityConsumer;
    }

    public void setTimestampSupplier(Supplier timestampSupplier) {
        this.timestampSupplier = timestampSupplier;
    }

    public void setUserSupplier(Supplier userSupplier) {
        this.userSupplier = userSupplier;
    }

    public void setPositionSupplier(Supplier positionSupplier) {
        this.positionSupplier = positionSupplier;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy