package com.capgemini.reports.builder;

import com.capgemini.reports.database.javabean.enumeration.ReportCriteriaEnum;
import com.capgemini.reports.definition.r13report.execution.AbstractExecutionR13;
import com.capgemini.reports.exceptions.InsufficientParametersException;
import com.capgemini.reports.reports.parameters.EffectivityDate;

import java.util.Map;

/**
 * Reconstructs a R13 (CR) report from two delimiter dates.
 * 
 * @param <T> the sub-type of R13 report (CR/FPP)
 */
public abstract class AbstractR13Builder<T extends AbstractExecutionR13<T, ?>> implements ReportBuilder {

    @Override
    public T build(Map<ReportCriteriaEnum, String> data) throws InsufficientParametersException {
        // Get the request raw data
        String dateDebutStr = data.get(ReportCriteriaEnum.DATE_DEBUT);
        String dateFinStr = data.get(ReportCriteriaEnum.DATE_FIN);

        // no mandatory args
        EffectivityDate dateDebut = dateDebutStr == null ? null : new EffectivityDate(dateDebutStr); // defautlt is null
        EffectivityDate dateFin = dateFinStr == null ? new EffectivityDate() : new EffectivityDate(dateFinStr); // default is no args Ctor

        // Build the report
        return this.make(dateDebut, dateFin);
    }

    /**
     * Create an Execution instance of the right type, using the verified parameters as-is.
     * 
     * @param dateDebut a beginning date (checked)
     * @param dateFinStr an end date (checked)
     * @return the Execution.
     */
    protected abstract T make(EffectivityDate dateDebut, EffectivityDate dateFinStr);

}
