/** * 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 { CORE_DND_TYPE, type ElementDragSource, useEditorStore, type UMLEditorElementDropTarget, } from '@finos/legend-application-studio'; import { observer } from 'mobx-react-lite'; import { DataSpaceEditorState } from '../stores/DataSpaceEditorState.js'; import { ActionAlertActionType, ActionAlertType, } from '@finos/legend-application'; import { BlankPanelPlaceholder, clsx, CheckCircleIcon, EmptyCircleIcon, CustomSelectorInput, Dialog, LongArrowRightIcon, ModalTitle, PanelContent, PanelDivider, PanelDropZone, PanelFormSection, PanelFormTextField, PanelFormValidatedTextField, PanelHeader, PlusIcon, PURE_MappingIcon, PURE_RuntimeIcon, TimesIcon, WarningIcon, } from '@finos/legend-art'; import { DataProduct, LakehouseBaseRuntime, Mapping, ModelAccessPointGroup, PackageableElementExplicitReference, PackageableRuntime, } from '@finos/legend-graph'; import { buildElementOption, type PackageableElementOption, } from '@finos/legend-lego/graph-editor'; import { DataSpaceExecutionContext, DataSpaceMappingProvider, type DataSpaceExecutionContext as DataSpaceExecutionContextType, } from '@finos/legend-extension-dsl-data-space/graph'; import { useCallback, useState } from 'react'; import { useDrop } from 'react-dnd'; import type { DataSpaceExecutionContextState } from '../stores/DataSpaceExecutionContextState.js'; import { dataSpace_setDefaultExecutionContext, dataSpace_setExecutionContextDefaultRuntime, dataSpace_setExecutionContextDescription, dataSpace_setExecutionContextMapping, dataSpace_setExecutionContextMappingProvider, dataSpace_setExecutionContextName, dataSpace_setExecutionContextTitle, dataSpace_setMappingProviderKeys, } from '../stores/studio/DSL_DataSpace_GraphModifierHelper.js'; import { collectExecutionContextValidationIssues, hasExecutionContextValidationError, hasNoMappingSource, InlineIssue, } from './DataSpaceGeneralEditor/DataSpaceValidation.js'; type MappingSourceKind = 'mapping' | 'mappingProvider'; const getDataProductsWithModelAccessPointGroups = ( editorStore: ReturnType, ): DataProduct[] => editorStore.graphManagerState.graph.dataProducts.filter((dataProduct) => dataProduct.accessPointGroups.some( (group) => group instanceof ModelAccessPointGroup, ), ); const getModelAccessPointGroupIds = (dataProduct: DataProduct): string[] => dataProduct.accessPointGroups .filter((group) => group instanceof ModelAccessPointGroup) .map((group) => group.id); const getMappingSourceKind = ( executionContext: DataSpaceExecutionContextType, ): MappingSourceKind | undefined => { if (executionContext.mappingProvider) { return 'mappingProvider'; } if (executionContext.mapping) { return 'mapping'; } return undefined; }; export const NewExecutionContextModal = observer( (props: { executionContextState: DataSpaceExecutionContextState; isReadOnly: boolean; }) => { const { executionContextState, isReadOnly } = props; const existingNames = executionContextState.executionContexts.map( (context) => context.name, ); const [name, setName] = useState(''); const [isNameValid, setIsNameValid] = useState(false); const validateName = (val: string): string | undefined => { if (!val) { return `Execution context name can't be empty`; } if (existingNames.includes(val)) { return `Execution context '${val}' already exists`; } return undefined; }; const closeModal = (): void => executionContextState.setNewExecutionContextModal(false); const create = (): void => { if (isReadOnly || !isNameValid) { return; } const context = new DataSpaceExecutionContext(); context.name = name; executionContextState.addExecutionContext(context); closeModal(); }; return (
{ event.preventDefault(); create(); }} className="modal search-modal modal--dark" > setName(value ?? '')} validate={validateName} onValidate={(issue): void => setIsNameValid(!issue)} isReadOnly={isReadOnly} placeholder="Enter a unique name" fullWidth={true} />
); }, ); const MappingSourceEditor = observer( (props: { executionContextState: DataSpaceExecutionContextState; executionContext: DataSpaceExecutionContextType; isReadOnly: boolean; sourceKind: MappingSourceKind; setSourceKind: (val: MappingSourceKind) => void; }) => { const { executionContextState, executionContext, isReadOnly, sourceKind, setSourceKind, } = props; const editorStore = executionContextState.editorStore; const applicationStore = editorStore.applicationStore; const darkMode = !applicationStore.layoutService.TEMPORARY__isLightColorThemeEnabled; const mapping = executionContext.mapping?.value; const mappingOptions = editorStore.graphManagerState.usableMappings.map(buildElementOption); const onMappingChange = ( option: PackageableElementOption | null, ): void => dataSpace_setExecutionContextMapping( executionContext, option ? PackageableElementExplicitReference.create(option.value) : undefined, ); const mappingProvider = executionContext.mappingProvider; const providerElement = mappingProvider?.element.value; const providerDataProduct = providerElement instanceof DataProduct ? providerElement : undefined; const dataProductOptions = getDataProductsWithModelAccessPointGroups(editorStore).map( buildElementOption, ); const onDataProductChange = ( option: PackageableElementOption | null, ): void => { if (!option) { dataSpace_setExecutionContextMappingProvider( executionContext, undefined, ); return; } const provider = new DataSpaceMappingProvider(); provider.element = PackageableElementExplicitReference.create( option.value, ); const groupIds = getModelAccessPointGroupIds(option.value); provider.keys = groupIds.length === 1 && groupIds[0] ? [groupIds[0]] : []; dataSpace_setExecutionContextMappingProvider(executionContext, provider); }; // The two sources are exclusive, so choosing one drops the other; ask first // if that throws away something the user set. const selectSourceKind = (kind: MappingSourceKind): void => { const otherSourcePath = kind === 'mapping' ? mappingProvider?.element.value.path : executionContext.mapping?.value.path; if (otherSourcePath === undefined) { setSourceKind(kind); return; } const kindLabel = kind === 'mapping' ? 'mapping' : 'mapping provider'; const otherSourceLabel = kind === 'mapping' ? 'mapping provider' : 'mapping'; applicationStore.alertService.setActionAlertInfo({ // both are set (e.g. from grammar) when the clicked kind is already shown message: kind === sourceKind ? `Keep only the ${kindLabel}?` : `Switch to a ${kindLabel}?`, prompt: `An execution context takes either a mapping or a mapping provider. This removes ${otherSourceLabel} '${otherSourcePath}' from '${executionContext.name}'.`, type: ActionAlertType.CAUTION, actions: [ { label: `Remove ${otherSourceLabel}`, type: ActionAlertActionType.PROCEED_WITH_CAUTION, handler: (): void => { if (kind === 'mapping') { dataSpace_setExecutionContextMappingProvider( executionContext, undefined, ); } else { dataSpace_setExecutionContextMapping( executionContext, undefined, ); } setSourceKind(kind); }, }, { label: 'Cancel', type: ActionAlertActionType.PROCEED, default: true, }, ], }); }; const accessPointGroupIds = providerDataProduct ? getModelAccessPointGroupIds(providerDataProduct) : []; const accessPointGroupOptions = accessPointGroupIds.map((id) => ({ label: id, value: id, })); const accessPointGroupId = mappingProvider?.keys[0]; const onAccessPointGroupChange = ( option: { value: string } | null, ): void => { if (mappingProvider) { dataSpace_setMappingProviderKeys( mappingProvider, option ? [option.value] : [], ); } }; const handleMappingDrop = useCallback( (item: UMLEditorElementDropTarget): void => { const element = item.data.packageableElement; if (!isReadOnly && element instanceof Mapping) { dataSpace_setExecutionContextMapping( executionContext, PackageableElementExplicitReference.create(element), ); setSourceKind('mapping'); } }, [isReadOnly, executionContext, setSourceKind], ); const [{ isMappingDragOver }, mappingDropConnector] = useDrop< ElementDragSource, void, { isMappingDragOver: boolean } >( () => ({ accept: [CORE_DND_TYPE.PROJECT_EXPLORER_MAPPING], drop: (item) => handleMappingDrop(item), collect: (monitor) => ({ isMappingDragOver: monitor.isOver({ shallow: true }), }), }), [handleMappingDrop], ); const providerElementIssue = providerElement && !providerDataProduct ? `'${providerElement.path}' is not a data product.` : undefined; const providerKeyIssue = providerDataProduct && accessPointGroupId && !accessPointGroupIds.includes(accessPointGroupId) ? `Access point group '${accessPointGroupId}' is not defined on '${providerDataProduct.path}'.` : undefined; return (
Mapping Source
Where this context gets its model from. Choose a mapping directly, or indirectly via a mapping provider.
{hasNoMappingSource(executionContext) && ( )}
{sourceKind === 'mapping' && (
)} {sourceKind === 'mappingProvider' && (
{providerDataProduct && (
Model Access Point Group
)} {providerElementIssue && ( )} {providerKeyIssue && ( )}
)}
); }, ); const DefaultRuntimeEditor = observer( (props: { executionContextState: DataSpaceExecutionContextState; executionContext: DataSpaceExecutionContextType; isReadOnly: boolean; }) => { const { executionContextState, executionContext, isReadOnly } = props; const editorStore = executionContextState.editorStore; const applicationStore = editorStore.applicationStore; const darkMode = !applicationStore.layoutService.TEMPORARY__isLightColorThemeEnabled; const mapping = executionContext.mapping?.value; const defaultRuntime = executionContext.defaultRuntime?.value; const compatibleRuntimes = editorStore.graphManagerState.graph.runtimes.filter( (runtime) => !mapping || runtime.runtimeValue.mappings.map((m) => m.value).includes(mapping), ); const runtimeOptions = ( compatibleRuntimes.length ? compatibleRuntimes : editorStore.graphManagerState.graph.runtimes ).map(buildElementOption); const runtimeWarning = defaultRuntime && mapping && !(defaultRuntime.runtimeValue instanceof LakehouseBaseRuntime) && !compatibleRuntimes.includes(defaultRuntime) ? `Runtime is not associated with mapping '${mapping.path}'.` : undefined; const onRuntimeChange = ( option: PackageableElementOption | null, ): void => dataSpace_setExecutionContextDefaultRuntime( executionContext, option ? PackageableElementExplicitReference.create(option.value) : undefined, ); const handleRuntimeDrop = useCallback( (item: UMLEditorElementDropTarget): void => { const element = item.data.packageableElement; if (!isReadOnly && element instanceof PackageableRuntime) { dataSpace_setExecutionContextDefaultRuntime( executionContext, PackageableElementExplicitReference.create(element), ); } }, [isReadOnly, executionContext], ); const [{ isRuntimeDragOver }, runtimeDropConnector] = useDrop< ElementDragSource, void, { isRuntimeDragOver: boolean } >( () => ({ accept: [CORE_DND_TYPE.PROJECT_EXPLORER_RUNTIME], drop: (item) => handleRuntimeDrop(item), collect: (monitor) => ({ isRuntimeDragOver: monitor.isOver({ shallow: true }), }), }), [handleRuntimeDrop], ); return (
Default Runtime
{runtimeWarning && ( )}
); }, ); const ExecutionContextConfigurationEditor = observer( (props: { executionContextState: DataSpaceExecutionContextState; executionContext: DataSpaceExecutionContextType; isReadOnly: boolean; }) => { const { executionContextState, executionContext, isReadOnly } = props; const dataSpace = executionContextState.dataSpace; const [sourceKindOverride, setSourceKindOverride] = useState< MappingSourceKind | undefined >(undefined); const [trackedContext, setTrackedContext] = useState(executionContext); if (trackedContext !== executionContext) { setTrackedContext(executionContext); setSourceKindOverride(undefined); } const sourceKind = getMappingSourceKind(executionContext) ?? sourceKindOverride ?? 'mapping'; const otherNames = executionContextState.executionContexts .filter((context) => context !== executionContext) .map((context) => context.name); const validateName = (val: string): string | undefined => { if (!val) { return `Execution context name can't be empty`; } if (otherNames.includes(val)) { return `Execution context '${val}' already exists`; } return undefined; }; const isDefault = dataSpace.defaultExecutionContext === executionContext; const toggleDefault = (): void => dataSpace_setDefaultExecutionContext( dataSpace, isDefault ? undefined : executionContext, ); const issues = collectExecutionContextValidationIssues(executionContext); const nameError = validateName(executionContext.name); return (
General
How this execution context is named and described.
dataSpace_setExecutionContextName(executionContext, value ?? '') } isReadOnly={isReadOnly} placeholder="Enter a unique name" fullWidth={true} hasError={Boolean(nameError)} errorMessage={nameError} errorClassName="dataSpace-editor__field--error" /> dataSpace_setExecutionContextTitle(executionContext, value) } isReadOnly={isReadOnly} placeholder="Enter title" fullWidth={true} />