Skip to content

Commit

Permalink
enabled merge/replace also for JSON import
Browse files Browse the repository at this point in the history
  • Loading branch information
r0light committed Apr 9, 2024
1 parent 6099a1a commit a560505
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
41 changes: 37 additions & 4 deletions src/modeling/ModelingApp.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
:appSettings="modelingData ? modelingData.appSettings : getDefaultAppSettings()"
@update:systemName="setCurrentSystemName" @update:appSettings="setCurrentAppSettings"
@click:exit-request-trace-view="resetRequestTraceSelection" @click:print-active-paper="onPrintRequested"
@click:exportSvg="onSvgExportRequested" @click:validate="triggerValidation" @load:fromJson="loadFromJson"
@click:exportSvg="onSvgExportRequested" @click:validate="triggerValidation" @load:fromJson="requestLoadFromJson"
@save:toJson="saveToJson" @load:fromTosca="requestLoadFromTosca" @save:toTosca="saveToTosca"></Toolbar>
<div class="app-body">
<div :id="`entity-sidebar-${pageId}`" class="entityShapes-sidebar-container d-print-none">
Expand Down Expand Up @@ -121,7 +121,7 @@ onMounted(() => {
if (props.modelingData.toImport.fileName) {
if (props.modelingData.toImport.fileName.endsWith("json")) {
loaded = loadFromJson(props.modelingData.toImport.fileContent, props.modelingData.toImport.fileName);
loaded = loadFromJson(props.modelingData.toImport.fileContent, props.modelingData.toImport.fileName, "replace");
} else if (props.modelingData.toImport.fileName.endsWith("yaml")
|| props.modelingData.toImport.fileName.endsWith("yml")
|| props.modelingData.toImport.fileName.endsWith("tosca")) {
Expand Down Expand Up @@ -160,10 +160,43 @@ function resetAllHighlighting() {
resetRequestTraceSelection();
}
function loadFromJson(jsonString: string, fileName: string): Promise<void> {
function requestLoadFromJson(jsonString: string, fileName: string) {
confirmationModalManager.value = {
show: true,
dialogMetaData: {
dialogSize: DialogSize.DEFAULT,
header: {
iconClass: "fa-solid fa-question",
svgRepresentation: "",
text: "Replace or merge?"
},
footer: {
showCancelButton: true,
cancelButtonText: "Cancel",
actionButtons: [{ buttonIconClass: "", buttonText: "Merge"}, { buttonIconClass: "", buttonText: "Replace"}]
},
},
confirmationPrompt: "Do you want to replace the current model with the imported model or merge the imported model into the current model?",
onCancel: () => confirmationModalManager.value.show = false,
actions: [
function decideToMerge() {
confirmationModalManager.value.show = false;
systemEntityManager.getSystemEntity();
loadFromJson(jsonString, fileName, "merge");
},
function decideToReplace() {
confirmationModalManager.value.show = false;
loadFromJson(jsonString, fileName, "replace");
},
]
}
}
function loadFromJson(jsonString: string, fileName: string, strategy: "replace" | "merge"): Promise<void> {
resetAllHighlighting();
let loadResult = systemEntityManager.loadFromJson(jsonString, fileName);
let loadResult = systemEntityManager.loadFromJson(jsonString, fileName, strategy);
if (loadResult.error) {
showError("Import from JSON failed", loadResult.error);
Expand Down
26 changes: 18 additions & 8 deletions src/modeling/systemEntityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
Component as ComponentElement, Service as ServiceElement, BackingService as BackingServiceElement, StorageBackingService as StorageBackingServiceElement,
Endpoint as EndpointElement, ExternalEndpoint as ExternalEndpointElement, Link as LinkElement,
Infrastructure as InfrastructureElement, DeploymentMapping as DeploymentMappingElement,
RequestTrace as RequestTraceElement, DataAggregate as DataAggregateElement, BackingData as BackingDataElement
RequestTrace as RequestTraceElement, DataAggregate as DataAggregateElement, BackingData as BackingDataElement,
entityShapes
} from './config/entityShapes'
import { DataAggregate } from "../core/entities";
import { FormContentConfig } from "./config/actionDialogConfig";
Expand Down Expand Up @@ -171,19 +172,28 @@ class SystemEntityManager {
return jsonSerializedGraph;
}

loadFromJson(stringifiedJson: string, fileName: string): { createdCells: dia.Cell[], error: string } {

loadFromJson(stringifiedJson: string, fileName: string, strategy: "replace" | "merge"): { createdCells: dia.Cell[], error: string } {
let jsonGraph = {};
try {
let jsonGraph: any = JSON.parse(stringifiedJson);
this.#currentSystemGraph.clear();
this.#currentSystemGraph.fromJSON(jsonGraph);
this.#currentSystemGraph.trigger("reloaded");
jsonGraph = JSON.parse(stringifiedJson);
} catch (e) {
return { createdCells: [], error: e.toString() }
}

this.#currentSystemEntity.setSystemName = fileName.replace(/\..*$/g, "");
if (strategy === "replace") {
this.#currentSystemGraph.clear();
this.#currentSystemGraph.fromJSON(jsonGraph);
} else if (strategy === "merge") {
let newGraph: dia.Graph = new dia.Graph({}, { cellNamespace: entityShapes });
newGraph.fromJSON(jsonGraph);
newGraph.getCells().forEach(cell => {
this.#currentSystemGraph.addCell(cell);
});
}

this.#currentSystemGraph.trigger("reloaded");

this.#currentSystemEntity.setSystemName = fileName.replace(/\..*$/g, "");
return { createdCells: this.#currentSystemGraph.getCells(), error: null }
}

Expand Down

0 comments on commit a560505

Please sign in to comment.