| 67 | | mapfish.Protocol.prototype.initialize.apply(this, arguments); |
|---|
| 68 | | this.options = OpenLayers.Util.extend({}, options); |
|---|
| 69 | | |
|---|
| 70 | | if (!this.url) { |
|---|
| 71 | | OpenLayers.Console.error( |
|---|
| 72 | | "Mandatory param is missing: url"); |
|---|
| 73 | | } |
|---|
| 74 | | |
|---|
| 75 | | this.protocol = new OpenLayers.Protocol.HTTP({ |
|---|
| 76 | | 'url': this.url, |
|---|
| 77 | | 'scope': this, |
|---|
| 78 | | 'format': new OpenLayers.Format.GeoJSON() |
|---|
| 79 | | }); |
|---|
| 80 | | }, |
|---|
| 81 | | |
|---|
| 82 | | /** |
|---|
| 83 | | * APIMethod: destroy |
|---|
| 84 | | * Clean up the protocol. |
|---|
| 85 | | */ |
|---|
| 86 | | destroy: function() { |
|---|
| 87 | | this.protocol.destroy(); |
|---|
| 88 | | this.protocol = null; |
|---|
| 89 | | mapfish.Protocol.prototype.destroy.apply(this, arguments); |
|---|
| 90 | | }, |
|---|
| 91 | | |
|---|
| 92 | | setFormat: function(format) { |
|---|
| 93 | | // Ignore format change. The mapfish.Protocol.MapFish format is |
|---|
| 94 | | // GeoJSON and cna't be changed. |
|---|
| | 46 | options.format = new OpenLayers.Format.GeoJSON(); |
|---|
| | 47 | OpenLayers.Protocol.HTTP.prototype.initialize.apply(this, arguments); |
|---|
| 106 | | */ |
|---|
| 107 | | 'create': function(features, options) { |
|---|
| 108 | | options = OpenLayers.Util.applyDefaults(options, this.options); |
|---|
| 109 | | var callback = function(resp) { |
|---|
| 110 | | if (options.callback) { |
|---|
| 111 | | var code = resp.priv.status; |
|---|
| 112 | | if (code == 201) { |
|---|
| 113 | | // success |
|---|
| 114 | | resp.code = OpenLayers.Protocol.Response.SUCCESS; |
|---|
| 115 | | } else { |
|---|
| 116 | | // failure |
|---|
| 117 | | resp.features = null; |
|---|
| 118 | | resp.code = OpenLayers.Protocol.Response.FAILURE; |
|---|
| 119 | | } |
|---|
| 120 | | options.callback.call(options.scope, resp); |
|---|
| 121 | | } |
|---|
| 122 | | }; |
|---|
| 123 | | var hijackedOptions = OpenLayers.Util.applyDefaults({ |
|---|
| 124 | | 'callback': callback, |
|---|
| 125 | | 'headers': { |
|---|
| 126 | | 'Content-Type': 'plain/text' |
|---|
| 127 | | } |
|---|
| 128 | | }, options |
|---|
| 129 | | ); |
|---|
| 130 | | this.protocol.create(features, hijackedOptions); |
|---|
| | 59 | * |
|---|
| | 60 | * Returns: |
|---|
| | 61 | * {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response> |
|---|
| | 62 | * object, whose "priv" property references the HTTP request, this |
|---|
| | 63 | * object is also passed to the callback function when the request |
|---|
| | 64 | * completes, its "features" property is then populated with the |
|---|
| | 65 | * the features received from the server. |
|---|
| | 66 | */ |
|---|
| | 67 | "create": function(features, options) { |
|---|
| | 68 | options.headers = OpenLayers.Util.extend( |
|---|
| | 69 | options.headers, {"Content-Type": "plain/text"}); |
|---|
| | 70 | return OpenLayers.Protocol.HTTP.prototype.create.apply(this, arguments); |
|---|
| | 71 | }, |
|---|
| | 72 | |
|---|
| | 73 | /** |
|---|
| | 74 | * Method: callbackCreate |
|---|
| | 75 | * |
|---|
| | 76 | * Parameters: |
|---|
| | 77 | * options - {Object} The user options passed to the read call. |
|---|
| | 78 | * resp - {<OpenLayers.Protocol.Response>} The |
|---|
| | 79 | * <OpenLayers.Protocol.Response> object to pass to the user |
|---|
| | 80 | * callback. |
|---|
| | 81 | * request - {Object} The request object returned by XHR. |
|---|
| | 82 | */ |
|---|
| | 83 | callbackCreate: function(options, resp, request) { |
|---|
| | 84 | if (options.callback) { |
|---|
| | 85 | var code = request.status; |
|---|
| | 86 | if (code == 201) { |
|---|
| | 87 | // success |
|---|
| | 88 | resp.features = this.readResponse(request); |
|---|
| | 89 | resp.code = OpenLayers.Protocol.Response.SUCCESS; |
|---|
| | 90 | } else { |
|---|
| | 91 | // failure |
|---|
| | 92 | resp.features = null; |
|---|
| | 93 | resp.code = OpenLayers.Protocol.Response.FAILURE; |
|---|
| | 94 | } |
|---|
| | 95 | options.callback.call(options.scope, resp); |
|---|
| | 96 | } |
|---|
| 147 | | var callback = function(resp) { |
|---|
| 148 | | if (options.callback) { |
|---|
| 149 | | var code = resp.priv.status; |
|---|
| 150 | | if (code == 200) { |
|---|
| 151 | | // success |
|---|
| 152 | | resp.code = OpenLayers.Protocol.Response.SUCCESS; |
|---|
| 153 | | } else { |
|---|
| 154 | | // failure |
|---|
| 155 | | resp.features = null; |
|---|
| 156 | | resp.code = OpenLayers.Protocol.Response.FAILURE; |
|---|
| 157 | | } |
|---|
| 158 | | options.callback.call(options.scope, resp); |
|---|
| 159 | | } |
|---|
| 160 | | }; |
|---|
| 161 | | var hijackedOptions = OpenLayers.Util.applyDefaults({ |
|---|
| 162 | | 'callback': callback |
|---|
| 163 | | }, options |
|---|
| 164 | | ); |
|---|
| 165 | | this.protocol.read(hijackedOptions); |
|---|
| 166 | | }, |
|---|
| 167 | | |
|---|
| 168 | | /** |
|---|
| 169 | | * Method: update |
|---|
| 170 | | * Construct a request updating modified features. |
|---|
| 171 | | * |
|---|
| 172 | | * Parameters: |
|---|
| 173 | | * features - {Array({<OpenLayers.Feature.Vector>})} or |
|---|
| 174 | | * {<OpenLayers.Feature.Vector>} |
|---|
| 175 | | * options - {Object} Optional object for configuring the request. |
|---|
| 176 | | * This object is modified and should not be reused. |
|---|
| 177 | | */ |
|---|
| 178 | | 'update': function(features, options) { |
|---|
| 179 | | var url = options.url || |
|---|
| 180 | | features.url || |
|---|
| 181 | | this.options.url + '/' + features.fid; |
|---|
| 182 | | options = OpenLayers.Util.applyDefaults(options, this.options); |
|---|
| 183 | | var callback = function(resp) { |
|---|
| 184 | | if (options.callback) { |
|---|
| 185 | | var code = resp.priv.status; |
|---|
| 186 | | if (code == 201) { |
|---|
| 187 | | // success |
|---|
| 188 | | resp.code = OpenLayers.Protocol.Response.SUCCESS; |
|---|
| 189 | | } else { |
|---|
| 190 | | // failure |
|---|
| 191 | | resp.features = null; |
|---|
| 192 | | resp.code = OpenLayers.Protocol.Response.FAILURE; |
|---|
| 193 | | } |
|---|
| 194 | | options.callback.call(options.scope, resp); |
|---|
| 195 | | } |
|---|
| 196 | | }; |
|---|
| 197 | | var hijackedOptions = OpenLayers.Util.applyDefaults({ |
|---|
| 198 | | 'url': url, |
|---|
| 199 | | 'callback': callback, |
|---|
| 200 | | 'headers': { |
|---|
| 201 | | 'Content-Type': 'plain/text' |
|---|
| 202 | | } |
|---|
| 203 | | }, options |
|---|
| 204 | | ); |
|---|
| 205 | | this.protocol.update(features, hijackedOptions); |
|---|
| 206 | | }, |
|---|
| 207 | | |
|---|
| 208 | | /** |
|---|
| 209 | | * Method: delete |
|---|
| 210 | | * Construct a request deleting a removed feature. |
|---|
| 211 | | * |
|---|
| 212 | | * Parameters: |
|---|
| 213 | | * feature - {<OpenLayers.Feature.Vector>} |
|---|
| 214 | | * options - {Object} Optional object for configuring the request. |
|---|
| 215 | | * This object is modified and should not be reused. |
|---|
| 216 | | */ |
|---|
| 217 | | 'delete': function(feature, options) { |
|---|
| 218 | | var url = options.url || |
|---|
| 219 | | feature.url || |
|---|
| 220 | | this.options.url + '/' + feature.fid; |
|---|
| 221 | | options = OpenLayers.Util.applyDefaults(options, this.options); |
|---|
| 222 | | var callback = function(resp) { |
|---|
| 223 | | if (options.callback) { |
|---|
| 224 | | var code = resp.priv.status; |
|---|
| 225 | | if (code == 204) { |
|---|
| 226 | | // success |
|---|
| 227 | | resp.code = OpenLayers.Protocol.Response.SUCCESS; |
|---|
| 228 | | } else { |
|---|
| 229 | | // failure |
|---|
| 230 | | resp.code = OpenLayers.Protocol.Response.FAILURE; |
|---|
| 231 | | } |
|---|
| 232 | | options.callback.call(options.scope, resp); |
|---|
| 233 | | } |
|---|
| 234 | | }; |
|---|
| 235 | | var hijackedOptions = OpenLayers.Util.applyDefaults({ |
|---|
| 236 | | 'url': url, |
|---|
| 237 | | 'callback': callback |
|---|
| 238 | | }, options |
|---|
| 239 | | ); |
|---|
| 240 | | this.protocol['delete'](feature, hijackedOptions); |
|---|
| 241 | | }, |
|---|
| 242 | | |
|---|
| 243 | | /** |
|---|
| 244 | | * Method: commit |
|---|
| 245 | | * Go over the features in the layer and for each take action |
|---|
| 246 | | * based on the feature state. Possible actions are create, |
|---|
| 247 | | * update and delete. |
|---|
| 248 | | * |
|---|
| 249 | | * Parameters: |
|---|
| 250 | | * features - {Array({<OpenLayers.Feature.Vector>})} |
|---|
| 251 | | * options - {Object} Map of options, the keys of the map are |
|---|
| 252 | | * 'create', 'update', and 'delete'. |
|---|
| 253 | | * This object is modified and should not be reused. |
|---|
| 254 | | * |
|---|
| 255 | | * Returns: |
|---|
| 256 | | * {Integer} - The number of create/update/delete operations, |
|---|
| 257 | | * corresponding to the number of calls to the callbacks |
|---|
| 258 | | * when commit is done |
|---|
| 259 | | */ |
|---|
| 260 | | commit: function(features, options) { |
|---|
| 261 | | // not too good, but have no better way |
|---|
| 262 | | return this.protocol.commit.apply(this, arguments); |
|---|
| | 118 | return OpenLayers.Protocol.HTTP.prototype.read.apply(this, arguments); |
|---|
| | 119 | }, |
|---|
| | 120 | |
|---|
| | 121 | /** |
|---|
| | 122 | * Method: callbackRead |
|---|
| | 123 | * |
|---|
| | 124 | * Parameters: |
|---|
| | 125 | * options - {Object} The user options passed to the read call. |
|---|
| | 126 | * resp - {<OpenLayers.Protocol.Response>} The |
|---|
| | 127 | * <OpenLayers.Protocol.Response> object to pass to the user |
|---|
| | 128 | * callback. |
|---|
| | 129 | * request - {Object} The request object returned by XHR. |
|---|
| | 130 | */ |
|---|
| | 131 | callbackRead: function(options, resp, request) { |
|---|
| | 132 | if (options.callback) { |
|---|
| | 133 | var code = request.status; |
|---|
| | 134 | if (code == 200) { |
|---|
| | 135 | // success |
|---|
| | 136 | resp.features = this.readResponse(request); |
|---|
| | 137 | resp.code = OpenLayers.Protocol.Response.SUCCESS; |
|---|
| | 138 | } else { |
|---|
| | 139 | // failure |
|---|
| | 140 | resp.features = null; |
|---|
| | 141 | resp.code = OpenLayers.Protocol.Response.FAILURE; |
|---|
| | 142 | } |
|---|
| | 143 | options.callback.call(options.scope, resp); |
|---|
| | 144 | } |
|---|
| | 189 | /** |
|---|
| | 190 | * Method: update |
|---|
| | 191 | * Construct a request updating modified features. |
|---|
| | 192 | * |
|---|
| | 193 | * Parameters: |
|---|
| | 194 | * features - {Array({<OpenLayers.Feature.Vector>})} or |
|---|
| | 195 | * {<OpenLayers.Feature.Vector>} |
|---|
| | 196 | * options - {Object} Optional object for configuring the request. |
|---|
| | 197 | * This object is modified and should not be reused. |
|---|
| | 198 | * |
|---|
| | 199 | * Returns: |
|---|
| | 200 | * {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response> |
|---|
| | 201 | * object, whose "priv" property references the HTTP request, this |
|---|
| | 202 | * object is also passed to the callback function when the request |
|---|
| | 203 | * completes, its "features" property is then populated with the |
|---|
| | 204 | * the features received from the server. |
|---|
| | 205 | */ |
|---|
| | 206 | "update": function(features, options) { |
|---|
| | 207 | var url = options.url || |
|---|
| | 208 | features.url || |
|---|
| | 209 | this.options.url + '/' + features.fid; |
|---|
| | 210 | options.url = url; |
|---|
| | 211 | options.headers = OpenLayers.Util.extend( |
|---|
| | 212 | options.headers, {"Content-Type": "plain/text"}); |
|---|
| | 213 | return OpenLayers.Protocol.HTTP.prototype.update.apply(this, arguments); |
|---|
| | 214 | }, |
|---|
| | 215 | |
|---|
| | 216 | /** |
|---|
| | 217 | * Method: callbackUpdate |
|---|
| | 218 | * |
|---|
| | 219 | * Parameters: |
|---|
| | 220 | * options - {Object} The user options passed to the read call. |
|---|
| | 221 | * resp - {<OpenLayers.Protocol.Response>} The |
|---|
| | 222 | * <OpenLayers.Protocol.Response> object to pass to the user |
|---|
| | 223 | * callback. |
|---|
| | 224 | * request - {Object} The request object returned by XHR. |
|---|
| | 225 | */ |
|---|
| | 226 | callbackUpdate: function(options, resp, request) { |
|---|
| | 227 | if (options.callback) { |
|---|
| | 228 | var code = request.status; |
|---|
| | 229 | if (code == 201) { |
|---|
| | 230 | // success |
|---|
| | 231 | resp.features = this.readResponse(request); |
|---|
| | 232 | resp.code = OpenLayers.Protocol.Response.SUCCESS; |
|---|
| | 233 | } else { |
|---|
| | 234 | // failure |
|---|
| | 235 | resp.features = null; |
|---|
| | 236 | resp.code = OpenLayers.Protocol.Response.FAILURE; |
|---|
| | 237 | } |
|---|
| | 238 | options.callback.call(options.scope, resp); |
|---|
| | 239 | } |
|---|
| | 240 | }, |
|---|
| | 241 | |
|---|
| | 242 | /** |
|---|
| | 243 | * Method: delete |
|---|
| | 244 | * Construct a request deleting a removed feature. |
|---|
| | 245 | * |
|---|
| | 246 | * Parameters: |
|---|
| | 247 | * feature - {<OpenLayers.Feature.Vector>} |
|---|
| | 248 | * options - {Object} Optional object for configuring the request. |
|---|
| | 249 | * This object is modified and should not be reused. |
|---|
| | 250 | * |
|---|
| | 251 | * Returns: |
|---|
| | 252 | * {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response> |
|---|
| | 253 | * object, whose "priv" property references the HTTP request, this |
|---|
| | 254 | * object is also passed to the callback function when the request |
|---|
| | 255 | * completes. |
|---|
| | 256 | */ |
|---|
| | 257 | "delete": function(feature, options) { |
|---|
| | 258 | var url = options.url || |
|---|
| | 259 | feature.url || |
|---|
| | 260 | this.options.url + '/' + feature.fid; |
|---|
| | 261 | options.url = url; |
|---|
| | 262 | return OpenLayers.Protocol.HTTP.prototype["delete"].apply(this, arguments); |
|---|
| | 263 | }, |
|---|
| | 264 | |
|---|
| | 265 | /** |
|---|
| | 266 | * Method: callbackDelete |
|---|
| | 267 | * |
|---|
| | 268 | * Parameters: |
|---|
| | 269 | * options - {Object} The user options passed to the read call. |
|---|
| | 270 | * resp - {<OpenLayers.Protocol.Response>} The |
|---|
| | 271 | * <OpenLayers.Protocol.Response> object to pass to the user |
|---|
| | 272 | * callback. |
|---|
| | 273 | * request - {Object} The request object returned by XHR. |
|---|
| | 274 | */ |
|---|
| | 275 | callbackDelete: function(options, resp, request) { |
|---|
| | 276 | if (options.callback) { |
|---|
| | 277 | var code = request.status; |
|---|
| | 278 | if (code == 204) { |
|---|
| | 279 | // success |
|---|
| | 280 | resp.code = OpenLayers.Protocol.Response.SUCCESS; |
|---|
| | 281 | } else { |
|---|
| | 282 | // failure |
|---|
| | 283 | resp.code = OpenLayers.Protocol.Response.FAILURE; |
|---|
| | 284 | } |
|---|
| | 285 | options.callback.call(options.scope, resp); |
|---|
| | 286 | } |
|---|
| | 287 | }, |
|---|
| | 288 | |
|---|