Skip to content

Commit

Permalink
feat: finish filename process
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey committed Nov 22, 2024
1 parent 521f3d3 commit 20f735f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void exportZipCollection(String ids, String acceptHeader, HttpServletResp

private String getFileNameForExport(CollectionForExport collection, ConceptsCollectionsResources.Language lg){
String label = (lg == ConceptsCollectionsResources.Language.lg2 && collection.getPrefLabelLg2() != null) ? collection.getPrefLabelLg2() : collection.getPrefLabelLg1();
return FilesUtils.reduceFileNameSize(CaseUtils.toCamelCase(label, false) + "-" + collection.getId(), maxLength);
return FilesUtils.generateFinalFileNameWithoutExtension(collection.getId() + "-" + label, maxLength);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class OperationsDocumentationsImpl extends RdfService implements Operati
@Value("classpath:bauhaus-sims.json")
org.springframework.core.io.Resource simsDefaultValue;

@Value("${fr.insee.rmes.bauhaus.filenames.maxlength}") int maxLength;

@Autowired
DocumentationsUtils documentationsUtils;

Expand Down Expand Up @@ -147,13 +149,13 @@ public ResponseEntity<Resource> exportMetadataReport(String id, boolean includeE
ErrorCodes.SIMS_EXPORT_WITHOUT_LANGUAGE,
"at least one language must be selected for export",
"in export of sims: " + id);
return documentationsExport.exportMetadataReport(id,includeEmptyMas, lg1, lg2, document, Constants.GOAL_RMES);
return documentationsExport.exportMetadataReport(id,includeEmptyMas, lg1, lg2, document, Constants.GOAL_RMES, maxLength);

}

@Override
public ResponseEntity<?> exportMetadataReportForLabel(String id) throws RmesException {
return documentationsExport.exportMetadataReport(id,true, true, false, false, Constants.GOAL_COMITE_LABEL);
return documentationsExport.exportMetadataReport(id,true, true, false, false, Constants.GOAL_COMITE_LABEL, maxLength);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public DocumentationExport(@Value("${fr.insee.rmes.bauhaus.filenames.maxlength}"
* the export will be a .ODT file.
*/
public ResponseEntity<Resource> exportAsResponse(String id, Map<String, String> xmlContent, String targetType, boolean includeEmptyFields, boolean lg1,
boolean lg2, boolean documents, String goal) throws RmesException {
boolean lg2, boolean documents, String goal, int maxLength) throws RmesException {

PatternAndZip patternAndZip = PatternAndZip.of(goal);
String parametersXML = XsltUtils.buildParams(lg1, lg2, includeEmptyFields, targetType);
Expand All @@ -99,18 +99,18 @@ public ResponseEntity<Resource> exportAsResponse(String id, Map<String, String>
JSONObject sims = this.documentationsUtils.getDocumentationByIdSims(id);

if (documents) {
exporter = (xml, xsl, xmlPattern, zip, documentation) -> exportAsZip(sims, xml, xsl, xmlPattern, zip, documentation );
exporter = (xml, xsl, xmlPattern, zip, documentation) -> exportAsZip(sims, xml, xsl, xmlPattern, zip, documentation, maxLength);
} else{
String fileName = sims.getString(Constants.LABEL_LG1);
String fileName = FilesUtils.generateFinalFileNameWithoutExtension(sims.getString(Constants.LABEL_LG1), maxLength);
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 {
public ResponseEntity<Resource> exportAsZip(JSONObject sims, Map<String, String> xmlContent, String xslFile, String xmlPattern, String zip, String objectType, int maxLength) throws RmesException {
String simsId = sims.getString("id");
logger.debug("Begin to download the SIMS {} with its documents", simsId);
String fileName = FilesUtils.removeAsciiCharacters(sims.getString(Constants.LABEL_LG1));
String fileName = FilesUtils.generateFinalFileNameWithoutExtension(sims.getString(Constants.LABEL_LG1), maxLength);

try {

Expand Down Expand Up @@ -141,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(Constants.LABEL_LG1), FilesUtils.ZIP_EXTENSION, this.maxLength);
HttpHeaders responseHeaders = HttpUtils.generateHttpHeaders(fileName, FilesUtils.ZIP_EXTENSION);
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 @@ -171,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(FilesUtils.removeAsciiCharacters(UriUtils.getLastPartFromUri(url)), maxLength);
String documentFileName = FilesUtils.generateFinalFileNameWithExtension(UriUtils.getLastPartFromUri(url), maxLength);
try (InputStream inputStream = Files.newInputStream(documentPath)){
Path documentDirectory = Path.of(directory.toString(), "documents");
if (!Files.exists(documentDirectory)) {
Expand Down Expand Up @@ -205,12 +205,12 @@ public ResponseEntity<Object> exportXmlFiles(Map<String, String> xmlContent, Str
}


public ResponseEntity<Resource> exportMetadataReport(String id, Boolean includeEmptyMas, Boolean lg1, Boolean lg2, Boolean document, String goal) throws RmesException {
public ResponseEntity<Resource> exportMetadataReport(String id, Boolean includeEmptyMas, Boolean lg1, Boolean lg2, Boolean document, String goal, int maxLength) throws RmesException {
Map<String,String> xmlContent = new HashMap<>();
String targetType = getXmlContent(id, xmlContent);
String msdXML = buildShellSims();
xmlContent.put("msdFile", msdXML);
return exportAsResponse(id, xmlContent,targetType,includeEmptyMas,lg1,lg2, document, goal);
return exportAsResponse(id, xmlContent,targetType,includeEmptyMas,lg1,lg2, document, goal, maxLength);
}


Expand Down
6 changes: 3 additions & 3 deletions src/main/java/fr/insee/rmes/utils/ExportUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ResponseEntity<Resource> exportAsODS(String fileName, Map<String, String>

private ResponseEntity<Resource> exportAsFileByExtension(String fileName, Map<String, String> xmlContent, String xslFile, String xmlPattern, String zip, String objectType, String extension) throws RmesException {
logger.debug("Begin To export {} as Response", objectType);
fileName = fileName.replace(extension, "");
fileName = FilesUtils.generateFinalFileNameWithoutExtension(fileName.replace(extension, ""), maxLength);

InputStream input = exportAsInputStream(fileName, xmlContent, xslFile, xmlPattern, zip, objectType, extension);
if (input == null)
Expand All @@ -67,7 +67,7 @@ private ResponseEntity<Resource> exportAsFileByExtension(String fileName, Map<St
}
logger.debug("End To export {} as Response", objectType);

HttpHeaders responseHeaders = HttpUtils.generateHttpHeaders(fileName, extension, this.maxLength);
HttpHeaders responseHeaders = HttpUtils.generateHttpHeaders(fileName, extension);

return ResponseEntity.ok()
.headers(responseHeaders)
Expand Down Expand Up @@ -151,7 +151,7 @@ public ResponseEntity<Object> exportFilesAsResponse(Map<String, String> xmlConte

logger.debug("End To export temp files as Response");

HttpHeaders responseHeaders = HttpUtils.generateHttpHeaders("xmlFiles", FilesUtils.ZIP_EXTENSION, this.maxLength);
HttpHeaders responseHeaders = HttpUtils.generateHttpHeaders("xmlFiles", FilesUtils.ZIP_EXTENSION);
Resource resource = new UrlResource(Paths.get(tempDir.toString(), tempDir.getFileName() + FilesUtils.ZIP_EXTENSION).toUri());
return ResponseEntity.ok()
.headers(responseHeaders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void testExportMetadataReport_Success() throws RmesException {
boolean document = true;
Resource resource = new ByteArrayResource("Mocked Document Content".getBytes());

when(documentationsExport.exportMetadataReport(id, includeEmptyMas, lg1, lg2, document, Constants.GOAL_RMES))
when(documentationsExport.exportMetadataReport(id, includeEmptyMas, lg1, lg2, document, Constants.GOAL_RMES, 0))
.thenReturn(ResponseEntity.ok().body(resource));

ResponseEntity<Resource> response = metadataReportService.exportMetadataReport(id, includeEmptyMas, lg1, lg2, document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ public void testExportAsZip_success() throws Exception {


InputStream inputStreamMock = mock(InputStream.class);
when(exportUtils.exportAsInputStream(eq("simsLabel"), eq(xmlContent), eq(xslFile), eq(xmlPattern), eq(zip), eq(objectType), eq(FilesUtils.ODT_EXTENSION)))
when(exportUtils.exportAsInputStream(eq("simslabel"), eq(xmlContent), eq(xslFile), eq(xmlPattern), eq(zip), eq(objectType), eq(FilesUtils.ODT_EXTENSION)))
.thenReturn(inputStreamMock);
when(inputStreamMock.readAllBytes()).thenReturn(new byte[0]);

ResponseEntity<Resource> response = documentationExport.exportAsZip(sims, xmlContent, xslFile, xmlPattern, zip, objectType);
ResponseEntity<Resource> response = documentationExport.exportAsZip(sims, xmlContent, xslFile, xmlPattern, zip, objectType, 50);

assertEquals(HttpStatus.OK, response.getStatusCode());
assertNotNull(response.getBody());
Expand All @@ -116,7 +116,7 @@ public void testExportMetadataReport_Success_WithoutDocuments_Label() throws Rm
when(documentationsUtils.getFullSimsForXml(id)).thenReturn(new Documentation());
when(exportUtils.exportAsODT(any(), any(), any(), any(), any(), any())).thenReturn(ResponseEntity.ok().body(resource));

ResponseEntity<Resource> response = documentationExport.exportMetadataReport(id, includeEmptyMas, lg1, lg2, document, goal);
ResponseEntity<Resource> response = documentationExport.exportMetadataReport(id, includeEmptyMas, lg1, lg2, document, goal, 100);
assertEquals(ResponseEntity.ok().body(resource), response);
}

Expand All @@ -135,7 +135,7 @@ public void testExportMetadataReport_Failure_UnknownGoal() throws RmesException
when(documentationsUtils.getFullSimsForXml(id)).thenReturn(new Documentation());

RmesBadRequestException exception = assertThrows(RmesBadRequestException.class, () -> {
documentationExport.exportMetadataReport(id, includeEmptyMas, lg1, lg2, document, goal);
documentationExport.exportMetadataReport(id, includeEmptyMas, lg1, lg2, document, goal, 100);
});

assertEquals("{\"message\":\"The goal is unknown\"}", exception.getDetails());
Expand Down
7 changes: 0 additions & 7 deletions src/test/java/fr/insee/rmes/utils/ExportUtilsTest.java

This file was deleted.

0 comments on commit 20f735f

Please sign in to comment.