package com.capgemini.reports.builder;

import com.capgemini.reports.database.javabean.enumeration.ReportCriteriaEnum;
import com.capgemini.reports.definition.r26report.ExecutionR26;
import com.capgemini.reports.exceptions.InsufficientParametersException;
import com.capgemini.reports.reports.parameters.EffectivityDate;
import com.google.common.base.Joiner;

import java.util.Map;

/** Builds a R26 report from part Id and effectivity date (initialized to sysdate). */
public class R26Builder implements ReportBuilder {

    @Override
    public ExecutionR26 build(Map<ReportCriteriaEnum, String> data) throws InsufficientParametersException {
        // Get the request raw data
        String component = data.get(ReportCriteriaEnum.PART_ID);
        String effectivityStr = data.get(ReportCriteriaEnum.EFFECTIVITY_DATE);

        // Check presence of all required args
        if (component == null) {
            String dataStr = Joiner.on('|').withKeyValueSeparator("=").useForNull("null").join(data);
            throw new InsufficientParametersException("Missing parameter for R26 launch. Obtained: " + dataStr);
        }

        EffectivityDate anEffectivity = new EffectivityDate(effectivityStr);

        // Build the report
        return new ExecutionR26(component, anEffectivity);
    }
}
