/*
 * Copyright 2020, 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.system.server;

import com.google.common.annotations.VisibleForTesting;
import io.spine.base.EntityState;
import io.spine.server.BoundedContext;
import io.spine.server.entity.Repository;

import java.util.Optional;

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

/**
 * The default implementation of {@link SystemClient}.
 *
 * <p>This client uses the {@linkplain SystemReadSide#newInstance default} implementation of
 * the read side and the {@linkplain SystemWriteSide#newInstance default} implementation of
 * the write side.
 *
 * @see SystemContext#createClient()
 */
final class DefaultSystemClient implements SystemClient {

    private final SystemWriteSide writeSide;
    private final SystemReadSide readSide;
    private final SystemContext context;
    private final BoundedContext.InternalAccess contextAccess;

    DefaultSystemClient(SystemContext system) {
        this.context = checkNotNull(system);
        this.contextAccess = context.internalAccess();
        this.readSide = SystemReadSide.newInstance(context);
        this.writeSide = SystemWriteSide.newInstance(context);
    }

    @Override
    public SystemWriteSide writeSide() {
        return writeSide;
    }

    @Override
    public SystemReadSide readSide() {
        return readSide;
    }

    @Override
    public Optional<Repository<?, ?>> systemRepositoryFor(Class<? extends EntityState> stateClass) {
        if (!context.hasEntitiesWithState(stateClass)) {
            return Optional.empty();
        }
        return contextAccess.findRepository(stateClass);
    }

    @Override
    public void closeSystemContext() throws Exception {
        context.close();
    }

    @VisibleForTesting
    BoundedContext target() {
        return context;
    }
}

@@ -1806,71 +1806,8 @@
 ext;
-%0A    private final BoundedContext.InternalAccess contextAccess;
 %0A%0A  
@@ -1901,63 +1901,8 @@
 m);%0A
-        this.contextAccess = context.internalAccess();%0A
     
@@ -2432,28 +2432,108 @@
     
-return contextAccess
+Optional%3CRepository%3C?, ?%3E%3E result =%0A                context.internalAccess()%0A                       
 .fin
@@ -2557,16 +2557,39 @@
 Class);%0A
+        return result;%0A
     %7D%0A%0A 

Remove redundant field