Commit 78786f3a authored by marceteau's avatar marceteau
Browse files

correctifs changement de nom de fichiers en SEDA1

parent a858684b
......@@ -36,14 +36,17 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import javax.xml.bind.JAXBContext;
......@@ -141,7 +144,12 @@ public class Seda1ToSeda21Converter extends AbstractArchiveConverter {
*/
private InformationPackage informationPackage;
int archiveUnitCounter = 0;
int archiveUnitCounter = 0;
/**
* Compteur pour gérer les problèmes de fichiers avec le même checksum
*/
private Map<String, Integer> fileNameCounter = new HashMap<String, Integer>();
//variables liées au fichier properties
private static final String MAP_FILE_PLAN = "FilePlanPosition";
......@@ -455,7 +463,7 @@ public class Seda1ToSeda21Converter extends AbstractArchiveConverter {
for(KeywordType keyword : currentArchive.getContentDescription().getKeyword()) {
String tag;
if(keyword.getKeywordContent().getRole().trim().equalsIgnoreCase("")) {
if(keyword.getKeywordContent().getRole() == null || keyword.getKeywordContent().getRole().trim().equalsIgnoreCase("")) {
tag = keyword.getKeywordContent().getValue();
} else {
tag = keyword.getKeywordContent().getRole() + " : " + keyword.getKeywordContent().getValue();
......@@ -1046,7 +1054,9 @@ public class Seda1ToSeda21Converter extends AbstractArchiveConverter {
content.setStartDate(contentDescription.getOldestDate().toString());
}
if(!contentDescription.getKeyword().isEmpty()) {
if(!contentDescription.getKeyword().isEmpty()
&& properties.getProperty(KEYWORD_CONTENT) != null
&& properties.getProperty(KEYWORD_CONTENT).equalsIgnoreCase("true")) {
for(KeywordType keyword : contentDescription.getKeyword()) {
String tag;
if(keyword.getKeywordContent().getRole().trim().equalsIgnoreCase("")) {
......@@ -1184,7 +1194,7 @@ public class Seda1ToSeda21Converter extends AbstractArchiveConverter {
size = currentFile.length();
} else if(document.getAttachment().getFilename().equalsIgnoreCase("Content/" + this.archiveType)) {
//Le fichier n'est pas encore transféré dans le répertoire Content, il faut donc le peser à la racine
File archiveFile = this.rootFolder.resolve(Paths.get(this.archiveType)).toFile();
File archiveFile = this.rootFolder.resolve(Paths.get(fileInfo.getFilename())).toFile();
size = archiveFile.length();
}
}
......@@ -1198,13 +1208,14 @@ public class Seda1ToSeda21Converter extends AbstractArchiveConverter {
// Le format - identifiant pronom va dans le bloc <FormatIdentification>
Path filePathString = Paths.get(document.getAttachment().getFilename()) ;
//On ne garde que le nom du fichier, et non pas le chemin relatif (qui ne correspond pas)
String fileNameString = filePathString.getFileName().toString();
String fileNameString_old = filePathString.getFileName().toString();
String fileNameString = binaryDataObject.getFileInfo().getFilename();
/**
* Chemin du fichier sur le système pour la validation de format
*/
Path filePath;
if(fileNameString.trim().equalsIgnoreCase(this.archiveType)) {
if(fileNameString_old.trim().equalsIgnoreCase(this.archiveType)) {
filePath = this.rootFolder.resolve(Paths.get(fileNameString));
} else {
filePath = this.dataContentFolder.resolve(Paths.get(fileNameString));
......@@ -1342,7 +1353,7 @@ public class Seda1ToSeda21Converter extends AbstractArchiveConverter {
private FileInfoType extractFileInfo(DocumentType document) {
ObjectFactory objectFactory = new ObjectFactory();
FileInfoType fileInfo = objectFactory.createFileInfoType();
/**
* Nom du fichier SEDA 1.0
*/
......@@ -1350,12 +1361,56 @@ public class Seda1ToSeda21Converter extends AbstractArchiveConverter {
Path path = Paths.get(filePathString);
Path seda21Path;
seda21Path = path.subpath(path.getNameCount()-1, path.getNameCount());
String finalName;
String usedFileName = null;
if(seda21Path.toString().trim().equalsIgnoreCase("ArchiveTransfer.xml") || seda21Path.toString().trim().equalsIgnoreCase("Archive.xml")) {
usedFileName = path.getFileName().toString().trim();
} else if(path.getFileName().toString().contains(".")) {
String extensionFileName = seda21Path.toString().split("\\.")[seda21Path.toString().trim().split("\\.").length-1];
String empreinte = Base64.getEncoder().encodeToString(document.getIntegrity().getValue());
usedFileName = empreinte + "." + extensionFileName;
}
if(!fileNameCounter.containsKey(usedFileName)) {
fileNameCounter.put(usedFileName, new Integer(0));
}
finalName = Integer.toString(fileNameCounter.get(usedFileName)) + "_" + usedFileName;
Integer oldCounter = fileNameCounter.get(usedFileName);
fileNameCounter.replace(usedFileName, oldCounter + 1) ;
fileInfo.setFilename(finalName);
File[] directories = this.rootFolder.toFile().listFiles(File::isDirectory);
Path filePath;
if(seda21Path.toString().trim().equalsIgnoreCase("ArchiveTransfer.xml") || seda21Path.toString().trim().equalsIgnoreCase("Archive.xml") ) {
filePath = Paths.get(this.rootFolder + "/" + path.getFileName().toString().trim());
renameFileName(filePath, finalName);
} else {
filePath = Paths.get(directories[0].getPath() + "/" + path.getFileName().toString().trim());
renameFileName(filePath, finalName);
}
return fileInfo;
}
/**
* Renomme un fichier
* @param filePath
* @param newPath
*/
private void renameFileName(Path filePath, String newName) {
logger.debug("renommage ", filePath);
Path newPath = filePath.getParent().resolve(newName);
fileInfo.setFilename(seda21Path.toString());
try {
Files.move(filePath, newPath);
} catch (IOException e) {
logger.error("Impossible de renommer le fichier {} ", filePath);
}
return fileInfo;
}
/**
......
......@@ -198,10 +198,15 @@ public class Seda1ToSeda21TarMaker implements ArchiveConverter {
* @param rootFolderSEDA1
*/
private void moveArchiveXML(Path rootFolderSEDA1, String archiveType) {
Path archivePath = rootFolderSEDA1.resolve(archiveType);
Path archiveContentPath = rootFolderSEDA1.resolve("Content").resolve(archiveType);
if (!Files.exists(archivePath))
logger.error("Le fichier {} n'existe pas.", archivePath);
Path archivePath = rootFolderSEDA1.resolve("0_" + archiveType);
Path archiveContentPath = rootFolderSEDA1.resolve("Content").resolve("0_" + archiveType);
if (!Files.exists(archivePath)) {
archivePath = rootFolderSEDA1.resolve("1_" + archiveType);
archiveContentPath = rootFolderSEDA1.resolve("Content").resolve("1_" + archiveType);
if (!Files.exists(archivePath))
logger.error("Le fichier {} n'existe pas.", archivePath);
}
try {
Files.move(archivePath, archiveContentPath, StandardCopyOption.REPLACE_EXISTING);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment