diff --git a/build.gradle.kts b/build.gradle.kts index bf4b973..79f643a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -37,6 +37,8 @@ dependencies { implementation("io.ktor:ktor-html-builder:1.6.3") implementation("junit:junit:4.13.1") testImplementation("junit:junit:4.13.1") + implementation("org.apache.poi:poi:5.0.0") + implementation("org.apache.poi:poi-ooxml:5.0.0") diff --git a/src/main/kotlin/main/Main.kt b/src/main/kotlin/main/Main.kt index 11b27b8..59b7088 100644 --- a/src/main/kotlin/main/Main.kt +++ b/src/main/kotlin/main/Main.kt @@ -22,7 +22,14 @@ import java.awt.Frame import java.io.File import java.io.FileWriter import java.io.IOException +<<<<<<< src/main/kotlin/main/Main.kt +======= +import org.apache.poi.ss.usermodel.CellType +import org.apache.poi.ss.usermodel.Workbook +import org.apache.poi.xssf.usermodel.XSSFWorkbook +import java.io.FileInputStream +>>>>>>> src/main/kotlin/main/Main.kt val customRedColor = Color(0xFFB70D1B) @@ -57,15 +64,18 @@ fun homePage( Text("Glossaire") } - Button(onClick = onCodeAVerifierClick, + Button( + onClick = onCodeAVerifierClick, colors = ButtonDefaults.buttonColors( - backgroundColor = customRedColor, - contentColor = Color.White - ) ) { + backgroundColor = customRedColor, + contentColor = Color.White + ) + ) { Text("Code à Vérifier") } - Button(onClick = { /* Action de Comparer */ }, + Button( + onClick = { /* Action de Comparer */ }, colors = ButtonDefaults.buttonColors( backgroundColor = customRedColor, contentColor = Color.White @@ -90,28 +100,14 @@ fun app() { onCodeAVerifierClick = { currentPage.value = "choixLangage" } ) } + "glossaire" -> { glossairePage( onAjouterMotClick = { currentPage.value = "formulaire" }, onImporterClick = { - val fileDialog = FileDialog(Frame(), "Select a file", FileDialog.LOAD) - fileDialog.file = "Untitled.csv" // Initial file name - fileDialog.isMultipleMode = false // To enable selecting only one file - - fileDialog.setFile("*.csv") - fileDialog.isVisible = true - - val selectedFile = fileDialog.file - val selectedDirectory = fileDialog.directory - - if (selectedFile != null) { - val filePath = selectedDirectory + selectedFile - println("Opening: $filePath") - - importCSV(filePath) - - } else { - println("Open command cancelled by user.") + selectFile(setOf("csv", "xlsx")) { filePath -> + println("Importing file: $filePath") + importFile(filePath) } }, onExporterClick = { @@ -131,9 +127,11 @@ fun app() { onRetourClick = { currentPage.value = "accueil" } ) } + "formulaire" -> { formulairePage(onAnnulerClick = { currentPage.value = "glossaire" }) } + "choixLangage" -> { val pythonExtensions = setOf("py") val javaExtensions = setOf("java") @@ -143,7 +141,8 @@ fun app() { onPythonClick = { selectFile(pythonExtensions) { filePath -> println("Python file selected: $filePath") // Change by parser functions - } }, + } + }, onJavaClick = { selectFile(javaExtensions) { filePath -> println("Java file selected: $filePath") // Change by parser functions @@ -168,7 +167,6 @@ fun selectFile(extensions: Set, onFileSelected: (String) -> Unit) { fileDialog.isVisible = true - val selectedFile = fileDialog.file val selectedDirectory = fileDialog.directory @@ -208,7 +206,23 @@ fun exportToCSV(csvFilePath: String) { } } -fun importCSV(cheminFichier: String) { +fun importFile(cheminFichier: String) { + val fileExtension = File(cheminFichier).extension.lowercase() + + when { + fileExtension == "csv" -> { + importCSVFile(cheminFichier) + } + fileExtension == "xlsx" -> { + importXLSXFile(cheminFichier) + } + else -> { + println("Unsupported file format.") + } + } +} + +fun importCSVFile(cheminFichier: String) { val file = File(cheminFichier).readText(Charsets.UTF_8) val lines = file.split("\n") @@ -248,7 +262,63 @@ fun importCSV(cheminFichier: String) { } } +private fun importXLSXFile(cheminFichier: String) { + val workbook: Workbook + try { + FileInputStream(cheminFichier).use { fileInputStream -> + workbook = XSSFWorkbook(fileInputStream) + } + val sheet = workbook.getSheetAt(0) // Assuming the data is in the first sheet + + val headerRow = sheet.getRow(0) + val header = mutableListOf() + for (i in 0 until headerRow.lastCellNum) { + header.add(headerRow.getCell(i).stringCellValue) + } + + val dataLines = mutableListOf>() + + for (i in 1 until sheet.physicalNumberOfRows) { + val currentRow = sheet.getRow(i) + val data = mutableMapOf() + + for (j in 0 until currentRow.lastCellNum) { + val cell = currentRow.getCell(j) + val cellValue = when (cell.cellType) { + CellType.STRING -> cell.stringCellValue + CellType.NUMERIC -> cell.numericCellValue.toString() + else -> "" + } + data[header[j]] = cellValue + } + + dataLines.add(data) + } + + dataLines.forEach { line -> + // Check if the line is not empty or blank + if (line.isNotEmpty()) { + // Process each line as needed + val nouveauMot = Mot( + nom = line["Mot"] ?: "", + description = line["Description"] ?: "", + contextePrincipal = line["Contexte principal"] ?: "", + contexte2 = line["Contexte 2"] ?: "", + lieA = line["Lié à"] ?: "", + synonyme = line["Synonyme"] ?: "", + antonyme = line["Antonyme"] ?: "" + ) + + if (nouveauMot.nom.isNotBlank() && verifierChampsRequis(line)) { + ajouterMotAuGlossaire(nouveauMot) + } + } + } + } catch (e: IOException) { + e.printStackTrace() + } +} fun verifierChampsRequis(mot: Map): Boolean { //Vérifier que les headers sont égales aux champs requis @@ -317,7 +387,8 @@ fun choixLangagePage( colors = ButtonDefaults.buttonColors( backgroundColor = customRedColor, contentColor = Color.White - )) { + ) + ) { Text("Java") } @@ -326,7 +397,8 @@ fun choixLangagePage( colors = ButtonDefaults.buttonColors( backgroundColor = customRedColor, contentColor = Color.White - )) { + ) + ) { Text("JavaScript") } } @@ -342,7 +414,8 @@ fun choixLangagePage( colors = ButtonDefaults.buttonColors( backgroundColor = customRedColor, contentColor = Color.White - )) { + ) + ) { Text("Retour") } } @@ -379,7 +452,8 @@ fun glossairePage( colors = ButtonDefaults.buttonColors( backgroundColor = customRedColor, contentColor = Color.White - )) { + ) + ) { Text("Ajouter un mot") } @@ -388,8 +462,9 @@ fun glossairePage( colors = ButtonDefaults.buttonColors( backgroundColor = customRedColor, contentColor = Color.White - )) { - Text("Importer un fichier CSV") + ) + ) { + Text("Importer un fichier CSV ou XLSX") } Button( @@ -397,7 +472,8 @@ fun glossairePage( colors = ButtonDefaults.buttonColors( backgroundColor = customRedColor, contentColor = Color.White - )) { + ) + ) { Text("Exporter un fichier CSV") } @@ -414,17 +490,23 @@ fun glossairePage( colors = ButtonDefaults.buttonColors( backgroundColor = customRedColor, contentColor = Color.White - )) { + ) + ) { Text("Retour") } } } +<<<<<<< src/main/kotlin/main/Main.kt @OptIn(ExperimentalMaterialApi::class) +======= +>>>>>>> src/main/kotlin/main/Main.kt @Composable fun formulairePage(onAnnulerClick: () -> Unit) { + // State to track whether to show the snackbar + val snackbarVisibleState = remember { mutableStateOf(false) } val nom = remember { mutableStateOf("") } val description = remember { mutableStateOf("") } @@ -477,16 +559,12 @@ fun formulairePage(onAnnulerClick: () -> Unit) { verticalArrangement = Arrangement.spacedBy(8.dp), horizontalAlignment = Alignment.CenterHorizontally ) { - Text(text = "Nouveau contexte métier", style = MaterialTheme.typography.h5) + Text(text = "Nouveau mot", style = MaterialTheme.typography.h5) TextField( value = nom.value, onValueChange = { nom.value = it }, - label = { Text("Mot") }, - colors = TextFieldDefaults.textFieldColors( - focusedIndicatorColor = customRedColor, // Change this to the color you want - unfocusedIndicatorColor = Color.Gray // Change this to the color you want - ) + label = { Text("Mot *") }, ) TextField( @@ -494,18 +572,18 @@ fun formulairePage(onAnnulerClick: () -> Unit) { onValueChange = { description.value = it }, label = { Text("Description") }, colors = TextFieldDefaults.textFieldColors( - focusedIndicatorColor = customRedColor, // Change this to the color you want - unfocusedIndicatorColor = Color.Gray // Change this to the color you want + focusedIndicatorColor = customRedColor, + unfocusedIndicatorColor = Color.Gray ) ) TextField( value = contextePrincipal.value, onValueChange = { contextePrincipal.value = it }, - label = { Text("Contexte principal") }, + label = { Text("Contexte principal *") }, colors = TextFieldDefaults.textFieldColors( - focusedIndicatorColor = customRedColor, // Change this to the color you want - unfocusedIndicatorColor = Color.Gray // Change this to the color you want + focusedIndicatorColor = customRedColor, + unfocusedIndicatorColor = Color.Gray ) ) @@ -514,8 +592,8 @@ fun formulairePage(onAnnulerClick: () -> Unit) { onValueChange = { contexte2.value = it }, label = { Text("Contexte 2") }, colors = TextFieldDefaults.textFieldColors( - focusedIndicatorColor = customRedColor, // Change this to the color you want - unfocusedIndicatorColor = Color.Gray // Change this to the color you want + focusedIndicatorColor = customRedColor, + unfocusedIndicatorColor = Color.Gray ) ) @@ -524,8 +602,8 @@ fun formulairePage(onAnnulerClick: () -> Unit) { onValueChange = { lieA.value = it }, label = { Text("Lié à") }, colors = TextFieldDefaults.textFieldColors( - focusedIndicatorColor = customRedColor, // Change this to the color you want - unfocusedIndicatorColor = Color.Gray // Change this to the color you want + focusedIndicatorColor = customRedColor, + unfocusedIndicatorColor = Color.Gray ) ) @@ -534,8 +612,8 @@ fun formulairePage(onAnnulerClick: () -> Unit) { onValueChange = { synonyme.value = it }, label = { Text("Synonyme") }, colors = TextFieldDefaults.textFieldColors( - focusedIndicatorColor = customRedColor, // Change this to the color you want - unfocusedIndicatorColor = Color.Gray // Change this to the color you want + focusedIndicatorColor = customRedColor, + unfocusedIndicatorColor = Color.Gray ) ) @@ -544,8 +622,8 @@ fun formulairePage(onAnnulerClick: () -> Unit) { onValueChange = { antonyme.value = it }, label = { Text("Antonyme") }, colors = TextFieldDefaults.textFieldColors( - focusedIndicatorColor = customRedColor, // Change this to the color you want - unfocusedIndicatorColor = Color.Gray // Change this to the color you want + focusedIndicatorColor = customRedColor, + unfocusedIndicatorColor = Color.Gray ) ) @@ -566,20 +644,34 @@ fun formulairePage(onAnnulerClick: () -> Unit) { Button( onClick = { // Validation du formulaire + if (nom.value.isBlank() || contextePrincipal.value.isBlank()) { + // Show the snackbar when validation fails + snackbarVisibleState.value = true + return@Button + } val nouveauMot = Mot( - nom=nom.value, - description=description.value, - contextePrincipal=contextePrincipal.value, - contexte2=contexte2.value, - lieA=lieA.value, - synonyme=synonyme.value, - antonyme=antonyme.value + nom = nom.value, + description = description.value, + contextePrincipal = contextePrincipal.value, + contexte2 = contexte2.value, + lieA = lieA.value, + synonyme = synonyme.value, + antonyme = antonyme.value ) ajouterMotAuGlossaire(nouveauMot) openDialog.value = true + // Réinitialiser les champs après l'ajout + nom.value = "" + description.value = "" + contextePrincipal.value = "" + contexte2.value = "" + lieA.value = "" + synonyme.value = "" + antonyme.value = "" + }, colors = ButtonDefaults.buttonColors( backgroundColor = customRedColor, @@ -591,10 +683,24 @@ fun formulairePage(onAnnulerClick: () -> Unit) { } } + // Show the snackbar when the state is true + if (snackbarVisibleState.value) { + Snackbar( + modifier = Modifier.padding(16.dp), + action = { + Button(onClick = { snackbarVisibleState.value = false }) { + Text("OK") + } + } + ) { + Text("Les champs 'Mot' et 'Contexte principal' sont obligatoires.") + } + } } } } + fun chargerDonneesDepuisFichier(): List { //if file is empty, return empty list if (!File("glossaire.json").exists() || File("glossaire.json").length() == 0L) { @@ -609,20 +715,31 @@ fun chargerDonneesDepuisFichier(): List { } } +private val json = Json { prettyPrint = true } + fun sauvegarderDonneesDansFichier(listeMots: List) { try { - val content = Json.encodeToString(listeMots) + val content = json.encodeToString(listeMots) File("glossaire.json").writeText(content) } catch (e: IOException) { e.printStackTrace() - // Handle the exception as needed } } fun ajouterMotAuGlossaire(nouveauMot: Mot) { val listeMots = chargerDonneesDepuisFichier().toMutableList() + + // Vérifier si le mot existe déjà dans le glossaire + if (listeMots.any { it.nom.equals(nouveauMot.nom, ignoreCase = true) }) { + println("Le mot '${nouveauMot.nom}' existe déjà dans le glossaire. Ajout annulé.") + return + } + + // Ajouter le nouveau mot seulement s'il n'existe pas déjà listeMots.add(nouveauMot) sauvegarderDonneesDansFichier(listeMots) + + println("Mot ajouté avec succès : ${nouveauMot.nom}") }