Search in sources :

Example 6 with TenantAwareOperation

use of io.spine.server.tenant.TenantAwareOperation in project core-java by SpineEventEngine.

the class EventStore method appendAll.

/**
 * Appends the passed events to the history of events.
 *
 * <p>If the passed {@link Iterable} is empty, no action is performed.
 *
 * <p>If the passed {@linkplain Event Events} belong to the different
 * {@linkplain TenantId tenants}, an {@link IllegalArgumentException} is thrown.
 *
 * @param events the events to append
 */
public void appendAll(final Iterable<Event> events) {
    checkNotNull(events);
    final Optional<Event> tenantDefiningEvent = tryFind(events, Predicates.<Event>notNull());
    if (!tenantDefiningEvent.isPresent()) {
        return;
    }
    final Event event = tenantDefiningEvent.get();
    final TenantAwareOperation op = new EventOperation(event) {

        @Override
        public void run() {
            if (isTenantSet()) {
                // If multitenant context
                ensureSameTenant(events);
            }
            store(events);
        }
    };
    op.execute();
    logStored(events);
}
Also used : EventOperation(io.spine.server.tenant.EventOperation) Event(io.spine.core.Event) TenantAwareOperation(io.spine.server.tenant.TenantAwareOperation)

Example 7 with TenantAwareOperation

use of io.spine.server.tenant.TenantAwareOperation in project core-java by SpineEventEngine.

the class Stand method subscribe.

/**
 * Subscribes for all further changes of an entity state, which satisfies the {@link Topic}.
 *
 * <p>Once this instance of {@code Stand} receives an update of an entity
 * with the given {@code TypeUrl}, all such callbacks are executed.
 *
 * @param topic an instance {@link Topic}, defining the entity and criteria,
 *              which changes should be propagated to the {@code callback}
 */
public void subscribe(final Topic topic, final StreamObserver<Subscription> responseObserver) {
    topicValidator.validate(topic, responseObserver);
    final TenantId tenantId = topic.getContext().getTenantId();
    final TenantAwareOperation op = new TenantAwareOperation(tenantId) {

        @Override
        public void run() {
            final Subscription subscription = subscriptionRegistry.add(topic);
            responseObserver.onNext(subscription);
            responseObserver.onCompleted();
        }
    };
    op.execute();
}
Also used : TenantId(io.spine.core.TenantId) Subscription(io.spine.client.Subscription) TenantAwareOperation(io.spine.server.tenant.TenantAwareOperation)

Example 8 with TenantAwareOperation

use of io.spine.server.tenant.TenantAwareOperation in project core-java by SpineEventEngine.

the class CommandStore method updateStatus.

/**
     * Updates the status of the command with the business failure.
     *  @param commandEnvelope the command to update
     * @param failure the business failure occurred during command processing
     */
private void updateStatus(CommandEnvelope commandEnvelope, final Failure failure) {
    keepTenantId(commandEnvelope.getCommand());
    final TenantAwareOperation op = new Operation(this, commandEnvelope) {

        @Override
        public void run() {
            storage.updateStatus(commandId(), failure);
        }
    };
    op.execute();
}
Also used : CommandOperation(io.spine.server.tenant.CommandOperation) TenantAwareOperation(io.spine.server.tenant.TenantAwareOperation) TenantAwareOperation(io.spine.server.tenant.TenantAwareOperation)

Example 9 with TenantAwareOperation

use of io.spine.server.tenant.TenantAwareOperation in project core-java by SpineEventEngine.

the class RepositoryShould method createAndStoreEntities.

/**
 * Creates three entities in the repository.
 */
private void createAndStoreEntities() {
    final TenantAwareOperation op = new TenantAwareOperation(tenantId) {

        @Override
        public void run() {
            repository.initStorage(storageFactory);
            createAndStore("Eins");
            createAndStore("Zwei");
            createAndStore("Drei");
        }
    };
    op.execute();
}
Also used : TenantAwareOperation(io.spine.server.tenant.TenantAwareOperation)

Example 10 with TenantAwareOperation

use of io.spine.server.tenant.TenantAwareOperation in project core-java by SpineEventEngine.

the class AggregateRepositoryShould method throw_ISE_if_unable_to_load_entity_by_id_from_storage_index.

@Test(expected = IllegalStateException.class)
public void throw_ISE_if_unable_to_load_entity_by_id_from_storage_index() {
    // Store a valid aggregate.
    givenStoredAggregate();
    // Store a troublesome entity, which cannot be loaded.
    final TenantAwareOperation op = new TenantAwareOperation(newUuid()) {

        @Override
        public void run() {
            givenStoredAggregateWithId(ProjectAggregateRepository.troublesome.getId());
        }
    };
    op.execute();
    final Iterator<ProjectAggregate> iterator = repository.iterator(Predicates.<ProjectAggregate>alwaysTrue());
    // This should iterate through all and fail.
    Lists.newArrayList(iterator);
}
Also used : ProjectAggregate(io.spine.server.aggregate.given.AggregateRepositoryTestEnv.ProjectAggregate) TenantAwareOperation(io.spine.server.tenant.TenantAwareOperation) Test(org.junit.Test)

Aggregations

TenantAwareOperation (io.spine.server.tenant.TenantAwareOperation)18 CommandOperation (io.spine.server.tenant.CommandOperation)7 Event (io.spine.core.Event)4 TenantId (io.spine.core.TenantId)2 EventOperation (io.spine.server.tenant.EventOperation)2 Test (org.junit.Test)2 Subscription (io.spine.client.Subscription)1 Command (io.spine.core.Command)1 ProjectAggregate (io.spine.server.aggregate.given.AggregateRepositoryTestEnv.ProjectAggregate)1 TenantAwareFunction0 (io.spine.server.tenant.TenantAwareFunction0)1 Iterator (java.util.Iterator)1