Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 59 additions & 19 deletions src/main/java/fr/insee/rmes/services/datasets/DataSetsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import fr.insee.rmes.modelSwagger.dataset.*;
import fr.insee.rmes.persistence.FreeMarkerUtils;
import fr.insee.rmes.persistence.RdfService;
import fr.insee.security.User;
import fr.insee.rmes.services.codelists.CodeListsServices;
import fr.insee.rmes.utils.Constants;
import fr.insee.rmes.utils.config.Config;
import fr.insee.rmes.utils.exceptions.RmesException;
import fr.insee.security.User;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
Expand All @@ -26,6 +26,7 @@
import org.springframework.web.client.RestClient;

import java.util.*;

@Service
public class DataSetsImpl extends RdfService implements DataSetsServices {

Expand Down Expand Up @@ -117,7 +118,6 @@ public ResponseEntity<String> patchDataset(String datasetId, PatchDatasetDTO pat
return httpPatchRequest(token,patchDataset,datasetId, user);
}


protected ResponseEntity<String> httpPatchRequest(String token, PatchDatasetDTO body, String datasetId, Optional<User> user){

Optional<String> id = user.map(User::id);
Expand Down Expand Up @@ -164,6 +164,24 @@ protected DataSetModelSwagger findDataSetModelSwagger(String id) throws RmesExce
}
}

protected IdLabel labelInformation(String id,String label1,String label2){
List<LangContent> listOfLangContent = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Etre plus explicite dans le nom des paramètres, par exemple labelLg1 à la place de string1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modifié en conséquence.


if( label1.isEmpty() && !label2.isEmpty()){
listOfLangContent = constructLangContent2(label2);
}

if( !label1.isEmpty() && label2.isEmpty()){
listOfLangContent = constructLangContent1(label1);
}

if( !label1.isEmpty() && !label2.isEmpty()){
listOfLangContent = constructLangContent(label1,label2);
}

return new IdLabel(id,listOfLangContent);
}

private void removeEmptyKeys(JSONObject jsonObject) {
Iterator<String> keys = jsonObject.keys();
while (keys.hasNext()) {
Expand All @@ -174,16 +192,20 @@ private void removeEmptyKeys(JSONObject jsonObject) {
}
}


protected void testPresenceVariablePuisAjout(DataSetModelSwagger reponse, JSONObject catalogue_result, JSONObject adms_result, JSONObject codes_result, JSONObject organisations_result, JSONObject structures_result) throws RmesException, JsonProcessingException {
//récupération de le date de mofidication
//récupération de le date de modification
if (catalogue_result.has("dateModification")) {
Modified modified = new Modified(catalogue_result.getString("dateModification"));
reponse.setModified(modified.toString());
}
//récupération de la liste de creators
if (!catalogue_result.optString("creators").isEmpty()){
List<String> creatorUris = List.of(catalogue_result.getString("creators").split(","));
List<IdLabel> creator = getCreator(creatorUris);
String idCatalogResult = catalogue_result.getString("id");
params.put("ID",idCatalogResult );
JSONObject creatorUrisSet = repoGestion.getResponseAsObject(buildRequest(Constants.DATASETS_QUERIES_PATH+DATASET_BY_ID_PATH, "getDataSetByIdCreatorInformation.ftlh", params));
List<String> creatorUris =List.of(creatorUrisSet.getString("names").split(","));
List<IdLabel> creator = getCreator(creatorUris,idCatalogResult);
reponse.setCreator(creator);
}
//récupération du subtitle
Expand Down Expand Up @@ -229,10 +251,13 @@ protected void testPresenceVariablePuisAjout(DataSetModelSwagger reponse, JSONOb
ProcessStep processStep = constructCodeList(processStepResult.getString("notation"));
reponse.setProcessStep(processStep);
}

//récupération de publisher
if (organisations_result.has("idPublisher")) {
IdLabel publisher = constructIdLabel(organisations_result.getString("idPublisher"),organisations_result.getString("labelPublisherLg1"),organisations_result.getString("labelPublisherLg2"));
reponse.setPublisher(publisher);
String id = organisations_result.getString("idPublisher");
String label1 =organisations_result.getString("labelPublisherLg1");
String label2 =organisations_result.getString("labelPublisherLg2");
reponse.setPublisher(labelInformation(id,label1,label2));
}

//récupération de type
Expand Down Expand Up @@ -389,7 +414,6 @@ protected void testPresenceVariablePuisAjout(DataSetModelSwagger reponse, JSONOb
List<IdLabel> spatialResolutionList = getSpatialResolution(urisSpatialResolution);
reponse.setSpatialResolution(spatialResolutionList);
}

}

@Override
Expand All @@ -402,7 +426,6 @@ public String getDataSetByIDSummary(String id) throws RmesException, JsonProcess
JSONObject dataSetId = repoGestion.getResponseAsObject(buildRequest(Constants.DATASETS_QUERIES_PATH+DATASET_BY_ID_PATH, "getDataSetByIDSummary.ftlh", params));
if (dataSetId.has("id")) {
DataSet dataSet = objectMapper.readValue(dataSetId.toString(), DataSet.class);

Id id1 = new Id(dataSet.getId());
Uri uri = new Uri(dataSet.getUri());
Modified modified = new Modified(dataSet.getDateMiseAJour());
Expand Down Expand Up @@ -501,21 +524,32 @@ public Distributions[] getDataSetDistributionsById(String id) throws RmesExcepti
return distributionReponse;
}

private List<IdLabel> getCreator(List<String> creatorUris,String idCatalogue) throws RmesException {

List<String> listCreatorUrisWithoutExternalSpace= new ArrayList<>();
for (String s : creatorUris) {listCreatorUrisWithoutExternalSpace.add(s.trim());}
List<String> listWithoutDuplicates = new ArrayList<>(new LinkedHashSet<>(listCreatorUrisWithoutExternalSpace));

private List<IdLabel> getCreator(List<String> creatorUris) throws RmesException {
List<String> identifiers = new ArrayList<>();
List<IdLabel> creator = new ArrayList<>();
for (String s : creatorUris){

params.put("URI", s.replace(" ", ""));

for (String s : listWithoutDuplicates){
params.put("CREATOR", s);
params.put("ID", idCatalogue);
JSONObject creator_result = repoGestion.getResponseAsObject(buildRequest(Constants.DATASETS_QUERIES_PATH+DATASET_BY_ID_PATH, "getDataSetByIdCreator.ftlh", params));
List<LangContent> creatorTitles = constructLangContent(creator_result.getString("labelCreatorLg1"),creator_result.getString("labelCreatorLg2"));
IdLabel creatorIdLabel = new IdLabel(creator_result.getString("idCreator"),creatorTitles);
creator.add(creatorIdLabel);

String id = creator_result.getString("idCreator");

if(!identifiers.contains(id.trim())){
String labelCreatorLg1 = creator_result.getString("labelCreatorLg1");
String labelCreatorLg2 = creator_result.getString("labelCreatorLg2");
creator.add(labelInformation(id.trim(),labelCreatorLg1,labelCreatorLg2));
identifiers.add(id.trim());
}
}
return creator;
}

private List<IdLabel> getWasGeneratedBy(List<String> operationStat) throws RmesException {
List<IdLabel> wasGeneratedBy = new ArrayList<>();
for (String s : operationStat){
Expand Down Expand Up @@ -600,11 +634,19 @@ private List<ThemeModelSwagger> getThemeModelSwaggerS(JSONObject dataSetId) thro
}
}


protected List<LangContent> constructLangContent(String elementLg1, String elementLg2) {
return List.of(LangContent.lg1(elementLg1), LangContent.lg2(elementLg2));
}

protected List<LangContent> constructLangContent1(String elementLg1) {
return List.of(LangContent.lg1(elementLg1));
}

protected List<LangContent> constructLangContent2(String elementLg2) {
return List.of(LangContent.lg2(elementLg2));
}


protected List<LangContent> constructLangContentList(String stringListLg, String lg) {
List<String> listLg= List.of(stringListLg.split(","));
List<LangContent> rep = new ArrayList<>();
Expand All @@ -615,7 +657,6 @@ protected List<LangContent> constructLangContentList(String stringListLg, String
return rep;
}


private IdLabel constructIdLabel(String id, String labelLg1, String labelLg2) {
List<LangContent> langContentList = constructLangContent(labelLg1,labelLg2);
return new IdLabel(id,langContentList);
Expand All @@ -626,7 +667,6 @@ private WasDerivedFrom constructWasDerivedFrom(List<String> datasets, String der
return new WasDerivedFrom(datasets,descriptions);
}


private ProcessStep constructCodeList(String notation) throws RmesException {
String codeListString = codeListsServices.getCodesListForDataset(notation);
JSONObject jsonCodeList = new JSONObject(codeListString);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
SELECT DISTINCT
?creator ?labelCreatorLg1 ?labelCreatorLg2 ?idCreator
FROM <${DATASETS_GRAPH}>
FROM <${ORGANISATIONS_GRAPH}>
FROM <${ORGANISATIONS_GRAPH}/insee>

WHERE {
?uri a dcat:CatalogRecord ;
foaf:primaryTopic ?uriDataset .
?uriDataset dcterms:identifier "${ID}" .
?uriDataset dcterms:identifier '${ID}' .

OPTIONAL{?uriDataset dcterms:creator ?creator .
?creator dcterms:identifier ?idCreator .
?creator dcterms:identifier ?idCreator.
?creator dcterms:identifier '${CREATOR}'.
?creator skos:prefLabel ?labelCreatorLg1 .
FILTER(lang(?labelCreatorLg1)= '${LG1}') .
?creator skos:prefLabel ?labelCreatorLg2 .
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
SELECT DISTINCT (group_concat(distinct ?idCreator;separator=",") as ?names)

WHERE {
?uri a dcat:CatalogRecord ;
foaf:primaryTopic ?uriDataset .
?uriDataset dcterms:identifier '${ID}' .

OPTIONAL{?uriDataset dcterms:creator ?creator .
?creator dcterms:identifier ?idCreator.
?creator skos:prefLabel ?labelCreatorLg1 .
FILTER(lang(?labelCreatorLg1)= '${LG1}') .
?creator skos:prefLabel ?labelCreatorLg2 .
FILTER(lang(?labelCreatorLg2)= '${LG2}') .}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import fr.insee.rmes.dto.datasets.PatchDatasetDTO;
import fr.insee.rmes.model.datasets.Theme;
import fr.insee.rmes.model.datasets.*;
import fr.insee.rmes.modelSwagger.dataset.*;
import fr.insee.rmes.persistence.RepositoryGestion;
import fr.insee.rmes.services.utils.DataSetsUtilsTest;
Expand Down Expand Up @@ -44,6 +44,54 @@ class DataSetsImplTest {
RepositoryGestion repoGestion;
public static final ObjectMapper MAPPER = new JsonMapper();

@Test
void shouldGetDataSetBySummary() throws JsonProcessingException {

JSONObject dataSetId = new JSONObject();
dataSetId.put("id","ID");
dataSetId.put("uri","URI");
dataSetId.put("dateMiseAJour","today");

DataSetModelSwagger dataSetModelSwagger = null;

if (dataSetId.has("id")) {
ObjectMapper objectMapper = new ObjectMapper();
DataSet dataSet = objectMapper.readValue(dataSetId.toString(), DataSet.class);
Id id1 = new Id(dataSet.getId());
Uri uri = new Uri(dataSet.getUri());
Modified modified = new Modified(dataSet.getDateMiseAJour());
dataSetModelSwagger = new DataSetModelSwagger(id1, uri, modified);
}
assertNotNull(dataSetModelSwagger);
}

@Test
void shouldReturnIdLabelFromTwoStrings() {
IdLabel actual = dataSetsImpl.labelInformation("2025","elementLg1","elementLg2");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expliciter davantage le nom de la méthode (au lieu de One/Two/Three/Four)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modifié en conséquence ( shouldReturnIdLabelFromTwoStrings())

IdLabel expected= new IdLabel("2025", dataSetsImpl.constructLangContent("elementLg1","elementLg2"));
assertEquals(expected.toString(),actual.toString());
}

@Test
void shouldReturnIdLabelFromTwoNullStrings() {
IdLabel actual = dataSetsImpl.labelInformation("2025","","");
IdLabel expected= new IdLabel("2025", null);
assertEquals(expected.toString(),actual.toString());
}


@Test
void shouldReturnStepsOfGetCreator() {
List<String> creatorUris = List.of(" Insee ","Ined ","Ine d","Ined");
List<String> creatorUrisWithoutExternalSpaces = new ArrayList<>();
for (String s : creatorUris) {creatorUrisWithoutExternalSpaces.add(s.trim());}
List<String> deleteDuplicatesCreatorUris = new ArrayList<>(new LinkedHashSet<>(creatorUrisWithoutExternalSpaces));
boolean correctDeletionOfSpaces = creatorUrisWithoutExternalSpaces.equals(List.of("Insee", "Ined", "Ine d", "Ined"));
boolean goodDuplicateRemoval = deleteDuplicatesCreatorUris .toString().equals("[Insee, Ined, Ine d]");
assertTrue(correctDeletionOfSpaces && goodDuplicateRemoval);
}


@Test
void getListDataSetsTest() throws RmesException, JsonProcessingException {
JSONArray mockJSON = new JSONArray(DataSetsUtilsTest.DATA_SET_LIST);
Expand Down Expand Up @@ -207,9 +255,6 @@ void getDataSetByIDDateMiseAJourFalse_shouldReturn404IfInexistentId() throws Rme
.hasMessageContaining("Non existent dataset identifier");
}




@Test
void getDataSetByIDDateMiseAJour_shouldReturn404IfInexistentId() throws RmesException {
JSONObject mockJSON = new JSONObject(ResponseUtilsTest.EMPTY_JSON_OBJECT);
Expand All @@ -220,7 +265,6 @@ void getDataSetByIDDateMiseAJour_shouldReturn404IfInexistentId() throws RmesExce
.hasMessageContaining("Non existent dataset identifier");
}


@Test
void patchDataset_shouldReturn400() {
assertThatThrownBy(() -> dataSetsImpl.patchDataset("jdtest", new PatchDatasetDTO(null, null, null, null, null), ResponseUtilsTest.FAKE_TOKEN, empty())).isInstanceOf(RuntimeException.class)
Expand Down Expand Up @@ -248,8 +292,6 @@ void testConstructLangContentList(){
assertThat(actual_1).isEqualTo(expected_1);
assertThat(actual_2).isEqualTo(expected_2);


}



}