[Import JSON] Support namespaces (#2483)

* Retain target namespace in generated identifiers

* Also replace identifier objects

* Do not duplicate newIds when replacing. Rename ID to Id
This commit is contained in:
Andrew Henry 2019-09-13 13:45:32 -07:00 committed by Pegah Sarram
parent e98d0cc7c5
commit 7c32700b69
2 changed files with 30 additions and 11 deletions

View File

@ -19,8 +19,7 @@
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
define(['zepto'], function ($) {
define(['zepto', '../../../../src/api/objects/object-utils.js'], function ($, objectUtils) {
/**
* The ImportAsJSONAction is available from context menus and allows a user
@ -61,7 +60,9 @@ define(['zepto'], function ($) {
ImportAsJSONAction.prototype.importObjectTree = function (objTree) {
var parent = this.context.domainObject;
var tree = this.generateNewIdentifiers(objTree);
var namespace = parent.useCapability('adapter').identifier.namespace;
var tree = this.generateNewIdentifiers(objTree, namespace);
var rootId = tree.rootId;
var rootObj = this.instantiate(tree.openmct[rootId], rootId);
var newStyleParent = parent.useCapability('adapter');
@ -105,7 +106,6 @@ define(['zepto'], function ($) {
if (!tree[keystring] || seen.includes(keystring)) {
return;
}
newObj = this.instantiate(tree[keystring], keystring);
newObj.getCapability("location")
.setPrimaryLocation(tree[keystring].location);
@ -114,11 +114,17 @@ define(['zepto'], function ($) {
}
};
ImportAsJSONAction.prototype.generateNewIdentifiers = function (tree) {
ImportAsJSONAction.prototype.generateNewIdentifiers = function (tree, namespace) {
// For each domain object in the file, generate new ID, replace in tree
Object.keys(tree.openmct).forEach(function (domainObjectId) {
var newId = this.identifierService.generate();
tree = this.rewriteId(domainObjectId, newId, tree);
let newId = {
namespace: namespace,
key: this.identifierService.generate()
};
let oldId = objectUtils.parseKeyString(domainObjectId);
tree = this.rewriteId(oldId, newId, tree);
}, this);
return tree;
};
@ -129,9 +135,21 @@ define(['zepto'], function ($) {
*
* @private
*/
ImportAsJSONAction.prototype.rewriteId = function (oldID, newID, tree) {
tree = JSON.stringify(tree).replace(new RegExp(oldID, 'g'), newID);
return JSON.parse(tree);
ImportAsJSONAction.prototype.rewriteId = function (oldId, newId, tree) {
let newIdKeyString = this.openmct.objects.makeKeyString(newId);
let oldIdKeyString = this.openmct.objects.makeKeyString(oldId);
tree = JSON.stringify(tree).replace(new RegExp(oldIdKeyString, 'g'), newIdKeyString);
return JSON.parse(tree, (key, value) => {
if (Object.prototype.hasOwnProperty.call(value, 'key') &&
Object.prototype.hasOwnProperty.call(value, 'namespace') &&
value.key === oldId.key &&
value.namespace === oldId.namespace) {
return newId
} else {
return value;
}
});
};
ImportAsJSONAction.prototype.getFormModel = function () {

View File

@ -73,7 +73,8 @@ define([
) {
var bundleMap = {
LocalStorage: 'platform/persistence/local',
MyItems: 'platform/features/my-items'
MyItems: 'platform/features/my-items',
CouchDB: 'platform/persistence/couch'
};
var plugins = _.mapValues(bundleMap, function (bundleName, pluginName) {