OpenLayers OpenLayers

Changeset 7918

Show
Ignore:
Timestamp:
09/01/08 14:11:09 (3 months ago)
Author:
elemoine
Message:

commit changes made to patch attached to #1699 (Protocol.Gears)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/camptocamp/unhcr/lib/OpenLayers/Protocol/SQL/Gears.js

    r7877 r7918  
    6363     * Property: saveFeatureState 
    6464     * {Boolean} Whether to save the feature state (<OpenLayers.State>) 
    65      *      into the database
     65     *      into the database, defaults to true
    6666     */     
    6767    saveFeatureState: true, 
     
    142142     * 
    143143     * 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. 
    146151     * 
    147152     * Returns: 
     
    152157        options = OpenLayers.Util.applyDefaults(options, this.options); 
    153158 
    154         var features = []; 
     159        var feature, features = []; 
    155160        var rs = this.db.execute("SELECT * FROM " + this.tableName); 
    156161        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); 
    158167            rs.next(); 
    159168        } 
     
    227236     * Parameters: 
    228237     * features - {Array({<OpenLayers.Feature.Vector>})} or 
    229      *            {<OpenLayers.Feature.Vector>}. 
     238     *            {<OpenLayers.Feature.Vector>} The features to create in 
     239     *            the database. 
    230240     * options - {Object} Optional object for configuring the request. 
    231241     * 
     
    235245     */ 
    236246    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; 
    238257    }, 
    239258 
     
    244263     * Parameters: 
    245264     * features - {Array({<OpenLayers.Feature.Vector>})} or 
    246      *            {<OpenLayers.Feature.Vector>}. 
     265     *            {<OpenLayers.Feature.Vector>} The features to update in 
     266     *            the database. 
    247267     * options - {Object} Optional object for configuring the request. 
    248268     * 
     
    252272     */ 
    253273    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; 
    255284    }, 
    256285 
     
    262291     * Parameters: 
    263292     * 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. 
    267295     * 
    268296     * Returns: 
     
    270298     *          object. 
    271299     */ 
    272     createOrUpdate: function(features, options, requestType) { 
     300    createOrUpdate: function(features) { 
    273301        if (!(features instanceof Array)) { 
    274302            features = [features]; 
    275303        } 
    276  
    277         options = OpenLayers.Util.applyDefaults(options, this.options); 
    278304 
    279305        var i, len = features.length, feature; 
     
    282308        for (i = 0; i < len; i++) { 
    283309            feature = features[i]; 
     310            var params = this.freezeFeature(feature); 
    284311            this.db.execute( 
    285312                "REPLACE INTO " + this.tableName +  
    286313                " (fid, geometry, properties, state)" +  
    287314                " VALUES (?, ?, ?, ?)", 
    288                 this.freezeFeature(feature) 
    289             ); 
     315                params); 
     316 
    290317            var clone = feature.clone(); 
    291             clone.fid = feature.fid
     318            clone.fid = this.extractFidFromField(params[0])
    292319            insertedFeatures[i] = clone; 
    293320        } 
    294321 
    295         var resp = new OpenLayers.Protocol.Response({ 
     322        return new OpenLayers.Protocol.Response({ 
    296323            code: OpenLayers.Protocol.Response.SUCCESS, 
    297             requestType: requestType, 
    298324            features: insertedFeatures, 
    299325            reqFeatures: features 
    300326        }); 
    301  
    302         if (options && options.callback) { 
    303             options.callback.call(options.scope, resp); 
    304         } 
    305  
    306         return resp; 
    307327    }, 
    308328 
     
    312332     * Parameters: 
    313333     * feature - {<OpenLayers.Feature.Vector>} 
     334     * state - {String} The feature state to store in the database. 
    314335     * 
    315336     * Returns: 
     
    317338     */ 
    318339    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 
    319344        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
    323348            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; 
    333379    }, 
    334380 
     
    340386     * features - {Array({<OpenLayers.Feature.Vector>})} or 
    341387     *            {<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. 
    346389     *       This object is modified and should not be reused. 
    347390     * 
     
    361404            feature = features[i]; 
    362405 
    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)) { 
    368410                var toDelete = feature.clone(); 
    369411                toDelete.fid = feature.fid; 
     
    373415                } 
    374416                toDelete.state = feature.state; 
    375                  
    376                 this.update(toDelete); 
     417                this.createOrUpdate(toDelete); 
    377418            } else { 
    378419                this.db.execute( 
     
    393434 
    394435        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))); 
    395453    }, 
    396454 
  • sandbox/camptocamp/unhcr/tests/Protocol/SQL/Gears.html

    r7877 r7918  
    251251     } 
    252252 
    253      function test_createOrUpdate(t) { 
     253     function test_create(t) { 
    254254        var protocol = new OpenLayers.Protocol.SQL.Gears(); 
    255255        if (!protocol.supported()) { 
     
    262262        var resp; 
    263263        var scope = {"fake": "scope"}; 
    264         var requestType = "create"; 
    265264 
    266265        var options = { 
     
    268267                t.eq(resp.CLASS_NAME, "OpenLayers.Protocol.Response", 
    269268                     "user callback is passed a response"); 
    270                 t.eq(resp.requestType, requestType
     269                t.eq(resp.requestType, "create"
    271270                     "user callback is passed correct request type in resp"); 
    272271                t.ok(this == scope, 
     
    281280        feature.attributes.fake = "properties"; 
    282281        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]); 
    284317        t.eq(resp.CLASS_NAME, "OpenLayers.Protocol.Response", 
    285318             "createOrUpdate returns a response"); 
     
    287320        // check what we have in the DB 
    288321        // 4 tests 
    289         resp = protocol.read(); 
     322        resp = protocol.read({"noFeatureStateReset": true}); 
    290323        t.eq(resp.features.length, 1, 
    291324             "createOrUpdate inserts feature in the DB"); 
     
    323356        // 1 test 
    324357        fid = 1000; 
     358        protocol.saveFeatureState = false; 
    325359        createOneAndDeleteOne(fid) 
    326360        resp = protocol.read(); 
    327361        t.eq(resp.features.length, 0, 
    328              "delete deletes feature in the DB"); 
     362             "delete deletes feature if saveFeatureState is false"); 
    329363        protocol.clear(); 
    330364 
    331365        // 1 test 
    332366        fid = 1000; 
    333         createOneAndDeleteOne(fid, {dontDelete: true}); 
     367        protocol.saveFeatureState = true; 
     368        createOneAndDeleteOne(fid); 
    334369        resp = protocol.read(); 
    335370        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"); 
    337372        protocol.clear(); 
    338373 
    339374        // 1 test 
    340375        fid = "1000"; 
    341         createOneAndDeleteOne(fid, {dontDelete: true}); 
     376        protocol.saveFeatureState = true; 
     377        createOneAndDeleteOne(fid); 
    342378        resp = protocol.read(); 
    343379        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"); 
    345381        protocol.clear(); 
    346382 
    347383        // 1 test 
    348384        fid = protocol.FID_PREFIX + "1000"; 
     385        protocol.saveFeatureState = true; 
    349386        createOneAndDeleteOne(fid, {dontDelete: true}); 
    350387        resp = protocol.read(); 
    351388        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"); 
    353390        protocol.clear(); 
    354391