Removed use of composition and mutation because they trigger the search indexer too early and it tries to retrieve objects that have not been persisted yet

This commit is contained in:
Henry 2015-12-02 19:10:10 -08:00
parent 8e3c5db3bf
commit cee0ecf0ef
2 changed files with 63 additions and 40 deletions

View File

@ -71,7 +71,7 @@ define(
*/ */
CopyService.prototype.perform = function (domainObject, parent) { CopyService.prototype.perform = function (domainObject, parent) {
var $q = this.$q, var $q = this.$q,
copyTask = new CopyTask(domainObject, parent, this.persistenceService, this.$q, this.now); copyTask = new CopyTask(domainObject, parent, this.persistenceService, this.policyService, this.$q, this.now);
if (this.validate(domainObject, parent)) { if (this.validate(domainObject, parent)) {
return copyTask.perform(); return copyTask.perform();
} else { } else {

View File

@ -38,12 +38,13 @@ define(
* @param now * @param now
* @constructor * @constructor
*/ */
function CopyTask (domainObject, parent, persistenceService, $q, now){ function CopyTask (domainObject, parent, persistenceService, policyService, $q, now){
this.domainObject = domainObject; this.domainObject = domainObject;
this.parent = parent; this.parent = parent;
this.$q = $q; this.$q = $q;
this.deferred = undefined; this.deferred = undefined;
this.persistenceService = persistenceService; this.persistenceService = persistenceService;
this.policyService = policyService;
this.persistenceSpace = parent.getCapability("persistence") && parent.getCapability("persistence").getSpace(); this.persistenceSpace = parent.getCapability("persistence") && parent.getCapability("persistence").getSpace();
this.persisted = 0; this.persisted = 0;
this.now = now; this.now = now;
@ -54,20 +55,30 @@ define(
//Once copied, associate each cloned //Once copied, associate each cloned
// composee with its parent clone // composee with its parent clone
//Could check islink here, and not set the location if it is a parent.getModel().composition.push(child.getId());
// link? Object should have been contextualized during
// composition, so isLink should work. //Check if the object being composed is a link
child.model.location = parent.id; if (!child.getCapability("location").isLink()) {
parent.model.composition = parent.model.composition || []; child.getModel().location = parent.getId();
return parent.model.composition.push(child.id); }
} }
function cloneObjectModel(objectModel) { function cloneObjectModel(objectModel) {
var clone = JSON.parse(JSON.stringify(objectModel)); var clone = JSON.parse(JSON.stringify(objectModel));
delete clone.composition; /**
* Reset certain fields.
*/
//If has a composition, set it to an empty array. Will be
// recomposed later with the ids of its cloned children.
if (clone.composition) {
//Important to set it to an empty array here, otherwise
// hasCapability("composition") returns false;
clone.composition = [];
}
delete clone.persisted; delete clone.persisted;
delete clone.modified; delete clone.modified;
delete clone.location;
return clone; return clone;
} }
@ -78,13 +89,10 @@ define(
* result in automatic request batching by the browser. * result in automatic request batching by the browser.
*/ */
function persistObjects(self) { function persistObjects(self) {
return self.$q.all(self.clones.map(function(clone){ return self.$q.all(self.clones.map(function(clone){
clone.model.persisted = self.now(); return clone.getCapability("persistence").persist().then(function(){
return self.persistenceService.createObject(self.persistenceSpace, clone.id, clone.model) self.deferred.notify({phase: "copying", totalObjects: self.clones.length, processed: ++self.persisted});
.then(function(){ });
self.deferred.notify({phase: "copying", totalObjects: self.clones.length, processed: ++self.persisted});
});
})).then(function(){ })).then(function(){
return self; return self;
}); });
@ -96,15 +104,13 @@ define(
function addClonesToParent(self) { function addClonesToParent(self) {
var parentClone = self.clones[self.clones.length-1]; var parentClone = self.clones[self.clones.length-1];
if (!self.parent.hasCapability('composition')){ //self.persistenceService
return self.$q.reject(); // .updateObject(self.persistenceSpace,
} // parentClone.id, parentClone.model)
return parentClone.getCapability("persistence").persist()
return self.persistenceService .then(function(){self.parent.getCapability("composition").add(parentClone.getId())})
.updateObject(self.persistenceSpace, parentClone.id, parentClone.model) .then(function(){return self.parent.getCapability("persistence").persist();})
.then(function(){return self.parent.getCapability("composition").add(parentClone.id);}) .then(function(){return parentClone;});
.then(function(){return self.parent.getCapability("persistence").persist();})
.then(function(){return parentClone;});
// Ensure the clone of the original domainObject is returned // Ensure the clone of the original domainObject is returned
} }
@ -123,7 +129,7 @@ define(
return promise.then(function(){ return promise.then(function(){
// ...to recursively copy it (and its children) // ...to recursively copy it (and its children)
return self.copy(composee, originalParent).then(function(composee){ return self.copy(composee, originalParent).then(function(composee){
composeChild(composee, clonedParent); return composeChild(composee, clonedParent);
}); });
});}, self.$q.when(undefined) });}, self.$q.when(undefined)
); );
@ -142,24 +148,37 @@ define(
*/ */
CopyTask.prototype.copy = function(originalObject, originalParent) { CopyTask.prototype.copy = function(originalObject, originalParent) {
var self = this, var self = this,
modelClone = {
id: uuid(),
model: cloneObjectModel(originalObject.getModel())
},
clone; clone;
return this.$q.when(originalObject.useCapability('composition')).then(function(composees){ //Check if the type of the object being copied allows for
self.deferred.notify({phase: "preparing"}); // creation of new instances. If it does not, then a link to the
//Duplicate the object's children, and their children, and // original will be created instead.
// so on down to the leaf nodes of the tree. if (this.policyService.allow("creation", originalObject.getCapability("type"))){
//If it is a link, don't both with children //create a new clone of the original object. Use the
return self.copyComposees(composees, modelClone, originalObject).then(function (){ // creation capability of the targetParent to create the
//Add the clone to the list of clones that will // new clone. This will ensure that the correct persistence
//be returned by this function // space is used.
self.clones.push(modelClone); clone = this.parent.hasCapability("instantiation") && originalParent.useCapability("instantiation", cloneObjectModel(originalObject.getModel()));
return modelClone;
//Iterate through child tree
return this.$q.when(originalObject.useCapability('composition')).then(function(composees){
self.deferred.notify({phase: "preparing"});
//Duplicate the object's children, and their children, and
// so on down to the leaf nodes of the tree.
//If it is a link, don't both with children
return self.copyComposees(composees, clone, originalObject).then(function (){
//Add the clone to the list of clones that will
//be returned by this function
self.clones.push(clone);
return clone;
});
}); });
}); } else {
//Creating a link, no need to iterate children
return $q.when(originalObject);
}
}; };
/** /**
@ -191,6 +210,10 @@ define(
CopyTask.prototype.perform = function(){ CopyTask.prototype.perform = function(){
this.deferred = this.$q.defer(); this.deferred = this.$q.defer();
if (!this.parent.hasCapability('composition')){
return self.$q.reject();
}
this.buildCopyPlan() this.buildCopyPlan()
.then(persistObjects) .then(persistObjects)
.then(addClonesToParent) .then(addClonesToParent)