2
* Copyright (C) 2007 Camptocamp
4
* This file is part of MapFish Client
6
* MapFish Client is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* MapFish Client is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with MapFish Client. If not, see <http://www.gnu.org/licenses/>.
21
* @requires OpenLayers/Util.js
22
* @requires OpenLayers/Protocol/HTTP.js
23
* @requires OpenLayers/Feature/Vector.js
24
* @requires OpenLayers/Format/GeoJSON.js
25
* @requires core/Protocol.js
29
* Class: mapfish.Protocol.MapFish
30
* MapFish Protocol class. This class is a decorator class to
31
* <OpenLayers.Protocol.HTTP>
34
* - <OpenLayers.Protocol.HTTP>
37
mapfish.Protocol.MapFish = OpenLayers.Class(OpenLayers.Protocol.HTTP, {
40
* Constructor: mapfish.Protocol.MapFish
45
initialize: function(options) {
46
options = options || {};
47
if (!options.format) {
48
options.format = new OpenLayers.Format.GeoJSON();
50
OpenLayers.Protocol.HTTP.prototype.initialize.call(this, options);
58
* features - {Array({<OpenLayers.Feature.Vector>})} or
59
* {<OpenLayers.Feature.Vector>}
60
* options - {Object} Optional object for configuring the request.
61
* This object is modified and should not be reused.
64
* {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response>
65
* object, whose "priv" property references the HTTP request, this
66
* object is also passed to the callback function when the request
67
* completes, its "features" property is then populated with the
68
* the features received from the server.
70
"create": function(features, options) {
71
options = options || {};
72
options.headers = OpenLayers.Util.extend(
73
options.headers, {"Content-Type": "plain/text"});
74
return OpenLayers.Protocol.HTTP.prototype.create.call(
75
this, features, options);
79
* Method: handleCreate
80
* This method overrides that of the parent class, this is to be more
81
* restrictive on HTTP status code.
84
* resp - {<OpenLayers.Protocol.Response>} The response object to pass to
86
* options - {Object} The user options passed to the create call.
88
handleCreate: function(resp, options) {
89
this.handleCreateUpdate(resp, options);
94
* Construct a request for reading new features.
97
* options - {Object} Optional object for configuring the request.
98
* This object is modified and should not be reused.
101
* {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response>
102
* object, whose "priv" property references the HTTP request, this
103
* object is also passed to the callback function when the request
104
* completes, its "features" property is then populated with the
105
* the features received from the server.
107
"read": function(options) {
108
// workaround a bug in OpenLayers
109
options.params = OpenLayers.Util.applyDefaults(
110
options.params, this.options.params);
112
this.filterAdapter(options);
114
return OpenLayers.Protocol.HTTP.prototype.read.call(
120
* This method overrides that of the parent class, this is to be more
121
* restrictive on HTTP status code.
124
* resp - {<OpenLayers.Protocol.Response>} The response object to pass to
126
* options - {Object} The user options passed to the read call.
128
handleRead: function(resp, options) {
129
var request = resp.priv;
130
if (options.callback) {
131
var code = request.status;
134
resp.features = this.parseFeatures(request);
135
resp.code = OpenLayers.Protocol.Response.SUCCESS;
138
resp.features = null;
139
resp.code = OpenLayers.Protocol.Response.FAILURE;
141
options.callback.call(options.scope, resp);
146
* Method: _filterToParams
147
* Private method to convert a <OpenLayers.Filter> object to key/values
151
* filter - {OpenLayers.Filter} filter to convert.
152
* params - {Object} Object where to store the result.
155
* {Boolean} True if the conversion suceeded, false otherwise.
157
_filterToParams: function(filter, params) {
158
var className = filter.CLASS_NAME;
159
var str = className.substring(
160
className.indexOf('.') + 1, className.lastIndexOf('.')
162
if (str != "Filter") {
166
var filterType = className.substring(className.lastIndexOf('.') + 1);
168
switch (filterType) {
170
if (filter.type != OpenLayers.Filter.Spatial.BBOX) {
171
OpenLayers.Console.error('Unsupported spatial filter type ' +
176
OpenLayers.Console.error('Filter contains multiple ' +
177
'Spatial BBOX entries');
178
// We should merge with the old bbox, but OL does not
179
// proving geometry merging.
182
params["box"] = filter.value.toBBOX();
185
// Note: the comparison type is not included in the request
186
// parameters. It is the server responsibility to deal with the
187
// appropriate comparison type from the parameter name.
189
if (params[filter.property]) {
190
OpenLayers.Console.error('Filter contains multiple Comparison ' +
191
'filters for the same property ' + filter.property);
194
params[filter.property] = filter.value;
197
if (filter.type != OpenLayers.Filter.Logical.AND) {
198
OpenLayers.Console.error('Unsupported logical filter type ' +
202
if (filter.filters.length == 0) {
203
OpenLayers.Console.error('Empty logical AND filter');
206
for (var i = 0; i < filter.filters.length; i++) {
207
var f = filter.filters[i];
208
if (!this._filterToParams(f, params))
213
OpenLayers.Console.warn("Unknown filter type " + filterType);
221
* Method: filterAdapter
222
* If params has a filter property and if that filter property
223
* is an OpenLayers.Filter that the MapFish protocol can deal
224
* with, the filter is adapted to the MapFish protocol.
229
filterAdapter: function(options) {
232
!options.filter.CLASS_NAME) {
238
if (this._filterToParams(options.filter, params))
239
options.params = OpenLayers.Util.extend(options.params, params);
240
delete options.filter;
245
* Construct a request updating modified features.
248
* features - {Array({<OpenLayers.Feature.Vector>})} or
249
* {<OpenLayers.Feature.Vector>}
250
* options - {Object} Optional object for configuring the request.
251
* This object is modified and should not be reused.
254
* {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response>
255
* object, whose "priv" property references the HTTP request, this
256
* object is also passed to the callback function when the request
257
* completes, its "features" property is then populated with the
258
* the features received from the server.
260
"update": function(features, options) {
261
options = options || {};
262
var url = options.url ||
264
this.options.url + '/' + features.fid;
266
options.headers = OpenLayers.Util.extend(
267
options.headers, {"Content-Type": "plain/text"});
268
return OpenLayers.Protocol.HTTP.prototype.update.call(
269
this, features, options);
273
* Method: handleUpdate
274
* This method overrides that of the parent class, this is to be more
275
* restrictive on HTTP status code.
278
* resp - {<OpenLayers.Protocol.Response>} The response object to pass to
280
* options - {Object} The user options passed to the update call.
282
handleUpdate: function(resp, options) {
283
this.handleCreateUpdate(resp, options);
287
* Method: handleCreateUpdate
290
* resp - {<OpenLayers.Protocol.Response>} The response object to pass to
292
* options - {Object} The user options passed to the update call.
294
handleCreateUpdate: function(resp, options) {
295
var request = resp.priv;
296
if (options.callback) {
297
var code = request.status;
300
resp.features = this.parseFeatures(request);
301
resp.code = OpenLayers.Protocol.Response.SUCCESS;
304
resp.features = null;
305
resp.code = OpenLayers.Protocol.Response.FAILURE;
307
options.callback.call(options.scope, resp);
313
* Construct a request deleting a removed feature.
316
* feature - {<OpenLayers.Feature.Vector>}
317
* options - {Object} Optional object for configuring the request.
318
* This object is modified and should not be reused.
321
* {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response>
322
* object, whose "priv" property references the HTTP request, this
323
* object is also passed to the callback function when the request
326
"delete": function(feature, options) {
327
options = options || {};
328
var url = options.url ||
330
this.options.url + '/' + feature.fid;
332
return OpenLayers.Protocol.HTTP.prototype["delete"].call(
333
this, feature, options);
337
* Method: handleDelete
340
* resp - {<OpenLayers.Protocol.Response>} The response object to pass to
342
* options - {Object} The user options passed to the delete call.
344
handleDelete: function(resp, options) {
345
var request = resp.priv;
346
if (options.callback) {
347
var code = request.status;
350
resp.code = OpenLayers.Protocol.Response.SUCCESS;
353
resp.code = OpenLayers.Protocol.Response.FAILURE;
355
options.callback.call(options.scope, resp);
359
CLASS_NAME: "mapfish.Protocol.MapFish"