tiagobento commented on code in PR #2508:
URL: 
https://github.com/apache/incubator-kie-tools/pull/2508#discussion_r1707822910


##########
packages/dmn-editor/src/diagram/Diagram.tsx:
##########
@@ -1009,12 +1010,13 @@ export const Diagram = React.forwardRef<DiagramRef, { 
container: React.RefObject
                 addDecisionToDecisionService({
                   definitions: state.dmn.model.definitions,
                   drdIndex: state.computed(state).getDrdIndex(),
-                  decisionId: selectedNodes[i].data.dmnObject!["@_id"]!, // We 
can assume that all selected nodes are Decisions because the contaiment was 
validated above.
+                  decisionHref: selectedNodes[i].id, // We can assume that all 
selected nodes are Decisions because the contaiment was validated above.
                   decisionServiceId: state
                     .computed(state)
                     .getDiagramData(externalModelsByNamespace)
                     
.nodesById.get(dropTargetNode.id)!.data.dmnObject!["@_id"]!,
                   snapGrid: state.diagram.snapGrid,
+                  externalModelsByNamespace: externalModelsByNamespace,

Review Comment:
   Just for conciseness... 
   ```suggestion
                     externalModelsByNamespace,
   ```



##########
packages/dmn-editor/stories/dev/DevWebApp.stories.tsx:
##########
@@ -18,20 +18,19 @@
  */
 
 import * as React from "react";
-import type { Meta, StoryObj } from "@storybook/react";
 import { useCallback, useMemo, useRef, useState } from "react";
+import type { Meta, StoryObj } from "@storybook/react";

Review Comment:
   I think Prettier doesn't change the imports... Maybe it's your VS Code 
"Organize imports"?



##########
packages/dmn-editor/src/mutations/deleteDecisionFromDecisionService.ts:
##########
@@ -44,11 +66,10 @@ export function deleteDecisionFromDecisionService({
     );
   }
 
-  decisionService.outputDecision = (decisionService.outputDecision ?? 
[]).filter(
-    (s) => s["@_href"] !== `#${decisionId}`
-  );
+  const xmlHref = buildXmlHref({ namespace: href.namespace, id: href.id });
+  decisionService.outputDecision = (decisionService.outputDecision ?? 
[]).filter((s) => s["@_href"] !== `${xmlHref}`);
   decisionService.encapsulatedDecision = (decisionService.encapsulatedDecision 
?? []).filter(
-    (s) => s["@_href"] !== `#${decisionId}`
+    (s) => s["@_href"] !== `${xmlHref}`
   );

Review Comment:
   Nice



##########
packages/dmn-editor/src/mutations/deleteDecisionFromDecisionService.ts:
##########
@@ -20,21 +20,43 @@
 import { DMN15__tDefinitions } from 
"@kie-tools/dmn-marshaller/dist/schemas/dmn-1_5/ts-gen/types";
 import { repopulateInputDataAndDecisionsOnDecisionService } from 
"./repopulateInputDataAndDecisionsOnDecisionService";
 import { Normalized } from "../normalization/normalize";
+import { buildXmlHref, parseXmlHref } from "../xml/xmlHrefs";
+import { DmnLatestModel } from "@kie-tools/dmn-marshaller/dist";
+import { ExternalModelsIndex } from "../DmnEditor";
 
 export function deleteDecisionFromDecisionService({
   definitions,
-  decisionId,
+  decisionHref,
   decisionServiceId,
+  externalModelsByNamespace,
 }: {
   definitions: Normalized<DMN15__tDefinitions>;
-  decisionId: string;
+  decisionHref: string;
   decisionServiceId: string;
+  externalModelsByNamespace: ExternalModelsIndex | undefined;
 }) {
-  console.debug(`DMN MUTATION: Deleting Decision '${decisionId}' from Decision 
Service '${decisionServiceId}'`);
+  console.debug(`DMN MUTATION: Deleting Decision '${decisionHref}' from 
Decision Service '${decisionServiceId}'`);
 
-  const decision = definitions.drgElement?.find((s) => s["@_id"] === 
decisionId);
-  if (decision?.__$$element !== "decision") {
-    throw new Error(`DMN MUTATION: DRG Element with id '${decisionId}' is 
either not a Decision or doesn't exist.`);
+  const href = parseXmlHref(decisionHref);
+
+  const externalModel = externalModelsByNamespace?.[href.namespace ?? ""];
+  if (href.namespace && !externalModel) {
+    throw new Error(`DMN MUTATION: Namespace '${href.namespace}' not found.`);
+  }
+
+  if (externalModel && (externalModel.model as 
Normalized<DmnLatestModel>).definitions) {
+    const externalDrgs = (externalModel.model as 
Normalized<DmnLatestModel>).definitions.drgElement;
+    const decision = externalDrgs?.find((drgElement) => drgElement["@_id"] === 
href.id);

Review Comment:
   Also same as above.



##########
packages/dmn-editor/src/mutations/addDecisionToDecisionService.ts:
##########
@@ -65,10 +91,10 @@ export function addDecisionToDecisionService({
   const section = getSectionForDecisionInsideDecisionService({ decisionShape, 
decisionServiceShape, snapGrid });
   if (section === "encapsulated") {
     decisionService.encapsulatedDecision ??= [];
-    decisionService.encapsulatedDecision.push({ "@_href": `#${decisionId}` });
+    decisionService.encapsulatedDecision.push({ "@_href": `${hrefString}` });
   } else if (section === "output") {
     decisionService.outputDecision ??= [];
-    decisionService.outputDecision.push({ "@_href": `#${decisionId}` });
+    decisionService.outputDecision.push({ "@_href": `${hrefString}` });

Review Comment:
   Nice



##########
packages/dmn-editor/src/mutations/deleteDecisionFromDecisionService.ts:
##########
@@ -20,21 +20,43 @@
 import { DMN15__tDefinitions } from 
"@kie-tools/dmn-marshaller/dist/schemas/dmn-1_5/ts-gen/types";
 import { repopulateInputDataAndDecisionsOnDecisionService } from 
"./repopulateInputDataAndDecisionsOnDecisionService";
 import { Normalized } from "../normalization/normalize";
+import { buildXmlHref, parseXmlHref } from "../xml/xmlHrefs";
+import { DmnLatestModel } from "@kie-tools/dmn-marshaller/dist";
+import { ExternalModelsIndex } from "../DmnEditor";
 
 export function deleteDecisionFromDecisionService({
   definitions,
-  decisionId,
+  decisionHref,
   decisionServiceId,
+  externalModelsByNamespace,
 }: {
   definitions: Normalized<DMN15__tDefinitions>;
-  decisionId: string;
+  decisionHref: string;
   decisionServiceId: string;
+  externalModelsByNamespace: ExternalModelsIndex | undefined;
 }) {
-  console.debug(`DMN MUTATION: Deleting Decision '${decisionId}' from Decision 
Service '${decisionServiceId}'`);
+  console.debug(`DMN MUTATION: Deleting Decision '${decisionHref}' from 
Decision Service '${decisionServiceId}'`);
 
-  const decision = definitions.drgElement?.find((s) => s["@_id"] === 
decisionId);
-  if (decision?.__$$element !== "decision") {
-    throw new Error(`DMN MUTATION: DRG Element with id '${decisionId}' is 
either not a Decision or doesn't exist.`);
+  const href = parseXmlHref(decisionHref);
+
+  const externalModel = externalModelsByNamespace?.[href.namespace ?? ""];
+  if (href.namespace && !externalModel) {
+    throw new Error(`DMN MUTATION: Namespace '${href.namespace}' not found.`);
+  }
+
+  if (externalModel && (externalModel.model as 
Normalized<DmnLatestModel>).definitions) {

Review Comment:
   ```suggestion
     if (href.namespace) {
   ```
   
   Same as above.



##########
packages/dmn-editor/src/mutations/addDecisionToDecisionService.ts:
##########
@@ -25,25 +25,48 @@ import { SnapGrid } from "../store/Store";
 import { MIN_NODE_SIZES } from "../diagram/nodes/DefaultSizes";
 import { NODE_TYPES } from "../diagram/nodes/NodeTypes";
 import { Normalized } from "../normalization/normalize";
+import { ExternalModelsIndex } from "../DmnEditor";
+import { buildXmlHref, parseXmlHref } from "../xml/xmlHrefs";
+import { DmnLatestModel } from "@kie-tools/dmn-marshaller/dist";
+import { xmlHrefToQName } from "../xml/xmlHrefToQName";
 
 export function addDecisionToDecisionService({
   definitions,
-  decisionId,
+  decisionHref,
   decisionServiceId,
   drdIndex,
   snapGrid,
+  externalModelsByNamespace,
 }: {
   definitions: Normalized<DMN15__tDefinitions>;
-  decisionId: string;
+  decisionHref: string;
   decisionServiceId: string;
   drdIndex: number;
   snapGrid: SnapGrid;
+  externalModelsByNamespace: ExternalModelsIndex | undefined;
 }) {
-  console.debug(`DMN MUTATION: Adding Decision '${decisionId}' to Decision 
Service '${decisionServiceId}'`);
+  console.debug(`DMN MUTATION: Adding Decision '${decisionHref}' to Decision 
Service '${decisionServiceId}'`);
 
-  const decision = definitions.drgElement?.find((s) => s["@_id"] === 
decisionId);
-  if (decision?.__$$element !== "decision") {
-    throw new Error(`DMN MUTATION: DRG Element with id '${decisionId}' is 
either not a Decision or doesn't exist.`);
+  const href = parseXmlHref(decisionHref);
+
+  const externalModel = externalModelsByNamespace?.[href.namespace ?? ""];
+  if (href.namespace && !externalModel) {
+    throw new Error(`DMN MUTATION: Namespace '${href.namespace}' not found.`);
+  }
+
+  if (externalModel && (externalModel.model as 
Normalized<DmnLatestModel>).definitions) {

Review Comment:
   ```suggestion
     if (href.namespace) {
   ```
   
   ?
   
   I mean, we only want to enter this branch if it is an external node... Right?



##########
packages/dmn-editor/src/mutations/addDecisionToDecisionService.ts:
##########
@@ -25,25 +25,48 @@ import { SnapGrid } from "../store/Store";
 import { MIN_NODE_SIZES } from "../diagram/nodes/DefaultSizes";
 import { NODE_TYPES } from "../diagram/nodes/NodeTypes";
 import { Normalized } from "../normalization/normalize";
+import { ExternalModelsIndex } from "../DmnEditor";
+import { buildXmlHref, parseXmlHref } from "../xml/xmlHrefs";
+import { DmnLatestModel } from "@kie-tools/dmn-marshaller/dist";
+import { xmlHrefToQName } from "../xml/xmlHrefToQName";
 
 export function addDecisionToDecisionService({
   definitions,
-  decisionId,
+  decisionHref,
   decisionServiceId,
   drdIndex,
   snapGrid,
+  externalModelsByNamespace,
 }: {
   definitions: Normalized<DMN15__tDefinitions>;
-  decisionId: string;
+  decisionHref: string;
   decisionServiceId: string;
   drdIndex: number;
   snapGrid: SnapGrid;
+  externalModelsByNamespace: ExternalModelsIndex | undefined;
 }) {
-  console.debug(`DMN MUTATION: Adding Decision '${decisionId}' to Decision 
Service '${decisionServiceId}'`);
+  console.debug(`DMN MUTATION: Adding Decision '${decisionHref}' to Decision 
Service '${decisionServiceId}'`);
 
-  const decision = definitions.drgElement?.find((s) => s["@_id"] === 
decisionId);
-  if (decision?.__$$element !== "decision") {
-    throw new Error(`DMN MUTATION: DRG Element with id '${decisionId}' is 
either not a Decision or doesn't exist.`);
+  const href = parseXmlHref(decisionHref);
+
+  const externalModel = externalModelsByNamespace?.[href.namespace ?? ""];
+  if (href.namespace && !externalModel) {
+    throw new Error(`DMN MUTATION: Namespace '${href.namespace}' not found.`);
+  }
+
+  if (externalModel && (externalModel.model as 
Normalized<DmnLatestModel>).definitions) {
+    const externalDrgs = (externalModel.model as 
Normalized<DmnLatestModel>).definitions.drgElement;
+    const decision = externalDrgs?.find((drgElement) => drgElement["@_id"] === 
href.id);

Review Comment:
   Personal preference, but I always think we should avoid intermediate 
variables to avoid giving it a name. It's one more thing to hold onto my head 
when reading code...



##########
packages/dmn-editor/src/externalNodes/ExternalNodesPanel.tsx:
##########
@@ -159,6 +159,7 @@ export function ExternalNodesPanel() {
                           externalDrgElementId: drgElement["@_id"]!,
                         })
                       }
+                      
data-testid={`kie-tools--dmn-editor--external-node-${_import["@_name"] === "" ? 
_import["@_id"] : _import["@_name"]}-${drgElement["@_name"]}`}

Review Comment:
   Why dont' we use the FEEL name of the node? If it's imported without a name 
we just use `{nodeName}`, if its import has a name, we do 
`{importName}.{nodeName}`?



-- 
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]

Reply via email to