From 9545e016a2967698428dcc8a0889dff7ece5d0fb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr>
Date: Fri, 14 Oct 2022 16:06:49 +0200
Subject: [PATCH] refactor: set Structure._loiDebit to private, add setter to
 update properties

refs #328
---
 src/structure/structure.ts                       | 16 ++++++++--------
 src/structure/structure_gate_cem88d.ts           |  2 +-
 src/structure/structure_gate_cem88v.ts           |  2 +-
 src/structure/structure_gate_cunge80.ts          |  2 +-
 src/structure/structure_kivi.ts                  |  2 +-
 src/structure/structure_orifice_free.ts          |  4 ++--
 src/structure/structure_orifice_submerged.ts     |  2 +-
 src/structure/structure_props.ts                 |  2 +-
 .../structure_rectangular_orifice_free.ts        |  2 +-
 .../structure_rectangular_orifice_submerged.ts   |  2 +-
 src/structure/structure_triangular_trunc_weir.ts |  2 +-
 src/structure/structure_triangular_weir.ts       |  2 +-
 src/structure/structure_triangular_weir_broad.ts |  2 +-
 src/structure/structure_vanlev_larinier.ts       |  2 +-
 src/structure/structure_vanlev_villemonte.ts     |  2 +-
 src/structure/structure_weir_cem88d.ts           |  2 +-
 src/structure/structure_weir_cem88v.ts           |  2 +-
 src/structure/structure_weir_cunge80.ts          |  2 +-
 src/structure/structure_weir_free.ts             |  4 ++--
 src/structure/structure_weir_submerged.ts        |  2 +-
 .../structure_weir_submerged_larinier.ts         |  2 +-
 src/structure/structure_weir_villemonte.ts       |  2 +-
 22 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/src/structure/structure.ts b/src/structure/structure.ts
index 6425e6ae..b9d3a0c9 100644
--- a/src/structure/structure.ts
+++ b/src/structure/structure.ts
@@ -82,7 +82,7 @@ export abstract class Structure extends ChildNub {
     /** Peut-on calculer ZDV ? */
     protected _isZDVcalculable: boolean;
 
-    protected _loiDebit: LoiDebit;
+    private _loiDebit: LoiDebit;
 
     constructor(prms: StructureParams, dbg: boolean = false) {
         super(prms, dbg);
@@ -97,13 +97,6 @@ export abstract class Structure extends ChildNub {
 
     /** Returns Props object (observable set of key-values) associated to this Nub */
     public get properties(): Props {
-        // completes props with calcType, structureType and loiDebit if not already set
-        if (this._props.getPropValue("loiDebit") === undefined) {
-            this._props.setPropValue("loiDebit", this._loiDebit);
-        }
-        if (this._props.getPropValue("structureType") === undefined) {
-            this._props.setPropValue("structureType", StructureProperties.findCompatibleStructure(this._props.getPropValue("loiDebit"), this.parent as ParallelStructure));
-        }
         return this._props;
     }
 
@@ -142,6 +135,13 @@ export abstract class Structure extends ChildNub {
         return this._loiDebit;
     }
 
+    protected setLoiDebit(ld: LoiDebit) {
+        // completes props with structureType and loiDebit
+        this._loiDebit = ld;
+        this._props.setPropValue("loiDebit", this._loiDebit);
+        this._props.setPropValue("structureType", StructureProperties.findCompatibleStructure(this._loiDebit, this.parent as ParallelStructure));
+    }
+
     /**
      * Returns the nth visible parameter (used in nghyd/PabTable)
      */
diff --git a/src/structure/structure_gate_cem88d.ts b/src/structure/structure_gate_cem88d.ts
index 0cbd2019..6e5ee8a0 100644
--- a/src/structure/structure_gate_cem88d.ts
+++ b/src/structure/structure_gate_cem88d.ts
@@ -12,7 +12,7 @@ export class StructureGateCem88d extends RectangularStructure {
 
     constructor(prms: RectangularStructureParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.GateCem88d;
+        this.setLoiDebit(LoiDebit.GateCem88d);
         this._isZDVcalculable = false;
         this.prms.W.visible = true;
         this.prms.CdWR.visible = true;
diff --git a/src/structure/structure_gate_cem88v.ts b/src/structure/structure_gate_cem88v.ts
index eea96f17..8cd007c6 100644
--- a/src/structure/structure_gate_cem88v.ts
+++ b/src/structure/structure_gate_cem88v.ts
@@ -12,7 +12,7 @@ export class StructureGateCem88v extends RectangularStructure {
 
     constructor(prms: RectangularStructureParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.GateCem88v;
+        this.setLoiDebit(LoiDebit.GateCem88v);
         this.prms.W.visible = true;
         this.prms.CdGR.visible = true;
     }
diff --git a/src/structure/structure_gate_cunge80.ts b/src/structure/structure_gate_cunge80.ts
index 698bd507..b2a71ead 100644
--- a/src/structure/structure_gate_cunge80.ts
+++ b/src/structure/structure_gate_cunge80.ts
@@ -16,7 +16,7 @@ export class StructureGateCunge80 extends RectangularStructure {
 
     constructor(prms: RectangularStructureParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.GateCunge80;
+        this.setLoiDebit(LoiDebit.GateCunge80);
         if (prms.W.v !== Infinity) {
             this._isZDVcalculable = false;
         }
diff --git a/src/structure/structure_kivi.ts b/src/structure/structure_kivi.ts
index 77c7cb19..a84bd2d2 100644
--- a/src/structure/structure_kivi.ts
+++ b/src/structure/structure_kivi.ts
@@ -10,7 +10,7 @@ export class StructureKivi extends Structure {
 
     constructor(prms: StructureKiviParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.KIVI;
+        this.setLoiDebit(LoiDebit.KIVI);
     }
 
     /**
diff --git a/src/structure/structure_orifice_free.ts b/src/structure/structure_orifice_free.ts
index afa71811..ce1cdfb4 100644
--- a/src/structure/structure_orifice_free.ts
+++ b/src/structure/structure_orifice_free.ts
@@ -12,7 +12,7 @@ export class StructureOrificeFree extends Structure {
 
     constructor(prms: StructureOrificeFreeParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.OrificeFree;
+        this.setLoiDebit(LoiDebit.OrificeFree);
         this._isZDVcalculable = false;
     }
 
@@ -25,7 +25,7 @@ export class StructureOrificeFree extends Structure {
 
     public Calc(sVarCalc: string, rInit?: number): Result {
         this.currentResult = super.Calc(sVarCalc, rInit);
-        if (this._loiDebit === LoiDebit.OrificeFree && this.prms.Z2.v > this.prms.Zco.v) {
+        if (this.loiDebit === LoiDebit.OrificeFree && this.prms.Z2.v > this.prms.Zco.v) {
             this._result.resultElement.addMessage(new Message(
                 MessageCode.WARNING_ORIFICE_FREE_DOWNSTREAM_ELEVATION_POSSIBLE_SUBMERSION,
                 {
diff --git a/src/structure/structure_orifice_submerged.ts b/src/structure/structure_orifice_submerged.ts
index df3878b2..bf72df9e 100644
--- a/src/structure/structure_orifice_submerged.ts
+++ b/src/structure/structure_orifice_submerged.ts
@@ -11,7 +11,7 @@ export class StructureOrificeSubmerged extends Structure {
 
     constructor(prms: StructureOrificeSubmergedParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.OrificeSubmerged;
+        this.setLoiDebit(LoiDebit.OrificeSubmerged);
         this._isZDVcalculable = false;
     }
 
diff --git a/src/structure/structure_props.ts b/src/structure/structure_props.ts
index a6fc2b01..0ec01341 100644
--- a/src/structure/structure_props.ts
+++ b/src/structure/structure_props.ts
@@ -140,7 +140,7 @@ export class StructureProperties {
      * du module de calcul parentNub @TODO la 1ère ? normalement il n'y en a qu'une !
      */
     public static findCompatibleStructure(loi: LoiDebit, parentNub: ParallelStructure): StructureType {
-        const loisAdmissibles = parentNub.getLoisAdmissibles();
+        const loisAdmissibles = parentNub?.getLoisAdmissibles();
         for (const st in loisAdmissibles) {
             if (loisAdmissibles.hasOwnProperty(st)) {
                 const lds: LoiDebit[] = loisAdmissibles[st];
diff --git a/src/structure/structure_rectangular_orifice_free.ts b/src/structure/structure_rectangular_orifice_free.ts
index 0a91a37d..b5082d17 100644
--- a/src/structure/structure_rectangular_orifice_free.ts
+++ b/src/structure/structure_rectangular_orifice_free.ts
@@ -13,7 +13,7 @@ export class StructureRectangularOrificeFree extends RectangularStructure {
 
     constructor(prms: RectangularStructureParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.RectangularOrificeFree;
+        this.setLoiDebit(LoiDebit.RectangularOrificeFree);
         this.prms.W.visible = true;
         this.prms.CdGR.visible = true;
     }
diff --git a/src/structure/structure_rectangular_orifice_submerged.ts b/src/structure/structure_rectangular_orifice_submerged.ts
index 9182c615..d1715d16 100644
--- a/src/structure/structure_rectangular_orifice_submerged.ts
+++ b/src/structure/structure_rectangular_orifice_submerged.ts
@@ -12,7 +12,7 @@ export class StructureRectangularOrificeSubmerged extends RectangularStructure {
 
     constructor(prms: RectangularStructureParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.RectangularOrificeSubmerged;
+        this.setLoiDebit(LoiDebit.RectangularOrificeSubmerged);
         if (prms.W.v !== Infinity) {
             this._isZDVcalculable = false;
         }
diff --git a/src/structure/structure_triangular_trunc_weir.ts b/src/structure/structure_triangular_trunc_weir.ts
index 7b72b299..5ef3cb3e 100644
--- a/src/structure/structure_triangular_trunc_weir.ts
+++ b/src/structure/structure_triangular_trunc_weir.ts
@@ -12,7 +12,7 @@ export class StructureTriangularTruncWeirFree extends Structure {
 
     constructor(prms: TriangularTruncStructureParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.TriangularTruncWeirFree;
+        this.setLoiDebit(LoiDebit.TriangularTruncWeirFree);
     }
 
     /**
diff --git a/src/structure/structure_triangular_weir.ts b/src/structure/structure_triangular_weir.ts
index 02798fbb..e4c445fb 100644
--- a/src/structure/structure_triangular_weir.ts
+++ b/src/structure/structure_triangular_weir.ts
@@ -13,7 +13,7 @@ export class StructureTriangularWeir extends StructureTriangularWeirFree {
 
     constructor(prms: TriangularStructureParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.TriangularWeirFree; // First name of the law for backward compatibility
+        this.setLoiDebit(LoiDebit.TriangularWeirFree); // First name of the law for backward compatibility
     }
 
     /**
diff --git a/src/structure/structure_triangular_weir_broad.ts b/src/structure/structure_triangular_weir_broad.ts
index 46581dcf..4f4e1101 100644
--- a/src/structure/structure_triangular_weir_broad.ts
+++ b/src/structure/structure_triangular_weir_broad.ts
@@ -13,7 +13,7 @@ export class StructureTriangularWeirBroad extends StructureTriangularWeirFree {
 
     constructor(prms: TriangularStructureParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.TriangularWeirBroad; // First name of the law for backward compatibility
+        this.setLoiDebit(LoiDebit.TriangularWeirBroad); // First name of the law for backward compatibility
     }
 
     /**
diff --git a/src/structure/structure_vanlev_larinier.ts b/src/structure/structure_vanlev_larinier.ts
index 33342264..9fb690d9 100644
--- a/src/structure/structure_vanlev_larinier.ts
+++ b/src/structure/structure_vanlev_larinier.ts
@@ -6,7 +6,7 @@ export class StructureVanLevLarinier extends StructureWeirSubmergedLarinier {
 
     constructor(prms: StructureVanLevParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.VanLevLarinier;
+        this.setLoiDebit(LoiDebit.VanLevLarinier);
     }
 
     /**
diff --git a/src/structure/structure_vanlev_villemonte.ts b/src/structure/structure_vanlev_villemonte.ts
index 07e186e7..ca803315 100644
--- a/src/structure/structure_vanlev_villemonte.ts
+++ b/src/structure/structure_vanlev_villemonte.ts
@@ -6,7 +6,7 @@ export class StructureVanLevVillemonte extends StructureWeirVillemonte {
 
     constructor(prms: StructureVanLevParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.VanLevVillemonte;
+        this.setLoiDebit(LoiDebit.VanLevVillemonte);
     }
 
     /**
diff --git a/src/structure/structure_weir_cem88d.ts b/src/structure/structure_weir_cem88d.ts
index 4ca6ff98..a35c7c87 100644
--- a/src/structure/structure_weir_cem88d.ts
+++ b/src/structure/structure_weir_cem88d.ts
@@ -7,7 +7,7 @@ export class StructureWeirCem88d extends StructureGateCem88d {
 
     constructor(prms: RectangularStructureParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.WeirCem88d;
+        this.setLoiDebit(LoiDebit.WeirCem88d);
         this._isZDVcalculable = true;
         // Gestion de l'affichage l'ouverture de vanne
         this.prms.W.visible = false;
diff --git a/src/structure/structure_weir_cem88v.ts b/src/structure/structure_weir_cem88v.ts
index f4c44d33..aa29d7f0 100644
--- a/src/structure/structure_weir_cem88v.ts
+++ b/src/structure/structure_weir_cem88v.ts
@@ -7,7 +7,7 @@ export class StructureWeirCem88v extends StructureGateCem88v {
 
     constructor(prms: RectangularStructureParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.WeirCem88v;
+        this.setLoiDebit(LoiDebit.WeirCem88v);
         this.prms.W.visible = false;
     }
 
diff --git a/src/structure/structure_weir_cunge80.ts b/src/structure/structure_weir_cunge80.ts
index c46ffb63..c2fc2e4c 100644
--- a/src/structure/structure_weir_cunge80.ts
+++ b/src/structure/structure_weir_cunge80.ts
@@ -7,7 +7,7 @@ export class StructureWeirCunge80 extends StructureGateCunge80 {
 
     constructor(prms: RectangularStructureParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.WeirCunge80;
+        this.setLoiDebit(LoiDebit.WeirCunge80);
         this.prms.W.visible = false;
     }
 
diff --git a/src/structure/structure_weir_free.ts b/src/structure/structure_weir_free.ts
index 3ac77c7a..4bc0607f 100644
--- a/src/structure/structure_weir_free.ts
+++ b/src/structure/structure_weir_free.ts
@@ -12,14 +12,14 @@ export class StructureWeirFree extends RectangularStructure {
 
     constructor(prms: RectangularStructureParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.WeirFree;
+        this.setLoiDebit(LoiDebit.WeirFree);
         this.prms.CdWR.visible = true;
     }
 
     public Calc(sVarCalc: string, rInit?: number): Result {
         this.currentResult = super.Calc(sVarCalc, rInit);
         // do not check h2 for derived classes (ex: StructureWeirVillemonte)
-        if (this._loiDebit === LoiDebit.WeirFree && this.prms.h2.v > 0) {
+        if (this.loiDebit === LoiDebit.WeirFree && this.prms.h2.v > 0) {
             this._result.resultElement.addMessage(new Message(
                 MessageCode.WARNING_DOWNSTREAM_ELEVATION_POSSIBLE_SUBMERSION,
                 {
diff --git a/src/structure/structure_weir_submerged.ts b/src/structure/structure_weir_submerged.ts
index 6fbd5e3c..76925f3f 100644
--- a/src/structure/structure_weir_submerged.ts
+++ b/src/structure/structure_weir_submerged.ts
@@ -15,7 +15,7 @@ export class StructureWeirSubmerged extends RectangularStructure {
 
     constructor(prms: RectangularStructureParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.WeirSubmerged;
+        this.setLoiDebit(LoiDebit.WeirSubmerged);
         this.prms.CdWS.visible = true;
     }
 
diff --git a/src/structure/structure_weir_submerged_larinier.ts b/src/structure/structure_weir_submerged_larinier.ts
index c8651759..63df6fd3 100644
--- a/src/structure/structure_weir_submerged_larinier.ts
+++ b/src/structure/structure_weir_submerged_larinier.ts
@@ -14,7 +14,7 @@ export class StructureWeirSubmergedLarinier extends RectangularStructure {
 
     constructor(prms: RectangularStructureParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.WeirSubmergedLarinier;
+        this.setLoiDebit(LoiDebit.WeirSubmergedLarinier);
         this.prms.CdWSL.visible = true;
     }
 
diff --git a/src/structure/structure_weir_villemonte.ts b/src/structure/structure_weir_villemonte.ts
index 8df84235..e701fd24 100644
--- a/src/structure/structure_weir_villemonte.ts
+++ b/src/structure/structure_weir_villemonte.ts
@@ -10,7 +10,7 @@ export class StructureWeirVillemonte extends StructureWeirFree {
 
     constructor(prms: RectangularStructureParams, dbg: boolean = false) {
         super(prms, dbg);
-        this._loiDebit = LoiDebit.WeirVillemonte;
+        this.setLoiDebit(LoiDebit.WeirVillemonte);
     }
 
     public Calc(sVarCalc: string, rInit?: number): Result {
-- 
GitLab