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

com.github.rahulsom.grooves.grails.BlockingEventSource Maven / Gradle / Ivy

There is a newer version: 0.6.1
Show newest version
package com.github.rahulsom.grooves.grails;

import com.github.rahulsom.grooves.api.AggregateType;
import com.github.rahulsom.grooves.api.events.BaseEvent;
import com.github.rahulsom.grooves.api.snapshots.Snapshot;
import com.github.rahulsom.grooves.queries.QuerySupport;
import com.github.rahulsom.grooves.queries.internal.BaseQuery;
import org.grails.datastore.gorm.GormEntity;
import rx.Observable;

import java.util.Date;
import java.util.List;

import static com.github.rahulsom.grooves.grails.QueryUtil.*;
import static org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod;
import static rx.Observable.defer;
import static rx.Observable.from;

/**
 * Supplies Events from a Blocking Gorm Source.
 *
 * @param   The aggregate over which the query executes
 * @param     The type of the Event's id field
 * @param       The type of the Event
 * @param  The type of the Snapshot's id field
 * @param    The type of the Snapshot
 *
 * @author Rahul Somasunderam
 */
public interface BlockingEventSource<
        AggregateIdT,
        AggregateT extends AggregateType,
        EventIdT,
        EventT extends BaseEvent & GormEntity,
        SnapshotIdT,
        SnapshotT extends Snapshot,
        QueryT extends BaseQuery
        > extends QuerySupport {

    @Override
    default Observable getUncomputedEvents(
            AggregateT aggregate, SnapshotT lastSnapshot, long version) {
        final long position =
                lastSnapshot == null || lastSnapshot.getLastEventPosition() == null ? 0 :
                        lastSnapshot.getLastEventPosition();
        //noinspection unchecked
        return defer(() -> from((List) invokeStaticMethod(
                getEventClass(),
                UNCOMPUTED_EVENTS_BY_VERSION,
                new Object[]{aggregate, position, version, INCREMENTAL_BY_POSITION}
        )));
    }

    @Override
    default Observable getUncomputedEvents(
            AggregateT aggregate, SnapshotT lastSnapshot, Date snapshotTime) {
        final Date lastEventTimestamp = lastSnapshot.getLastEventTimestamp();
        final String method = lastEventTimestamp == null ?
                UNCOMPUTED_EVENTS_BEFORE_DATE :
                UNCOMPUTED_EVENTS_BY_DATE_RANGE;
        final Object[] params = lastEventTimestamp == null ?
                new Object[]{aggregate, snapshotTime, INCREMENTAL_BY_TIMESTAMP} :
                new Object[]{aggregate, lastEventTimestamp, snapshotTime, INCREMENTAL_BY_TIMESTAMP};
        //noinspection unchecked
        return defer(() ->
                from((List) invokeStaticMethod(getEventClass(), method, params)));
    }

    /**
     * The class of events that this returns.
     *
     * @return The class of events
     */
    Class getEventClass();

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy