1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
public void nettoyageEntetes(param1 param1, FileService fileService, properties properties) throws ReportPtgFileException {
int numberOfLinesToDelete = 0;
File file = Paths.get(
param1.getStep().getFlow().getParameterPath().getWorkingDirectory(),
param1.getStep().getFlow().getTriggerFilename()
).toFile();
try (BufferedReader bufferedReader = Files.newBufferedReader(file.toPath(), Charset.forName(Constants.PRH_DEFAULT_ENCODING))) {
String firstLine = bufferedReader.readLine();
String secondLine = bufferedReader.readLine();
String thirdLine = null;
if (!StringUtils.isEmpty(secondLine))
thirdLine = bufferedReader.readLine();
if (StringUtils.isEmpty(thirdLine) && StringUtils.isEmpty(firstLine))
numberOfLinesToDelete = 3;
else if (StringUtils.isEmpty(firstLine))
numberOfLinesToDelete = 1;
else if (!StringUtils.isEmpty(firstLine) && firstLine.contains("Titre"))
numberOfLinesToDelete = 1;
else if (!StringUtils.isEmpty(firstLine) && secondLine.contains("Titre"))
numberOfLinesToDelete = 2;
else if (!StringUtils.isEmpty(firstLine) && secondLine.contains(Constants.SPLIT_FIELD))
numberOfLinesToDelete = 1;
} catch (IOException e) {
throw ReportPtgFileException.build(Constants.PRH_ERROR_CODE_REJET)
.withMessage("Erreur de lecture lors de la vérification pour nettoyage: %s", e.getMessage())
.withData(param1.getStep().getFlow().getTriggerFilename());
}
if (numberOfLinesToDelete != 0) {
File tmpFile = Paths.get(param1.getStep().getFlow().getParameterPath().getWorkingDirectory(),
param1.getStep().getFlow().getTriggerFilename().concat(".temp")).toFile();
tmpFile.delete();
try (BufferedReader bufferedReader = Files.newBufferedReader(file.toPath(), Charset.forName(Constants.PRH_DEFAULT_ENCODING));
BufferedWriter bufferedWriter = Files.newBufferedWriter(tmpFile.toPath(), Charset.forName(Constants.PRH_DEFAULT_ENCODING))) {
for (int i = 0; i < numberOfLinesToDelete; i++) {
bufferedReader.readLine();
}
String line;
while ((line = bufferedReader.readLine()) != null) {
bufferedWriter.append(line);
bufferedWriter.newLine();
}
} catch (IOException e) {
throw ReportPtgFileException.build(Constants.PRH_ERROR_CODE_REJET)
.withMessage("Erreur de lecture lors du nettoyage : %s", e.getMessage())
.withData(param1.getStep().getFlow().getTriggerFilename());
}
File old = Paths.get(
param1.getStep().getFlow().getParameterPath().getWorkingDirectory(),
param1.getStep().getFlow().getTriggerFilename().concat(".old")
).toFile();
try {
Files.copy(file.toPath(), old.toPath());
Files.delete(file.toPath());
Files.copy(tmpFile.toPath(), file.toPath());
} catch (IOException e) {
// Utilisation de la fonction copyFile afin de récupérer les droits du répertoire parent (répertoire de Rejets)
fileService.copyFile(param1.getStep().getFlow().getParameterPath().getWorkingDirectory(),
Paths.get(properties.getRootDirectory(), Constants.PRH_REJETS_DIR).toString(),
param1.getStep().getFlow().getTriggerFilename());
throw new ServiceException("Méthode nettoyageEntetes - Nombre de lignes d'entête à supprimer > 0 - Erreur dans la copie / delete du fichier", e);
}
} |
Partager