Include objectStyles reference to conditionSetIdentifier in imports ()

* Include objectStyles reference to conditionSetIdentifier in imports

* Add tests for export

* Refactored some code and removed console log
This commit is contained in:
Shefali Joshi 2022-06-21 11:34:45 -07:00 committed by GitHub
parent 417b225505
commit c384cf67da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 146 additions and 19 deletions
src/plugins
conditionWidget/components
exportAsJSONAction
importFromJSONAction

@ -136,8 +136,8 @@ export default {
this.url = url; this.url = url;
} }
const conditionSetIdentifier = domainObject.configuration.objectStyles.conditionSetIdentifier; const conditionSetIdentifier = domainObject.configuration?.objectStyles?.conditionSetIdentifier;
if (this.conditionSetIdentifier !== conditionSetIdentifier) { if (conditionSetIdentifier && this.conditionSetIdentifier !== conditionSetIdentifier) {
this.conditionSetIdentifier = conditionSetIdentifier; this.conditionSetIdentifier = conditionSetIdentifier;
} }

@ -128,6 +128,30 @@ export default class ExportAsJSONAction {
return copyOfChild; return copyOfChild;
} }
/**
* @private
* @param {object} child
* @param {object} parent
* @returns {object}
*/
_rewriteLinkForReference(child, parent) {
const childId = this._getId(child);
this.externalIdentifiers.push(childId);
const copyOfChild = JSON.parse(JSON.stringify(child));
copyOfChild.identifier.key = uuid();
const newIdString = this._getId(copyOfChild);
const parentId = this._getId(parent);
this.idMap[childId] = newIdString;
copyOfChild.location = null;
parent.configuration.objectStyles.conditionSetIdentifier = copyOfChild.identifier;
this.tree[newIdString] = copyOfChild;
this.tree[parentId].configuration.objectStyles.conditionSetIdentifier = copyOfChild.identifier;
return copyOfChild;
}
/** /**
* @private * @private
*/ */
@ -159,23 +183,27 @@ export default class ExportAsJSONAction {
"rootId": this._getId(this.root) "rootId": this._getId(this.root)
}; };
} }
/** /**
* @private * @private
* @param {object} parent * @param {object} parent
*/ */
_write(parent) { _write(parent) {
this.calls++; this.calls++;
//conditional object styles are not saved on the composition, so we need to check for them
let childObjectReferenceId = parent.configuration?.objectStyles?.conditionSetIdentifier;
const composition = this.openmct.composition.get(parent); const composition = this.openmct.composition.get(parent);
if (composition !== undefined) { if (composition !== undefined) {
composition.load() composition.load()
.then((children) => { .then((children) => {
children.forEach((child, index) => { children.forEach((child, index) => {
// Only export if object is creatable // Only export if object is creatable
if (this._isCreatableAndPersistable(child)) { if (this._isCreatableAndPersistable(child)) {
// Prevents infinite export of self-contained objs // Prevents infinite export of self-contained objs
if (!Object.prototype.hasOwnProperty.call(this.tree, this._getId(child))) { if (!Object.prototype.hasOwnProperty.call(this.tree, this._getId(child))) {
// If object is a link to something absent from // If object is a link to something absent from
// tree, generate new id and treat as new object // tree, generate new id and treat as new object
if (this._isLinkedObject(child, parent)) { if (this._isLinkedObject(child, parent)) {
child = this._rewriteLink(child, parent); child = this._rewriteLink(child, parent);
} else { } else {
@ -186,18 +214,41 @@ export default class ExportAsJSONAction {
} }
} }
}); });
this.calls--; this._decrementCallsAndSave();
if (this.calls === 0) {
this._rewriteReferences();
this._saveAs(this._wrapTree());
}
}); });
} else { } else if (!childObjectReferenceId) {
this.calls--; this._decrementCallsAndSave();
if (this.calls === 0) { }
this._rewriteReferences();
this._saveAs(this._wrapTree()); if (childObjectReferenceId) {
} this.openmct.objects.get(childObjectReferenceId)
.then((child) => {
// Only export if object is creatable
if (this._isCreatableAndPersistable(child)) {
// Prevents infinite export of self-contained objs
if (!Object.prototype.hasOwnProperty.call(this.tree, this._getId(child))) {
// If object is a link to something absent from
// tree, generate new id and treat as new object
if (this._isLinkedObject(child, parent)) {
child = this._rewriteLinkForReference(child, parent);
} else {
this.tree[this._getId(child)] = child;
}
this._write(child);
}
}
this._decrementCallsAndSave();
});
}
}
_decrementCallsAndSave() {
this.calls--;
if (this.calls === 0) {
this._rewriteReferences();
this._saveAs(this._wrapTree());
} }
} }
} }

@ -322,4 +322,57 @@ describe('Export as JSON plugin', () => {
exportAsJSONAction.invoke([parent]); exportAsJSONAction.invoke([parent]);
}); });
it('ExportAsJSONAction exports object references from tree', (done) => {
const parent = {
composition: [],
configuration: {
objectStyles: {
conditionSetIdentifier: {
key: 'child',
namespace: ''
}
}
},
identifier: {
key: 'parent',
namespace: ''
},
name: 'Parent',
type: 'folder',
modified: 1503598129176,
location: 'mine',
persisted: 1503598129176
};
const child = {
composition: [],
identifier: {
key: 'child',
namespace: ''
},
name: 'Child',
type: 'folder',
modified: 1503598132428,
location: null,
persisted: 1503598132428
};
spyOn(openmct.objects, 'get').and.callFake(object => {
return Promise.resolve(child);
});
spyOn(exportAsJSONAction, '_saveAs').and.callFake(completedTree => {
expect(Object.keys(completedTree).length).toBe(2);
const conditionSetId = Object.keys(completedTree.openmct)[1];
expect(Object.prototype.hasOwnProperty.call(completedTree, 'openmct')).toBeTruthy();
expect(Object.prototype.hasOwnProperty.call(completedTree, 'rootId')).toBeTruthy();
expect(Object.prototype.hasOwnProperty.call(completedTree.openmct, 'parent')).toBeTruthy();
expect(completedTree.openmct[conditionSetId].name).toBe('Child');
done();
});
exportAsJSONAction.invoke([parent]);
});
}); });

@ -82,12 +82,14 @@ export default class ImportAsJSONAction {
* @param {object} seen * @param {object} seen
*/ */
_deepInstantiate(parent, tree, seen) { _deepInstantiate(parent, tree, seen) {
if (this.openmct.composition.get(parent)) { let objectIdentifiers = this._getObjectReferenceIds(parent);
if (objectIdentifiers.length) {
let newObj; let newObj;
seen.push(parent.id); seen.push(parent.id);
parent.composition.forEach(async (childId) => { objectIdentifiers.forEach(async (childId) => {
const keystring = this.openmct.objects.makeKeyString(childId); const keystring = this.openmct.objects.makeKeyString(childId);
if (!tree[keystring] || seen.includes(keystring)) { if (!tree[keystring] || seen.includes(keystring)) {
return; return;
@ -101,6 +103,27 @@ export default class ImportAsJSONAction {
}, this); }, this);
} }
} }
/**
* @private
* @param {object} parent
* @returns [identifiers]
*/
_getObjectReferenceIds(parent) {
let objectIdentifiers = [];
let parentComposition = this.openmct.composition.get(parent);
if (parentComposition) {
objectIdentifiers = Array.from(parentComposition.domainObject.composition);
}
//conditional object styles are not saved on the composition, so we need to check for them
let parentObjectReference = parent.configuration?.objectStyles?.conditionSetIdentifier;
if (parentObjectReference) {
objectIdentifiers.push(parentObjectReference);
}
return objectIdentifiers;
}
/** /**
* @private * @private
* @param {object} tree * @param {object} tree