Running an Output Creation Operation By Job (Using JSON)

Problem

You want to run an output creation operation to produce print output using an output creation preset and a list of existing Jobs 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 output creation operation. There is also the option of cancelling an operation during processing if required. These requests can be submitted via the Output Creation REST service:

Process Output Creation (By Job) (JSON) /rest/serverengine/workflow/outputcreation/{configId}/jobs POST
Get Progress of Operation /rest/serverengine/workflow/outputcreation/getProgress/{operationId} GET
Get Result of Operation /rest/serverengine/workflow/outputcreation/getResult/{operationId} POST
Get Result of Operation (as Text) /rest/serverengine/workflow/outputcreation/getResultTxt/{operationId} POST
Cancel an Operation /rest/serverengine/workflow/outputcreation/cancel/{operationId} POST

Example

HTML5

oc-process-by-je-json.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Process Output Creation (By Job) (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/oc-process-by-je-json.js"></script>
        <link rel="stylesheet" href="../../common/css/styles.css">
    </head>
    <body>
        <h2>Output Creation Service - Process Output Creation (By Job) (JSON) Example</h2>
        <form>
            <fieldset>
                <legend>Inputs</legend>
                <div>
                    <label for="jobs">Job ID(s):</label>
                    <input id="jobs" type="text" placeholder="1234, 2345, 3456, ..." required>
                </div>
                <div>
                    <label for="ocpreset">Output Creation Preset ID/Name:</label>
                    <input id="ocpreset" type="text" placeholder="1234 or Filename" required>
                </div>
            </fieldset>
            <fieldset>
                <legend>Options</legend>
                <div>
                    <label for="createonly">Create Only:</label>
                    <input id="createonly" type="checkbox">
                </div>
                <div>
                    <label for="resultastxt">Get Result as Text:</label>
                    <input id="resultastxt" type="checkbox">
                </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>

JavaScript/jQuery

oc-process-by-je-json.js

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

        c.setupExample();

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

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

                /* Cancel an Operation */
                $.ajax({
                    type:   "POST",
                    url:    "/rest/serverengine/workflow/outputcreation/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 jobIds = $("#jobs").val(),
                configId = $("#ocpreset").val(),
                createOnly = $("#createonly").prop("checked");

            var getFinalResult = function () {

                var result = ($("#resultastxt").prop("checked")) ? "getResultTxt" : "getResult";

                /* Get Result of Operation */
                $.ajax({
                    type:   "POST",
                    url:    "/rest/serverengine/workflow/outputcreation/" + result + "/" + operationId
                })
                    .done(function (response, status, request) {
                        if (request.getResponseHeader("Content-Type") === "application/octet-stream")
                            response = "&lt;&lt;OCTET-STREAM FILE DATA&gt;&gt;";
                        c.displayHeading("Operation Result");
                        c.displaySubResult("Output", response);
                    })
                    .fail(c.displayDefaultFailure);
            };

            /* Process Output Creation (By Job) (JSON) */
            $.ajax({
                type:           "POST",
                url:            "/rest/serverengine/workflow/outputcreation/" + configId + "/jobs",
                data:           JSON.stringify(c.plainIDListToJson(jobIds, createOnly)),
                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("Output 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/outputcreation/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 Job IDs and the Managed File ID or Name of your output creation preset (previously uploaded to the file store) into the appropriate text fields, and then check any options that you may require:

  • Create Only – Create the output in server but do not send spool file to its final destination. In this example this would mean that the output files(s) would not be sent to the output directory specified in the output creation preset.
  • Get Result as Text – Return the result as text specifically. In this example this would return the absolute path to the output file(s).

Lastly, select the Submit button to start the Output 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 output creation operation has completed, the output result will be returned and displayed to the Results area.

If the result returned is expected to be file data, then then a download link will be displayed.

Further Reading

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