Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use sims name when exporting #796

Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,21 @@ public ResponseEntity<Resource> exportAsResponse(String id, Map<String, String>
xmlContent.put(Constants.PARAMETERS_FILE, parametersXML);

Exporter exporter;
JSONObject sims = this.documentationsUtils.getDocumentationByIdSims(id);

if (documents) {
JSONObject sims = this.documentationsUtils.getDocumentationByIdSims(id);
exporter = (xml, xsl, xmlPattern, zip, documentation) -> exportAsZip(sims, xml, xsl, xmlPattern, zip, documentation );
} else{
exporter = (xml, xsl, xmlPattern, zip, documentation) -> exportUtils.exportAsODT(id, xml, xsl, xmlPattern, zip, documentation );
String fileName = sims.getString(Constants.LABEL_LG1);
exporter = (xml, xsl, xmlPattern, zip, documentation) -> exportUtils.exportAsODT(fileName, xml, xsl, xmlPattern, zip, documentation );
}
return export(exporter, xmlContent, patternAndZip);
}

public ResponseEntity<Resource> exportAsZip(JSONObject sims, Map<String, String> xmlContent, String xslFile, String xmlPattern, String zip, String objectType) throws RmesException {
String simsId = sims.getString("id");
logger.debug("Begin to download the SIMS {} with its documents", simsId);
String fileName = sims.getString("labelLg1");
String fileName = FilesUtils.removeAsciiCharacters(sims.getString(Constants.LABEL_LG1));
EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved

try {

Expand Down Expand Up @@ -139,7 +141,7 @@ public ResponseEntity<Resource> exportAsZip(JSONObject sims, Map<String, String>
FilesUtils.zipDirectory(simsDirectory.toFile());

logger.debug("Zip created for the SIMS {}", simsId);
HttpHeaders responseHeaders = HttpUtils.generateHttpHeaders(sims.getString("labelLg1"), FilesUtils.ZIP_EXTENSION, this.maxLength);
HttpHeaders responseHeaders = HttpUtils.generateHttpHeaders(sims.getString(Constants.LABEL_LG1), FilesUtils.ZIP_EXTENSION, this.maxLength);
EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved
responseHeaders.set("X-Missing-Documents", String.join(",", missingDocuments));
Resource resource = new UrlResource(Paths.get(simsDirectory.toString(), simsDirectory.getFileName() + FilesUtils.ZIP_EXTENSION).toUri());
return ResponseEntity.ok()
Expand Down Expand Up @@ -169,7 +171,7 @@ private Set<String> exportRubricsDocuments(JSONObject sims, Path directory) thro
if(!Files.exists(documentPath)){
missingDocuments.add(document.getString("id"));
} else {
String documentFileName = FilesUtils.reduceFileNameSize(UriUtils.getLastPartFromUri(url), maxLength);
String documentFileName = FilesUtils.reduceFileNameSize(FilesUtils.removeAsciiCharacters(UriUtils.getLastPartFromUri(url)), maxLength);
EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved
try (InputStream inputStream = Files.newInputStream(documentPath)){
Path documentDirectory = Path.of(directory.toString(), "documents");
if (!Files.exists(documentDirectory)) {
Expand Down
34 changes: 7 additions & 27 deletions src/main/java/fr/insee/rmes/utils/FilesUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,13 @@ public static String reduceFileNameSize(String fileName, int maxLength) {
return fileName.substring(0, Math.min(fileName.length(), maxLength));
}

public static File streamToFile(InputStream in, String fileName, String fileExtension) throws IOException {
final File tempFile = File.createTempFile(fileName, fileExtension);
tempFile.deleteOnExit();
try (FileOutputStream out = new FileOutputStream(tempFile)) {
IOUtils.copy(in, out);
}
return tempFile;
}

public static String cleanFileNameAndAddExtension(String fileName, String extension) {
fileName = fileName.toLowerCase().trim();
fileName = StringUtils.normalizeSpace(fileName);
fileName = fileName.replace(" ", "-");
fileName = Normalizer.normalize(fileName, Normalizer.Form.NFD).replace("[^\\p{ASCII}]", "");
if (extension.startsWith(".")) {
fileName += extension;
} else {
fileName += "." + extension;
}
return fileName;
public static String removeAsciiCharacters(String fileName) {
return Normalizer.normalize(fileName, Normalizer.Form.NFD)
.replaceAll("œ", "oe")
EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved
.replaceAll("Œ", "OE")
EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved
.replaceAll("\\p{M}+", "")
.replaceAll("\\p{Punct}", "")
.replaceAll(":", "");
EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved
}

public static void addFileToZipFolder(File fileToAdd, File zipArchive) {
Expand All @@ -99,13 +86,6 @@ public static void zipDirectory(File directoryToZip) throws IOException {
}

}
public static String removeAsciiCharacters(String fileName) {
return Normalizer.normalize(fileName, Normalizer.Form.NFD)
.replaceAll("œ", "oe")
.replaceAll("Œ", "OE")
.replaceAll("\\p{M}+", "")
.replaceAll("\\p{Punct}", "");
}

private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {
if (fileToZip.isHidden() || fileName.endsWith(".zip")) {
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/fr/insee/rmes/utils/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ private StringUtils() {
throw new IllegalStateException("Utility class");
}

public static boolean stringContainsItemFromList(String string, String[] list) {
return Arrays.stream(list).anyMatch(string::contains);
}

public static List<String> stringToList(String value) {
List<String> val = new ArrayList<>();
val.add(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public void testExportMetadataReport_Success_WithoutDocuments_Label() throws Rm

Resource resource = new ByteArrayResource("Mocked Document Content".getBytes());

when(documentationsUtils.getDocumentationByIdSims(id)).thenReturn(new JSONObject().put("labelLg1", "labelLg1"));
when(parentUtils.getDocumentationTargetTypeAndId(id)).thenReturn(new String[]{targetType, "someId"});
when(documentationsUtils.getFullSimsForXml(id)).thenReturn(new Documentation());
when(exportUtils.exportAsODT(any(), any(), any(), any(), any(), any())).thenReturn(ResponseEntity.ok().body(resource));
Expand Down
18 changes: 1 addition & 17 deletions src/test/java/fr/insee/rmes/utils/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FileUtilsTest {
@Test
public void testReplaceSpecialCharacters() {
// Cas de test avec des caractères spéciaux
String input = "Œil et cœur sont liés. Ça coûte très cher !";
String input = "Œil: et cœur sont liés. Ça coûte très cher !";
String expectedOutput = "OEil et coeur sont lies Ca coute tres cher ";
String actualOutput = FilesUtils.removeAsciiCharacters(input);
assertEquals(expectedOutput, actualOutput);
Expand Down Expand Up @@ -50,20 +50,4 @@ public void testGetExtension() {
// Test with unknown input
assertEquals(".odt", FilesUtils.getExtension("unknown/type"));
}

@ParameterizedTest
@ValueSource(strings = { "Carrières complètes ", "carrières-complètes", " Carrières complètes " })
void givenCleanFileName_whenString_thenResponseIsClean(String name) throws RmesException {

String cleanFileName = FilesUtils.cleanFileNameAndAddExtension(name, "odt");
assertEquals("carrières-complètes.odt", cleanFileName);
}

@Test
void givenCleanFileName_whenStringWithPointExtension_thenResponseIsClean() throws RmesException {

String cleanFileName = FilesUtils.cleanFileNameAndAddExtension("test de nommage bidon ", ".odt");
assertEquals("test-de-nommage-bidon.odt", cleanFileName);
}

}