/*
 * 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.Any;
import com.google.protobuf.Message;
import io.spine.client.Query;
import io.spine.core.Event;
import io.spine.core.Events;
import io.spine.system.server.SystemEventFactory;
import io.spine.system.server.SystemGateway;

import java.util.Iterator;
import java.util.Optional;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @author Dmytro Dashenkov
 */
final class FilteringGateway implements SystemGateway {

    private final SystemGateway delegate;
    private final EventFilter filter;

    private FilteringGateway(SystemGateway delegate, EventFilter filter) {
        this.delegate = delegate;
        this.filter = filter;
    }

    static SystemGateway atopOf(SystemGateway delegate, EventFilter filter) {
        checkNotNull(delegate);
        checkNotNull(filter);
        return new FilteringGateway(delegate, filter);
    }

    @Override
    public void postCommand(Message systemCommand) {
        delegate.postCommand(systemCommand);
    }

    @Override
    public void postEvent(Message systemEvent) {
        SystemEventFactory eventFactory = SystemEventFactory.forMessage(systemEvent, false);
        Event event = eventFactory.createEvent(systemEvent, null);
        Optional<Event> filtered = this.filter.filter(event);
        filtered.map(Events::getMessage)
                .ifPresent(delegate::postEvent);
    }

    @Override
    public Iterator<Any> readDomainAggregate(Query query) {
        return delegate.readDomainAggregate(query);
    }
}

@@ -1415,16 +1415,131 @@
 l;%0A%0A/**%0A
+ * A %7B@link SystemGateway%7D which applies the given %7B@link EventFilter%7D to system events before%0A * posting them.%0A *%0A
  * @auth
@@ -1854,324 +1854,1084 @@
     
-static SystemGateway atopOf(SystemGateway delegate, EventFilter filter) %7B%0A        checkNotNull(delegate);%0A        checkNotNull(filter);%0A        return new FilteringGateway(delegate, filter);%0A    %7D%0A%0A    @Override%0A    public void postCommand(Message systemCommand) %7B%0A        delegate.postCommand(systemCommand);
+/**%0A     * Creates a new instance of %7B@code FilteringGateway%7D atop of the given gateway delegate and%0A     * %7B@link EventFilter%7D instances.%0A     *%0A     * @param delegate%0A     *         the %7B@link SystemGateway%7D which performs all the operations for the resulting gateway%0A     * @param filter%0A     *         the %7B@link EventFilter%7D to apply to the system events before posting%0A     * @return new instance of %7B@code FilteringGateway%7D%0A     */%0A    static SystemGateway atopOf(SystemGateway delegate, EventFilter filter) %7B%0A        checkNotNull(delegate);%0A        checkNotNull(filter);%0A        return new FilteringGateway(delegate, filter);%0A    %7D%0A%0A    /**%0A     * %7B@inheritDoc%7D%0A     *%0A     * %3Cp%3EBefore posting a system event, applies the given %7B@link EventFilter%7D to it. The event is%0A     * posted only if the filter allows it.%0A     *%0A     * %3Cp%3EIf the filter alters the event message, the altered version is posted. The internal event%0A     * parameters, such as ID, context, etc. are preserved, i.e. if the filter alters them,%0A     * the %3Cb%3Eoriginal%3C/b%3E parameters are posted.
 %0A    
-%7D%0A
+ */
 %0A   
@@ -3305,24 +3305,143 @@
 nt);%0A    %7D%0A%0A
+    @Override%0A    public void postCommand(Message systemCommand) %7B%0A        delegate.postCommand(systemCommand);%0A    %7D%0A%0A
     @Overrid

Document FilteringGateway.