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

358 #405

Merged
merged 8 commits into from
Oct 2, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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{
Expand Down Expand Up @@ -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<String, String> 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);
}
Expand All @@ -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<String, String> 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);
}
Expand Down Expand Up @@ -124,7 +131,7 @@ public void exportZipCollection(String ids, String acceptHeader, HttpServletResp

CollectionForExport collection = collectionExport.getCollectionData(id);
Map<String, String> xmlContent = ConceptUtils.convertCollectionInXml(collection);
String fileName = conceptUtils.getFileNameForExport(collection, lg);
String fileName = conceptsUtils.getCollectionExportFileName(collection, lg);
collections.put(fileName, xmlContent);

if(conceptsIds.size() > 0){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -219,7 +218,7 @@ public ResponseEntity<?> exportConcept(String id, String acceptHeader) throws Rm
}

Map<String, String> xmlContent = convertConceptInXml(concept);
String fileName = getFileNameForExport(concept);
String fileName = conceptsUtils.getConceptExportFileName(concept);
return conceptsExport.exportAsResponse(fileName,xmlContent,true,true,true);
}

Expand All @@ -242,7 +241,7 @@ public void exportZipConcept(String ids, String acceptHeader, HttpServletRespons


Map<String, String> xmlContent = ConceptUtils.convertCollectionInXml(collection);
String fileName = conceptUtils.getFileNameForExport(collection, lg);
String fileName = conceptsUtils.getCollectionExportFileName(collection, lg);
collections.put(fileName, xmlContent);

if(withConcepts){
Expand All @@ -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());
Expand All @@ -279,7 +274,7 @@ public Map<String, InputStream> getConceptsExportIS(List<String> ids, List<Membe
try {
ConceptForExport concept = conceptsExport.getConceptData(id);
Map<String, String> 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){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
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;
import java.util.Map;

@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<String, String> convertCollectionInXml(CollectionForExport collection) {
String collectionXml = XMLUtils.produceXMLResponse(collection);
Map<String,String> xmlContent = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -26,8 +19,7 @@ public abstract class RdfService {

@Autowired
protected StampsRestrictionsService stampsRestrictionsService;
@Autowired
protected FilesUtils filesUtils;

@Autowired
protected ConceptUtils conceptUtils;

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/fr/insee/rmes/utils/ExportUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -216,7 +217,9 @@ 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));
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);

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/fr/insee/rmes/utils/FilesUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
}

Expand Down
7 changes: 5 additions & 2 deletions src/test/java/fr/insee/rmes/utils/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down