From b50929744d4e16431ddf99ad7021ed4fbce3223f Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Tue, 11 Aug 2020 11:21:10 +0200
Subject: [PATCH] Fix #442 - ParallelStructures: add device number in log
 messages

---
 jalhyd_branch                                 |  2 +-
 .../log-entry/log-entry.component.ts          |  2 +-
 .../services/internationalisation.service.ts  | 42 ++++++++++++++++++-
 src/locale/messages.en.json                   |  8 +++-
 src/locale/messages.fr.json                   |  8 +++-
 5 files changed, 54 insertions(+), 8 deletions(-)

diff --git a/jalhyd_branch b/jalhyd_branch
index 1a206a71f..5c9832c54 100644
--- a/jalhyd_branch
+++ b/jalhyd_branch
@@ -1 +1 @@
-259-bug-erreur-de-formulation-de-la-loi-de-cunge-en-orifice-denoye
+260-messages-de-log-maintenir-un-pointeur-vers-le-resultat-afin-de-pouvoir-contextualiser-par-la
diff --git a/src/app/components/log-entry/log-entry.component.ts b/src/app/components/log-entry/log-entry.component.ts
index 5cdb305db..63225d391 100644
--- a/src/app/components/log-entry/log-entry.component.ts
+++ b/src/app/components/log-entry/log-entry.component.ts
@@ -1,6 +1,6 @@
 import { Component, Input, OnChanges } from "@angular/core";
 
-import { Message, MessageSeverity } from "jalhyd";
+import { Message, MessageSeverity, Result, Nub } from "jalhyd";
 
 import { I18nService } from "../../services/internationalisation.service";
 import { ApplicationSetupService } from "../../services/app-setup.service";
diff --git a/src/app/services/internationalisation.service.ts b/src/app/services/internationalisation.service.ts
index bc3f5b5a6..5ba795217 100644
--- a/src/app/services/internationalisation.service.ts
+++ b/src/app/services/internationalisation.service.ts
@@ -1,6 +1,6 @@
 import { Injectable, isDevMode } from "@angular/core";
 
-import { Message, MessageCode, Observable, Observer, Nub } from "jalhyd";
+import { Message, MessageCode, Observable, Observer, Nub, CalculatorType } from "jalhyd";
 
 import { StringMap } from "../stringmap";
 import { ApplicationSetupService } from "./app-setup.service";
@@ -141,6 +141,7 @@ export class I18nService extends Observable implements Observer {
      * @param nDigits nombre de chiffres à utiliser pour l'arrondi dans le cas de données numériques
      */
     public localizeMessage(r: Message, nDigits: number = 3): string {
+        let text: string;
         let m: string = this.getMessageFromCode(r.code);
 
         for (const k in r.extraVar) {
@@ -191,7 +192,44 @@ export class I18nService extends Observable implements Observer {
             return this.localizeText(p1);
         });
 
-        return decodeHtml(m);
+        text = decodeHtml(m);
+
+        // prefix message if needed
+        if (r.parent && r.parent.parent && r.parent.parent.sourceNub) {
+            text = this.prefix(r.parent.parent.sourceNub, text);
+        }
+        return text;
+    }
+
+    /**
+     * Prefix given text message with context read from given Nub
+     * @param n Nub associated to original Message object, to read context from
+     * @param text text message to prefix
+     */
+    private prefix(n: Nub, text: string): string {
+        let prefixed: string = text;
+        if (n.parent) {
+            // get child name and position from Nub's parent
+            const pos = String(n.findPositionInParent() + 1);
+            const name = this.childName(n.parent);
+            let m: Message;
+            // Detect downwalls
+            if (n.calcType === CalculatorType.CloisonAval) {
+                m = new Message(MessageCode.INFO_PARENT_PREFIX_DOWNWALL);
+            } else {
+                m = new Message(MessageCode.INFO_PARENT_PREFIX);
+                m.extraVar.name = name;
+                m.extraVar.position = pos;
+            }
+            const prefix = this.localizeMessage(m);
+            prefixed =
+                prefix.substring(0, 1).toUpperCase() + prefix.substring(1)
+                + " "
+                + prefixed.substring(0, 1).toLowerCase() + prefixed.substring(1);
+            // recursivity
+            prefixed = this.prefix(n.parent, prefixed);
+        }
+        return prefixed;
     }
 
     private replaceAll(str: string, find: string, replace: string) {
diff --git a/src/locale/messages.en.json b/src/locale/messages.en.json
index ca693f149..295b0256d 100644
--- a/src/locale/messages.en.json
+++ b/src/locale/messages.en.json
@@ -167,6 +167,8 @@
     "INFO_STUFF_MOVED": "%s #%s moved",
     "INFO_STUFF_REMOVED": "%s #%s removed",
     "INFO_STUFF_N": "%s #",
+    "INFO_CHILD_TYPE_CLOISON": "wall",
+    "INFO_CHILD_TYPE_CLOISON_PLUR": "walls",
     "INFO_CHILD_TYPE_OUVRAGE": "device",
     "INFO_CHILD_TYPE_OUVRAGE_PLUR": "devices",
     "INFO_CHILD_TYPE_MACRORUGO": "apron",
@@ -543,7 +545,7 @@
     "WARNING_LECHAPT_CALMON_SPEED_OUTSIDE_04_2": "This formula is discouraged for a speed that is not between 0.4 and 2 m/s",
     "WARNING_UPSTREAM_BOTTOM_HIGHER_THAN_WATER": "Upstream water elevation is lower or equal to bottom elevation",
     "WARNING_DOWNSTREAM_BOTTOM_HIGHER_THAN_WATER": "Downstream water elevation is lower or equal to bottom elevation",
-    "WARNING_NEGATIVE_SILL": "Sill of device#%number# should be positive",
+    "WARNING_NEGATIVE_SILL": "Sill should be positive",
     "WARNING_RAMP_WIDTH_LOWER_THAN_PATTERN_WIDTH": "Ramp width should contain at least one block (%pattern%m)",
     "WARNING_RAMP_WIDTH_NOT_MULTIPLE_OF_HALF_PATTERN_WIDTH": "Ramp width should be a multiple of half the block width (%halfPattern%m). Nearest values are %lower% and %higher%",
     "WARNING_YN_SECTION_PENTE_NEG_NULLE_HNORMALE_INF": "Normal depth: slope is negative or zero, normal depth is infinite",
@@ -566,5 +568,7 @@
     "WARNING_PAR_N_ROUNDED_TO_05": "Number of patterns was rounded to %val%",
     "WARNING_PAR_M_ROUNDED_TO_1": "Number of longitudinal strips was rounded to %val%",
     "WARNING_PAR_NOT_SUBMERGED": "Insufficient downstream submergence : fall at the last downstream baffle (%DH% m).",
-    "ERROR_PAR_M_GREATER_THAN_2_N": "Number of longitudinal strips exceeds 2N = %max%"
+    "ERROR_PAR_M_GREATER_THAN_2_N": "Number of longitudinal strips exceeds 2N = %max%",
+    "INFO_PARENT_PREFIX": "%name% #%position%: ",
+    "INFO_PARENT_PREFIX_DOWNWALL": "downwall: "
 }
diff --git a/src/locale/messages.fr.json b/src/locale/messages.fr.json
index 590e66d88..7825124eb 100644
--- a/src/locale/messages.fr.json
+++ b/src/locale/messages.fr.json
@@ -167,6 +167,8 @@
     "INFO_STUFF_MOVED": "%s n°%s déplacé(e)",
     "INFO_STUFF_REMOVED": "%s n°%s supprimé(e)",
     "INFO_STUFF_N": "%s n°",
+    "INFO_CHILD_TYPE_CLOISON": "cloison",
+    "INFO_CHILD_TYPE_CLOISON_PLUR": "cloisons",
     "INFO_CHILD_TYPE_OUVRAGE": "ouvrage",
     "INFO_CHILD_TYPE_OUVRAGE_PLUR": "ouvrages",
     "INFO_CHILD_TYPE_MACRORUGO": "radier",
@@ -544,7 +546,7 @@
     "WARNING_LECHAPT_CALMON_SPEED_OUTSIDE_04_2": "Cette formule n'est pas conseillée pour une vitesse non comprise entre 0.4 et 2 m/s",
     "WARNING_UPSTREAM_BOTTOM_HIGHER_THAN_WATER": "La cote de l'eau à l'amont est plus basse ou égale à la cote de fond",
     "WARNING_DOWNSTREAM_BOTTOM_HIGHER_THAN_WATER": "La cote de l'eau à l'aval est plus basse ou égale à la cote de fond",
-    "WARNING_NEGATIVE_SILL": "La pelle du seuil de l'ouvrage n°%number% devrait être positive",
+    "WARNING_NEGATIVE_SILL": "La pelle du seuil devrait être positive",
     "WARNING_RAMP_WIDTH_LOWER_THAN_PATTERN_WIDTH": "La largeur de la rampe devrait comprendre au moins un motif de plot (%pattern%m)",
     "WARNING_RAMP_WIDTH_NOT_MULTIPLE_OF_HALF_PATTERN_WIDTH": "La largeur de la rampe devrait être un multiple d'un demi motif (%halfPattern%m). Les valeurs voisines sont %lower% et %higher%",
     "WARNING_YN_SECTION_PENTE_NEG_NULLE_HNORMALE_INF": "Hauteur normale: pente négative ou nulle, hauteur normale infinie",
@@ -567,5 +569,7 @@
     "WARNING_PAR_N_ROUNDED_TO_05": "Le nombre de motifs a été arrondi à %val%",
     "WARNING_PAR_M_ROUNDED_TO_1": "Le nombre de bandes a été arrondi à %val%",
     "WARNING_PAR_NOT_SUBMERGED": "Ennoiement par l'aval insuffisant : chute au niveau du dernier ralentisseur aval (%DH% m).",
-    "ERROR_PAR_M_GREATER_THAN_2_N": "Le nombre de bandes dépasse 2N = %max%"
+    "ERROR_PAR_M_GREATER_THAN_2_N": "Le nombre de bandes dépasse 2N = %max%",
+    "INFO_PARENT_PREFIX": "%name% n°%position% : ",
+    "INFO_PARENT_PREFIX_DOWNWALL": "cloison aval : "
 }
-- 
GitLab