From 6d08c81b3b83b6e3563b4e4579fa5fa422a798ab Mon Sep 17 00:00:00 2001 From: Henry Date: Tue, 20 Oct 2015 12:18:30 -0700 Subject: [PATCH] First iteration of duplication complete --- bundles.json | 2 +- .../entanglement/src/services/CopyService.js | 45 ++++++++++++------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/bundles.json b/bundles.json index c85b681bf5..3940336656 100644 --- a/bundles.json +++ b/bundles.json @@ -22,7 +22,7 @@ "platform/features/events", "platform/forms", "platform/identity", - "platform/persistence/local", + "platform/persistence/elastic", "platform/persistence/queue", "platform/policy", "platform/entanglement", diff --git a/platform/entanglement/src/services/CopyService.js b/platform/entanglement/src/services/CopyService.js index 6798443b48..843ec6cd2a 100644 --- a/platform/entanglement/src/services/CopyService.js +++ b/platform/entanglement/src/services/CopyService.js @@ -62,6 +62,13 @@ define( * @param domainObject */ CopyService.prototype.buildCopyGraph = function(domainObject, parent) { + /* TODO: Use contextualized objects here. + Parent should be fully contextualized, and either the + original parent or a contextualized clone. The subsequent + composition changes can then be performed regardless of + whether it is the top level composition of the original + parent being updated, or of one of the cloned children. */ + var clones = [], $q = this.$q, self = this; @@ -70,15 +77,16 @@ define( return JSON.parse(JSON.stringify(object)); } - function copy(object, parent) { - var modelClone = clone(object.getModel()); + function copy(originalObject, originalParent) { + var modelClone = clone(originalObject.getModel()); modelClone.composition = []; + modelClone.id = uuid(); - if (object.hasCapability('composition')) { - return object.useCapability('composition').then(function(composees){ + if (originalObject.hasCapability('composition')) { + return originalObject.useCapability('composition').then(function(composees){ return composees.reduce(function(promise, composee){ return promise.then(function(){ - return copy(composee, object).then(function(composeeClone){ + return copy(composee, originalObject).then(function(composeeClone){ /* TODO: Use the composition capability for this. Just not sure how to contextualize the as-yet non-existent modelClone object. */ @@ -87,12 +95,14 @@ define( }); }); }, $q.when(undefined)).then(function (){ - modelClone.id = uuid(); - clones.push({persistence: parent.getCapability('persistence'), model: modelClone}); + /* Todo: Move this outside of promise and avoid + duplication below */ + clones.push({persistence: originalParent.getCapability('persistence'), model: modelClone}); return modelClone; }); }); } else { + clones.push({persistence: originalParent.getCapability('persistence'), model: modelClone}); return $q.when(modelClone); } }; @@ -106,15 +116,20 @@ define( self = this; if (this.validate(domainObject, parent)) { return this.buildCopyGraph(domainObject, parent).then(function(clones){ - return clones.reduce(function(promise, clone){ - /* - TODO: Persist the clone. We need to bypass the creation service on this because it wants to create the composition along the way, which we want to avoid. The composition has already been done directly in the model. - */ - return promise.then(function(){ + return $q.all(clones.map(function(clone){ return self.persistenceService.createObject(clone.persistence.getSpace(), clone.model.id, clone.model); - }); - }, $q.when(undefined)); - }) + })).then(function(){ return clones}); + }).then(function(clones) { + var parentClone = clones[clones.length-1]; + parentClone.model.location = parent.getId() + return $q.when( + parent.hasCapability('composition') && + parent.getCapability('composition').add(parentClone.model.id) + .then( + function(){ + parent.getCapability("persistence").persist() + })); + }); } else { throw new Error( "Tried to copy objects without validating first."