/*
 * 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 de.syquel.bushytail.helper.BushyTailCSRFProtectionHelper;
import de.syquel.bushytail.service.BushyTailEdmProvider;
import de.syquel.bushytail.service.BushyTailEntityProcessor;
import org.apache.commons.lang3.ClassUtils;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.edm.provider.CsdlSchema;
import org.apache.olingo.commons.api.edmx.EdmxReference;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.ODataHttpHandler;
import org.apache.olingo.server.api.ServiceMetadata;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The BushyTail class should be used by the developer.
 * <p/>
 * It converts JPA entities and {@link de.syquel.bushytail.controller.IBushyTailController}s to an Olingo OData Version 4 service.
 *
 * @author Clemens Bartz
 * @author Frederik Boster
 * @since 1.0
 */
public class BushyTail {

    private BushyTailCSRFProtectionHelper csrfProtectionHelper = new BushyTailCSRFProtectionHelper();

    private List<CsdlSchema> odataSchemas;
    private Map<Class<?>, IBushyTailController<?>> entityControllerMap;
    private Map<FullQualifiedName, Class<?>> entityTypeMap;

    BushyTail(List<CsdlSchema> odataSchemas, Map<Class<?>, IBushyTailController<?>> entityControllerMap, Map<FullQualifiedName, Class<?>> entityTypeMap) {
        this.odataSchemas = odataSchemas;
        this.entityControllerMap = entityControllerMap;
        this.entityTypeMap = entityTypeMap;
    }

    /**
     * Serve a request from a Servlet.
     *
     * @param req The HTTP request as given by {@link javax.servlet.http.HttpServlet}.
     * @param resp The HTTP response as given by {@link javax.servlet.http.HttpServlet}, which will be modified.
     */
    public void service(final HttpServletRequest req, final HttpServletResponse resp) {
        csrfProtectionHelper.process(req, resp);

        final OData oData = OData.newInstance();
        final ServiceMetadata edm = oData.createServiceMetadata(new BushyTailEdmProvider(), new ArrayList<EdmxReference>(0));

        final ODataHttpHandler handler = oData.createHandler(edm);
        handler.register(new BushyTailEntityProcessor(entityTypeMap, entityControllerMap));

        handler.process(req, resp);
    }

}

@@ -1691,19 +1691,92 @@
 ervice.%0A
+ * %3Cp/%3E%0A * You can create a new instance using %7B@link BushyTailBuilder%7D.%0A
  *%0A
-
  * @auth
@@ -1833,16 +1833,41 @@
 nce 1.0%0A
+ * @see BushyTailBuilder%0A
  */%0Apubl
@@ -1888,16 +1888,59 @@
 Tail %7B%0A%0A
+    /** The helper for CSRF protection. */%0A
     priv
@@ -2030,24 +2030,55 @@
 nHelper();%0A%0A
+    /** The list of schema. */%0A
     private 
@@ -2077,16 +2077,22 @@
 private 
+final 
 List%3CCsd
@@ -2109,24 +2109,72 @@
 dataSchemas;
+%0A%0A    /** The map of classes and controllers. */
 %0A    private
@@ -2170,24 +2170,30 @@
     private 
+final 
 Map%3CClass%3C?%3E
@@ -2235,24 +2235,67 @@
 ntrollerMap;
+%0A%0A    /** The mapp of FQDNs and classes. */
 %0A    private
@@ -2295,16 +2295,22 @@
 private 
+final 
 Map%3CFull

Add documentation to BushyTail.