mirror of
https://github.com/nasa/openmct.git
synced 2024-12-30 09:58:52 +00:00
Json export fix (#2043)
* Refactoring export to json to use new style objects * Added convenience function to Object API to get identifiers as strings * Updated tests for ExportAsJSON * Fixed bug with parent composition not being updated with new ID. Also rewrite IDs in configuration etc. Fixes #2042
This commit is contained in:
parent
90a6bbc13e
commit
07fb20c32f
@ -49,9 +49,11 @@ define([
|
|||||||
"category": "contextual",
|
"category": "contextual",
|
||||||
"cssClass": "icon-export",
|
"cssClass": "icon-export",
|
||||||
"depends": [
|
"depends": [
|
||||||
|
"openmct",
|
||||||
"exportService",
|
"exportService",
|
||||||
"policyService",
|
"policyService",
|
||||||
"identifierService"
|
"identifierService",
|
||||||
|
"typeService"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
* at runtime from the About dialog for additional information.
|
* at runtime from the About dialog for additional information.
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
define([], function () {
|
define(['lodash'], function (_) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The ExportAsJSONAction is available from context menus and allows a user
|
* The ExportAsJSONAction is available from context menus and allows a user
|
||||||
@ -31,12 +31,14 @@ define([], function () {
|
|||||||
* @memberof platform/import-export
|
* @memberof platform/import-export
|
||||||
*/
|
*/
|
||||||
function ExportAsJSONAction(
|
function ExportAsJSONAction(
|
||||||
|
openmct,
|
||||||
exportService,
|
exportService,
|
||||||
policyService,
|
policyService,
|
||||||
identifierService,
|
identifierService,
|
||||||
|
typeService,
|
||||||
context
|
context
|
||||||
) {
|
) {
|
||||||
|
this.openmct = openmct;
|
||||||
this.root = {};
|
this.root = {};
|
||||||
this.tree = {};
|
this.tree = {};
|
||||||
this.calls = 0;
|
this.calls = 0;
|
||||||
@ -45,15 +47,21 @@ define([], function () {
|
|||||||
this.exportService = exportService;
|
this.exportService = exportService;
|
||||||
this.policyService = policyService;
|
this.policyService = policyService;
|
||||||
this.identifierService = identifierService;
|
this.identifierService = identifierService;
|
||||||
|
this.typeService = typeService;
|
||||||
|
|
||||||
|
this.idMap = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
ExportAsJSONAction.prototype.perform = function () {
|
ExportAsJSONAction.prototype.perform = function () {
|
||||||
this.root = this.context.domainObject;
|
var root = this.context.domainObject.useCapability('adapter');
|
||||||
this.tree[this.root.getId()] = this.root.getModel();
|
this.root = this.copyObject(root);
|
||||||
|
var rootId = this.getId(this.root);
|
||||||
|
this.tree[rootId] = this.root;
|
||||||
|
|
||||||
this.saveAs = function (completedTree) {
|
this.saveAs = function (completedTree) {
|
||||||
this.exportService.exportJSON(
|
this.exportService.exportJSON(
|
||||||
completedTree,
|
completedTree,
|
||||||
{filename: this.root.getModel().name + '.json'}
|
{filename: this.root.name + '.json'}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -68,22 +76,23 @@ define([], function () {
|
|||||||
* @param {Object} parent
|
* @param {Object} parent
|
||||||
*/
|
*/
|
||||||
ExportAsJSONAction.prototype.write = function (parent) {
|
ExportAsJSONAction.prototype.write = function (parent) {
|
||||||
|
|
||||||
this.calls++;
|
this.calls++;
|
||||||
if (parent.hasCapability('composition')) {
|
var composition = this.openmct.composition.get(parent);
|
||||||
parent.useCapability('composition')
|
|
||||||
|
if (composition !== undefined) {
|
||||||
|
composition.load()
|
||||||
.then(function (children) {
|
.then(function (children) {
|
||||||
children.forEach(function (child, index) {
|
children.forEach(function (child, index) {
|
||||||
// Only export if object is creatable
|
// Only export if object is creatable
|
||||||
if (this.isCreatable(child)) {
|
if (this.isCreatable(child)) {
|
||||||
// Prevents infinite export of self-contained objs
|
// Prevents infinite export of self-contained objs
|
||||||
if (!this.tree.hasOwnProperty(child.getId())) {
|
if (!this.tree.hasOwnProperty(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.isExternal(child, parent)) {
|
if (this.isExternal(child, parent)) {
|
||||||
this.rewriteLink(child, parent);
|
child = this.rewriteLink(child, parent);
|
||||||
} else {
|
} else {
|
||||||
this.tree[child.getId()] = child.getModel();
|
this.tree[this.getId(child)] = child;
|
||||||
}
|
}
|
||||||
this.write(child);
|
this.write(child);
|
||||||
}
|
}
|
||||||
@ -91,12 +100,14 @@ define([], function () {
|
|||||||
}.bind(this));
|
}.bind(this));
|
||||||
this.calls--;
|
this.calls--;
|
||||||
if (this.calls === 0) {
|
if (this.calls === 0) {
|
||||||
|
this.rewriteReferences();
|
||||||
this.saveAs(this.wrapTree());
|
this.saveAs(this.wrapTree());
|
||||||
}
|
}
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
} else {
|
} else {
|
||||||
this.calls--;
|
this.calls--;
|
||||||
if (this.calls === 0) {
|
if (this.calls === 0) {
|
||||||
|
this.rewriteReferences();
|
||||||
this.saveAs(this.wrapTree());
|
this.saveAs(this.wrapTree());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,29 +120,34 @@ define([], function () {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
ExportAsJSONAction.prototype.rewriteLink = function (child, parent) {
|
ExportAsJSONAction.prototype.rewriteLink = function (child, parent) {
|
||||||
this.externalIdentifiers.push(child.getId());
|
this.externalIdentifiers.push(this.getId(child));
|
||||||
var parentModel = parent.getModel();
|
var index = _.findIndex(parent.composition, function (id) {
|
||||||
var childModel = child.getModel();
|
return _.isEqual(child.identifier, id);
|
||||||
var index = parentModel.composition.indexOf(child.getId());
|
});
|
||||||
var newModel = this.copyModel(childModel);
|
var copyOfChild = this.copyObject(child);
|
||||||
var newId = this.identifierService.generate();
|
copyOfChild.identifier.key = this.identifierService.generate();
|
||||||
|
var newIdString = this.getId(copyOfChild);
|
||||||
|
var parentId = this.getId(parent);
|
||||||
|
|
||||||
newModel.location = parent.getId();
|
this.idMap[this.getId(child)] = newIdString;
|
||||||
this.tree[newId] = newModel;
|
copyOfChild.location = parentId;
|
||||||
this.tree[parent.getId()] = this.copyModel(parentModel);
|
parent.composition[index] = copyOfChild.identifier;
|
||||||
this.tree[parent.getId()].composition[index] = newId;
|
this.tree[newIdString] = copyOfChild;
|
||||||
|
this.tree[parentId].composition[index] = newIdString;
|
||||||
|
|
||||||
|
return copyOfChild;
|
||||||
};
|
};
|
||||||
|
|
||||||
ExportAsJSONAction.prototype.copyModel = function (model) {
|
ExportAsJSONAction.prototype.copyObject = function (object) {
|
||||||
var jsonString = JSON.stringify(model);
|
var jsonString = JSON.stringify(object);
|
||||||
return JSON.parse(jsonString);
|
return JSON.parse(jsonString);
|
||||||
};
|
};
|
||||||
|
|
||||||
ExportAsJSONAction.prototype.isExternal = function (child, parent) {
|
ExportAsJSONAction.prototype.isExternal = function (child, parent) {
|
||||||
if (child.getModel().location !== parent.getId() &&
|
if (child.location !== this.getId(parent) &&
|
||||||
!Object.keys(this.tree).includes(child.getModel().location) &&
|
!Object.keys(this.tree).includes(child.location) &&
|
||||||
child.getId() !== this.root.getId() ||
|
this.getId(child) !== this.getId(this.root) ||
|
||||||
this.externalIdentifiers.includes(child.getId())) {
|
this.externalIdentifiers.includes(this.getId(child))) {
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -147,16 +163,36 @@ define([], function () {
|
|||||||
ExportAsJSONAction.prototype.wrapTree = function () {
|
ExportAsJSONAction.prototype.wrapTree = function () {
|
||||||
return {
|
return {
|
||||||
"openmct": this.tree,
|
"openmct": this.tree,
|
||||||
"rootId": this.root.getId()
|
"rootId": this.getId(this.root)
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
ExportAsJSONAction.prototype.isCreatable = function (domainObject) {
|
ExportAsJSONAction.prototype.isCreatable = function (domainObject) {
|
||||||
|
var type = this.typeService.getType(domainObject.type);
|
||||||
return this.policyService.allow(
|
return this.policyService.allow(
|
||||||
"creation",
|
"creation",
|
||||||
domainObject.getCapability("type")
|
type
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
ExportAsJSONAction.prototype.getId = function (domainObject) {
|
||||||
|
return this.openmct.objects.makeKeyString(domainObject.identifier);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
ExportAsJSONAction.prototype.rewriteReferences = function () {
|
||||||
|
var treeString = JSON.stringify(this.tree);
|
||||||
|
Object.keys(this.idMap).forEach(function (oldId) {
|
||||||
|
var newId = this.idMap[oldId];
|
||||||
|
treeString = treeString.split(oldId).join(newId);
|
||||||
|
}.bind(this));
|
||||||
|
this.tree = JSON.parse(treeString);
|
||||||
|
};
|
||||||
|
|
||||||
return ExportAsJSONAction;
|
return ExportAsJSONAction;
|
||||||
});
|
});
|
||||||
|
@ -23,9 +23,11 @@
|
|||||||
define(
|
define(
|
||||||
[
|
[
|
||||||
"../../src/actions/ExportAsJSONAction",
|
"../../src/actions/ExportAsJSONAction",
|
||||||
"../../../entanglement/test/DomainObjectFactory"
|
"../../../entanglement/test/DomainObjectFactory",
|
||||||
|
"../../../../src/MCT",
|
||||||
|
'../../../../src/adapter/capabilities/AdapterCapability'
|
||||||
],
|
],
|
||||||
function (ExportAsJSONAction, domainObjectFactory) {
|
function (ExportAsJSONAction, domainObjectFactory, MCT, AdapterCapability) {
|
||||||
|
|
||||||
describe("The export JSON action", function () {
|
describe("The export JSON action", function () {
|
||||||
|
|
||||||
@ -33,29 +35,48 @@ define(
|
|||||||
action,
|
action,
|
||||||
exportService,
|
exportService,
|
||||||
identifierService,
|
identifierService,
|
||||||
|
typeService,
|
||||||
|
openmct,
|
||||||
policyService,
|
policyService,
|
||||||
mockType,
|
mockType,
|
||||||
|
mockObjectProvider,
|
||||||
exportedTree;
|
exportedTree;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
|
openmct = new MCT();
|
||||||
|
mockObjectProvider = {
|
||||||
|
objects: {},
|
||||||
|
get: function (id) {
|
||||||
|
return Promise.resolve(mockObjectProvider.objects[id.key]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
openmct.objects.addProvider('', mockObjectProvider);
|
||||||
|
|
||||||
exportService = jasmine.createSpyObj('exportService',
|
exportService = jasmine.createSpyObj('exportService',
|
||||||
['exportJSON']);
|
['exportJSON']);
|
||||||
identifierService = jasmine.createSpyObj('identifierService',
|
identifierService = jasmine.createSpyObj('identifierService',
|
||||||
['generate']);
|
['generate']);
|
||||||
policyService = jasmine.createSpyObj('policyService',
|
policyService = jasmine.createSpyObj('policyService',
|
||||||
['allow']);
|
['allow']);
|
||||||
mockType =
|
mockType = jasmine.createSpyObj('type', ['hasFeature']);
|
||||||
jasmine.createSpyObj('type', ['hasFeature']);
|
typeService = jasmine.createSpyObj('typeService', [
|
||||||
|
'getType'
|
||||||
|
]);
|
||||||
|
|
||||||
mockType.hasFeature.andCallFake(function (feature) {
|
mockType.hasFeature.andCallFake(function (feature) {
|
||||||
return feature === 'creation';
|
return feature === 'creation';
|
||||||
});
|
});
|
||||||
|
|
||||||
|
typeService.getType.andReturn(mockType);
|
||||||
|
|
||||||
context = {};
|
context = {};
|
||||||
context.domainObject = domainObjectFactory(
|
context.domainObject = domainObjectFactory(
|
||||||
{
|
{
|
||||||
name: 'test',
|
name: 'test',
|
||||||
id: 'someID',
|
id: 'someID',
|
||||||
capabilities: {type: mockType}
|
capabilities: {'adapter': {
|
||||||
|
invoke: invokeAdapter
|
||||||
|
}}
|
||||||
});
|
});
|
||||||
identifierService.generate.andReturn('brandNewId');
|
identifierService.generate.andReturn('brandNewId');
|
||||||
exportService.exportJSON.andCallFake(function (tree, options) {
|
exportService.exportJSON.andCallFake(function (tree, options) {
|
||||||
@ -65,10 +86,15 @@ define(
|
|||||||
return type.hasFeature(capability);
|
return type.hasFeature(capability);
|
||||||
});
|
});
|
||||||
|
|
||||||
action = new ExportAsJSONAction(exportService, policyService,
|
action = new ExportAsJSONAction(openmct, exportService, policyService,
|
||||||
identifierService, context);
|
identifierService, typeService, context);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function invokeAdapter() {
|
||||||
|
var newStyleObject = new AdapterCapability(context.domainObject).invoke();
|
||||||
|
return newStyleObject;
|
||||||
|
}
|
||||||
|
|
||||||
it("initializes happily", function () {
|
it("initializes happily", function () {
|
||||||
expect(action).toBeDefined();
|
expect(action).toBeDefined();
|
||||||
});
|
});
|
||||||
@ -81,32 +107,24 @@ define(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var parentComposition =
|
typeService.getType.andReturn(nonCreatableType);
|
||||||
jasmine.createSpyObj('parentComposition', ['invoke']);
|
|
||||||
|
|
||||||
var parent = domainObjectFactory({
|
var parent = domainObjectFactory({
|
||||||
name: 'parent',
|
name: 'parent',
|
||||||
model: { name: 'parent', location: 'ROOT'},
|
model: { name: 'parent', location: 'ROOT', composition: ['childId']},
|
||||||
id: 'parentId',
|
id: 'parentId',
|
||||||
capabilities: {
|
capabilities: {'adapter': {
|
||||||
composition: parentComposition,
|
invoke: invokeAdapter
|
||||||
type: mockType
|
}}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
var child = domainObjectFactory({
|
var child = {
|
||||||
|
identifier: {namespace: '', key: 'childId'},
|
||||||
name: 'child',
|
name: 'child',
|
||||||
model: { name: 'child', location: 'parentId' },
|
location: 'parentId'
|
||||||
id: 'childId',
|
};
|
||||||
capabilities: {
|
|
||||||
type: nonCreatableType
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
parentComposition.invoke.andReturn(
|
|
||||||
Promise.resolve([child])
|
|
||||||
);
|
|
||||||
context.domainObject = parent;
|
context.domainObject = parent;
|
||||||
|
addChild(child);
|
||||||
|
|
||||||
var init = false;
|
var init = false;
|
||||||
runs(function () {
|
runs(function () {
|
||||||
@ -128,42 +146,25 @@ define(
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("can export self-containing objects", function () {
|
it("can export self-containing objects", function () {
|
||||||
var infiniteParentComposition =
|
|
||||||
jasmine.createSpyObj('infiniteParentComposition',
|
|
||||||
['invoke']
|
|
||||||
);
|
|
||||||
|
|
||||||
var infiniteChildComposition =
|
|
||||||
jasmine.createSpyObj('infiniteChildComposition',
|
|
||||||
['invoke']
|
|
||||||
);
|
|
||||||
|
|
||||||
var parent = domainObjectFactory({
|
var parent = domainObjectFactory({
|
||||||
name: 'parent',
|
name: 'parent',
|
||||||
model: { name: 'parent', location: 'ROOT'},
|
model: { name: 'parent', location: 'ROOT', composition: ['infiniteChildId']},
|
||||||
id: 'infiniteParentId',
|
id: 'infiniteParentId',
|
||||||
capabilities: {
|
capabilities: {
|
||||||
composition: infiniteParentComposition,
|
'adapter': {
|
||||||
type: mockType
|
invoke: invokeAdapter
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var child = domainObjectFactory({
|
var child = {
|
||||||
|
identifier: {namespace: '', key: 'infiniteChildId'},
|
||||||
name: 'child',
|
name: 'child',
|
||||||
model: { name: 'child', location: 'infiniteParentId' },
|
location: 'infiniteParentId',
|
||||||
id: 'infiniteChildId',
|
composition: ['infiniteParentId']
|
||||||
capabilities: {
|
};
|
||||||
composition: infiniteChildComposition,
|
addChild(child);
|
||||||
type: mockType
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
infiniteParentComposition.invoke.andReturn(
|
|
||||||
Promise.resolve([child])
|
|
||||||
);
|
|
||||||
infiniteChildComposition.invoke.andReturn(
|
|
||||||
Promise.resolve([parent])
|
|
||||||
);
|
|
||||||
context.domainObject = parent;
|
context.domainObject = parent;
|
||||||
|
|
||||||
var init = false;
|
var init = false;
|
||||||
@ -188,11 +189,6 @@ define(
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("exports links to external objects as new objects", function () {
|
it("exports links to external objects as new objects", function () {
|
||||||
var externallyLinkedComposition =
|
|
||||||
jasmine.createSpyObj('externallyLinkedComposition',
|
|
||||||
['invoke']
|
|
||||||
);
|
|
||||||
|
|
||||||
var parent = domainObjectFactory({
|
var parent = domainObjectFactory({
|
||||||
name: 'parent',
|
name: 'parent',
|
||||||
model: {
|
model: {
|
||||||
@ -200,24 +196,18 @@ define(
|
|||||||
composition: ['externalId'],
|
composition: ['externalId'],
|
||||||
location: 'ROOT'},
|
location: 'ROOT'},
|
||||||
id: 'parentId',
|
id: 'parentId',
|
||||||
capabilities: {
|
capabilities: {'adapter': {
|
||||||
composition: externallyLinkedComposition,
|
invoke: invokeAdapter
|
||||||
type: mockType
|
}}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
var externalObject = domainObjectFactory({
|
var externalObject = {
|
||||||
name: 'external',
|
name: 'external',
|
||||||
model: { name: 'external', location: 'outsideOfTree'},
|
location: 'outsideOfTree',
|
||||||
id: 'externalId',
|
identifier: {namespace: '', key: 'externalId'}
|
||||||
capabilities: {
|
};
|
||||||
type: mockType
|
addChild(externalObject);
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
externallyLinkedComposition.invoke.andReturn(
|
|
||||||
Promise.resolve([externalObject])
|
|
||||||
);
|
|
||||||
context.domainObject = parent;
|
context.domainObject = parent;
|
||||||
|
|
||||||
var init = false;
|
var init = false;
|
||||||
@ -230,7 +220,7 @@ define(
|
|||||||
|
|
||||||
waitsFor(function () {
|
waitsFor(function () {
|
||||||
return init;
|
return init;
|
||||||
}, "Exported tree sohuld have been built");
|
}, "Exported tree should have been built");
|
||||||
|
|
||||||
runs(function () {
|
runs(function () {
|
||||||
expect(Object.keys(action.tree).length).toBe(2);
|
expect(Object.keys(action.tree).length).toBe(2);
|
||||||
@ -261,6 +251,10 @@ define(
|
|||||||
expect(exportedTree.hasOwnProperty('rootId')).toBeTruthy();
|
expect(exportedTree.hasOwnProperty('rootId')).toBeTruthy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function addChild(object) {
|
||||||
|
mockObjectProvider.objects[object.identifier.key] = object;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -207,6 +207,14 @@ define([
|
|||||||
return mutableObject.stopListening.bind(mutableObject);
|
return mutableObject.stopListening.bind(mutableObject);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {module:openmct.ObjectAPI~Identifier} identifier
|
||||||
|
* @returns {string} A string representation of the given identifier, including namespace and key
|
||||||
|
*/
|
||||||
|
ObjectAPI.prototype.makeKeyString = function (identifier) {
|
||||||
|
return utils.makeKeyString(identifier);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uniquely identifies a domain object.
|
* Uniquely identifies a domain object.
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user