Running a Job Creation Operation By Content Set with Runtime Parameters (Using JSON)

Problem

You want to run a job creation operation to produce a Job Set using a job creation preset, an existing set of Content Sets and runtime parameters as inputs.

Solution

The solution is to make a series of requests using the following URIs and method types to submit, monitor progress and ultimately retrieve the result of the job creation operation. There is also the option of cancelling an operation during processing if required. These requests can be submitted via the Job Creation REST service:

Process Job Creation (By Content Set) (Runtime Parameters) (JSON) /rest/serverengine/workflow/jobcreation/{configId} POST
Get Progress of Operation /rest/serverengine/workflow/jobcreation/getProgress/{operationId} GET
Get Result of Operation /rest/serverengine/workflow/jobcreation/getResult/{operationId} POST
Cancel an Operation /rest/serverengine/workflow/jobcreation/cancel/{operationId} POST

Example

HTML5

jc-process-by-cse-params-json.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Process Job Creation (By Content Set) (Runtime Parameters) (JSON) Example</title>
        <script src="../../common/lib/js/jquery-3.4.1.min.js"></script>
        <script src="../../common/js/common.js"></script>
        <script src="js/jc-process-by-cse-params-json.js"></script>
        <link rel="stylesheet" href="../../common/css/styles.css">
    </head>
    <body>
        <h2>Job Creation Service - Process Job Creation (By Content Set) (Runtime Parameters) (JSON) Example</h2>
        <form>
            <fieldset>
                <legend>Inputs</legend>
                <div>
                    <label for="contentsets">Content Set ID(s):</label>
                    <input id="contentsets" type="text" placeholder="1234, 2345, 3456, ..." required>
                </div>
                <div>
                    <label for="jcpreset">Job Creation Preset ID/Name:</label>
                    <input id="jcpreset" type="text" placeholder="1234 or Filename" required>
                </div>
                <div>
                    <label for="parameters">Runtime Parameters:</label>
                    <table id="parameters" class="name-value parameters">
                        <tbody>
                            <tr>
                                <th></th>
                                <th>Name</th>
                                <th>Type</th>
                                <th>Value</th>
                            </tr>
                            <tr class="placeholder" hidden>
                                <td></td>
                                <td>
                                    <input type="text" disabled>
                                </td>
                                <td>
                                    <select disabled>
                                        <option value="string">String</option>
                                    </select>
                                </td>
                                <td>
                                    <input type="text" disabled>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <div>
                    <input class="remove-parameter" type="button" value="Remove Selected" disabled>
                    <input class="add-parameter" type="button" value="Add Parameter">
                </div>
            </fieldset>
            <fieldset>
                <legend>Progress &amp; Actions</legend>
                <div>
                    <progress value="0" max="100"></progress>
                </div>
                <div>
                    <input id="cancel" type="button" value="Cancel" disabled>
                    <input id="submit" type="submit" value="Submit">
                </div>
            </fieldset>
        </form>
    </body>
</html>

markup.html

<!-- OL Connect REST API Cookbook - Working Examples [Markup HTML Snippet] -->
<div id="input-name-type-value-pair-row">
    <table>
        <tr class="root">
            <td>
                <input class="use-option" type="checkbox">
            </td>
            <td class="name">
                <input type="text" placeholder="Name" required>
            </td>
            <td class="type">
                <select id="type" class="options-selector">
                    <option value="string">String</option>
                    <option value="number">Number</option>
                    <option value="boolean">Boolean</option>
                    <option value="date">Date</option>
                </select>
            </td>
            <td class="option type-string value">
                <input type="text" placeholder="Value" required>
            </td>
            <td class="option type-number value">
                <input type="number" step="0.001" placeholder="Value" required />
            </td>
            <td class="option type-boolean value">
                <input type="checkbox" />
            </td>
            <td class="option type-date value">
                <input id="value1" type="date" required />
            </td>
        </tr>
    </table>
</div>

JavaScript/jQuery

jc-process-by-cse-params-json.js

/* Job Creation Service - Process Job Creation (By Content Set) (Runtime Parameters) (JSON) Example */
(function ($, c) {
    "use strict";
    $(function () {

        c.setupExample();

        var $parameters = $("#parameters"),
            $submitButton = $("#submit"),
            $cancelButton = $("#cancel"),
            $progressBar = $("progress"),
            operationId = null;

        c.setupRuntimeParametersTableInput($parameters);

        $cancelButton.on("click", function () {
            if (operationId !== null) {

                /* Cancel an Operation */
                $.ajax({
                    type: "POST",
                    url:  "/rest/serverengine/workflow/jobcreation/cancel/" + operationId
                })
                    .done(function (response) {
                        c.displayInfo("Operation Cancelled!");
                        operationId = null;
                        setTimeout(function () {
                            $progressBar.attr("value", 0);
                            $submitButton.prop("disabled", false);
                            $cancelButton.prop("disabled", true);
                        }, 100);
                    })
                    .fail(c.displayDefaultFailure);
            }
        });

        $("form").on("submit", function (event) {

            event.preventDefault();
            if (!c.checkSessionValid()) return;

            var contentSetIds = $("#contentsets").val(),
                configId = $("#jcpreset").val();

            var getFinalResult = function () {

                /* Get Result of Operation */
                $.ajax({
                    type:   "POST",
                    url:    "/rest/serverengine/workflow/jobcreation/getResult/" + operationId
                })
                    .done(function (response, status, request) {
                        c.displayHeading("Operation Result");
                        c.displaySubResult("Job Set ID", response);
                    })
                    .fail(c.displayDefaultFailure);
            };

            /* Construct JSON Identifier List (with Runtime Parameters) */
            var jsonConfig      = c.plainIDListToJson(contentSetIds),
                jsonParameters  = c.tableToJsonRuntimeParameters($parameters);

            if (Object.keys(jsonParameters.parameters).length)
                jsonConfig.parameters = jsonParameters.parameters;

            /* Process Job Creation (By Content Set) (Runtime Parameters) (JSON) */
            $.ajax({
                type:        "POST",
                url:         "/rest/serverengine/workflow/jobcreation/" + configId,
                data:        JSON.stringify(jsonConfig),
                contentType: "application/json"
            })
                .done(function (response, status, request) {

                    var progress = null;
                    operationId = request.getResponseHeader("operationId");

                    $submitButton.prop("disabled", true);
                    $cancelButton.prop("disabled", false);

                    c.displayStatus("Job Creation Operation Successfully Submitted");
                    c.displayResult("Operation ID", operationId);

                    var getProgress = function () {
                        if (operationId !== null) {

                            // Get Progress of Operation
                            $.ajax({
                                type:  "GET",
                                cache: false,
                                url:   "/rest/serverengine/workflow/jobcreation/getProgress/" + operationId
                            })
                                .done(function (response, status, request) {

                                    if (response !== "done") {
                                        if (response !== progress) {
                                            progress = response;
                                            $progressBar.attr("value", progress);
                                        }
                                        setTimeout(getProgress, 1000);
                                    } else {
                                        $progressBar.attr("value", (progress = 100));
                                        c.displayInfo("Operation Completed");
                                        getFinalResult();
                                        operationId = null;
                                        setTimeout(function () {
                                            $progressBar.attr("value", 0);
                                            $submitButton.prop("disabled", false);
                                            $cancelButton.prop("disabled", true);
                                        }, 100);
                                    }
                                })
                                .fail(c.displayDefaultFailure);
                        }
                    };
                    getProgress();
                })
                .fail(c.displayDefaultFailure);
        });
    });
}(jQuery, Common));

Screenshot & Output

Usage

To run the example simply enter a comma delimited list of your Content Set IDs and the Managed File ID/Name of your job creation preset (previously uploaded to the file store) into the appropriate text fields.

Next, specify one or more Runtime Parameters. Parameters can be added and removed using the Add Parameter and Remove Selected buttons respectively, and once added the following fields can be then populated for each parameter:

  • Name – The name of the runtime parameter (as specified in the Job Creation Preset)

  • Type – The type of the runtime parameter as either String, Number, Boolean or Date (as specified in the Job Creation Preset)

  • Value – The value of the runtime parameter (specific to the type selected)

Lastly, select the Submit button to start the job creation operation.

Once the operation has started processing, the Operation ID will be displayed in the Results area and the Cancel button will become enabled, giving you the option to cancel the running operation.

The progress of the operation will be displayed in the progress bar, and once the job creation operation has completed, the ID of the Job Set created will be returned and displayed to the Results area.

Further Reading

See the Job Creation Service page of the REST API Reference section for further detail.