From 5eab063f0914895424de094fa624d1e8ac7ceb20 Mon Sep 17 00:00:00 2001
From: Riegel <alexander.riegel@hft-stuttgart.de>
Date: Mon, 27 Jan 2025 14:35:53 +0100
Subject: [PATCH] Refactor: Rework export of ZipEntryManager

Changed the export functionality of ZipEntryManager to exporting the
validation reports of validation entries, instead of exporting carbon
copies of the input Zip files' CityGML files.
---
 .../CityDoctorLocalization.properties         |  1 +
 .../CityDoctorLocalization_de.properties      |  1 +
 .../citydoctor2/gui/CityDoctorController.java | 42 +++++++++++++++++++
 .../citydoctor2/gui/ZipEntryManager.java      | 22 ++++++----
 4 files changed, 58 insertions(+), 8 deletions(-)

diff --git a/CityDoctorParent/CityDoctorModel/src/main/resources/CityDoctorLocalization.properties b/CityDoctorParent/CityDoctorModel/src/main/resources/CityDoctorLocalization.properties
index 613ac54..7834913 100644
--- a/CityDoctorParent/CityDoctorModel/src/main/resources/CityDoctorLocalization.properties
+++ b/CityDoctorParent/CityDoctorModel/src/main/resources/CityDoctorLocalization.properties
@@ -20,6 +20,7 @@ CityDoctorController.noDatamodel=Datamodel is null, no checks could be done
 CityDoctorController.noSourceFile=Source file is null, no checks could be done
 CityDoctorController.noZipFile=ZIP archive is null, no checks could be done
 CityDoctorController.exportSuccess=Successfully exported feature
+CityDoctorController.saveZipArchiveReports=Successfully exported validation reports!
 ExceptionDialog.stacktrace=The exception stacktrace was:
 FilterPane.buildings=Buildings
 FilterPane.bridges=Bridges
diff --git a/CityDoctorParent/CityDoctorModel/src/main/resources/CityDoctorLocalization_de.properties b/CityDoctorParent/CityDoctorModel/src/main/resources/CityDoctorLocalization_de.properties
index 2ca4bbb..864c13b 100644
--- a/CityDoctorParent/CityDoctorModel/src/main/resources/CityDoctorLocalization_de.properties
+++ b/CityDoctorParent/CityDoctorModel/src/main/resources/CityDoctorLocalization_de.properties
@@ -18,6 +18,7 @@ CityDoctorController.noDatamodel=Datenmodell ist null, keine Pr\u00fcfungen konn
 CityDoctorController.noSourceFile=Quelldatei ist null, keine Pr\u00fcfungen konnten ausgef\u00fchrt werden
 CityDoctorController.noZipFile=ZIP-Archiv ist null, keine Pr\u00fcfungen konnten ausgef\u00fchrt werden
 CityDoctorController.exportSuccess=Feature export erfolgreich abgeschlossen
+CityDoctorController.saveZipArchiveReports=Validierungsberichte erfolgreich exportiert!
 ExceptionDialog.stacktrace=Der Stacktrace des Fehlers war:
 FilterPane.buildings=Geb\u00e4ude
 FilterPane.bridges=Br\u00fccken
diff --git a/CityDoctorParent/Extensions/CityDoctorGUI/src/main/java/de/hft/stuttgart/citydoctor2/gui/CityDoctorController.java b/CityDoctorParent/Extensions/CityDoctorGUI/src/main/java/de/hft/stuttgart/citydoctor2/gui/CityDoctorController.java
index 6c71bdc..f8c1be3 100644
--- a/CityDoctorParent/Extensions/CityDoctorGUI/src/main/java/de/hft/stuttgart/citydoctor2/gui/CityDoctorController.java
+++ b/CityDoctorParent/Extensions/CityDoctorGUI/src/main/java/de/hft/stuttgart/citydoctor2/gui/CityDoctorController.java
@@ -11,6 +11,7 @@ import de.hft.stuttgart.citydoctor2.gui.table.ErrorStat;
 import de.hft.stuttgart.citydoctor2.gui.tree.*;
 import de.hft.stuttgart.citydoctor2.mapper.citygml3.GMLValidationHandler;
 import de.hft.stuttgart.citydoctor2.parser.*;
+import de.hft.stuttgart.citydoctor2.utils.ArchivePacker;
 import de.hft.stuttgart.citydoctor2.utils.Localization;
 import de.hft.stuttgart.citydoctor2.zip.CityGmlZipArchive;
 import de.hft.stuttgart.citydoctor2.zip.CityGmlZipEntry;
@@ -25,6 +26,7 @@ import javafx.scene.control.TreeItem;
 import javafx.scene.control.TreeView;
 import javafx.stage.FileChooser;
 import javafx.stage.FileChooser.ExtensionFilter;
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.FilenameUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
@@ -1011,6 +1013,46 @@ public class CityDoctorController {
 
     }
 
+    public void saveZipArchiveReports(File outputZipDirectory) {
+        if (zipArchive == null) {
+            return;
+        }
+        String directoryName = FilenameUtils.removeExtension(outputZipDirectory.getAbsolutePath());
+        Path tempOutput = Path.of(directoryName + File.separator);
+        File temp = null;
+        try {
+            temp = tempOutput.toFile();
+            for (CityGmlZipEntry entry : zipArchive.getEntries()) {
+                if (entry.getErrorType() != null || entry.getModel() == null) {
+                    continue;
+                }
+                model = entry.getModel();
+                if (model.isValidated()) {
+                    Checker checker = previousCheckers.get(model);
+                    Files.createDirectories(tempOutput.resolve(entry.getFullFileName()).getParent());
+                    checker.writeXmlReport(tempOutput.resolve(entry.getDisplayName() + "_report.xml").toString());
+                    checker.writePdfReport(tempOutput.resolve(entry.getDisplayName() + "_report.pdf").toString());
+                }
+            }
+            ArchivePacker.packAndDeleteDirectory(tempOutput.toString());
+            temp = null;
+            Alert alert = new Alert(Alert.AlertType.INFORMATION);
+            alert.setTitle("Export Report");
+            alert.setContentText(Localization.getText("CityDoctorController.saveZipArchiveReports"));
+            alert.showAndWait();
+        } catch (IOException e) {
+            logger.error(e);
+        } finally {
+            try {
+                if (temp != null) {
+                    FileUtils.deleteDirectory(temp);
+                }
+            } catch (IOException e) {
+                logger.error(e);
+            }
+        }
+    }
+
     void updateFeatureTrees() {
         updateTree(mainWindow.getBuildingsView().getRoot());
         updateTree(mainWindow.getVegetationView().getRoot());
diff --git a/CityDoctorParent/Extensions/CityDoctorGUI/src/main/java/de/hft/stuttgart/citydoctor2/gui/ZipEntryManager.java b/CityDoctorParent/Extensions/CityDoctorGUI/src/main/java/de/hft/stuttgart/citydoctor2/gui/ZipEntryManager.java
index 8d868c8..0678a08 100644
--- a/CityDoctorParent/Extensions/CityDoctorGUI/src/main/java/de/hft/stuttgart/citydoctor2/gui/ZipEntryManager.java
+++ b/CityDoctorParent/Extensions/CityDoctorGUI/src/main/java/de/hft/stuttgart/citydoctor2/gui/ZipEntryManager.java
@@ -287,15 +287,21 @@ public class ZipEntryManager {
         showReportBtn.setDisable(true);
 
         saveBtn.setOnAction(e -> {
-            FileChooser fileChooser = new FileChooser();
-            fileChooser.setTitle("Export Zip file");
-            FileChooser.ExtensionFilter zipFilter = new FileChooser.ExtensionFilter("Zip File", "*.zip");
-            fileChooser.getExtensionFilters().add(zipFilter);
-            File f = fileChooser.showSaveDialog(stage.getOwner());
-            if (f != null) {
-                ArchivePacker.packArchive(f.getAbsolutePath(), archive);
+            disableTaskButtons();
+            setEntryListLocked(true);
+            try {
+                FileChooser fileChooser = new FileChooser();
+                fileChooser.setTitle("Export Zip file");
+                FileChooser.ExtensionFilter zipFilter = new FileChooser.ExtensionFilter("Zip File", "*.zip");
+                fileChooser.getExtensionFilters().add(zipFilter);
+                File f = fileChooser.showSaveDialog(stage.getOwner());
+                if (f != null) {
+                    controller.saveZipArchiveReports(f);
+                }
+            } finally {
+                setEntryListLocked(false);
+                refresh();
             }
-
         });
         saveBtn.setDisable(true);
 
-- 
GitLab