Update csv export

main
CAPEL Maxime 2023-12-05 14:42:06 +01:00
parent a09b749133
commit dfa960e968
1 changed files with 31 additions and 46 deletions

View File

@ -21,18 +21,14 @@ import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.window.* import androidx.compose.ui.window.*
import database.DatabaseManager import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put import kotlinx.serialization.json.put
import java.io.File import java.io.File
import java.io.FileWriter import java.io.FileWriter
import java.io.IOException import java.io.IOException
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
val connection = DatabaseManager.getConnection()
val customRedColor = Color(0xFFB70D1B) val customRedColor = Color(0xFFB70D1B)
@OptIn(ExperimentalComposeUiApi::class) @OptIn(ExperimentalComposeUiApi::class)
@ -124,23 +120,24 @@ fun App() {
} }
}, },
onExporterClick = { onExporterClick = {
val fileDialog = FileDialog(Frame(), "Save as", FileDialog.SAVE) val fileDialog = FileDialog(Frame(), "Save as CSV", FileDialog.SAVE)
fileDialog.file = "GlossaireExporte.csv" fileDialog.file = "glossaire_exporte.csv" // Initial file name
fileDialog.isVisible = true fileDialog.isVisible = true
val selectedFile = fileDialog.file val selectedFile = fileDialog.file
val selectedDirectory = fileDialog.directory val selectedDirectory = fileDialog.directory
if (selectedFile != null) { if (selectedFile != null) {
val csvFilePath = selectedDirectory + selectedFile val csvFilePath = "$selectedDirectory$selectedFile"
val jsonFilePath = "glossaire.json" // Replace with the actual path to your JSON file
println("Exporting to: $csvFilePath") println("Exporting to: $csvFilePath")
exportJsonToCSV("glossaire.json", csvFilePath) exportToCSV(jsonFilePath, csvFilePath)
} else { } else {
println("Export command cancelled by user.") println("Export command cancelled by user.")
} }
} },
,
onRetourClick = { currentPage.value = "accueil" } onRetourClick = { currentPage.value = "accueil" }
) )
} }
@ -208,48 +205,36 @@ fun App() {
} }
} }
fun exportJsonToCSV(jsonFilePath: String, csvFilePath: String) { fun exportToCSV(jsonFilePath: String, csvFilePath: String) {
try { val glossary = readJsonFile(jsonFilePath)
// Read JSON file
val jsonContent = File(jsonFilePath).readText(Charsets.UTF_8)
val glossaryData = Json.parseToJsonElement(jsonContent)
if (glossaryData is JsonObject) {
val header = glossaryData.keys.toList()
val csvContent = buildString { val csvContent = buildString {
// Append the header appendLine("nom,description,contextePrincipal,contexte2,lieA,synonyme,antonyme")
append(header.joinToString(separator = ";")) glossary.forEach { entry ->
append("\n") appendLine(
"${entry["nom"]},${entry["description"]},${entry["contextePrincipal"]}," +
// Append the data "${entry["contexte2"]},${entry["lieA"]},${entry["synonyme"]},${entry["antonyme"]}"
appendJsonToCSV(glossaryData, this) )
}
} }
// Write to CSV file try {
File(csvFilePath).writeText(csvContent, Charsets.UTF_8) FileWriter(csvFilePath).use { fileWriter ->
println("Export successful. File saved at: $csvFilePath") fileWriter.write(csvContent)
} else {
println("Invalid JSON file format.")
} }
} catch (e: IOException) { } catch (e: IOException) {
println("Error exporting data: ${e.message}") e.printStackTrace()
} }
} }
fun appendJsonToCSV(json: JsonObject, stringBuilder: StringBuilder) { fun readJsonFile(filePath: String): List<Map<String, String>> {
val values = json.values.map { return try {
when (it) { val content = File(filePath).readText(Charsets.UTF_8)
is JsonPrimitive -> it.content Json.decodeFromString(content)
is JsonObject -> { } catch (e: Exception) {
val nestedValues = buildString { appendJsonToCSV(it, this) } e.printStackTrace()
"\"$nestedValues\"" emptyList()
} }
else -> ""
}
}
stringBuilder.append(values.joinToString(separator = ";"))
stringBuilder.append("\n")
} }
fun importCSV(cheminFichier: String) { fun importCSV(cheminFichier: String) {