~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/mapfish/core/Protocol/MapFish.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007  Camptocamp
 
3
 *
 
4
 * This file is part of MapFish Client
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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/>.
 
18
 */
 
19
 
 
20
/*
 
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
 
26
 */
 
27
 
 
28
/**
 
29
 * Class: mapfish.Protocol.MapFish
 
30
 * MapFish Protocol class. This class is a decorator class to
 
31
 * <OpenLayers.Protocol.HTTP>
 
32
 *
 
33
 * Inherits from:
 
34
 * - <OpenLayers.Protocol.HTTP>
 
35
 */
 
36
 
 
37
mapfish.Protocol.MapFish = OpenLayers.Class(OpenLayers.Protocol.HTTP, {
 
38
 
 
39
    /**
 
40
     * Constructor: mapfish.Protocol.MapFish
 
41
     *
 
42
     * Parameters:
 
43
     * options - {Object}
 
44
     */
 
45
    initialize: function(options) {
 
46
        options = options || {};
 
47
        if (!options.format) {
 
48
            options.format = new OpenLayers.Format.GeoJSON();
 
49
        }
 
50
        OpenLayers.Protocol.HTTP.prototype.initialize.call(this, options);
 
51
    },
 
52
 
 
53
    /**
 
54
     * APIMethod: create
 
55
     *      Create features.
 
56
     *
 
57
     * Parameters:
 
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.
 
62
     *
 
63
     * Returns:
 
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.
 
69
     */
 
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);
 
76
    },
 
77
 
 
78
    /**
 
79
     * Method: handleCreate
 
80
     * This method overrides that of the parent class, this is to be more
 
81
     * restrictive on HTTP status code.
 
82
     *
 
83
     * Parameters:
 
84
     * resp - {<OpenLayers.Protocol.Response>} The response object to pass to
 
85
     *     any user callback.
 
86
     * options - {Object} The user options passed to the create call.
 
87
     */
 
88
    handleCreate: function(resp, options) {
 
89
        this.handleCreateUpdate(resp, options);
 
90
    },
 
91
 
 
92
    /**
 
93
     * APIMethod: read
 
94
     * Construct a request for reading new features.
 
95
     *
 
96
     * Parameters:
 
97
     * options - {Object} Optional object for configuring the request.
 
98
     *     This object is modified and should not be reused.
 
99
     *
 
100
     * Returns:
 
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.
 
106
     */
 
107
    "read": function(options) {
 
108
        // workaround a bug in OpenLayers
 
109
        options.params = OpenLayers.Util.applyDefaults(
 
110
            options.params, this.options.params);
 
111
        if (options) {
 
112
            this.filterAdapter(options);
 
113
        }
 
114
        return OpenLayers.Protocol.HTTP.prototype.read.call(
 
115
            this, options);
 
116
    },
 
117
 
 
118
    /**
 
119
     * Method: handleRead
 
120
     * This method overrides that of the parent class, this is to be more
 
121
     * restrictive on HTTP status code.
 
122
     *
 
123
     * Parameters:
 
124
     * resp - {<OpenLayers.Protocol.Response>} The response object to pass to
 
125
     *     the user callback.
 
126
     * options - {Object} The user options passed to the read call.
 
127
     */
 
128
    handleRead: function(resp, options) {
 
129
        var request = resp.priv;
 
130
        if (options.callback) {
 
131
            var code = request.status;
 
132
            if (code == 200) {
 
133
                // success
 
134
                resp.features = this.parseFeatures(request);
 
135
                resp.code = OpenLayers.Protocol.Response.SUCCESS;
 
136
            } else {
 
137
                // failure
 
138
                resp.features = null;
 
139
                resp.code = OpenLayers.Protocol.Response.FAILURE;
 
140
            }
 
141
            options.callback.call(options.scope, resp);
 
142
        }
 
143
    },
 
144
 
 
145
    /**
 
146
     * Method: _filterToParams
 
147
     *      Private method to convert a <OpenLayers.Filter> object to key/values
 
148
     *      in an object.
 
149
     *
 
150
     * Parameters:
 
151
     * filter - {OpenLayers.Filter} filter to convert.
 
152
     * params - {Object} Object where to store the result.
 
153
     *
 
154
     * Returns:
 
155
     * {Boolean} True if the conversion suceeded, false otherwise.
 
156
     */
 
157
    _filterToParams: function(filter, params) {
 
158
        var className = filter.CLASS_NAME;
 
159
        var str = className.substring(
 
160
            className.indexOf('.') + 1, className.lastIndexOf('.')
 
161
        );
 
162
        if (str != "Filter") {
 
163
            // bail out
 
164
            return false;
 
165
        }
 
166
        var filterType = className.substring(className.lastIndexOf('.') + 1);
 
167
 
 
168
        switch (filterType) {
 
169
            case "Spatial":
 
170
                if (filter.type != OpenLayers.Filter.Spatial.BBOX) {
 
171
                    OpenLayers.Console.error('Unsupported spatial filter type ' +
 
172
                                             filter.type);
 
173
                    return false;
 
174
                }
 
175
                if (params["box"]) {
 
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.
 
180
                    return false;
 
181
                }
 
182
                params["box"] = filter.value.toBBOX();
 
183
                break;
 
184
            case "Comparison":
 
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.
 
188
 
 
189
                if (params[filter.property]) {
 
190
                    OpenLayers.Console.error('Filter contains multiple Comparison ' +
 
191
                               'filters for the same property ' + filter.property);
 
192
                        return false;
 
193
                }
 
194
                params[filter.property] = filter.value;
 
195
                break;
 
196
            case "Logical":
 
197
                if (filter.type != OpenLayers.Filter.Logical.AND) {
 
198
                    OpenLayers.Console.error('Unsupported logical filter type ' +
 
199
                                             filter.type);
 
200
                    return false;
 
201
                }
 
202
                if (filter.filters.length == 0) {
 
203
                    OpenLayers.Console.error('Empty logical AND filter');
 
204
                    return false;
 
205
                }
 
206
                for (var i = 0; i < filter.filters.length; i++) {
 
207
                    var f = filter.filters[i];
 
208
                    if (!this._filterToParams(f, params))
 
209
                        return false;
 
210
                }
 
211
                break;
 
212
            default:
 
213
                OpenLayers.Console.warn("Unknown filter type " + filterType);
 
214
                return false;
 
215
                break;
 
216
        }
 
217
        return true;
 
218
    },
 
219
 
 
220
    /**
 
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.
 
225
     *
 
226
     * Parameters:
 
227
     * options - {Object}
 
228
     */
 
229
    filterAdapter: function(options) {
 
230
        if (!options ||
 
231
            !options.filter ||
 
232
            !options.filter.CLASS_NAME) {
 
233
            // bail out
 
234
            return;
 
235
        }
 
236
 
 
237
        var params = {};
 
238
        if (this._filterToParams(options.filter, params))
 
239
            options.params = OpenLayers.Util.extend(options.params, params);
 
240
        delete options.filter;
 
241
    },
 
242
 
 
243
    /**
 
244
     * APIMethod: update
 
245
     * Construct a request updating modified features.
 
246
     *
 
247
     * Parameters:
 
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.
 
252
     *
 
253
     * Returns:
 
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.
 
259
     */
 
260
    "update": function(features, options) {
 
261
        options = options || {};
 
262
        var url = options.url ||
 
263
                  features.url ||
 
264
                  this.options.url + '/' + features.fid;
 
265
        options.url = url;
 
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);
 
270
    },
 
271
 
 
272
    /**
 
273
     * Method: handleUpdate
 
274
     * This method overrides that of the parent class, this is to be more
 
275
     * restrictive on HTTP status code.
 
276
     *
 
277
     * Parameters:
 
278
     * resp - {<OpenLayers.Protocol.Response>} The response object to pass to
 
279
     *     any user callback.
 
280
     * options - {Object} The user options passed to the update call.
 
281
     */
 
282
    handleUpdate: function(resp, options) {
 
283
        this.handleCreateUpdate(resp, options);
 
284
    },
 
285
 
 
286
    /**
 
287
     * Method: handleCreateUpdate
 
288
     *
 
289
     * Parameters:
 
290
     * resp - {<OpenLayers.Protocol.Response>} The response object to pass to
 
291
     *     any user callback.
 
292
     * options - {Object} The user options passed to the update call.
 
293
     */
 
294
    handleCreateUpdate: function(resp, options) {
 
295
        var request = resp.priv;
 
296
        if (options.callback) {
 
297
            var code = request.status;
 
298
            if (code == 201) {
 
299
                // success
 
300
                resp.features = this.parseFeatures(request);
 
301
                resp.code = OpenLayers.Protocol.Response.SUCCESS;
 
302
            } else {
 
303
                // failure
 
304
                resp.features = null;
 
305
                resp.code = OpenLayers.Protocol.Response.FAILURE;
 
306
            }
 
307
            options.callback.call(options.scope, resp);
 
308
        }
 
309
    },
 
310
 
 
311
    /**
 
312
     * APIMethod: delete
 
313
     * Construct a request deleting a removed feature.
 
314
     *
 
315
     * Parameters:
 
316
     * feature - {<OpenLayers.Feature.Vector>}
 
317
     * options - {Object} Optional object for configuring the request.
 
318
     *     This object is modified and should not be reused.
 
319
     *
 
320
     * Returns:
 
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
 
324
     *      completes.
 
325
     */
 
326
    "delete": function(feature, options) {
 
327
        options = options || {};
 
328
        var url = options.url ||
 
329
                  feature.url ||
 
330
                  this.options.url + '/' + feature.fid;
 
331
        options.url = url;
 
332
        return OpenLayers.Protocol.HTTP.prototype["delete"].call(
 
333
            this, feature, options);
 
334
    },
 
335
 
 
336
    /**
 
337
     * Method: handleDelete
 
338
     *
 
339
     * Parameters:
 
340
     * resp - {<OpenLayers.Protocol.Response>} The response object to pass to
 
341
     *     any user callback.
 
342
     * options - {Object} The user options passed to the delete call.
 
343
     */
 
344
    handleDelete: function(resp, options) {
 
345
        var request = resp.priv;
 
346
        if (options.callback) {
 
347
            var code = request.status;
 
348
            if (code == 204) {
 
349
                // success
 
350
                resp.code = OpenLayers.Protocol.Response.SUCCESS;
 
351
            } else {
 
352
                // failure
 
353
                resp.code = OpenLayers.Protocol.Response.FAILURE;
 
354
            }
 
355
            options.callback.call(options.scope, resp);
 
356
        }
 
357
    },
 
358
 
 
359
    CLASS_NAME: "mapfish.Protocol.MapFish"
 
360
});