thiagoelg commented on code in PR #3575:
URL: 
https://github.com/apache/incubator-kie-tools/pull/3575#discussion_r3284801364


##########
packages/bpmn-editor/src/normalization/normalize.ts:
##########
@@ -89,7 +170,63 @@ export function normalize(model: BpmnLatestModel): 
State["bpmn"]["model"] {
     }
   });
 
+  // Merge itemDefinitions that share the same structureRef.
+  deduplicateItemDefinitions(model.definitions);
+
   const normalizedModel = model as Normalized<BpmnLatestModel>;
 
   return normalizedModel;
 }
+
+// Keep one itemDefinition per structureRef and update references to point to 
it.
+export function deduplicateItemDefinitions(definitions: 
BpmnLatestModel["definitions"]): void {
+  const itemDefGroups = new Map<string, Array<{ "@_id"?: string }>>();
+  for (const rootElement of definitions.rootElement ?? []) {
+    if (rootElement.__$$element === "itemDefinition") {
+      const key = rootElement["@_structureRef"] ?? "";
+      addOrGetGroup(itemDefGroups, key).push(rootElement);
+    }
+  }
+
+  // Map each duplicate id to the id of the first occurrence we keep.
+  const itemDefIdRemap = new Map<string, string>();

Review Comment:
   ```suggestion
     const itemDefinitionIdsToRemap = new Map<string, string>();
   ```
   
   I prefer that the variable names be more descriptive. It makes it easier to 
read and follow the code.



##########
packages/bpmn-editor/src/normalization/normalize.ts:
##########
@@ -31,6 +32,86 @@ type WithRequiredDeep<T, K extends keyof any> = T extends 
undefined
         ? { [P in K]-?: NonNullable<WithRequiredDeep<T[P], K>> }
         : T);
 
+// Get the array for `key`, or create an empty one if missing.
+function addOrGetGroup<K, V>(map: Map<K, V[]>, key: K): V[] {
+  let group = map.get(key);
+  if (!group) {
+    group = [];
+    map.set(key, group);
+  }
+  return group;
+}
+
+function remapRef(remap: Map<string, string>, id: string | undefined): string 
| undefined {
+  return id === undefined ? undefined : remap.get(id) ?? id;
+}

Review Comment:
   This is more of a getter than a "remap" function.



##########
packages/bpmn-editor/src/normalization/normalize.ts:
##########
@@ -89,7 +170,63 @@ export function normalize(model: BpmnLatestModel): 
State["bpmn"]["model"] {
     }
   });
 
+  // Merge itemDefinitions that share the same structureRef.
+  deduplicateItemDefinitions(model.definitions);
+
   const normalizedModel = model as Normalized<BpmnLatestModel>;
 
   return normalizedModel;
 }
+
+// Keep one itemDefinition per structureRef and update references to point to 
it.
+export function deduplicateItemDefinitions(definitions: 
BpmnLatestModel["definitions"]): void {
+  const itemDefGroups = new Map<string, Array<{ "@_id"?: string }>>();

Review Comment:
   ```suggestion
     const itemDefinitionsGroupedStructureRef = new Map<string, Array<{ 
"@_id"?: string }>>();
   ```



##########
packages/bpmn-editor/src/normalization/normalize.ts:
##########
@@ -89,7 +170,63 @@ export function normalize(model: BpmnLatestModel): 
State["bpmn"]["model"] {
     }
   });
 
+  // Merge itemDefinitions that share the same structureRef.
+  deduplicateItemDefinitions(model.definitions);
+
   const normalizedModel = model as Normalized<BpmnLatestModel>;
 
   return normalizedModel;
 }
+
+// Keep one itemDefinition per structureRef and update references to point to 
it.
+export function deduplicateItemDefinitions(definitions: 
BpmnLatestModel["definitions"]): void {
+  const itemDefGroups = new Map<string, Array<{ "@_id"?: string }>>();
+  for (const rootElement of definitions.rootElement ?? []) {
+    if (rootElement.__$$element === "itemDefinition") {
+      const key = rootElement["@_structureRef"] ?? "";
+      addOrGetGroup(itemDefGroups, key).push(rootElement);
+    }
+  }
+
+  // Map each duplicate id to the id of the first occurrence we keep.
+  const itemDefIdRemap = new Map<string, string>();
+  for (const group of itemDefGroups.values()) {
+    if (group.length <= 1) {
+      continue;
+    }
+    const survivorId = group[0]["@_id"];
+    if (!survivorId) {
+      continue;
+    }
+    for (let i = 1; i < group.length; i++) {
+      const duplicateId = group[i]["@_id"];
+      if (duplicateId) {
+        itemDefIdRemap.set(duplicateId, survivorId);
+      }
+    }
+  }
+
+  if (itemDefIdRemap.size === 0) {
+    return;
+  }
+
+  // Remove the duplicate itemDefinitions and update every itemSubjectRef.
+  const idsToRemove = new Set(itemDefIdRemap.keys());

Review Comment:
   ```suggestion
     const itemDefinitionIdsToRemove = new Set(itemDefIdRemap.keys());
   ```



##########
packages/bpmn-editor/src/normalization/normalize.ts:
##########
@@ -31,6 +32,86 @@ type WithRequiredDeep<T, K extends keyof any> = T extends 
undefined
         ? { [P in K]-?: NonNullable<WithRequiredDeep<T[P], K>> }
         : T);
 
+// Get the array for `key`, or create an empty one if missing.
+function addOrGetGroup<K, V>(map: Map<K, V[]>, key: K): V[] {
+  let group = map.get(key);
+  if (!group) {
+    group = [];
+    map.set(key, group);
+  }
+  return group;
+}
+
+function remapRef(remap: Map<string, string>, id: string | undefined): string 
| undefined {
+  return id === undefined ? undefined : remap.get(id) ?? id;
+}

Review Comment:
   ```suggestion
   function getRefToRemap(remap: Map<string, string>, id: string | undefined): 
string | undefined {
     return id === undefined ? undefined : remap.get(id) ?? id;
   }
   ```
   
   I find it hard to understand what exactly this function is getting.



##########
packages/bpmn-editor/src/normalization/normalize.ts:
##########
@@ -89,7 +170,63 @@ export function normalize(model: BpmnLatestModel): 
State["bpmn"]["model"] {
     }
   });
 
+  // Merge itemDefinitions that share the same structureRef.
+  deduplicateItemDefinitions(model.definitions);
+
   const normalizedModel = model as Normalized<BpmnLatestModel>;
 
   return normalizedModel;
 }
+
+// Keep one itemDefinition per structureRef and update references to point to 
it.
+export function deduplicateItemDefinitions(definitions: 
BpmnLatestModel["definitions"]): void {
+  const itemDefGroups = new Map<string, Array<{ "@_id"?: string }>>();
+  for (const rootElement of definitions.rootElement ?? []) {
+    if (rootElement.__$$element === "itemDefinition") {
+      const key = rootElement["@_structureRef"] ?? "";
+      addOrGetGroup(itemDefGroups, key).push(rootElement);

Review Comment:
   ```suggestion
         const itemDefinitionStructureRef = rootElement["@_structureRef"] ?? "";
         addOrGetGroup(itemDefinitionsGroupedStructureRef, 
itemDefinitionStructureRef).push(rootElement);
   ```



##########
packages/bpmn-editor/src/normalization/normalize.ts:
##########
@@ -89,7 +170,63 @@ export function normalize(model: BpmnLatestModel): 
State["bpmn"]["model"] {
     }
   });
 
+  // Merge itemDefinitions that share the same structureRef.
+  deduplicateItemDefinitions(model.definitions);
+
   const normalizedModel = model as Normalized<BpmnLatestModel>;
 
   return normalizedModel;
 }
+
+// Keep one itemDefinition per structureRef and update references to point to 
it.
+export function deduplicateItemDefinitions(definitions: 
BpmnLatestModel["definitions"]): void {
+  const itemDefGroups = new Map<string, Array<{ "@_id"?: string }>>();
+  for (const rootElement of definitions.rootElement ?? []) {
+    if (rootElement.__$$element === "itemDefinition") {
+      const key = rootElement["@_structureRef"] ?? "";
+      addOrGetGroup(itemDefGroups, key).push(rootElement);
+    }
+  }
+
+  // Map each duplicate id to the id of the first occurrence we keep.
+  const itemDefIdRemap = new Map<string, string>();
+  for (const group of itemDefGroups.values()) {

Review Comment:
   ```suggestion
     for (const itemDefinitionsGroup of 
itemDefinitionsGroupedStructureRef.values()) {
   ```



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