[Create] Utilize passed-in filter

...when determining which objects should be cloned.
This commit is contained in:
Victor Woeltjen 2016-02-10 13:38:53 -08:00
parent f602aa9247
commit 72df464f0f
2 changed files with 25 additions and 9 deletions

View File

@ -81,10 +81,23 @@ define(
* domainObject when the duplication is successful. * domainObject when the duplication is successful.
*/ */
CopyService.prototype.perform = function (domainObject, parent, filter) { CopyService.prototype.perform = function (domainObject, parent, filter) {
var $q = this.$q, var policyService = this.policyService;
copyTask = new CopyTask(domainObject, parent, this.policyService, this.$q);
function completeFilter(domainObject) {
return (!filter || filter(domainObject)) &&
policyService.allow(
"creation",
domainObject.getCapability("type")
);
}
if (this.validate(domainObject, parent)) { if (this.validate(domainObject, parent)) {
return copyTask.perform(); return new CopyTask(
domainObject,
parent,
completeFilter,
this.$q
).perform();
} else { } else {
throw new Error( throw new Error(
"Tried to copy objects without validating first." "Tried to copy objects without validating first."

View File

@ -31,18 +31,21 @@ define(
* This class encapsulates the process of copying a domain object * This class encapsulates the process of copying a domain object
* and all of its children. * and all of its children.
* *
* @param domainObject The object to copy * @param {DomainObject} domainObject The object to copy
* @param parent The new location of the cloned object tree * @param {DomainObject} parent The new location of the cloned object tree
* @param $q * @param {platform/entanglement.CopyService~filter} filter
* a function used to filter out objects from
* the cloning process
* @param $q Angular's $q, for promises
* @constructor * @constructor
*/ */
function CopyTask (domainObject, parent, policyService, $q){ function CopyTask (domainObject, parent, filter, $q){
this.domainObject = domainObject; this.domainObject = domainObject;
this.parent = parent; this.parent = parent;
this.firstClone = undefined; this.firstClone = undefined;
this.$q = $q; this.$q = $q;
this.deferred = undefined; this.deferred = undefined;
this.policyService = policyService; this.filter = filter;
this.persisted = 0; this.persisted = 0;
this.clones = []; this.clones = [];
this.idMap = {}; this.idMap = {};
@ -198,7 +201,7 @@ define(
//Check if the type of the object being copied allows for //Check if the type of the object being copied allows for
// creation of new instances. If it does not, then a link to the // creation of new instances. If it does not, then a link to the
// original will be created instead. // original will be created instead.
if (this.policyService.allow("creation", originalObject.getCapability("type"))){ if (this.filter(originalObject)) {
//create a new clone of the original object. Use the //create a new clone of the original object. Use the
// creation capability of the targetParent to create the // creation capability of the targetParent to create the
// new clone. This will ensure that the correct persistence // new clone. This will ensure that the correct persistence