package com.capgemini.reports.builder;

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

import java.util.Map;

/**
 * Mother class for R15/17 Report Builders. Part id & revision are safely extracted here.
 *
 * @param <T> the real report type (R15/17)
 */
public abstract class AbstractR15R17Builder<T extends AbstractExecutionR15R17<T>> extends AbstractR15R16R17Builder<T> {

    @Override
    protected T make(Map<ReportCriteriaEnum, String> data, String language) throws InsufficientParametersException {
        // Get the request raw data
        String partId = data.get(ReportCriteriaEnum.PART_ID);
        String partRevision = data.get(ReportCriteriaEnum.PART_REVISION);

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

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

    protected abstract T make(String language, String partId, String partRevision);

}
