@finos/legend-studio
Version:
277 lines • 12 kB
JavaScript
/**
* 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 { computed, action, makeObservable, observable, flow, flowResult, } from 'mobx';
import { readFileAsText, assertErrorThrown, LogEvent, addUniqueEntry, deepEqual, guaranteeType, isEmpty, ActionState, deleteEntry, } from '@finos/legend-shared';
import { CompilationError, ConfigurationProperty, GenerationPropertyItemType, ExternalFormatSchema as Schema, SchemaSet, } from '@finos/legend-graph';
import { EntityChangeType } from '@finos/legend-server-sdlc';
import { ElementEditorState } from '../ElementEditorState.js';
import { LEGEND_STUDIO_APP_EVENT } from '../../../LegendStudioAppEvent.js';
import { configurationProperty_setValue } from '../../../graphModifier/DSLGeneration_GraphModifierHelper.js';
export var SCHEMA_SET_TAB_TYPE;
(function (SCHEMA_SET_TAB_TYPE) {
SCHEMA_SET_TAB_TYPE["GENERAL"] = "GENERAL";
SCHEMA_SET_TAB_TYPE["MODEL_GENERATION"] = "MODEL_GENERATION";
})(SCHEMA_SET_TAB_TYPE = SCHEMA_SET_TAB_TYPE || (SCHEMA_SET_TAB_TYPE = {}));
export class SchemaSetModelGenerationState {
isGenerating = false;
isImportingGeneratedElements = false;
schemaSetEditorState;
configurationProperties = [];
generationValue = '';
constructor(schemaSetEditorState) {
this.schemaSetEditorState = schemaSetEditorState;
makeObservable(this, {
configurationProperties: observable,
isGenerating: observable,
generationValue: observable,
setConfigurationProperty: action,
setGenerationValue: action,
generateModel: flow,
importGrammar: flow,
});
}
setConfigurationProperty(val) {
this.configurationProperties = val;
}
get description() {
return this.schemaSetEditorState.editorStore.graphState.graphGenerationState.externalFormatState.getTypeDescription(this.schemaSetEditorState.schemaSet.format);
}
get modelGenerationProperties() {
return this.description.modelGenerationProperties;
}
getConfigValue(name) {
return this.getConfig(name)?.value;
}
getConfig(name) {
return this.configurationProperties.find((property) => name === property.name);
}
setGenerationValue(val) {
this.generationValue = val;
}
updateGenerationParameters(generationProperty, newValue) {
if (generationProperty.type === GenerationPropertyItemType.MAP) {
if (!newValue ||
isEmpty(newValue) ||
deepEqual(newValue, generationProperty.defaultValue)) {
this.configurationProperties = this.configurationProperties.filter((e) => e.name !== generationProperty.name);
}
else {
const configProperty = this.getConfig(generationProperty.name);
if (configProperty) {
configurationProperty_setValue(configProperty, {
...newValue,
});
}
else {
const newItem = new ConfigurationProperty(generationProperty.name, newValue);
addUniqueEntry(this.configurationProperties, newItem);
}
}
}
else {
const configProperty = this.getConfig(generationProperty.name);
let useDefaultValue = generationProperty.defaultValue === newValue;
if (generationProperty.type === GenerationPropertyItemType.BOOLEAN) {
useDefaultValue =
(generationProperty.defaultValue === 'true') ===
newValue;
}
const newConfigValue = useDefaultValue ? undefined : newValue;
if (newConfigValue !== undefined) {
if (configProperty) {
configurationProperty_setValue(configProperty, newConfigValue);
}
else {
const newItem = new ConfigurationProperty(generationProperty.name, newConfigValue);
addUniqueEntry(this.configurationProperties, newItem);
}
}
else {
this.configurationProperties = this.configurationProperties.filter((e) => e.name !== generationProperty.name);
}
}
}
*generateModel() {
this.isGenerating = true;
try {
const SCHEMA_SET_PROPERTY_NAME = 'sourceSchemaSet';
const SCHEMA_FORMAT_PROPERTY_NAME = 'format';
const properties = [...this.configurationProperties];
if (!properties.find((e) => e.name === SCHEMA_SET_PROPERTY_NAME)) {
const genProperty = new ConfigurationProperty(SCHEMA_SET_PROPERTY_NAME, this.schemaSetEditorState.schemaSet.path);
properties.push(genProperty);
}
if (!properties.find((e) => e.name === SCHEMA_FORMAT_PROPERTY_NAME)) {
const genProperty = new ConfigurationProperty(SCHEMA_FORMAT_PROPERTY_NAME, this.schemaSetEditorState.schemaSet.format);
properties.push(genProperty);
}
const val = (yield this.schemaSetEditorState.editorStore.graphManagerState.graphManager.generateModelFromExternalFormat(properties, this.schemaSetEditorState.editorStore.graphManagerState.graph));
this.setGenerationValue(val);
}
catch (error) {
assertErrorThrown(error);
this.schemaSetEditorState.editorStore.applicationStore.log.error(LogEvent.create(LEGEND_STUDIO_APP_EVENT.EXTERNAL_FORMAT_FAILURE), error);
this.schemaSetEditorState.editorStore.applicationStore.notifyError(error);
}
finally {
this.isGenerating = false;
}
}
*importGrammar() {
try {
this.isImportingGeneratedElements = true;
const entities = (yield this.schemaSetEditorState.editorStore.graphManagerState.graphManager.pureCodeToEntities(this.generationValue));
const newEntities = entities.map((e) => ({
type: EntityChangeType.CREATE,
entityPath: e.path,
content: e.content,
}));
yield flowResult(this.schemaSetEditorState.editorStore.graphState.loadEntityChangesToGraph(newEntities, undefined));
this.schemaSetEditorState.editorStore.applicationStore.notifySuccess('Generated elements imported into project');
}
catch (error) {
assertErrorThrown(error);
this.schemaSetEditorState.editorStore.applicationStore.log.error(LogEvent.create(LEGEND_STUDIO_APP_EVENT.EXTERNAL_FORMAT_FAILURE), error);
this.schemaSetEditorState.editorStore.applicationStore.notifyError(error);
}
finally {
this.isImportingGeneratedElements = false;
}
}
}
export class ImportSchemaContentState {
editorStore;
schemaSetEditorState;
loadingSchemaContentState = ActionState.create();
files;
importSchemaModal = false;
constructor(schemaSetEditorState, editorStore) {
this.editorStore = editorStore;
this.schemaSetEditorState = schemaSetEditorState;
makeObservable(this, {
schemaSetEditorState: false,
editorStore: false,
files: observable,
importSchemaModal: observable,
loadingSchemaContentState: observable,
removeFile: action,
closeModal: action,
importSchemas: flow,
});
}
setImportSchemaModal(val) {
this.importSchemaModal = val;
}
setFiles(files) {
this.files = files;
}
removeFile(file) {
if (this.files) {
deleteEntry(this.files, file);
}
}
closeModal() {
this.files = undefined;
this.setImportSchemaModal(false);
}
*importSchemas(files) {
try {
this.loadingSchemaContentState.inProgress();
const schemaSet = this.schemaSetEditorState.schemaSet;
yield Promise.all(files.map((file) => readFileAsText(file).then((content) => {
const _schema = new Schema();
_schema.location = file.name;
_schema.content = content;
// https://stackoverflow.com/questions/4250364
_schema.id = file.name.replace(/\.[^/.]+$/, '');
schemaSet.schemas.push(_schema);
})));
if (schemaSet.schemas.length) {
this.schemaSetEditorState.setCurrentSchema(schemaSet.schemas[0]);
}
this.loadingSchemaContentState.complete();
this.closeModal();
}
catch (error) {
assertErrorThrown(error);
this.editorStore.applicationStore.notifyError(`Can't load patch: Error: ${error.message}`);
this.loadingSchemaContentState.fail();
}
}
}
export class SchemaSetEditorState extends ElementEditorState {
currentSchema;
selectedTab = SCHEMA_SET_TAB_TYPE.GENERAL;
schemaValidationError;
importSchemaContentState;
schemaSetModelGenerationState;
constructor(editorStore, element) {
super(editorStore, element);
makeObservable(this, {
schemaValidationError: observable,
currentSchema: observable,
selectedTab: observable,
schemaSet: computed,
setSelectedTab: action,
setCurrentSchema: action,
reprocess: action,
setSchemaValidationerror: action,
validateSchema: flow,
});
if (this.schemaSet.schemas.length !== 0) {
this.currentSchema =
this.schemaSet.schemas[this.schemaSet.schemas.length - 1];
}
this.schemaSetModelGenerationState = new SchemaSetModelGenerationState(this);
this.importSchemaContentState = new ImportSchemaContentState(this, this.editorStore);
}
get schemaSet() {
return guaranteeType(this.element, SchemaSet, 'Element inside schema set element editor state must be a SchemaSet');
}
setCurrentSchema(value) {
this.currentSchema = value;
}
setSelectedTab(tab) {
this.selectedTab = tab;
}
setSchemaValidationerror(error) {
this.schemaValidationError = error;
}
reprocess(newElement, editorStore) {
const schemaSetEditorState = new SchemaSetEditorState(editorStore, newElement);
return schemaSetEditorState;
}
*validateSchema(currentSchema) {
try {
const _schemaSet = new SchemaSet(this.schemaSet.name);
_schemaSet.format = this.schemaSet.format;
_schemaSet.schemas = [currentSchema];
const newGraph = this.editorStore.graphManagerState.createEmptyGraph();
newGraph.addElement(_schemaSet, this.schemaSet.package?.path);
yield this.editorStore.graphManagerState.graphManager.compileGraph(newGraph, {
keepSourceInformation: true,
});
}
catch (error) {
assertErrorThrown(error);
this.editorStore.applicationStore.log.error(LogEvent.create(LEGEND_STUDIO_APP_EVENT.EXTERNAL_FORMAT_FAILURE), error);
if (error instanceof CompilationError) {
this.setSchemaValidationerror(error);
}
}
}
}
//# sourceMappingURL=SchemaSetEditorState.js.map