Changeset 7918
- Timestamp:
- 09/01/08 14:11:09 (3 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
sandbox/camptocamp/unhcr/lib/OpenLayers/Protocol/SQL/Gears.js
r7877 r7918 63 63 * Property: saveFeatureState 64 64 * {Boolean} Whether to save the feature state (<OpenLayers.State>) 65 * into the database .65 * into the database, defaults to true. 66 66 */ 67 67 saveFeatureState: true, … … 142 142 * 143 143 * Parameters: 144 * options - {Object} Optional parameter that may contain a callback 145 * attribute. 144 * options - {Object} Optional object for configuring the request; it 145 * can have the {Boolean} property "noFeatureStateReset" which 146 * specifies if the state of features read from the Gears 147 * database must be reset to null, if "noFeatureStateReset" 148 * is undefined or false then each feature's state is reset 149 * to null, if "noFeatureStateReset" is true the feature state 150 * is preserved. 146 151 * 147 152 * Returns: … … 152 157 options = OpenLayers.Util.applyDefaults(options, this.options); 153 158 154 var feature s = [];159 var feature, features = []; 155 160 var rs = this.db.execute("SELECT * FROM " + this.tableName); 156 161 while (rs.isValidRow()) { 157 features.push(this.unfreezeFeature(rs)); 162 feature = this.unfreezeFeature(rs); 163 if (!options.noFeatureStateReset) { 164 feature.state = null; 165 } 166 features.push(feature); 158 167 rs.next(); 159 168 } … … 227 236 * Parameters: 228 237 * features - {Array({<OpenLayers.Feature.Vector>})} or 229 * {<OpenLayers.Feature.Vector>}. 238 * {<OpenLayers.Feature.Vector>} The features to create in 239 * the database. 230 240 * options - {Object} Optional object for configuring the request. 231 241 * … … 235 245 */ 236 246 create: function(features, options) { 237 return this.createOrUpdate(features, options, "create"); 247 options = OpenLayers.Util.applyDefaults(options, this.options); 248 249 var resp = this.createOrUpdate(features); 250 resp.requestType = "create"; 251 252 if (options && options.callback) { 253 options.callback.call(options.scope, resp); 254 } 255 256 return resp; 238 257 }, 239 258 … … 244 263 * Parameters: 245 264 * features - {Array({<OpenLayers.Feature.Vector>})} or 246 * {<OpenLayers.Feature.Vector>}. 265 * {<OpenLayers.Feature.Vector>} The features to update in 266 * the database. 247 267 * options - {Object} Optional object for configuring the request. 248 268 * … … 252 272 */ 253 273 update: function(features, options) { 254 return this.createOrUpdate(features, options, "update"); 274 options = OpenLayers.Util.applyDefaults(options, this.options); 275 276 var resp = this.createOrUpdate(features); 277 resp.requestType = "update"; 278 279 if (options && options.callback) { 280 options.callback.call(options.scope, resp); 281 } 282 283 return resp; 255 284 }, 256 285 … … 262 291 * Parameters: 263 292 * features - {Array({<OpenLayers.Feature.Vector>})} or 264 * {<OpenLayers.Feature.Vector>}. 265 * options - {Object} Optional object for configuring the request. 266 * requestType - {String} Either "create" or "update". 293 * {<OpenLayers.Feature.Vector>} The feature to create or update 294 * in the database. 267 295 * 268 296 * Returns: … … 270 298 * object. 271 299 */ 272 createOrUpdate: function(features , options, requestType) {300 createOrUpdate: function(features) { 273 301 if (!(features instanceof Array)) { 274 302 features = [features]; 275 303 } 276 277 options = OpenLayers.Util.applyDefaults(options, this.options);278 304 279 305 var i, len = features.length, feature; … … 282 308 for (i = 0; i < len; i++) { 283 309 feature = features[i]; 310 var params = this.freezeFeature(feature); 284 311 this.db.execute( 285 312 "REPLACE INTO " + this.tableName + 286 313 " (fid, geometry, properties, state)" + 287 314 " VALUES (?, ?, ?, ?)", 288 this.freezeFeature(feature)289 ); 315 params); 316 290 317 var clone = feature.clone(); 291 clone.fid = feature.fid;318 clone.fid = this.extractFidFromField(params[0]); 292 319 insertedFeatures[i] = clone; 293 320 } 294 321 295 var resp =new OpenLayers.Protocol.Response({322 return new OpenLayers.Protocol.Response({ 296 323 code: OpenLayers.Protocol.Response.SUCCESS, 297 requestType: requestType,298 324 features: insertedFeatures, 299 325 reqFeatures: features 300 326 }); 301 302 if (options && options.callback) {303 options.callback.call(options.scope, resp);304 }305 306 return resp;307 327 }, 308 328 … … 312 332 * Parameters: 313 333 * feature - {<OpenLayers.Feature.Vector>} 334 * state - {String} The feature state to store in the database. 314 335 * 315 336 * Returns: … … 317 338 */ 318 339 freezeFeature: function(feature) { 340 // 2 notes: 341 // - fid might not be a string 342 // - getFeatureStateForFreeze needs the feature fid to it's stored 343 // in the feature here 319 344 feature.fid = feature.fid != null ? 320 feature.fid : OpenLayers.Util.createUniqueID(this.FID_PREFIX);321 322 var geometry = feature.geometry ?345 "" + feature.fid : OpenLayers.Util.createUniqueID(this.FID_PREFIX); 346 347 var geometry = feature.geometry != null ? 323 348 feature.geometry.toString() : this.NULL_GEOMETRY; 324 var state = this.saveFeatureState ? 325 feature.state : this.NULL_FEATURE_STATE; 326 327 return [ 328 feature.fid, 329 geometry, 330 this.jsonParser.write(feature.attributes), 331 state 332 ]; 349 350 var properties = this.jsonParser.write(feature.attributes); 351 352 var state = this.getFeatureStateForFreeze(feature); 353 354 return [feature.fid, geometry, properties, state]; 355 }, 356 357 /** 358 * Method: getFeatureStateForFreeze 359 * Get the state of the feature to store into the database. 360 * 361 * Parameters: 362 * feature - {<OpenLayers.Feature.Vector>} The feature. 363 * 364 * Returns 365 * {String} The state 366 */ 367 getFeatureStateForFreeze: function(feature) { 368 var state; 369 if (!this.saveFeatureState) { 370 state = this.NULL_FEATURE_STATE; 371 } else if (this.createdOffline(feature)) { 372 // if the feature was created in offline mode, its 373 // state must remain INSERT 374 state = OpenLayers.State.INSERT; 375 } else { 376 state = feature.state; 377 } 378 return state; 333 379 }, 334 380 … … 340 386 * features - {Array({<OpenLayers.Feature.Vector>})} or 341 387 * {<OpenLayers.Feature.Vector>} 342 * options - {Object} Optional object for configuring the request, it 343 * may include a dontDelete property that indicates whether a 344 * feature that wasn't created offline must actually be deleted 345 * from the database or just be updated (with its new state). 388 * options - {Object} Optional object for configuring the request. 346 389 * This object is modified and should not be reused. 347 390 * … … 361 404 feature = features[i]; 362 405 363 // if the dontDelete flag is set in the options and if the feature 364 // wasn't created offline then don't delete it in the database 365 var doDelete = (typeof feature.fid == 'string' && 366 !!(feature.fid.match(this.fidRegExp))); 367 if (options.dontDelete && !doDelete) { 406 // if saveFeatureState is set to true and if the feature wasn't created 407 // in offline mode we don't delete it in the database but just update 408 // it state column 409 if (this.saveFeatureState && !this.createdOffline(feature)) { 368 410 var toDelete = feature.clone(); 369 411 toDelete.fid = feature.fid; … … 373 415 } 374 416 toDelete.state = feature.state; 375 376 this.update(toDelete); 417 this.createOrUpdate(toDelete); 377 418 } else { 378 419 this.db.execute( … … 393 434 394 435 return resp; 436 }, 437 438 /** 439 * Method: createdOffline 440 * Returns true if the feature had a feature id when it was created in 441 * the Gears database, false otherwise; this is determined by 442 * checking the form of the feature's fid value. 443 * 444 * Parameters: 445 * feature - {<OpenLayers.Feature.Vector>} 446 * 447 * Returns: 448 * {Boolean} 449 */ 450 createdOffline: function(feature) { 451 return (typeof feature.fid == "string" && 452 !!(feature.fid.match(this.fidRegExp))); 395 453 }, 396 454 sandbox/camptocamp/unhcr/tests/Protocol/SQL/Gears.html
r7877 r7918 251 251 } 252 252 253 function test_create OrUpdate(t) {253 function test_create(t) { 254 254 var protocol = new OpenLayers.Protocol.SQL.Gears(); 255 255 if (!protocol.supported()) { … … 262 262 var resp; 263 263 var scope = {"fake": "scope"}; 264 var requestType = "create";265 264 266 265 var options = { … … 268 267 t.eq(resp.CLASS_NAME, "OpenLayers.Protocol.Response", 269 268 "user callback is passed a response"); 270 t.eq(resp.requestType, requestType,269 t.eq(resp.requestType, "create", 271 270 "user callback is passed correct request type in resp"); 272 271 t.ok(this == scope, … … 281 280 feature.attributes.fake = "properties"; 282 281 feature.state = OpenLayers.State.INSERT; 283 resp = protocol.createOrUpdate([feature], options, requestType); 282 resp = protocol.create([feature], options); 283 t.eq(resp.CLASS_NAME, "OpenLayers.Protocol.Response", 284 "create returns a response"); 285 286 // check what we have in the DB 287 // 4 tests 288 resp = protocol.read({"noFeatureStateReset": true}); 289 t.eq(resp.features.length, 1, 290 "create inserts feature in the DB"); 291 t.eq(resp.features[0].fid, feature.fid, 292 "create inserts feature with correct fid"); 293 t.eq(resp.features[0].attributes.fake, feature.attributes.fake, 294 "create inserts feature with correct attributes"); 295 t.eq(resp.features[0].state, feature.state, 296 "create inserts feature with correct state"); 297 298 protocol.clear(); 299 protocol.destroy(); 300 } 301 302 function test_createOrUpdate(t) { 303 var protocol = new OpenLayers.Protocol.SQL.Gears(); 304 if (!protocol.supported()) { 305 t.plan(0); 306 return; 307 } 308 309 t.plan(5); 310 311 // 1 test 312 var feature = new OpenLayers.Feature.Vector(); 313 feature.fid = "1000"; 314 feature.attributes.fake = "properties"; 315 feature.state = OpenLayers.State.INSERT; 316 resp = protocol.createOrUpdate([feature]); 284 317 t.eq(resp.CLASS_NAME, "OpenLayers.Protocol.Response", 285 318 "createOrUpdate returns a response"); … … 287 320 // check what we have in the DB 288 321 // 4 tests 289 resp = protocol.read( );322 resp = protocol.read({"noFeatureStateReset": true}); 290 323 t.eq(resp.features.length, 1, 291 324 "createOrUpdate inserts feature in the DB"); … … 323 356 // 1 test 324 357 fid = 1000; 358 protocol.saveFeatureState = false; 325 359 createOneAndDeleteOne(fid) 326 360 resp = protocol.read(); 327 361 t.eq(resp.features.length, 0, 328 "delete deletes feature i n the DB");362 "delete deletes feature if saveFeatureState is false"); 329 363 protocol.clear(); 330 364 331 365 // 1 test 332 366 fid = 1000; 333 createOneAndDeleteOne(fid, {dontDelete: true}); 367 protocol.saveFeatureState = true; 368 createOneAndDeleteOne(fid); 334 369 resp = protocol.read(); 335 370 t.eq(resp.features.length, 1, 336 "delete does not delete feature if dontDelete is true");371 "delete does not delete feature if saveFeatureState is true"); 337 372 protocol.clear(); 338 373 339 374 // 1 test 340 375 fid = "1000"; 341 createOneAndDeleteOne(fid, {dontDelete: true}); 376 protocol.saveFeatureState = true; 377 createOneAndDeleteOne(fid); 342 378 resp = protocol.read(); 343 379 t.eq(resp.features.length, 1, 344 "delete does not delete feature if dontDelete is true");380 "delete does not delete feature if saveFeatureState is true"); 345 381 protocol.clear(); 346 382 347 383 // 1 test 348 384 fid = protocol.FID_PREFIX + "1000"; 385 protocol.saveFeatureState = true; 349 386 createOneAndDeleteOne(fid, {dontDelete: true}); 350 387 resp = protocol.read(); 351 388 t.eq(resp.features.length, 0, 352 "delete deletes feature if dontDelete is true and fid is prefixed");389 "delete deletes feature if saveFeatureState is true and fid is prefixed"); 353 390 protocol.clear(); 354 391
