Skip to content

Commit

Permalink
Merge pull request #96 from abes-esr/ITEM-400-back-controle-lexistenc…
Browse files Browse the repository at this point in the history
…e-dun-fichier-de-sauvegarde-csv-avant-ecriture

FIX ITEM-400-back-controle-lexistence-dun-fichier-de-sauvegarde-csv-a…
  • Loading branch information
SamuelQuetin authored Nov 27, 2024
2 parents 29fa1d0 + d8580d4 commit 1598915
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.*;
import java.nio.file.Path;
import java.util.List;

Expand Down Expand Up @@ -111,13 +108,25 @@ public void checkFileContent(Demande d) throws FileCheckingException, IOExceptio
}

public void writeHeader() {
try (FileWriter fw = new FileWriter(this.getPath().resolve(this.getFilename()).toString(), true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)) {
// ajout de la ligne
out.println(String.join(";", this.referenceService.constructHeaderCsv()));
if (!isHeaderExist()) {
try (FileWriter fw = new FileWriter(this.getPath().resolve(this.getFilename()).toString(), true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)) {
// ajout de la ligne
out.println(String.join(";", this.referenceService.constructHeaderCsv()));
} catch (IOException ex) {
throw new StorageException(Constant.ERR_FILE_WRITING + "de sauvegarde csv");
}
}
}

private boolean isHeaderExist() {
try (FileReader fr = new FileReader(this.getPath().resolve(this.getFilename()).toString());
BufferedReader br = new BufferedReader(fr)) {
String line = br.readLine();
return line != null && line.startsWith("TYPE (008);PPN;");
} catch (IOException ex) {
throw new StorageException("Impossible d'écrire dans le fichier de sauvegarde csv");
throw new StorageException(Constant.ERR_FILE_READING + " de sauvegarde csv");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public class Constant implements Serializable {
public static final String ERR_FILE_STORAGE_FILE = "Echec de stockage du fichier suivant : ";
public static final String ERR_FILE_STORAGE_FILE_UNREADABLE = "Fichier illisible, verifier que le format du fichier est bien un fichier texte.";
public static final String ERR_FILE_READING = "Ne peut pas lire le fichier : ";
public static final String ERR_FILE_WRITING = "Echec de l'écriture dans le fichier ";

/**Specific errors on file checking*/
public static final String ERR_FILE_LIGNE_ANORNALE = "L'en-tête du fichier est non conforme : il contient des caractères parasites. Merci de consulter la documentation utilisateur à cette adresse : <a href=\"http://documentation.abes.fr/aideitem/index.html\" target=\"_blank\" style=\"color:white\">http://documentation.abes.fr/aideitem/index.html</a>";
Expand Down

0 comments on commit 1598915

Please # to comment.