Adarshvk98 commented on code in PR #3031: URL: https://github.com/apache/incubator-kie-tools/pull/3031#discussion_r2030436404
########## packages/dmn-editor/src/dataTypes/useImportJavaClasses.ts: ########## @@ -0,0 +1,282 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 { useCallback, useMemo, useState } from "react"; +import { JavaClass, JavaCodeCompletionService } from "@kie-tools/import-java-classes-component"; +import { useExternalModels } from "../includedModels/DmnEditorDependenciesContext"; +import { useDmnEditorStore, useDmnEditorStoreApi } from "../store/StoreContext"; +import { findDataTypeById, getNewItemDefinition } from "./DataTypeSpec"; +import { EditItemDefinition } from "./DataTypes"; + +const NAME_SEPARATOR: string = "-"; + +export enum JavaClassConflictOptions { + REPLACE = "Replace", + KEEP_BOTH = "Keep Both", +} + +const useImportJavaClasses = () => { + const [isConflictsOccured, setIsConflictsOccured] = useState<boolean>(false); + const [conflictsClasses, setConflictsClasses] = useState<JavaClass[]>([]); + + const dmnEditorStoreApi = useDmnEditorStoreApi(); + const { externalModelsByNamespace } = useExternalModels(); + const allDataTypesById = useDmnEditorStore( + (s) => s.computed(s).getDataTypes(externalModelsByNamespace).allDataTypesById + ); + const dataTypesTree = useDmnEditorStore((s) => s.computed(s).getDataTypes(externalModelsByNamespace).dataTypesTree); + + const dataTypeNames = useMemo(() => { + const dataTypeNames = new Set<string>(); + for (let i = 0, len = dataTypesTree?.length; i < len; i++) { + dataTypeNames.add(dataTypesTree?.[i]?.feelName || dataTypesTree?.[i]?.itemDefinition?.["@_name"]); + } + return dataTypeNames; + }, [dataTypesTree]); + + const buildName = useCallback( + (nameCandidate: string, namesCount: Map<string, number>, nameSeparator: string = NAME_SEPARATOR): string => { + if (namesCount.has(nameCandidate)) { + const occurrences = namesCount.get(nameCandidate)!; + namesCount.set(nameCandidate, occurrences + 1); + return nameCandidate + nameSeparator + occurrences; + } + namesCount.set(nameCandidate, 1); + return nameCandidate; + }, + [] + ); + + const updatePropertiesReferences = useCallback( + (javaClasses: JavaClass[], javaClassNameToDMNTypeNameMap: Map<string, string>): JavaClass[] => { + return javaClasses.map((javaClass) => { + const namesCount: Map<string, number> = new Map(); + const updatedFields = (javaClass ?? [])?.fields?.map((field) => { + const newFieldName = buildName(field?.name, namesCount); + if (javaClassNameToDMNTypeNameMap.has(field.type)) { + const renamedFieldType = javaClassNameToDMNTypeNameMap.get(field.type)!; + return { ...field, name: newFieldName, dmnTypeRef: renamedFieldType }; + } + return { ...field, name: newFieldName }; + }); + return { ...javaClass, fields: updatedFields } as JavaClass; + }); + }, + [buildName] + ); + + const editItemDefinition = useCallback<EditItemDefinition>( + (id, consumer) => { + dmnEditorStoreApi.setState((state) => { + const { itemDefinition, items, index } = findDataTypeById({ + definitions: state.dmn.model.definitions, + itemDefinitionId: id, + allDataTypesById, + }); + + state.dmn.model.definitions.itemDefinition ??= []; + consumer(itemDefinition, items, index, state.dmn.model.definitions.itemDefinition, state); + }); + }, + [allDataTypesById, dmnEditorStoreApi] + ); + + const renameJavaClassToDMNName = useCallback( + (javaClasses: JavaClass[]): JavaClass[] => { + const namesCount: Map<string, number> = new Map(); + const javaClassNameToDMNTypeNameMap: Map<string, string> = new Map(); + + // Map the javaClasses to new renamed classes + const renamedJavaClasses = javaClasses.map((javaClass: JavaClass) => { + const nameCandidate = javaClass.name.substring(javaClass.name.lastIndexOf(".") + 1); + const newName = buildName(nameCandidate, namesCount); + javaClassNameToDMNTypeNameMap.set(javaClass.name, newName); + return { ...javaClass, name: newName } as JavaClass; + }); + + // Return the updated Java classes with renamed types + return updatePropertiesReferences(renamedJavaClasses, javaClassNameToDMNTypeNameMap); + }, + [updatePropertiesReferences, buildName] + ); + + const generateUniqueDMNTypeNames = useCallback( Review Comment: There are two cases in which we need to rename the DMN type. The first case occurs when there are conflicting class names within the selected Java classes by the user. The second case happens when there is a conflict of class names from the existing DMN type. The `generatingUniqueDMNTypeNames` function is used to solve the second case. Therefore, there are a few logical changes required, so I keep it separate. I’m not sure if this is what you were trying to point out here. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
