From 649b9c3572bea35052df6979012ea9db80ee47e2 Mon Sep 17 00:00:00 2001
From: Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
Date: Thu, 21 Jul 2022 17:37:40 +0200
Subject: [PATCH] [FIX] Correction de bugs divers

---
 CHANGELOG.md                                  |  4 ++
 README.md                                     | 31 ++++++-----
 .../unicaen/db-import/js/unicaen-db-import.js |  1 +
 .../Controller/SynchroController.php          | 15 +++++-
 .../ORM/Event/Listeners/SourceListener.php    |  2 +-
 .../Service/Database/DatabaseService.php      |  3 ++
 .../Service/Facade/SynchroFacadeService.php   | 51 ++++++++++++++++++-
 view/unicaen-db-import/import/index.phtml     |  3 ++
 view/unicaen-db-import/import/voir.phtml      |  3 ++
 view/unicaen-db-import/log/index.phtml        |  2 +-
 view/unicaen-db-import/synchro/index.phtml    |  3 ++
 view/unicaen-db-import/synchro/voir.phtml     |  3 ++
 12 files changed, 103 insertions(+), 18 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 503a728..cd5a956 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,10 @@
 Changelog
 =========
 
+5.2.0
+-----
+- [FIX] Correction de divers bugs.
+
 5.1.1
 -----
 - [FIX] Import : seules les données issues de la source concernée doivent être préalablement purgées, pas toute la table. 
diff --git a/README.md b/README.md
index 6b3d7e1..fe5086e 100644
--- a/README.md
+++ b/README.md
@@ -399,13 +399,14 @@ return [
             [
                 'name' => "IMPORT_SIMPLE",
                 'source' => [
-                    'code'               => 'UTILISATEUR_OCTOPUS',
+                    'code'               => 'OCTOPUS', // DOIT correspondre à un `CODE` de la table `SOURCE`
+                    'name'               => 'UTILISATEUR_OCTOPUS',
                     'table'              => 'UTILISATEUR', 
                     'connection'         => 'orm_B',
                     'source_code_column' => 'CODE',
                 ],
                 'destination' => [
-                    'code'               => 'TABLE UTILISATEUR LOCALE',
+                    'name'               => 'TABLE UTILISATEUR LOCALE',
                     'table'              => 'UTILISATEUR',
                     'connection'         => 'orm_A',
                     'source_code_column' => 'CODE',
@@ -434,12 +435,13 @@ return [
                 'name' => "IMPORT_REGIONS_API",
                 'source' => [
                     'code'               => 'geo_api', // DOIT correspondre à un `CODE` de la table `SOURCE`
+                    'name'               => 'API Geo',
                     'select'             => '/regions?fields=nom,code',
                     'connection'         => 'geo',
                     'source_code_column' => 'code',
                 ],
                 'destination' => [
-                    'code'               => 'Table REGION',
+                    'name'               => 'Table REGION',
                     'table'              => 'REGION',
                     'connection'         => 'orm_A',
                     'source_code_column' => 'CODE',
@@ -471,13 +473,14 @@ return [
             [
                 'name' => "SYNCHRO_LOCALE_ROLE",
                 'source' => [
-                    'code'               => 'app',
+                    'code'               => 'APP', // DOIT correspondre à un `CODE` de la table `SOURCE`
+                    'name'               => 'Roles',
                     'select'             => 'SELECT CODE, LIBELLE, DESCRIPTION FROM SRC_ROLE',
                     'connection'         => 'orm_A',
                     'source_code_column' => 'CODE',
                 ],
                 'destination' => [
-                    'code'               => 'Table ROLE',
+                    'name'               => 'Table ROLE',
                     'table'              => 'ROLE',
                     'connection'         => 'orm_A',
                     'source_code_column' => 'CODE',
@@ -515,13 +518,14 @@ return [
             [
                 'name' => "SYNCHRO_BDD_EXTERNE",
                 'source' => [
-                    'code'               => 'UTILISATEUR_OCTOPUS',
+                    'code'               => 'OCTOPUS', // DOIT correspondre à un `CODE` de la table `SOURCE`
+                    'name'               => 'UTILISATEUR_OCTOPUS',
                     'select'             => 'SELECT CODE, NOM, PRENOM, NAISSANCE FROM UTILISATEUR',
                     'connection'         => 'orm_B',
                     'source_code_column' => 'CODE',
                 ],
                 'destination' => [
-                    'code'               => 'Table UTILISATEUR',
+                    'name'               => 'Table UTILISATEUR',
                     'table'              => 'UTILISATEUR',
                     'connection'         => 'orm_A',
                     'source_code_column' => 'CODE',
@@ -559,12 +563,13 @@ return [
                 'name' => "SYNCHRO_COMMUNES",
                 'source' => [
                     'code'               => 'geo_api', // DOIT correspondre à un `CODE` de la table `SOURCE`
+                    'name'               => 'geo_api',
                     'select'             => '/departements/63/communes?fields=nom,code',
                     'connection'         => 'geo',
                     'source_code_column' => 'code',
                 ],
                 'destination' => [
-                    'code'               => 'Table locale',
+                    'name'               => 'Table locale',
                     'table'              => 'REGION',
                     'connection'         => 'orm_A',
                     'source_code_column' => 'CODE',
@@ -619,13 +624,14 @@ return [
             [
                 'name' => "IMPORT_PRÉALABLE", // Importation des données externes dans une table intermédiaire
                 'source' => [
-                    'code'               => 'FORMATION_FCA',
+                    'code'               => 'FCA', // DOIT correspondre à un `CODE` de la table `SOURCE`
+                    'name'               => 'FORMATION_FCA',
                     'select'             => 'SELECT ID, CODE, NOM, COMPOSANTE_CODE FROM FORMATION',
                     'connection'         => 'orm_B',
                     'source_code_column' => 'ID',
                 ],
                 'destination' => [
-                    'code'               => 'Table TMP_FORMATION locale',
+                    'name'               => 'Table TMP_FORMATION locale',
                     'table'              => 'TMP_FORMATION',
                     'connection'         => 'orm_A',
                     'source_code_column' => 'ID',
@@ -636,13 +642,14 @@ return [
             [
                 'name' => "SYNCHRO_FINALE", // Synchro des données mise en forme vers la table finale
                 'source' => [
-                    'code'               => 'TABLE SRC_FORMATION DE MA BASE A UTILISANT TMP_FORMATION',
+                    'code'               => 'APP', // DOIT correspondre à un `CODE` de la table `SOURCE`
+                    'name'               => 'TABLE SRC_FORMATION DE MA BASE A UTILISANT TMP_FORMATION',
                     'select'             => 'SELECT * FROM SRC_FORMATION',
                     'connection'         => 'orm_A',
                     'source_code_column' => 'ID',
                 ],
                 'destination' => [
-                    'code'               => 'TABLE FORMATION DE MA BASE A',
+                    'name'               => 'TABLE FORMATION DE MA BASE A',
                     'table'              => 'FORMATION',
                     'connection'         => 'orm_A',
                     'source_code_column' => 'ID',
diff --git a/public/unicaen/db-import/js/unicaen-db-import.js b/public/unicaen/db-import/js/unicaen-db-import.js
index 02fa340..e2e7d34 100644
--- a/public/unicaen/db-import/js/unicaen-db-import.js
+++ b/public/unicaen/db-import/js/unicaen-db-import.js
@@ -58,6 +58,7 @@ function installLancementMultiple()
         } else {
             btn.addClass('disabled');
         }
+        if(urlPattern  === undefined){return;}
         btn.prop("href", urlPattern.replace('__names__', generateNames($checkboxes)));
     }
 }
\ No newline at end of file
diff --git a/src/UnicaenDbImport/Controller/SynchroController.php b/src/UnicaenDbImport/Controller/SynchroController.php
index 3b65aae..c4a540b 100644
--- a/src/UnicaenDbImport/Controller/SynchroController.php
+++ b/src/UnicaenDbImport/Controller/SynchroController.php
@@ -2,6 +2,7 @@
 
 namespace UnicaenDbImport\Controller;
 
+use Exception;
 use Laminas\Http\PhpEnvironment\Response;
 use Laminas\Mvc\Controller\AbstractActionController;
 use Laminas\View\Model\JsonModel;
@@ -42,7 +43,12 @@ class SynchroController extends AbstractActionController
         foreach ($synchros as $synchro) {
             $importLog = $this->importLogService->findLastLogForSynchro($synchro);
             $importLogs[$synchro->getName()] = $importLog;
-            $diffCounts[$synchro->getName()] = $this->synchroService->fetchSynchroDiffCount($synchro);
+            try{
+                $diffCounts[$synchro->getName()] = $this->synchroService->fetchSynchroDiffCount($synchro);
+            }
+            catch (Exception $e){
+                $this->flashMessenger()->addErrorMessage(sprintf("Erreur rencontrée lors du calcul des différences pour la syncho %s", $synchro));
+            }
         }
 
         return [
@@ -62,7 +68,12 @@ class SynchroController extends AbstractActionController
     {
         $name = $this->params('name');
         $synchro = $this->synchroService->getSynchroByName($name);
-        $diffCount = $this->synchroService->fetchSynchroDiffCount($synchro);
+        try{
+            $diffCount = $this->synchroService->fetchSynchroDiffCount($synchro);
+        }
+        catch (Exception $e){
+            $this->flashMessenger()->addErrorMessage(sprintf("Erreur rencontrée lors du calcul des différences pour la syncho %s", $synchro));
+        }
         $importLog = $this->importLogService->findLastLogForSynchro($synchro);
 
         return [
diff --git a/src/UnicaenDbImport/ORM/Event/Listeners/SourceListener.php b/src/UnicaenDbImport/ORM/Event/Listeners/SourceListener.php
index 36a99e6..4d9f4e9 100644
--- a/src/UnicaenDbImport/ORM/Event/Listeners/SourceListener.php
+++ b/src/UnicaenDbImport/ORM/Event/Listeners/SourceListener.php
@@ -76,7 +76,7 @@ class SourceListener implements EventSubscriber
      * @param EntityManager $entityManager
      * @return Source
      */
-    private function fetchDefaultSource(EntityManager $entityManager)
+    private function fetchDefaultSource(EntityManager $entityManager): Source
     {
         if ($this->sourceEntityClass === null) {
             throw new RuntimeException("La classe d'entité Source n'a pas été spécifiée");
diff --git a/src/UnicaenDbImport/Service/Database/DatabaseService.php b/src/UnicaenDbImport/Service/Database/DatabaseService.php
index eb76d93..8df460c 100644
--- a/src/UnicaenDbImport/Service/Database/DatabaseService.php
+++ b/src/UnicaenDbImport/Service/Database/DatabaseService.php
@@ -468,6 +468,9 @@ class DatabaseService
                     "les données doivent contenir une colonne SOURCE_ID ou source_id");
             }
         }
+        else{
+            $row['SOURCE_ID'] = $sourceCode;
+        }
 
         return $row;
     }
diff --git a/src/UnicaenDbImport/Service/Facade/SynchroFacadeService.php b/src/UnicaenDbImport/Service/Facade/SynchroFacadeService.php
index 8cfc7dc..66e397a 100644
--- a/src/UnicaenDbImport/Service/Facade/SynchroFacadeService.php
+++ b/src/UnicaenDbImport/Service/Facade/SynchroFacadeService.php
@@ -64,9 +64,33 @@ class SynchroFacadeService extends AbstractFacadeService
      */
     public function fetchSynchroDiff(): array
     {
+        $intermediateTable = null;
+
+        if ($this->requiresIntermediateTable) {
+            $this->fetchSource();
+            $intermediateTable = $this->destination->getIntermediateTable();
+            if ($this->destination->getIntermediateTableAutoDrop()) {
+                $this->logger->info("Suppression de la table intermédiaire $intermediateTable");
+                $this->databaseService->dropIntermediateTable($intermediateTable);
+            } else {
+                $this->databaseService->checkIntermediateTableNotExists($intermediateTable);
+            }
+
+            $this->logger->info("Création de la table intermédiaire $intermediateTable");
+            $this->databaseService->createIntermediateTable($intermediateTable);
+            $this->logger->info("Peuplement de la table intermédiaire $intermediateTable");
+            $this->databaseService->populateTableFromSource($intermediateTable, false);
+        }
+
         $this->recreateDiffView();
+        $result = $this->databaseService->fetchDiffView();
 
-        return $this->databaseService->fetchDiffView();
+        if ($this->requiresIntermediateTable) {
+            $this->logger->info("Suppression de la table intermédiaire");
+            $this->databaseService->dropIntermediateTable($intermediateTable);
+        }
+
+        return $result;
     }
 
     /**
@@ -75,9 +99,32 @@ class SynchroFacadeService extends AbstractFacadeService
      */
     public function fetchSynchroDiffCount(): int
     {
+        $intermediateTable = null;
+
+        if ($this->requiresIntermediateTable) {
+            $this->fetchSource();
+            $intermediateTable = $this->destination->getIntermediateTable();
+            if ($this->destination->getIntermediateTableAutoDrop()) {
+                $this->logger->info("Suppression de la table intermédiaire $intermediateTable");
+                $this->databaseService->dropIntermediateTable($intermediateTable);
+            } else {
+                $this->databaseService->checkIntermediateTableNotExists($intermediateTable);
+            }
+
+            $this->logger->info("Création de la table intermédiaire $intermediateTable");
+            $this->databaseService->createIntermediateTable($intermediateTable);
+            $this->logger->info("Peuplement de la table intermédiaire $intermediateTable");
+            $this->databaseService->populateTableFromSource($intermediateTable, false);
+        }
+
         $this->recreateDiffView();
+        $result = $this->databaseService->fetchDiffViewCount();
 
-        return $this->databaseService->fetchDiffViewCount();
+        if ($this->requiresIntermediateTable) {
+            $this->logger->info("Suppression de la table intermédiaire");
+            $this->databaseService->dropIntermediateTable($intermediateTable);
+        }
+        return $result;
     }
 
     /**
diff --git a/view/unicaen-db-import/import/index.phtml b/view/unicaen-db-import/import/index.phtml
index 43fa49e..8bb1395 100644
--- a/view/unicaen-db-import/import/index.phtml
+++ b/view/unicaen-db-import/import/index.phtml
@@ -162,6 +162,9 @@ $lancable = $this->isAllowed(ImportPrivilege::getResourceId(ImportPrivilege::LAN
     $(function() {
         // `conn` est de type `WebSocket` (cf. https://developer.mozilla.org/fr/docs/Web/API/WebSocket)
         let conn = $("#livelog-div").data('conn');
+        if (conn === undefined) {
+            return;
+        }
         conn.addEventListener('open', function(e) {
             $("#a-lancer-multiple").on('click', function(event) {
                 let trigger = $(this).addClass('disabled');
diff --git a/view/unicaen-db-import/import/voir.phtml b/view/unicaen-db-import/import/voir.phtml
index 4c4f5a8..b553f4b 100644
--- a/view/unicaen-db-import/import/voir.phtml
+++ b/view/unicaen-db-import/import/voir.phtml
@@ -72,6 +72,9 @@ $lancable = $this->isAllowed(ImportPrivilege::getResourceId(ImportPrivilege::LAN
     $(function() {
         // `conn` est de type `WebSocket` (cf. https://developer.mozilla.org/fr/docs/Web/API/WebSocket)
         let conn = $("#livelog-div").data('conn');
+        if (conn === undefined) {
+            return;
+        }
         conn.addEventListener('open', function(e) {
             $("#lancer").on('click', function(event) {
                 let trigger = $(this).addClass('disabled');
diff --git a/view/unicaen-db-import/log/index.phtml b/view/unicaen-db-import/log/index.phtml
index 13d7e41..793c1a1 100644
--- a/view/unicaen-db-import/log/index.phtml
+++ b/view/unicaen-db-import/log/index.phtml
@@ -121,7 +121,7 @@ $synchroConsultable = $this->isAllowed(SynchroPrivilege::getResourceId(SynchroPr
 
 <?php endif ?>
 
-<?php echo $paginationControl = $this->paginationControl($importLogs, 'sliding', 'paginator.phtml', ['route' => 'unicaen-db-import/log']) ?>
+<?php echo $paginationControl = $this->paginationControl($importLogs, 'sliding', 'unicaen-db-import/paginator', ['route' => 'unicaen-db-import/log']) ?>
 
 <?php echo $this->partial('unicaen-db-import/paginator'); ?>
 
diff --git a/view/unicaen-db-import/synchro/index.phtml b/view/unicaen-db-import/synchro/index.phtml
index 3761577..3c13037 100644
--- a/view/unicaen-db-import/synchro/index.phtml
+++ b/view/unicaen-db-import/synchro/index.phtml
@@ -188,6 +188,9 @@ $lancable = $this->isAllowed(SynchroPrivilege::getResourceId(SynchroPrivilege::L
     $(function() {
         // `conn` est de type `WebSocket` (cf. https://developer.mozilla.org/fr/docs/Web/API/WebSocket)
         let conn = $("#livelog-div").data('conn');
+        if (conn === undefined) {
+            return;
+        }
         conn.addEventListener('open', function(e) {
             $("#a-lancer-multiple").on('click', function(event) {
                 let trigger = $(this).addClass('disabled');
diff --git a/view/unicaen-db-import/synchro/voir.phtml b/view/unicaen-db-import/synchro/voir.phtml
index d5f3def..435a42d 100644
--- a/view/unicaen-db-import/synchro/voir.phtml
+++ b/view/unicaen-db-import/synchro/voir.phtml
@@ -86,6 +86,9 @@ $lancable = $this->isAllowed(SynchroPrivilege::getResourceId(SynchroPrivilege::L
     $(function() {
         // `conn` est de type `WebSocket` (cf. https://developer.mozilla.org/fr/docs/Web/API/WebSocket)
         let conn = $("#livelog-div").data('conn');
+        if (conn === undefined) {
+            return;
+        }
         conn.addEventListener('open', function(e) {
             $("#lancer").on('click', function(event) {
                 let trigger = $(this).addClass('disabled');
-- 
GitLab