/*
 * Copyright 2018, TeamDev. 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.entity;

import com.google.protobuf.Message;
import io.spine.core.Event;
import io.spine.core.EventEnvelope;
import io.spine.server.event.EventDispatcher;
import io.spine.server.integration.ExternalMessage;
import io.spine.server.integration.ExternalMessageDispatcher;
import io.spine.server.integration.ExternalMessageEnvelope;
import io.spine.server.route.EventRoute;
import io.spine.server.route.EventRouting;

import java.util.Set;

import static io.spine.protobuf.AnyPacker.unpack;

/**
 * Abstract base for repositories that deliver events to entities they manage.
 *
 * @param <I> the type of IDs of entities
 * @param <E> the type of entities
 * @param <S> the type of entity state messages
 * @author Alexander Yevsyukov
 */
public abstract class EventDispatchingRepository<I,
                                                 E extends AbstractVersionableEntity<I, S>,
                                                 S extends Message>
        extends DefaultRecordBasedRepository<I, E, S>
        implements EventDispatcher<I> {

    private final EventRouting<I> eventRouting;

    /**
     * Creates new repository instance.
     *
     * @param defaultFunction the default function for getting target entity IDs
     */
    protected EventDispatchingRepository(EventRoute<I, Message> defaultFunction) {
        super();
        this.eventRouting = EventRouting.withDefault(defaultFunction);
    }

    /**
     * Obtains the {@link EventRouting} schema used by the repository for calculating identifiers
     * of event targets.
     */
    protected final EventRouting<I> getEventRouting() {
        return eventRouting;
    }

    /**
     * {@inheritDoc}
     *
     * <p>{@linkplain io.spine.server.event.EventBus#register(io.spine.server.bus.MessageDispatcher)
     * Registers} itself with the {@code EventBus} of the parent {@code BoundedContext}.
     */
    @Override
    public void onRegistered() {
        super.onRegistered();
        getBoundedContext().registerEventDispatcher(this);
    }

    /**
     * Logs error into the repository {@linkplain #log() log}.
     *
     * @param envelope  the message which caused the error
     * @param exception the error
     */
    @Override
    public void onError(EventEnvelope envelope, RuntimeException exception) {
        logError("Error dispatching event (class: %s, id: %s", envelope, exception);
    }

    /**
     * Obtains an external event dispatcher for this repository.
     *
     * <p>This method should be overridden by the repositories, which are eligible
     * to handle external events. In this case the implementation would typically delegate
     * the dispatching of external events to the repository itself.
     *
     * <p>Such a delegate-based approach is chosen, since it's not possible to make
     * {@code EventDispatchingRepository} extend another
     * {@link io.spine.server.bus.MessageDispatcher MessageDispatcher} interface due to clashes
     * in the class hierarchy.
     *
     * @return the external event dispatcher
     */
    //protected abstract ExternalMessageDispatcher<I> createExternalEventDispatcher();

    /**
     * An abstract base for the external message dispatchers, enabling
     * the {@code EventDispatchingRepository} instances to handle external events.
     */
    protected abstract class AbstractExternalEventDispatcher
            implements ExternalMessageDispatcher<I> {

        @Override
        public Set<I> dispatch(ExternalMessageEnvelope envelope) {
            ExternalMessage externalMessage = envelope.getOuterObject();
            Event event = unpack(externalMessage.getOriginalMessage());
            EventEnvelope eventEnvelope = EventEnvelope.of(event);
            return EventDispatchingRepository.this.dispatch(eventEnvelope);
        }
    }
}

@@ -3417,753 +3417,8 @@
  %7D%0A%0A
-    /**%0A     * Obtains an external event dispatcher for this repository.%0A     *%0A     * %3Cp%3EThis method should be overridden by the repositories, which are eligible%0A     * to handle external events. In this case the implementation would typically delegate%0A     * the dispatching of external events to the repository itself.%0A     *%0A     * %3Cp%3ESuch a delegate-based approach is chosen, since it's not possible to make%0A     * %7B@code EventDispatchingRepository%7D extend another%0A     * %7B@link io.spine.server.bus.MessageDispatcher MessageDispatcher%7D interface due to clashes%0A     * in the class hierarchy.%0A     *%0A     * @return the external event dispatcher%0A     */%0A    //protected abstract ExternalMessageDispatcher%3CI%3E createExternalEventDispatcher();%0A%0A
     

Remove redundant comments