diff --git a/lib/edit/attachmentsStore.js b/lib/edit/attachmentsStore.js index ffb1da6..32221e2 100644 --- a/lib/edit/attachmentsStore.js +++ b/lib/edit/attachmentsStore.js @@ -155,7 +155,36 @@ define([], function() } } - this.getUsage = function(callback){ + this.replaceFeatureId = function(featureLayerUrl, oldId, newId, callback) + { + var featureId = featureLayerUrl + "/" + oldId; + + var objectStore = this._db.transaction([OBJECT_STORE_NAME],"readwrite").objectStore(OBJECT_STORE_NAME); + var index = objectStore.index("featureId"); + var keyRange = IDBKeyRange.only(featureId); + var replacedCount = 0; + index.openCursor(keyRange).onsuccess = function(evt) + { + var cursor = evt.target.result; + if(cursor) + { + var newFeatureId = featureLayerUrl + "/" + newId; + var updated = cursor.value; + updated.featureId = newFeatureId; + updated.objectId = newId; + objectStore.put(updated); + replacedCount++; + cursor.continue() + } + else + { + callback(replacedCount); + } + } + } + + this.getUsage = function(callback) + { if(this._db != null){ var usage = { sizeBytes: 0, attachmentCount: 0 }; diff --git a/test/spec/attachmentsStoreSpec.js b/test/spec/attachmentsStoreSpec.js index bd8da46..ec97976 100644 --- a/test/spec/attachmentsStoreSpec.js +++ b/test/spec/attachmentsStoreSpec.js @@ -101,6 +101,27 @@ describe("attachments store module", function() }); }); + async.it("replace feature id", function(done) + { + g_attachmentsStore.replaceFeatureId("layer1",1,100, function(success) + { + expect(success).toBe(2); + g_attachmentsStore.getAttachmentsByFeatureId("layer1", 1, function(attachments) + { + expect(attachments.length).toBe(0); + g_attachmentsStore.getAttachmentsByFeatureId("layer1", 100, function(attachments) + { + expect(attachments.length).toBe(2); + expect(attachments[0].objectId).toBe(100); + expect(attachments[1].objectId).toBe(100); + expect(attachments[0].content).toContain("feature 1"); + expect(attachments[1].content).toContain("feature 1"); + done(); + }); + }); + }); + }); + async.it("delete attachments of a single feature", function(done) { g_attachmentsStore.deleteAttachmentsByFeatureId("layer1", 300, function(deletedCount) @@ -113,7 +134,7 @@ describe("attachments store module", function() expect(usage).not.toBeNull(); expect(usage.attachmentCount).toBe(testData.length); - g_attachmentsStore.deleteAttachmentsByFeatureId("layer1", 1, function(deletedCount) + g_attachmentsStore.deleteAttachmentsByFeatureId("layer1", 100, function(deletedCount) { expect(deletedCount).toBe(2); setTimeout(function()