diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsCollectionServiceImpl.java b/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsCollectionServiceImpl.java index 5456bd15d..5f5b414b5 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsCollectionServiceImpl.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsCollectionServiceImpl.java @@ -4,6 +4,7 @@ import fr.insee.rmes.bauhaus_services.ConceptsService; import fr.insee.rmes.bauhaus_services.concepts.collections.CollectionExportBuilder; import fr.insee.rmes.bauhaus_services.concepts.collections.ConceptUtils; +import fr.insee.rmes.bauhaus_services.concepts.concepts.ConceptsUtils; import fr.insee.rmes.bauhaus_services.rdf_utils.RdfService; import fr.insee.rmes.exceptions.RmesException; import fr.insee.rmes.model.concepts.CollectionForExport; @@ -26,11 +27,17 @@ public class ConceptsCollectionServiceImpl extends RdfService implements ConceptsCollectionService { static final Logger logger = LogManager.getLogger(ConceptsCollectionServiceImpl.class); - @Autowired - CollectionExportBuilder collectionExport; + private CollectionExportBuilder collectionExport; + private ConceptsService conceptsService; + private ConceptsUtils conceptsUtils; @Autowired - ConceptsService conceptsService; + public ConceptsCollectionServiceImpl(CollectionExportBuilder collectionExport, ConceptsService conceptsService, ConceptsUtils conceptsUtils) { + this.collectionExport = collectionExport; + this.conceptsService = conceptsService; + this.conceptsUtils = conceptsUtils; + } + @Override public String getCollections() throws RmesException{ @@ -70,7 +77,7 @@ public ResponseEntity getCollectionExportODT(String id, String accept, Concep CollectionForExport collection = collectionExport.getCollectionData(id); List conceptsIds = withConcepts ? getCollectionConceptsIds(id) : Collections.emptyList(); Map xmlContent = ConceptUtils.convertCollectionInXml(collection); - String fileName = super.conceptUtils.getFileNameForExport(collection, lg); + String fileName = conceptsUtils.getCollectionExportFileName(collection, lg); if(conceptsIds.size() == 0){ return collectionExport.exportAsResponseODT(fileName,xmlContent,true,true,true, lg); } @@ -96,7 +103,7 @@ public ResponseEntity getCollectionExportODS(String id, String accept, boolea CollectionForExport collection = collectionExport.getCollectionData(id); List conceptsIds = withConcepts ? getCollectionConceptsIds(id) : Collections.emptyList(); Map xmlContent = ConceptUtils.convertCollectionInXml(collection); - String fileName = conceptUtils.getFileNameForExport(collection, null); + String fileName = conceptsUtils.getCollectionExportFileName(collection, null); if(conceptsIds.size() == 0){ return collectionExport.exportAsResponseODS(fileName,xmlContent,true,true,true); } @@ -124,7 +131,7 @@ public void exportZipCollection(String ids, String acceptHeader, HttpServletResp CollectionForExport collection = collectionExport.getCollectionData(id); Map xmlContent = ConceptUtils.convertCollectionInXml(collection); - String fileName = conceptUtils.getFileNameForExport(collection, lg); + String fileName = conceptsUtils.getCollectionExportFileName(collection, lg); collections.put(fileName, xmlContent); if(conceptsIds.size() > 0){ diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsImpl.java b/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsImpl.java index b64b3502e..28dca08b2 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsImpl.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsImpl.java @@ -19,7 +19,6 @@ import fr.insee.rmes.model.concepts.MembersLg; import fr.insee.rmes.persistance.sparql_queries.concepts.CollectionsQueries; import fr.insee.rmes.persistance.sparql_queries.concepts.ConceptsQueries; -import fr.insee.rmes.utils.FilesUtils; import fr.insee.rmes.utils.XMLUtils; import fr.insee.rmes.webservice.ConceptsCollectionsResources; import org.apache.commons.text.CaseUtils; @@ -219,7 +218,7 @@ public ResponseEntity exportConcept(String id, String acceptHeader) throws Rm } Map xmlContent = convertConceptInXml(concept); - String fileName = getFileNameForExport(concept); + String fileName = conceptsUtils.getConceptExportFileName(concept); return conceptsExport.exportAsResponse(fileName,xmlContent,true,true,true); } @@ -242,7 +241,7 @@ public void exportZipConcept(String ids, String acceptHeader, HttpServletRespons Map xmlContent = ConceptUtils.convertCollectionInXml(collection); - String fileName = conceptUtils.getFileNameForExport(collection, lg); + String fileName = conceptsUtils.getCollectionExportFileName(collection, lg); collections.put(fileName, xmlContent); if(withConcepts){ @@ -257,10 +256,6 @@ public void exportZipConcept(String ids, String acceptHeader, HttpServletRespons } } - private String getFileNameForExport(ConceptForExport concept) { - return super.filesUtils.reduceFileNameSize(concept.getId() + "-" + CaseUtils.toCamelCase(concept.getPrefLabelLg1(), false)); - } - private MembersLg convertConceptIntoMembers(ConceptForExport concept){ MembersLg member = new MembersLg(); member.setId(concept.getId()); @@ -279,7 +274,7 @@ public Map getConceptsExportIS(List ids, List xmlContent = convertConceptInXml(concept); - String fileName = getFileNameForExport(concept); + String fileName = conceptsUtils.getConceptExportFileName(concept); ret.put(fileName, conceptsExport.exportAsInputStream(fileName,xmlContent,true,true,true)); if(members != null){ diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/concepts/collections/ConceptUtils.java b/src/main/java/fr/insee/rmes/bauhaus_services/concepts/collections/ConceptUtils.java index 26b8a7de6..28f11f502 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/concepts/collections/ConceptUtils.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/concepts/collections/ConceptUtils.java @@ -1,11 +1,7 @@ package fr.insee.rmes.bauhaus_services.concepts.collections; import fr.insee.rmes.model.concepts.CollectionForExport; -import fr.insee.rmes.utils.FilesUtils; import fr.insee.rmes.utils.XMLUtils; -import fr.insee.rmes.webservice.ConceptsCollectionsResources; -import org.apache.commons.text.CaseUtils; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.HashMap; @@ -13,20 +9,6 @@ @Component public class ConceptUtils { - private final FilesUtils filesUtils; - - @Autowired - public ConceptUtils(FilesUtils filesUtils) { - this.filesUtils = filesUtils; - } - - public String getFileNameForExport(CollectionForExport collection, ConceptsCollectionsResources.Language lg){ - if (lg == ConceptsCollectionsResources.Language.lg2){ - return this.filesUtils.reduceFileNameSize(CaseUtils.toCamelCase(collection.getPrefLabelLg2(), false) + "-" + collection.getId()); - } - return this.filesUtils.reduceFileNameSize(CaseUtils.toCamelCase(collection.getPrefLabelLg1(), false) + "-" + collection.getId()); - } - public static Map convertCollectionInXml(CollectionForExport collection) { String collectionXml = XMLUtils.produceXMLResponse(collection); Map xmlContent = new HashMap<>(); diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/concepts/concepts/ConceptsUtils.java b/src/main/java/fr/insee/rmes/bauhaus_services/concepts/concepts/ConceptsUtils.java index 02003f38a..a79297f18 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/concepts/concepts/ConceptsUtils.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/concepts/concepts/ConceptsUtils.java @@ -13,10 +13,15 @@ import fr.insee.rmes.exceptions.RmesException; import fr.insee.rmes.exceptions.RmesNotFoundException; import fr.insee.rmes.exceptions.RmesUnauthorizedException; +import fr.insee.rmes.model.concepts.CollectionForExport; import fr.insee.rmes.model.concepts.Concept; +import fr.insee.rmes.model.concepts.ConceptForExport; import fr.insee.rmes.persistance.ontologies.INSEE; import fr.insee.rmes.persistance.sparql_queries.concepts.ConceptsQueries; +import fr.insee.rmes.utils.FilesUtils; import fr.insee.rmes.utils.JSONUtils; +import fr.insee.rmes.webservice.ConceptsCollectionsResources; +import org.apache.commons.text.CaseUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.eclipse.rdf4j.model.IRI; @@ -41,11 +46,36 @@ public class ConceptsUtils extends RdfService { private static final Logger logger = LogManager.getLogger(ConceptsUtils.class); - @Autowired private ConceptsPublication conceptsPublication; - - @Autowired private NoteManager noteManager; + private FilesUtils filesUtils; + + @Autowired + public ConceptsUtils(FilesUtils filesUtils, NoteManager noteManager, ConceptsPublication conceptsPublication) { + this.filesUtils = filesUtils; + this.noteManager = noteManager; + this.conceptsPublication = conceptsPublication; + } + + public String getConceptExportFileName(ConceptForExport concept) { + return getAbstractExportFileName(concept.getId(), concept.getPrefLabelLg1(), concept.getPrefLabelLg2(), ConceptsCollectionsResources.Language.lg1); + } + + public String getCollectionExportFileName(CollectionForExport collection, ConceptsCollectionsResources.Language lg){ + return getAbstractExportFileName(collection.getId(), collection.getPrefLabelLg1(), collection.getPrefLabelLg2(), lg); + } + + private String getAbstractExportFileName(String id, String labelLg1, String labelLg2, ConceptsCollectionsResources.Language lg){ + var initialFileName = getInitialFileName(labelLg1, labelLg2, lg); + return this.filesUtils.reduceFileNameSize(id + "-" + FilesUtils.removeAsciiCharacters(CaseUtils.toCamelCase(initialFileName, false))); + } + + private String getInitialFileName(String labelLg1, String labelLg2, ConceptsCollectionsResources.Language lg){ + if(lg == ConceptsCollectionsResources.Language.lg2){ + return labelLg2; + } + return labelLg1; + } public String createID() throws RmesException { diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RdfService.java b/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RdfService.java index 876f8eac6..1e84031a7 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RdfService.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RdfService.java @@ -1,17 +1,10 @@ package fr.insee.rmes.bauhaus_services.rdf_utils; import fr.insee.rmes.bauhaus_services.concepts.collections.ConceptUtils; -import fr.insee.rmes.utils.FilesUtils; -import org.eclipse.rdf4j.model.IRI; -import org.eclipse.rdf4j.model.Model; -import org.eclipse.rdf4j.model.Resource; -import org.eclipse.rdf4j.model.Statement; -import org.eclipse.rdf4j.model.Value; -import org.springframework.beans.factory.annotation.Autowired; - import fr.insee.rmes.config.Config; import fr.insee.rmes.config.auth.security.restrictions.StampsRestrictionsService; -import org.springframework.boot.autoconfigure.AutoConfigureOrder; +import org.eclipse.rdf4j.model.*; +import org.springframework.beans.factory.annotation.Autowired; public abstract class RdfService { @@ -26,8 +19,7 @@ public abstract class RdfService { @Autowired protected StampsRestrictionsService stampsRestrictionsService; - @Autowired - protected FilesUtils filesUtils; + @Autowired protected ConceptUtils conceptUtils; diff --git a/src/main/java/fr/insee/rmes/utils/ExportUtils.java b/src/main/java/fr/insee/rmes/utils/ExportUtils.java index 4c76d4fdb..881599349 100644 --- a/src/main/java/fr/insee/rmes/utils/ExportUtils.java +++ b/src/main/java/fr/insee/rmes/utils/ExportUtils.java @@ -5,6 +5,7 @@ import fr.insee.rmes.exceptions.RmesException; import fr.insee.rmes.model.dissemination_status.DisseminationStatus; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.json.JSONArray; @@ -216,7 +217,9 @@ private Set exportRubricsDocuments(JSONObject sims, Path directory) thro if(!Files.exists(documentPath)){ missingDocuments.add(document.getString("id")); } else { - String documentFileName = filesUtils.reduceFileNameSize(UriUtils.getLastPartFromUri(url)); + String fileName = FilenameUtils.getBaseName(documentPath.getFileName().toString()); + String extension = FilenameUtils.getExtension(documentPath.getFileName().toString()); + String documentFileName = String.format("%s.%s", filesUtils.reduceFileNameSize(fileName), extension); InputStream inputStream = Files.newInputStream(documentPath); diff --git a/src/main/java/fr/insee/rmes/utils/FilesUtils.java b/src/main/java/fr/insee/rmes/utils/FilesUtils.java index 640e4e72e..e930b2cee 100644 --- a/src/main/java/fr/insee/rmes/utils/FilesUtils.java +++ b/src/main/java/fr/insee/rmes/utils/FilesUtils.java @@ -43,6 +43,10 @@ public static File streamToFile(InputStream in, String fileName, String fileExte return tempFile; } + public static String removeAsciiCharacters(String fileName) { + return Normalizer.normalize(fileName, Normalizer.Form.NFD).replaceAll("\\p{M}+", "").replaceAll("\\p{Punct}", ""); + } + public static String cleanFileNameAndAddExtension(String fileName, String extension) { fileName = fileName.toLowerCase().trim(); fileName = StringUtils.normalizeSpace(fileName); @@ -74,7 +78,7 @@ public static void zipDirectory(File directoryToZip) throws IOException { try { zipOut.close(); fos.close(); - }catch(IOException e ) { + } catch(IOException e ) { log.warn("outputStream already closed"); } diff --git a/src/test/java/fr/insee/rmes/utils/FileUtilsTest.java b/src/test/java/fr/insee/rmes/utils/FileUtilsTest.java index 34e863480..dfd227ce3 100644 --- a/src/test/java/fr/insee/rmes/utils/FileUtilsTest.java +++ b/src/test/java/fr/insee/rmes/utils/FileUtilsTest.java @@ -16,18 +16,21 @@ class FileUtilsTest { @ParameterizedTest @ValueSource(strings = { "Carrières complètes ", "carrières-complètes", " Carrières complètes " }) void givenCleanFileName_whenString_thenResponseIsClean(String name) { - String cleanFileName = FilesUtils.cleanFileNameAndAddExtension(name, "odt"); assertEquals("carrières-complètes.odt", cleanFileName); } @Test void givenCleanFileName_whenStringWithPointExtension_thenResponseIsClean() { - String cleanFileName = FilesUtils.cleanFileNameAndAddExtension("test de nommage bidon ", ".odt"); assertEquals("test-de-nommage-bidon.odt", cleanFileName); } + @Test + void shouldRemoveAsciiCharacters(){ + assertEquals("textWithE", filesUtils.removeAsciiCharacters("textWith:É")); + } + @Test void shouldNotSubStringIfShortName(){ assertEquals("name-name", filesUtils.reduceFileNameSize("name-name"));