package com.capgemini.reports.r10;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;

import com.capgemini.reports.common.AbstractSubmitServlet;
import com.capgemini.reports.definition.r10report.ExecutionR10;
import com.capgemini.reports.reports.parameters.EffectivityDate;
import com.capgemini.reports.reports.parameters.Generics;
import com.capgemini.reports.reports.parameters.MonoMulti;
import com.capgemini.reports.reports.parameters.ProductUnit;

/**
 * This class receives demands to perform a R10 report.<br>
 * It prepares the data and creates the appropriate {@link ExecutionR10} instance that will carry out the job.
 */
@WebServlet("/report_r10/submit")
public class SubmitServletR10 extends AbstractSubmitServlet<ExecutionR10> {

    private static final long serialVersionUID = 6034599018215480837L;

    @Override
    public ExecutionR10 prepareReport(final HttpServletRequest request) {

        // Obtaining report parameters
        String productID = request.getParameter("product_id");
        String unitStr = request.getParameter("unit");
        String generic = request.getParameter("generic");
        String genericAssemb = request.getParameter("generic_assemb");
        String effectivityStr = request.getParameter("datepicker");
        String monoMultiStr = request.getParameter("mono_multi");

        // Checking product-unit relationship
        ProductUnit productUnit = new ProductUnit(productID, unitStr);

        // Checking mono-multi
        MonoMulti monoMulti = MonoMulti.build(monoMultiStr);

        // Checking generics
        Generics genericParam = new Generics(productUnit, generic, genericAssemb);

        // Checking effectivity
        EffectivityDate effectivity = new EffectivityDate(effectivityStr);

        return new ExecutionR10(genericParam, monoMulti, effectivity);
    }
}
