/*
 * 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 org.spine3.examples.todolist.context;

import org.spine3.examples.todolist.repositories.TaskAggregateRepository;
import org.spine3.examples.todolist.repositories.TaskLabelAggregateRepository;
import org.spine3.server.BoundedContext;
import org.spine3.server.event.enrich.EventEnricher;

import javax.annotation.Nullable;
import java.util.Optional;

/**
 * Serves as a repository provider for all the application components
 * that are initialized before the {@link BoundedContext}.
 *
 * <p> As long as the {@code Aggregate} and {@code Projection} repositories
 * require the instance of the {@code BoundedContext} for their own initialization,
 * they are all {@code null} before the {@code BoundedContext} is built.
 *
 * <p> However, some of the app components (such as {@link EventEnricher}) at the same time
 * <ul>
 *     <li> need repository instances for own operation, thus requiring them for instantiation,
 *     <li> are involved into the {@code BoundedContext} initialization.
 * </ul>
 *
 * <p>In order to break such a cyclic dependency,  the instances of {@code TodoListRepositoryProvider}
 * serve as providers of the repository instances in "lazy" mode.
 *
 * @author Illia Shepilov
 */
class TodoListRepositoryProvider {

    @Nullable
    private TaskAggregateRepository taskAggregateRepository;
    @Nullable
    private TaskLabelAggregateRepository labelAggregateRepository;

    void setTaskAggregateRepository(TaskAggregateRepository taskAggregateRepo) {
        this.taskAggregateRepository = taskAggregateRepo;
    }

    void setLabelAggregateRepository(TaskLabelAggregateRepository labelAggregateRepo) {
        this.labelAggregateRepository = labelAggregateRepo;
    }

    /**
     * Returns the wrapped {@link TaskAggregateRepository} in the {@code Optional}.
     *
     * @return the {@code Optional}
     */
    Optional<TaskAggregateRepository> getTaskAggregateRepository() {
        final Optional<TaskAggregateRepository> result = Optional.ofNullable(this.taskAggregateRepository);
        return result;
    }

    /**
     * Returns the wrapped {@link TaskLabelAggregateRepository} in the {@code Optional}.
     *
     * @return the {@code Optional}
     */
    Optional<TaskLabelAggregateRepository> getLabelAggregateRepository() {
        final Optional<TaskLabelAggregateRepository> result = Optional.ofNullable(this.labelAggregateRepository);
        return result;
    }
}

@@ -1106,33 +1106,34 @@
 itories.Task
-Aggregate
+Definition
 Repository;%0A
@@ -1186,33 +1186,25 @@
 es.TaskLabel
-Aggregate
+s
 Repository;%0A
@@ -2215,16 +2215,69 @@
 lov%0A */%0A
+//TODO:2017-02-06:illiashepilov: is it still needed?%0A
 class To
@@ -2330,33 +2330,30 @@
 private Task
-Aggregate
+Labels
 Repository t
@@ -2355,35 +2355,26 @@
 ory task
-AggregateRepository
+LabelsRepo
 ;%0A    @N
@@ -2401,57 +2401,47 @@
 Task
-LabelAggregateRepository labelAggregateRepository
+DefinitionRepository taskDefinitionRepo
 ;%0A%0A 
@@ -2451,33 +2451,30 @@
 void setTask
-Aggregate
+Labels
 Repository(T
@@ -2472,33 +2472,30 @@
 ository(Task
-Aggregate
+Labels
 Repository t
@@ -2493,33 +2493,30 @@
 ository task
-Aggregate
+Labels
 Repo) %7B%0A    
@@ -2532,43 +2532,31 @@
 task
-AggregateRepository = taskAggregate
+LabelsRepo = taskLabels
 Repo
@@ -2576,30 +2576,30 @@
 void set
-LabelAggregate
+TaskDefinition
 Reposito
@@ -2601,38 +2601,34 @@
 ository(Task
-LabelAggregate
+Definition
 Repository l
@@ -2665,32 +2665,26 @@
 his.
-labelAggregateRepository
+taskDefinitionRepo
  = l
@@ -2751,33 +2751,30 @@
  %7B@link Task
-Aggregate
+Labels
 Repository%7D 
@@ -2862,33 +2862,30 @@
 ptional%3CTask
-Aggregate
+Labels
 Repository%3E 
@@ -2891,35 +2891,26 @@
  getTask
-AggregateRepository
+LabelsRepo
 () %7B%0A   
@@ -2933,25 +2933,22 @@
 nal%3CTask
-Aggregate
+Labels
 Reposito
@@ -2989,35 +2989,26 @@
 his.task
-AggregateRepository
+LabelsRepo
 );%0A     
@@ -3074,38 +3074,34 @@
  %7B@link Task
-LabelAggregate
+Definition
 Repository%7D 
@@ -3189,38 +3189,34 @@
 ptional%3CTask
-LabelAggregate
+Definition
 Repository%3E 
@@ -3222,32 +3222,26 @@
  get
-LabelAggregateRepository
+TaskDefinitionRepo
 () %7B
@@ -3272,22 +3272,18 @@
 Task
-LabelAggregate
+Definition
 Repo
@@ -3328,32 +3328,26 @@
 his.
-labelAggregateRepository
+taskDefinitionRepo
 );%0A 

Rework `TodoListRepositoryProvider` according to the aggregate parts.