Create Scenarios from Files

This task imports a list of files into new scenarios, supporting several formats.

    @Bean
    public ScriptedTaskDescription scenariosImportTask() {
        ScriptedTaskDescription task = new ScriptedTaskDescription("ScenariosImportTask", "Create scenarios from files");
        task.setDescription("Import a list of files into new scenarios, supporting several formats");
        setI18nKeys(task, "SCENARIOS_IMPORT");

        var folder = VariableAccessExpression.ofFolder();
        var scenarioFiles = VariableAccessExpression.ofFile();
        var scenarioCreationParameters = VariableAccessExpression.ofScenarioCreationExpression();

        // Variables for ForeachStatement
        var scenarioFile = VariableAccessExpression.of("scenarioFile");
        var createdScenarios = VariableAccessExpression.of("createdScenarios");

        task.getScript()
            .addStatement(AskInputStatement.ofVariable(folder, true, JobInputType.FOLDER_IN_WORKSPACE, "The Folder in a Workspace where to import the new Scenarios"))
            .addStatement(AskInputStatement.ofVariable(scenarioFiles, true, JobInputType.files(1, null, "xlsx", "dbrf", "gz", "zip"), "The Scenario files containing the data (.xlsx, .dbrf, .gz, .zip)"))
            .addStatement(AskInputStatement.ofVariable(scenarioCreationParameters, true, JobInputType.SCENARIO_CREATION_PARAMETERS, "Parameters to create the Scenario"))
            .addStatement(ForeachStatement.of(
                scenarioFile.getVariableName(),
                scenarioFiles,
                // Create the new scenario using metadata if exists.
                SetVariableStatement.of(
                    createdScenarios.getVariableName(),
                    ScenarioCreationExpression.of(
                            scenarioCreationParameters,
                            folder
                        )
                        .withFileForProperties(scenarioFile)
                        .withScenarioNamePrefix(
                            StringExpression.concat(
                                StringExpression.ofFileName(scenarioFile),
                                StringExpression.of(" - ")
                            ))
                ),

                // Import the scenarios or delete them if an error occurs
                DoStatementOnLockedScenariosOrDeleteThem.of(
                    createdScenarios,
                    // Save the file content in the data service
                    ImportScenarioStatement.of(createdScenarios, scenarioFile)
                )
            ));

        return task;
    }