package com.capgemini.reports.builder;

import com.capgemini.reports.database.javabean.enumeration.ReportCriteriaEnum;
import com.capgemini.reports.definition.r15r16r17reports.AbstractExecutionR15R16R17;
import com.capgemini.reports.exceptions.InsufficientParametersException;
import com.google.common.base.Joiner;

import java.util.Map;

/**
 * Mother class for R15/16/17 Report Builders. Language parameters is safely extracted here.
 *
 * @param <T> the real report type (R15/16/17)
 */
public abstract class AbstractR15R16R17Builder<T extends AbstractExecutionR15R16R17<T>> implements ReportBuilder {

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

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

        // Build the report
        return make(data, language);
    }

    protected abstract T make(Map<ReportCriteriaEnum, String> data, String language) throws InsufficientParametersException;

}
