Addressing review suggestions and fixing asynchronous function calls.
This commit addresses @bartvde's review comments, adds more documentation, and fixes asynchronous function calls. Previously, when creating multiple processes with the same identifier, the describe callback would only have been called for the first process. This was fixed to move DescribeProcess handling from WPSProcess to WPSClient.
This commit is contained in:
@@ -8,16 +8,19 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* @requires OpenLayers/Events.js
|
||||
* @requires OpenLayers/WPSProcess.js
|
||||
* @requires OpenLayers/Format/WKT.js
|
||||
* @requires OpenLayers/Format/GeoJSON.js
|
||||
* @requires OpenLayers/Format/WPSDescribeProcess.js
|
||||
* @requires OpenLayers/Format/WPSExecute.js
|
||||
* @requires OpenLayers/Request.js
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class: OpenLayers.WPSClient
|
||||
* High level API for interaction with Web Processing Services (WPS).
|
||||
* An <OpenLayers.WPSClient> instance is used to create <OpenLayers.WPSProcess>
|
||||
* instances for servers known to the WPSClient. The WPSClient also caches
|
||||
* DescribeProcess responses to reduce the number of requests sent to servers
|
||||
* when processes are created.
|
||||
*/
|
||||
OpenLayers.WPSClient = OpenLayers.Class({
|
||||
|
||||
@@ -28,11 +31,18 @@ OpenLayers.WPSClient = OpenLayers.Class({
|
||||
* Properties:
|
||||
* url - {String} the url of the server
|
||||
* version - {String} WPS version of the server
|
||||
* describeProcessResponse - {Object} Cache of raw DescribeProcess
|
||||
* processDescription - {Object} Cache of raw DescribeProcess
|
||||
* responses, keyed by process identifier.
|
||||
*/
|
||||
servers: null,
|
||||
|
||||
/**
|
||||
* Property: version
|
||||
* {String} The default WPS version to use if none is configured. Default
|
||||
* is '1.0.0'.
|
||||
*/
|
||||
version: '1.0.0',
|
||||
|
||||
/**
|
||||
* Property: lazy
|
||||
* {Boolean} Should the DescribeProcess be deferred until a process is
|
||||
@@ -40,6 +50,18 @@ OpenLayers.WPSClient = OpenLayers.Class({
|
||||
*/
|
||||
lazy: false,
|
||||
|
||||
/**
|
||||
* Property: events
|
||||
* {<OpenLayers.Events>}
|
||||
*
|
||||
* Supported event types:
|
||||
* describeprocess - Fires when the process description is available.
|
||||
* Listeners receive an object with a 'raw' property holding the raw
|
||||
* DescribeProcess response, and an 'identifier' property holding the
|
||||
* process identifier of the described process.
|
||||
*/
|
||||
events: null,
|
||||
|
||||
/**
|
||||
* Constructor: OpenLayers.WPSClient
|
||||
*
|
||||
@@ -67,18 +89,22 @@ OpenLayers.WPSClient = OpenLayers.Class({
|
||||
*/
|
||||
initialize: function(options) {
|
||||
OpenLayers.Util.extend(this, options);
|
||||
this.events = new OpenLayers.Events(this);
|
||||
this.servers = {};
|
||||
for (var s in options.servers) {
|
||||
this.servers[s] = typeof options.servers[s] == 'string' ? {
|
||||
url: options.servers[s],
|
||||
version: '1.0.0',
|
||||
describeProcessResponse: {}
|
||||
version: this.version,
|
||||
processDescription: {}
|
||||
} : options.servers[s];
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* APIMethod: execute
|
||||
* Shortcut to execute a process with a single function call. This is
|
||||
* equivalent to using <getProcess> and then calling execute on the
|
||||
* process.
|
||||
*
|
||||
* Parameters:
|
||||
* options - {Object} Options for the execute operation.
|
||||
@@ -92,12 +118,14 @@ OpenLayers.WPSClient = OpenLayers.Class({
|
||||
* For spatial data inputs, the value of an input is usually an
|
||||
* <OpenLayers.Geometry>, an <OpenLayers.Feature.Vector> or an array of
|
||||
* geometries or features.
|
||||
* output - {String} The identifier of an output to parse. Optional. If not
|
||||
* provided, the first output will be parsed.
|
||||
* success - {Function} Callback to call when the process is complete.
|
||||
* This function is called with an outputs object as argument, which
|
||||
* will have a property with the name of the requested output (e.g.
|
||||
* 'result'). For processes that generate spatial output, the value
|
||||
* will either be a single <OpenLayers.Feature.Vector> or an array of
|
||||
* features.
|
||||
* will have a property with the identifier of the requested output
|
||||
* (e.g. 'result'). For processes that generate spatial output, the
|
||||
* value will either be a single <OpenLayers.Feature.Vector> or an
|
||||
* array of features.
|
||||
* scope - {Object} Optional scope for the success callback.
|
||||
*/
|
||||
execute: function(options) {
|
||||
@@ -114,18 +142,18 @@ OpenLayers.WPSClient = OpenLayers.Class({
|
||||
* Creates an <OpenLayers.WPSProcess>.
|
||||
*
|
||||
* Parameters:
|
||||
* server - {String} Local identifier from the servers that this instance
|
||||
* serverID - {String} Local identifier from the servers that this instance
|
||||
* was constructed with.
|
||||
* identifier - {String} Process identifier known to the server.
|
||||
* processID - {String} Process identifier known to the server.
|
||||
*
|
||||
* Returns:
|
||||
* {<OpenLayers.WPSProcess>}
|
||||
*/
|
||||
getProcess: function(server, identifier) {
|
||||
getProcess: function(serverID, processID) {
|
||||
var process = new OpenLayers.WPSProcess({
|
||||
client: this,
|
||||
server: server,
|
||||
identifier: identifier
|
||||
server: serverID,
|
||||
identifier: processID
|
||||
});
|
||||
if (!this.lazy) {
|
||||
process.describe();
|
||||
@@ -133,6 +161,54 @@ OpenLayers.WPSClient = OpenLayers.Class({
|
||||
return process;
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: describeProcess
|
||||
*
|
||||
* Parameters:
|
||||
* serverID - {String} Identifier of the server
|
||||
* processID - {String} Identifier of the requested process
|
||||
* callback - {Function} Callback to call when the description is available
|
||||
* scope - {Object} Optional execution scope for the callback function
|
||||
*/
|
||||
describeProcess: function(serverID, processID, callback, scope) {
|
||||
var server = this.servers[serverID];
|
||||
if (!server.processDescription[processID]) {
|
||||
if (!(processID in server.processDescription)) {
|
||||
// set to null so we know a describeFeature request is pending
|
||||
server.processDescription[processID] = null;
|
||||
OpenLayers.Request.GET({
|
||||
url: server.url,
|
||||
params: {
|
||||
SERVICE: 'WPS',
|
||||
VERSION: server.version,
|
||||
REQUEST: 'DescribeProcess',
|
||||
IDENTIFIER: processID
|
||||
},
|
||||
success: function(response) {
|
||||
server.processDescription[processID] = response.responseText;
|
||||
this.events.triggerEvent('describeprocess', {
|
||||
identifier: processID,
|
||||
raw: response.responseText
|
||||
});
|
||||
},
|
||||
scope: this
|
||||
});
|
||||
} else {
|
||||
// pending request
|
||||
this.events.register('describeprocess', this, function describe(evt) {
|
||||
if (evt.identifier === processID) {
|
||||
this.events.unregister('describeprocess', this, describe);
|
||||
callback.call(scope, evt);
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
window.setTimeout(function() {
|
||||
callback.call(scope, server.processDescription[processID]);
|
||||
}, 0);
|
||||
}
|
||||
},
|
||||
|
||||
CLASS_NAME: 'OpenLayers.WPSClient'
|
||||
|
||||
});
|
||||
|
||||
@@ -8,26 +8,27 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* @requires OpenLayers/Events.js
|
||||
* @requires OpenLayers/Geometry.js
|
||||
* @requires OpenLayers/Feature/Vector.js
|
||||
* @requires OpenLayers/Format/WKT.js
|
||||
* @requires OpenLayers/Format/GeoJSON.js
|
||||
* @requires OpenLayers/Format/WPSExecute.js
|
||||
* @requires OpenLayers/Request.js
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class: OpenLayers.WPSProcess
|
||||
* Representation of a WPS process. Usually instances of
|
||||
* <OpenLayers.WPSProcess> are created by calling 'getProcess' on an
|
||||
* <OpenLayers.WPSClient> instance.
|
||||
*
|
||||
* Currently <OpenLayers.WPSProcess> supports processes that have geometries
|
||||
* or features as output, using WKT or GeoJSON as output format. It also
|
||||
* supports chaining of processes by using the <output> method to create a
|
||||
* handle that is used as process input instead of a static value.
|
||||
*/
|
||||
OpenLayers.WPSProcess = OpenLayers.Class({
|
||||
|
||||
/**
|
||||
* APIProperty: events
|
||||
* {<OpenLayers.Events>}
|
||||
*
|
||||
* Supported event types:
|
||||
* describeprocess - Fires when the process description is available for
|
||||
* the first time.
|
||||
*/
|
||||
events: null,
|
||||
|
||||
/**
|
||||
* Property: client
|
||||
* {<OpenLayers.WPSClient>} The client that manages this process.
|
||||
@@ -52,6 +53,13 @@ OpenLayers.WPSProcess = OpenLayers.Class({
|
||||
*/
|
||||
description: null,
|
||||
|
||||
/**
|
||||
* APIProperty: localWPS
|
||||
* {String} Service endpoint for locally chained WPS processes. Default is
|
||||
* 'http://geoserver/wps'.
|
||||
*/
|
||||
localWPS: 'http://geoserver/wps',
|
||||
|
||||
/**
|
||||
* Property: formats
|
||||
* {Object} OpenLayers.Format instances keyed by mimetype.
|
||||
@@ -60,7 +68,7 @@ OpenLayers.WPSProcess = OpenLayers.Class({
|
||||
|
||||
/**
|
||||
* Property: chained
|
||||
* {Integer} Number of chained processes for pending execute reqeusts that
|
||||
* {Integer} Number of chained processes for pending execute requests that
|
||||
* don't have a full configuration yet.
|
||||
*/
|
||||
chained: 0,
|
||||
@@ -79,18 +87,15 @@ OpenLayers.WPSProcess = OpenLayers.Class({
|
||||
* options - {Object} Object whose properties will be set on the instance.
|
||||
*
|
||||
* Avaliable options:
|
||||
* client - {<OpenLayers.WPSClient} Mandatory. Client that manages this
|
||||
* client - {<OpenLayers.WPSClient>} Mandatory. Client that manages this
|
||||
* process.
|
||||
* server - {String} Mandatory. Local client identifier of this process's
|
||||
* server.
|
||||
* identifier - {String} Mandatory. Process identifier known to the server.
|
||||
*/
|
||||
initialize: function(options) {
|
||||
OpenLayers.Util.extend(this, options);
|
||||
|
||||
this.events = new OpenLayers.Events(this);
|
||||
OpenLayers.Util.extend(this, options);
|
||||
this.executeCallbacks = [];
|
||||
|
||||
this.formats = {
|
||||
'application/wkt': new OpenLayers.Format.WKT(),
|
||||
'application/json': new OpenLayers.Format.GeoJSON()
|
||||
@@ -99,12 +104,10 @@ OpenLayers.WPSProcess = OpenLayers.Class({
|
||||
|
||||
/**
|
||||
* Method: describe
|
||||
* Issues a DescribeProcess request asynchronously and fires the
|
||||
* 'describeprocess' event as soon as the response is available in
|
||||
* <description>.
|
||||
* Makes the client ssues a DescribeProcess request asynchronously.
|
||||
*
|
||||
* Parameters:
|
||||
* options - {Object} Coniguration for the method call
|
||||
* options - {Object} Configuration for the method call
|
||||
*
|
||||
* Available options:
|
||||
* callback - {Function} Callback to execute when the description is
|
||||
@@ -115,45 +118,21 @@ OpenLayers.WPSProcess = OpenLayers.Class({
|
||||
*/
|
||||
describe: function(options) {
|
||||
options = options || {};
|
||||
function callback() {
|
||||
if (options.callback) {
|
||||
window.setTimeout(function() {
|
||||
options.callback.call(options.scope, this.description);
|
||||
}, 0);
|
||||
}
|
||||
}
|
||||
var server = this.client.servers[this.server];
|
||||
if (this.description !== null) {
|
||||
callback();
|
||||
return;
|
||||
} else if (server.describeProcessResponse[this.identifier] === null) {
|
||||
// pending request
|
||||
this.events.register('describeprocess', this, callback);
|
||||
return;
|
||||
} else if (this.identifier in server.describeProcessResponse) {
|
||||
// process description already cached on client
|
||||
this.parseDescription();
|
||||
callback();
|
||||
return;
|
||||
}
|
||||
// set to null so we know a describeFeature request is pending
|
||||
server.describeProcessResponse[this.identifier] = null;
|
||||
OpenLayers.Request.GET({
|
||||
url: server.url,
|
||||
params: {
|
||||
SERVICE: 'WPS',
|
||||
VERSION: server.version,
|
||||
REQUEST: 'DescribeProcess',
|
||||
IDENTIFIER: this.identifier
|
||||
},
|
||||
success: function(response) {
|
||||
this.parseDescription(response);
|
||||
if (!this.description) {
|
||||
this.client.describeProcess(this.server, this.identifier, function(description) {
|
||||
if (!this.description) {
|
||||
this.parseDescription(description);
|
||||
}
|
||||
if (options.callback) {
|
||||
options.callback.call(options.scope, this.description);
|
||||
}
|
||||
},
|
||||
scope: this
|
||||
});
|
||||
}, this);
|
||||
} else if (options.callback) {
|
||||
var description = this.description;
|
||||
window.setTimeout(function() {
|
||||
options.callback.call(options.scope, description);
|
||||
}, 0);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -175,25 +154,22 @@ OpenLayers.WPSProcess = OpenLayers.Class({
|
||||
* scope - {Object} Optional scope for the callback.
|
||||
*/
|
||||
configure: function(options) {
|
||||
if (!this.description) {
|
||||
this.describe({
|
||||
callback: function() {
|
||||
this.configure(options);
|
||||
},
|
||||
scope: this
|
||||
});
|
||||
return;
|
||||
}
|
||||
var description = this.description,
|
||||
inputs = options.inputs,
|
||||
input, i, ii;
|
||||
for (i=0, ii=description.dataInputs.length; i<ii; ++i) {
|
||||
input = description.dataInputs[i];
|
||||
this.setInputData(input, inputs[input.identifier]);
|
||||
}
|
||||
if (options.callback) {
|
||||
options.callback.call(options.scope);
|
||||
}
|
||||
this.describe({
|
||||
callback: function() {
|
||||
var description = this.description,
|
||||
inputs = options.inputs,
|
||||
input, i, ii;
|
||||
for (i=0, ii=description.dataInputs.length; i<ii; ++i) {
|
||||
input = description.dataInputs[i];
|
||||
this.setInputData(input, inputs[input.identifier]);
|
||||
}
|
||||
if (options.callback) {
|
||||
options.callback.call(options.scope);
|
||||
}
|
||||
},
|
||||
scope: this
|
||||
});
|
||||
return this;
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -212,10 +188,10 @@ OpenLayers.WPSProcess = OpenLayers.Class({
|
||||
* provided, the first output will be parsed.
|
||||
* success - {Function} Callback to call when the process is complete.
|
||||
* This function is called with an outputs object as argument, which
|
||||
* will have a property with the name of the requested output (e.g.
|
||||
* 'result'). For processes that generate spatial output, the value
|
||||
* will either be a single <OpenLayers.Feature.Vector> or an array of
|
||||
* features.
|
||||
* will have a property with the identifier of the requested output
|
||||
* (or 'result' if output was not configured). For processes that
|
||||
* generate spatial output, the value will be an array of
|
||||
* <OpenLayers.Feature.Vector> instances.
|
||||
* scope - {Object} Optional scope for the success callback.
|
||||
*/
|
||||
execute: function(options) {
|
||||
@@ -248,9 +224,12 @@ OpenLayers.WPSProcess = OpenLayers.Class({
|
||||
);
|
||||
//TODO For now we assume a spatial output
|
||||
var features = me.formats[mimeType].read(response.responseText);
|
||||
if (features instanceof OpenLayers.Feature.Vector) {
|
||||
features = [features];
|
||||
}
|
||||
if (options.success) {
|
||||
var outputs = {};
|
||||
outputs[output.identifier] = features;
|
||||
outputs[options.output || 'result'] = features;
|
||||
options.success.call(options.scope, outputs);
|
||||
}
|
||||
},
|
||||
@@ -298,17 +277,13 @@ OpenLayers.WPSProcess = OpenLayers.Class({
|
||||
* Parses the DescribeProcess response
|
||||
*
|
||||
* Parameters:
|
||||
* response - {Object}
|
||||
* description - {Object}
|
||||
*/
|
||||
parseDescription: function(response) {
|
||||
parseDescription: function(description) {
|
||||
var server = this.client.servers[this.server];
|
||||
if (response) {
|
||||
server.describeProcessResponse[this.identifier] = response.responseText;
|
||||
}
|
||||
this.description = new OpenLayers.Format.WPSDescribeProcess()
|
||||
.read(server.describeProcessResponse[this.identifier])
|
||||
.read(server.processDescription[this.identifier])
|
||||
.processDescriptions[this.identifier];
|
||||
this.events.triggerEvent('describeprocess');
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -331,9 +306,7 @@ OpenLayers.WPSProcess = OpenLayers.Class({
|
||||
input.reference = {
|
||||
method: 'POST',
|
||||
href: data.process.server === this.server ?
|
||||
//TODO what about implementations other than GeoServer?
|
||||
'http://geoserver/wps' :
|
||||
this.client.servers[data.process.server].url
|
||||
this.localWPS : this.client.servers[data.process.server].url
|
||||
};
|
||||
data.process.describe({
|
||||
callback: function() {
|
||||
@@ -415,7 +388,7 @@ OpenLayers.WPSProcess = OpenLayers.Class({
|
||||
*
|
||||
* Parameters:
|
||||
* input - {Object} The dataInput that the chained process provides.
|
||||
* chainLink - {<OpenLayers.WPSProcess.ChainLink} The process to chain.
|
||||
* chainLink - {<OpenLayers.WPSProcess.ChainLink>} The process to chain.
|
||||
*/
|
||||
chainProcess: function(input, chainLink) {
|
||||
var output = this.getOutputIndex(
|
||||
|
||||
Reference in New Issue
Block a user