package com.capgemini.reports.r15;

import com.capgemini.reports.auto.AbstractAutoLaunchServlet;
import com.capgemini.reports.definition.r15r16r17reports.r15r17reports.r15report.AttachDatasetR15Action;
import com.capgemini.reports.definition.r15r16r17reports.r15r17reports.r15report.ExecutionR15;
import com.capgemini.reports.exceptions.InsufficientParametersException;
import com.capgemini.reports.execution.actions.DoNothingAction;
import com.capgemini.reports.execution.actions.IPreExecutionStep;
import com.capgemini.reports.execution.strategies.AutomaticExecutionStrategy;

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

/** A Servlet which listens to calls from a workflow. */
@WebServlet("/autoR15real")
public class R15AutoServlet extends AbstractAutoLaunchServlet<ExecutionR15> {

    private static final long serialVersionUID = -8586399099581830304L;

    private static final String URL_PARAMETER_PART_ID = "itemID";
    private static final String URL_PARAMETER_PART_REV = "revID";

    // Intercept the request: The WKF sends the Part UID, the the report is expecting partID & partRev.
    // A Forward is required to add arguments
    @Override
    protected ExecutionR15 processFunctionalParams(final HttpServletRequest aRequest) throws InsufficientParametersException {
        // Parse part ID
        String partID = aRequest.getParameter(URL_PARAMETER_PART_ID);
        if (partID == null || partID.isEmpty()) {
            throw new InsufficientParametersException("No part ID was provided in URL");
        }
        // Parse part REV
        String revID = aRequest.getParameter(URL_PARAMETER_PART_REV);
        if (revID == null || revID.isEmpty()) {
            throw new InsufficientParametersException("No part Revision was provided in URL");
        }

        // Make the report
        ExecutionR15 exec = new ExecutionR15(partID, revID);
        exec.isWkf();
        return exec;
    }

    @Override
    protected void setupSchedulingStrategy(String relaunchURL, ExecutionR15 report) {
        IPreExecutionStep specializedPreAction = new DoNothingAction();
        report.setExecutionStrategy(new AutomaticExecutionStrategy(relaunchURL, specializedPreAction, new AttachDatasetR15Action()));
    }

}
