/*
 * Copyright 2017, TeamDev Ltd. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.server.aggregate;

import com.google.protobuf.Message;
import io.spine.core.ActorMessageEnvelope;
import io.spine.core.Event;
import io.spine.server.entity.EntityMessageEndpoint;
import io.spine.server.entity.LifecycleFlags;

import java.util.List;

/**
 * Abstract base for endpoints handling messages sent to aggregates.
 *
 * @param <I> the type of aggregate IDs
 * @param <A> the type of aggregates
 * @param <M> the type of message envelopes
 * @param <R> the type of the dispatch result, which is {@code <I>} for unicast dispatching, and
 *            {@code Set<I>} for multicast
 * @author Alexander Yevsyukov
 */
abstract class AggregateMessageEndpoint<I,
                                        A extends Aggregate<I, ?, ?>,
                                        M extends ActorMessageEnvelope<?, ?, ?>, R>
        extends EntityMessageEndpoint<I, A, M, R> {

    AggregateMessageEndpoint(AggregateRepository<I, A> repository, M envelope) {
        super(repository, envelope);
    }

    /**
     * Dispatched the message to the aggregate with the passed ID.
     *
     * @param aggregateId the ID of the aggregate to which dispatch the message
     */
    @Override
    protected void dispatchToOne(I aggregateId) {

        final M envelope = envelope();
        final AggregateEndpointDelivery<I, A, M> delivery = getEndpointDelivery(envelope);
        if(!delivery.shouldPostpone(aggregateId, envelope)) {
            deliverNow(aggregateId);
        }
    }

    void deliverNow(I aggregateId) {
        final A aggregate = repository().loadOrCreate(aggregateId);
        final LifecycleFlags flagsBefore = aggregate.getLifecycleFlags();

        final List<? extends Message> eventMessages = doDispatch(aggregate, envelope());
        final AggregateTransaction tx = startTransaction(aggregate);
        aggregate.apply(eventMessages, envelope());
        tx.commit();

        // Update lifecycle flags only if the message was handled successfully and flags changed.
        final LifecycleFlags flagsAfter = aggregate.getLifecycleFlags();
        if (flagsAfter != null && !flagsBefore.equals(flagsAfter)) {
            storage().writeLifecycleFlags(aggregateId, flagsAfter);
        }

        store(aggregate);
    }

    protected abstract AggregateEndpointDelivery<I, A, M> getEndpointDelivery(M envelope);

    protected AggregateTransaction startTransaction(A aggregate) {
        return AggregateTransaction.start(aggregate);
    }

    @Override
    protected void onModified(A entity) {
        repository().onModifiedAggregate(envelope().getTenantId(), entity);
    }

    @Override
    protected boolean isModified(A aggregate) {
        final List<Event> events = aggregate.getUncommittedEvents();
        return !events.isEmpty();
    }

    @Override
    protected AggregateRepository<I, A> repository() {
        return (AggregateRepository<I, A>)super.repository();
    }

    private AggregateStorage<I> storage() {
        return repository().aggregateStorage();
    }
}

@@ -2020,24 +2020,334 @@
 pe);%0A    %7D%0A%0A
+    /**%0A     * Obtains an instance of endpoint delivery for the given envelope.%0A     *%0A     * @param envelope the envelope to obtain an instance of delivery for%0A     * @return the instance of endpoint delivery%0A     */%0A    protected abstract AggregateEndpointDelivery%3CI, A, M%3E getEndpointDelivery(M envelope);%0A%0A
     /**%0A    
@@ -3583,100 +3583,8 @@
  %7D%0A%0A
-    protected abstract AggregateEndpointDelivery%3CI, A, M%3E getEndpointDelivery(M envelope);%0A%0A
     

Add missing Javadoc.