UNPKG

@finos/legend-studio

Version:
199 lines 9.39 kB
/** * Copyright (c) 2020-present, Goldman Sachs * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { observable, action, flow, makeObservable } from 'mobx'; import { EditorState } from './EditorState.js'; import { assertErrorThrown, LogEvent, UnsupportedOperationError, isNonNullable, } from '@finos/legend-shared'; import { LEGEND_STUDIO_APP_EVENT } from '../LegendStudioAppEvent.js'; import { ImportMode, } from '@finos/legend-graph'; import { TAB_SIZE } from '@finos/legend-application'; export var MODEL_UPDATER_INPUT_TYPE; (function (MODEL_UPDATER_INPUT_TYPE) { MODEL_UPDATER_INPUT_TYPE["ENTITIES"] = "ENTITIES"; MODEL_UPDATER_INPUT_TYPE["PURE_PROTOCOL"] = "PURE_PROTOCOL"; MODEL_UPDATER_INPUT_TYPE["PURE_GRAMMAR"] = "PURE_GRAMMAR"; })(MODEL_UPDATER_INPUT_TYPE = MODEL_UPDATER_INPUT_TYPE || (MODEL_UPDATER_INPUT_TYPE = {})); export class ModelLoaderState extends EditorState { modelText = this.getExampleEntitiesInputText(); currentModelLoadType = MODEL_UPDATER_INPUT_TYPE.ENTITIES; // TODO: remove model import in favor of external formats modelImportDescriptions = []; modelLoaderExtensionConfigurations = []; replace = true; isLoadingModel = false; constructor(editorStore) { super(editorStore); makeObservable(this, { modelText: observable, currentModelLoadType: observable, modelImportDescriptions: observable, modelLoaderExtensionConfigurations: observable, replace: observable, isLoadingModel: observable, setReplaceFlag: action, setModelText: action, loadCurrentProjectEntities: flow, loadModel: flow, fetchAvailableModelImportDescriptions: flow, }); //extensions this.modelLoaderExtensionConfigurations = this.editorStore.pluginManager .getApplicationPlugins() .flatMap((plugin) => plugin.getExtraModelLoaderExtensionConfigurations?.() ?? []) .filter(isNonNullable); } get headerName() { return 'Model Loader'; } getImportConfigurationDescription(key) { return this.modelImportDescriptions.find((i) => i.key === key); } getLoaderExtensionConfiguration(key) { return this.modelLoaderExtensionConfigurations.find((i) => i.modelGenerationConfig.key === key); } setReplaceFlag(val) { this.replace = val; } setModelText(modelText) { this.modelText = modelText; } setCurrentModelLoadType(modelLoadType) { if (modelLoadType !== this.currentModelLoadType) { this.currentModelLoadType = modelLoadType; this.modelText = this.getExampleText(modelLoadType); } } getExampleText(inputType) { if (inputType === MODEL_UPDATER_INPUT_TYPE.PURE_PROTOCOL) { return this.getExamplePureProtocolInputText(); } else if (inputType === MODEL_UPDATER_INPUT_TYPE.ENTITIES) { return this.getExampleEntitiesInputText(); } else if (this.getImportConfigurationDescription(inputType)) { return this.getExampleExternalFormatInputText(); } return ''; } /** * Current project entities will be taken from the current graph * If graph is not parsable, we will fall back to model loader */ *loadCurrentProjectEntities() { switch (this.currentModelLoadType) { case MODEL_UPDATER_INPUT_TYPE.PURE_PROTOCOL: { const graphEntities = this.editorStore.graphManagerState.graphBuildState .hasSucceeded ? this.editorStore.graphManagerState.graph.allOwnElements.map((element) => this.editorStore.graphManagerState.graphManager.elementToEntity(element)) : this.editorStore.changeDetectionState .workspaceLocalLatestRevisionState.entities; this.modelText = (yield this.editorStore.graphManagerState.graphManager.entitiesToPureProtocolText(graphEntities)); break; } case MODEL_UPDATER_INPUT_TYPE.ENTITIES: { const graphEntities = this.editorStore.graphManagerState.graphBuildState .hasSucceeded ? this.editorStore.graphManagerState.graph.allOwnElements.map((element) => this.editorStore.graphManagerState.graphManager.elementToEntity(element)) : this.editorStore.changeDetectionState .workspaceLocalLatestRevisionState.entities; this.modelText = JSON.stringify(graphEntities, undefined, TAB_SIZE); break; } case MODEL_UPDATER_INPUT_TYPE.PURE_GRAMMAR: { this.modelText = (yield this.editorStore.graphManagerState.graphManager.graphToPureCode(this.editorStore.graphManagerState.graph)); break; } default: throw new UnsupportedOperationError(`Can't load current project entities for input type of type '${this.currentModelLoadType}'`); } } *loadModel() { try { this.isLoadingModel = true; this.editorStore.setBlockingAlert({ message: 'Loading model...', prompt: 'Please do not close the application', showLoading: true, }); let entities; const externalConfigType = this.getImportConfigurationDescription(this.currentModelLoadType); if (externalConfigType) { entities = (yield this.editorStore.graphManagerState.graphManager.externalFormatTextToEntities(this.modelText, externalConfigType.key, ImportMode.SCHEMA_IMPORT)); } else { switch (this.currentModelLoadType) { case MODEL_UPDATER_INPUT_TYPE.PURE_PROTOCOL: { entities = this.editorStore.graphManagerState.graphManager.pureProtocolTextToEntities(this.modelText); break; } case MODEL_UPDATER_INPUT_TYPE.ENTITIES: { entities = JSON.parse(this.modelText); break; } case MODEL_UPDATER_INPUT_TYPE.PURE_GRAMMAR: { entities = (yield this.editorStore.graphManagerState.graphManager.pureCodeToEntities(this.modelText)); break; } default: throw new UnsupportedOperationError(`Can't load model for input of type '${this.currentModelLoadType}'`); } } const message = `loading entities from ${this.editorStore.applicationStore.config.appName} [${this.replace ? `potentially affected ` : ''} ${entities.length} entities]`; yield this.editorStore.sdlcServerClient.updateEntities(this.editorStore.sdlcState.activeProject.projectId, this.editorStore.sdlcState.activeWorkspace, { replace: this.replace, entities, message }); this.editorStore.applicationStore.navigator.reload(); } catch (error) { assertErrorThrown(error); this.editorStore.applicationStore.log.error(LogEvent.create(LEGEND_STUDIO_APP_EVENT.MODEL_LOADER_FAILURE), error); this.editorStore.applicationStore.notifyError(error); } finally { this.isLoadingModel = false; this.editorStore.setBlockingAlert(undefined); } } *fetchAvailableModelImportDescriptions() { try { this.modelImportDescriptions = (yield this.editorStore.graphManagerState.graphManager.getAvailableImportConfigurationDescriptions()); } catch (error) { assertErrorThrown(error); this.editorStore.applicationStore.log.error(LogEvent.create(LEGEND_STUDIO_APP_EVENT.MODEL_LOADER_FAILURE), error); this.editorStore.applicationStore.notifyError(error); } } getExampleEntitiesInputText() { return `// example entity\n${JSON.stringify([ { classifierPath: 'string', content: {}, path: 'string', }, ], undefined, TAB_SIZE)}`; } getExamplePureProtocolInputText() { return `// example Pure model context data\n${this.editorStore.graphManagerState.graphManager.getExamplePureProtocolText()}`; } getExampleExternalFormatInputText() { return `// example external format import data\n${this.editorStore.graphManagerState.graphManager.getExampleExternalFormatImportText()}`; } } //# sourceMappingURL=ModelLoaderState.js.map