/*
 * 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.server.aggregate;

import org.spine3.server.BoundedContext;

import java.lang.reflect.Constructor;

import static org.spine3.server.entity.Entities.createAggregatePartEntity;
import static org.spine3.server.entity.Entities.getAggregatePartConstructor;

/**
 * Common abstract base for repositories that manage {@code AggregatePart}s.
 *
 * @author Alexander Yevsyukov
 */
public abstract class AggregatePartRepository
        <I, A extends AggregatePart<I, ?, ?>, R extends AggregateRoot<I>>
        extends AggregateRepository<I, A> {

    private final R root;

    /**
     * {@inheritDoc}
     */
    protected AggregatePartRepository(BoundedContext boundedContext, R root) {
        super(boundedContext);
        this.root = root;
    }

    /**
     * {@inheritDoc}
     */
    @Override // to expose this method in the same package.
    protected Class<I> getIdClass() {
        return super.getIdClass();
    }

    @SuppressWarnings("MethodDoesntCallSuperMethod") // The call of the super method is not needed.
    @Override
    public A create(I id) {
        final Constructor<A> entityConstructor =
                getAggregatePartConstructor(getEntityClass(), root.getClass(), getIdClass());
        final A result = createAggregatePartEntity(entityConstructor, id, root);
        return result;
    }

    @SuppressWarnings("MethodDoesntCallSuperMethod") // The call of the super method is not needed.
    @Override
    protected Constructor<A> getEntityConstructor() {
        final Constructor<A> result =
                getAggregatePartConstructor(getEntityClass(), root.getClass(), getIdClass());
        return result;
    }
}

@@ -1161,29 +1161,34 @@
 .server.
-entity.Entiti
+aggregate.Aggregat
 es.creat
@@ -1245,51 +1245,54 @@
 ver.
-entity.Entities.getAggregatePartConstructor
+aggregate.Aggregates.createAggregateRootEntity
 ;%0A%0A/
@@ -1454,25 +1454,16 @@
 pository
-%0A        
 %3CI, A ex
@@ -1494,36 +1494,8 @@
 , ?%3E
-, R extends AggregateRoot%3CI%3E
 %3E%0A  
@@ -1555,22 +1555,49 @@
 e final 
-R root
+Class%3CAggregateRoot%3CI%3E%3E rootClass
 ;%0A%0A    /
@@ -1699,75 +1699,507 @@
 text
-, R root) %7B%0A        super(boundedContext);%0A        this.root = root
+) %7B%0A        super(boundedContext);%0A        rootClass = getAggregateRootClass();%0A    %7D%0A%0A    private %3CR extends AggregateRoot%3CI%3E%3E Class%3CR%3E getAggregateRootClass() %7B%0A        @SuppressWarnings(%22unchecked%22)%0A        // It is OK because %60getAggregateRootClass%60 method returns the %60R extends AggregateRoot%3CI%3E%60.%0A        // Where I is ID of the entity is passed with %60getEntityClass()%60.%0A        final Class%3CR%3E rootClass = (Class%3CR%3E) Aggregates.getAggregateRootClass(getEntityClass());%0A        return rootClass
 ;%0A  
@@ -2570,32 +2570,49 @@
 ructor =
-%0A       
+ getEntityConstructor();%0A
         
  getAggr
@@ -2595,36 +2595,38 @@
 ctor();%0A        
- get
+final 
 AggregatePartCon
@@ -2622,71 +2622,83 @@
 gate
-PartConstructor(getEntityClass(), root.getClass(), getId
+Root%3CI%3E root = createAggregateRootEntity(id, getBoundedContext(), root
 Class
-()
 );%0A 
@@ -3029,16 +3029,27 @@
         
+Aggregates.
 getAggre
@@ -3094,19 +3094,13 @@
 root
-.get
 Class
-()
 , ge

Rework `AggregatePartRepository`.