From 313183141a145d3e8520680e590143209297befe Mon Sep 17 00:00:00 2001
From: lorenz <stu203404@mail.uni-kiel.de>
Date: Sun, 19 Jun 2022 16:34:27 +0200
Subject: [PATCH] Csv export: Add commas to the column names

Add method addQuotationMarks to IOHandler.kt
Adapt the associated test
---
 .../kotlin/rocks/theodolite/core/IOHandler.kt | 27 +++++++++++++------
 .../rocks/theodolite/core/IOHandlerTest.kt    |  8 +++---
 2 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/theodolite/src/main/kotlin/rocks/theodolite/core/IOHandler.kt b/theodolite/src/main/kotlin/rocks/theodolite/core/IOHandler.kt
index 8721ea9d4..78b353a1e 100644
--- a/theodolite/src/main/kotlin/rocks/theodolite/core/IOHandler.kt
+++ b/theodolite/src/main/kotlin/rocks/theodolite/core/IOHandler.kt
@@ -71,21 +71,32 @@ class IOHandler {
     fun writeToCSVFile(fileURL: String, data: List<List<String>>, columns: List<String>) {
         val outputFile = File("$fileURL.csv")
         PrintWriter(outputFile).use { pw ->
-            pw.println(columns.joinToString(separator = ","))
+
+            val writeColumns = addQuotationMarks(columns)
+            pw.println(writeColumns.joinToString(separator = ","))
+
             data.forEach{ row ->
-                val writeRow = row.toMutableList()
-                // change entry if it contains a comma
-                writeRow.forEachIndexed { index, entry ->
-                    if (entry.contains(",")){
-                        writeRow[index] = "\"" + entry + "\""
-                    }
-                }
+                val writeRow = addQuotationMarks(row)
                 pw.println(writeRow.joinToString(separator = ","))
             }
         }
         logger.info { "Wrote CSV file: $fileURL to ${outputFile.absolutePath}." }
     }
 
+    /**
+     * For a list of Strings:
+     * Adds quotation marks around strings that contain a comma
+     */
+    private fun addQuotationMarks(stringList: List<String> ): List<String> {
+        val stringMutableList = stringList.toMutableList()
+        stringMutableList.forEachIndexed { index, entry ->
+            if (entry.contains(",")){
+                stringMutableList[index] = "\"" + entry + "\""
+            }
+        }
+        return stringMutableList
+    }
+
     /**
      * Write to text file
      *
diff --git a/theodolite/src/test/kotlin/rocks/theodolite/core/IOHandlerTest.kt b/theodolite/src/test/kotlin/rocks/theodolite/core/IOHandlerTest.kt
index acdf3aa6e..938eeaa7a 100644
--- a/theodolite/src/test/kotlin/rocks/theodolite/core/IOHandlerTest.kt
+++ b/theodolite/src/test/kotlin/rocks/theodolite/core/IOHandlerTest.kt
@@ -43,12 +43,13 @@ internal class IOHandlerTest {
         temporaryFolder.create()
         val folder = temporaryFolder.newFolder(FOLDER_URL)
 
+        val columns = listOf("Fruit", "Color")
+
         val testContent = listOf(
             listOf("apples", "red"),
             listOf("bananas", "yellow"),
             listOf("avocado", "brown")
         )
-        val columns = listOf("Fruit", "Color")
 
         IOHandler().writeToCSVFile(
             fileURL = "${folder.absolutePath}/test-file",
@@ -74,7 +75,8 @@ internal class IOHandlerTest {
         temporaryFolder.create()
         val folder = temporaryFolder.newFolder(FOLDER_URL)
 
-        val columns = listOf("Fruit", "Color")
+        val columns = listOf("Fruit, Fruit2", "Color")
+        val expectedColumns = listOf("\"Fruit, Fruit2\"", "Color")
 
         val testContent = listOf(
             listOf("apples, paprika", "red"),
@@ -95,7 +97,7 @@ internal class IOHandlerTest {
         )
 
         // construct string from the columns
-        var expected = columns.stream().collect(Collectors.joining(","))
+        var expected = expectedColumns.stream().collect(Collectors.joining(","))
 
         // add values from the expectedContent to expected string
         expectedContent.forEach{
-- 
GitLab