Readded structure for Mot and json file read

main
Cemal Odabasioglu 2023-12-06 09:29:03 +01:00
commit 4caef75230
1 changed files with 48 additions and 1 deletions

View File

@ -22,7 +22,11 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.window.*
import database.DatabaseManager
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
import java.io.File
import java.io.FileWriter
import java.io.IOException
import kotlinx.serialization.json.Json
import kotlinx.serialization.encodeToString
@ -122,7 +126,21 @@ fun App() {
println("Open command cancelled by user.")
}
},
onExporterClick = { /* Action d'export */ },
onExporterClick = {
val fileDialog = FileDialog(Frame(), "Save as CSV", FileDialog.SAVE)
fileDialog.file = "glossaire_exporte.csv" // Initial file name
fileDialog.isVisible = true
val selectedFile = fileDialog.file
val selectedDirectory = fileDialog.directory
if (selectedFile != null) {
val csvFilePath = "$selectedDirectory$selectedFile"
val jsonFilePath = "glossaire.json" // Replace with the actual path to your JSON file
println("Exporting to: $csvFilePath")
exportToCSV(jsonFilePath, csvFilePath)
} else {
println("Export command cancelled by user.")
}
},
onRetourClick = { currentPage.value = "accueil" }
)
}
@ -190,6 +208,35 @@ fun App() {
}
}
fun exportToCSV(jsonFilePath: String, csvFilePath: String) {
val glossary = readJsonFile(jsonFilePath)
val csvContent = buildString {
appendLine("nom,description,contextePrincipal,contexte2,lieA,synonyme,antonyme")
glossary.forEach { entry ->
appendLine(
"${entry["nom"]},${entry["description"]},${entry["contextePrincipal"]}," +
"${entry["contexte2"]},${entry["lieA"]},${entry["synonyme"]},${entry["antonyme"]}"
)
}
}
try {
FileWriter(csvFilePath).use { fileWriter ->
fileWriter.write(csvContent)
}
} catch (e: IOException) {
e.printStackTrace()
}
}
fun readJsonFile(filePath: String): List<Map<String, String>> {
return try {
val content = File(filePath).readText(Charsets.UTF_8)
Json.decodeFromString(content)
} catch (e: Exception) {
e.printStackTrace()
emptyList()
}
}
fun importCSV(cheminFichier: String) {
val file = File(cheminFichier).readText(Charsets.UTF_8)