/*
 * Copyright 2016, Frederik Boster
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.syquel.bushytail;

import de.syquel.bushytail.controller.IBushyTailController;
import de.syquel.bushytail.factory.OlingoMetadataFactory;
import org.apache.commons.lang3.ClassUtils;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.edm.provider.CsdlSchema;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BushyTailBuilder {

    private Map<Class<?>, IBushyTailController<?>> entityControllerMap = new HashMap<Class<?>, IBushyTailController<?>>();
    private Map<FullQualifiedName, Class<?>> entityTypeMap = new HashMap<FullQualifiedName, Class<?>>();

    /**
     * Add a JPA entity and associate it with a business controller.
     * Determines the namespace and entity name automatically.
     *
     * @param <T> The class type of the JPA entity.
     * @param entityClass The class type of the JPA entity.
     * @param entityController The business controller which shall be associated to the JPA entity class to handle CRUDQ operations.
     */
    public <T> void addEntity(Class<T> entityClass, IBushyTailController<T> entityController) {
        String namespace = ClassUtils.getPackageName(entityClass);
        String entityName = entityClass.getSimpleName();

        addEntity(entityClass, entityName, namespace, entityController);
    }

    /**
     * Add a JPA entity and associate it with a business controller.
     *
     * @param <T> The class type of the JPA entity.
     * @param entityClass The class type of the JPA entity.
     * @param entityName The OData resource name for this entity class.
     * @param namespace The namespace of this OData resource.
     * @param entityController The business controller which shall be associated to the JPA entity class to handle CRUDQ operations.
     */
    public <T> void addEntity(Class<T> entityClass, String entityName, String namespace, IBushyTailController<T> entityController) {
        FullQualifiedName entityFQN = new FullQualifiedName(namespace, entityName);

        addEntity(entityClass, entityFQN, entityController);
    }

    /**
     * Add a JPA entity and associate it with a business controller.
     *
     * @param <T> The class type of the JPA entity.
     * @param entityClass The class type of the JPA entity.
     * @param entityFQN The unique full qualified name of this Odata resource.
     * @param entityController The business controller which shall be associated to the JPA entity class to handle CRUDQ operations.
     */
    public <T> void addEntity(Class<T> entityClass, FullQualifiedName entityFQN, IBushyTailController<T> entityController) {
        entityControllerMap.put(entityClass, entityController);
        entityTypeMap.put(entityFQN, entityClass);
    }

    /**
     * Build the odata metadata and other internal helper classes from the previously added JPA entity types via {@link #addEntity(Class, IBushyTailController)}.
     *
     * This method makes heavy use of reflection. Thus it is advised to execute this method only once in the lifetime of the application.
     */
    public BushyTail build() {
        OlingoMetadataFactory metadataFactory = new OlingoMetadataFactory();

        for (Map.Entry<FullQualifiedName, Class<?>> entity : entityTypeMap.entrySet()) {
            FullQualifiedName entityFQN = entity.getKey();
            Class<?> entityType = entity.getValue();

            metadataFactory.addEntity(entityFQN, entityType);
        }

        List<CsdlSchema> odataSchemas = metadataFactory.createSchema("");

        return new BushyTail(odataSchemas, entityControllerMap, entityTypeMap);
    }

}

@@ -981,16 +981,142 @@
 l.Map;%0A%0A
+/**%0A * Builder for a new %7B@link BushyTail%7D instance.%0A *%0A * @author Frederik Boster%0A * @author Clemens Bartz%0A * @since 1.0%0A */%0A
 public c
@@ -1148,24 +1148,30 @@
     private 
+final 
 Map%3CClass%3C?%3E
@@ -1281,16 +1281,22 @@
 private 
+final 
 Map%3CFull
@@ -3864,16 +3864,22 @@
         
+final 
 OlingoMe
@@ -3935,24 +3935,24 @@
 aFactory();%0A
-
 %0A        for
@@ -4033,32 +4033,38 @@
 ) %7B%0A            
+final 
 FullQualifiedNam
@@ -4106,16 +4106,22 @@
         
+final 
 Class%3C?%3E
@@ -4227,24 +4227,24 @@
      %7D%0A%0A
-
         
 List%3CCsd
@@ -4235,16 +4235,22 @@
         
+final 
 List%3CCsd

Document BushyTailBuilder.