<!DOCTYPE html>
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="apple-mobile-web-app-capable" content="yes">
        <title>OpenLayers WPS Example</title>
        <link rel="stylesheet" href="../theme/default/style.css" type="text/css">
        <link rel="stylesheet" href="style.css" type="text/css">
        <script src="../lib/Firebug/firebug.js"></script>
        <script src="../lib/OpenLayers.js"></script>
        <script type="text/javascript">
            var map;

            var WPS_VERSION = "1.0.0";

            // this comes from WPS Shootout, FOSS4G, Denver 2011
            // servers urls can change in time
            var serverDefinition = {
                pywps: {
                    url: "http://localhost/cgi-bin/wpstutorial",
                    testIdentifier: "ogrbuffer",
                    dataInput: "data",
                    mimeType:"text/xml",
                    sizeInput: "size"
                },
                "52north":{
                    url: "http://geoprocessing.demo.52north.org:8081/wps/WebProcessingService",
                    testIdentifier: "org.n52.wps.server.algorithm.SimpleBufferAlgorithm",
                    dataInput: "data",
                    sizeInput: "width",
                    mimeType:"text/xml"

                },
                zoo: {
                    url: "http://zoo-project.org/wps-foss4g2011/zoo_loader.cgi",
                    testIdentifier: "Buffer",
                    dataInput: "InputPolygon",
                    mimeType:"text/xml",
                    sizeInput: "BufferDistance"
                },
                geoserver: {
                    url: "http://briseide02.ingr.briseide.eu:8080/geoserver/wps",
                    testIdentifier: "gt:buffer",
                    dataInput: "geom1",
                    mimeType:"text/xml",
                    sizeInput: "buffer"
                },
                deegree: {
                    url: "http://85.25.95.86:8080/wps-bakeoff-2011/services",
                    testIdentifier: "Buffer",
                    dataInput: "GMLInput",
                    mimeType:"text/xml",
                    sizeInput: "BufferDistance"
                }
            };

            var servers = undefined; // servers, which will be tested

            /**
             * will make the test
             * @function
             * @name init
             */
            function init() {
                OpenLayers.ProxyHost= "proxy.cgi?url=";

                // OpenLayers.ProxyHost= "/cgi-bin/hsproxy.cgi?url=";

                servers = [];

                for (var i = 0; i < document.forms["servers"].elements["server"].length; i++) {
                    var elem = document.forms["servers"].elements["server"][i];
                    if (elem.checked) {
                        servers.push(serverDefinition[elem.value]);
                    }
                }

                
                testCapabilities();
                document.getElementById("resultslist").appendChild(document.createElement("hr"));
                testDescriptions();
                document.getElementById("resultslist").appendChild(document.createElement("hr"));
                testExecutes();
                document.getElementById("resultslist").appendChild(document.createElement("hr"));
                log("DONE");
                document.getElementById("resultslist").appendChild(document.createElement("hr"));

            };

            /**
             * Do the test for each server
             */
            var testCapabilities = function() {
                servers.map(testCapability);
            };

            var testDescriptions = function() {
                servers.map(testDescription);
            };

            var testExecutes = function() {
                servers.map(testExecute);
            };

            /****************
             * Capabilities *
             ****************/
            var testCapability = function(server) {
                OpenLayers.Request.GET({
                    url: server.url,
                    params: {
                                request: "GetCapabilities",
                                service: "WPS",
                                version: WPS_VERSION
                            },
                    success: parseCapability,
                    async: false,
                    scope: server
                    });
            };

            /**
             * Display the result of GetCapabilities request call
             */
            var parseCapability = function(xhr) {
                var format = new OpenLayers.Format.WPSCapabilities.v1_0_0();
                this.wpsCapabilities = format.read(xhr.responseXML);
                OpenLayers.Console.log(this.wpsCapabilities);

                log("Server "+
                        this.wpsCapabilities.serviceIdentification.title+
                        " has "+
                        Object.keys(this.wpsCapabilities.processOfferings).length+
                        " processes");

            };

            /************
             * Describe *
             ************/

            var testDescription = function(server) {

                OpenLayers.Request.GET({
                    url: server.url,
                    params: {
                                request: "DescribeProcess",
                                service: "WPS",
                                identifier: server.testIdentifier,
                                version: WPS_VERSION
                            },
                    success: parseDescribeProcess,
                    async: false,
                    scope: server
                    });
            };

            /**
             * Display the result of DescribeProcess request call
             */
            var parseDescribeProcess = function(xhr) {
                var format = new OpenLayers.Format.WPSDescribeProcess();
                this.description = format.read(xhr.responseXML);

                OpenLayers.Console.log(this.description);

                log("Process "+
                    this.description.processDescriptions[this.testIdentifier].title +
                    " "+
                    +this.description.processDescriptions[this.testIdentifier].processVersion +
                    " has ["+
                    this.description.processDescriptions[this.testIdentifier].dataInputs[0].title +
                    ","+
                    this.description.processDescriptions[this.testIdentifier].dataInputs[1].title +
                    "] inputs and ["+
                    this.description.processDescriptions[this.testIdentifier].processOutputs[0].title +
                    "] output");
                     
            };

            /***********
             * Execute *
             ***********/
            var testExecute = function(server) {

                var xmlformat = new OpenLayers.Format.XML();
                var data = xmlformat.read("<gml:featureMembers xmlns:gml=\"http://www.opengis.net/gml\" xsi:schemaLocation=\"http://www.opengis.net/gml http://schemas.opengis.net/gml/3.1.1/profiles/gmlsfProfile/1.0.0/gmlsf.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><gml:null fid=\"OpenLayers.Feature.Vector_62\"><gml:geometry><gml:LineString><gml:posList>0 0 5 5</gml:posList></gml:LineString></gml:geometry></gml:null></gml:featureMembers>");
                // prepare input structure for OpenLayers.Format.WPSExecute
                var options = {
                    identifier: server.testIdentifier,
                    dataInputs: [
                        {
                            identifier: server.dataInput,
                            data: {
                                complexData: { 
                                    attributes: { 
                                            mimeType: "text/xml"
                                    },
                                    value: data.firstChild
                                }
                            }
                        },
                        {
                            identifier: server.sizeInput,
                            data: {
                                literalData: { 
                                        value: 2,
                                }
                            }
                        }
                    ],
                    responseForm:  { }
                };

                var format = new OpenLayers.Format.WPSExecute();


                OpenLayers.Request.POST({
                    url: server.url,
                    data: format.write(options),
                    async: false,
                    success: parseExecute,
                    scope: server
                });
            };

            /**
             * Display the result of Execute request call
             */
            var parseExecute = function(xhr) {
                var format = new OpenLayers.Format.WPSExecute();
                this.execute = format.read(xhr.responseText);
                OpenLayers.Console.log(this.execute);
                log(this.wpsCapabilities.serviceIdentification.title+ " returned something");
            };

            /**
             * custom logging facility
             */
            var log = function(str) {
                OpenLayers.Console.log(str);
                var node = document.createElement("li");
                node.appendChild(
                        document.createTextNode(str));

                document.getElementById("resultslist").appendChild( node);
            };

        </script>
    </head>
    <body>
        <h1 id="title">WPS Parser example</h1>
        <div id="tags">
            wps
        </div>
        <p id="shortdesc">
            Uses WPS protocols for reading some informations about
            processes.
        </p>
        <form name="servers" onsubmit="return false;">
            <fieldset>
                <legend>Servers:</legend>
                <input type="checkbox" name="server" value="pywps" checked="checked" /> PyWPS (localhost) <br />
                <input type="checkbox" name="server" value="52north" checked="checked" /> 52north <br />
                <input type="checkbox" name="server" value="zoo" checked="checked" /> Zoo<br />
                <input type="checkbox" name="server" value="geoserver" checked="checked" /> GeoServer (Briseide project)<br />
                <input type="checkbox" name="server" value="deegree" checked="checked" /> DeeGree<br />
            </fieldset>
            Don't forget to open Firebug. <br />
            <input type="button" value="Test them!" onclick="init();"/>
        </form>
        <div id="docs">
            <p>
            </p>
        </div>
        <div id="results">
            <ul id="resultslist"></ul>
        </div>
    </body>
</html>