diff --git a/src/main/java/fr/insee/rmes/services/datasets/DataSetsImpl.java b/src/main/java/fr/insee/rmes/services/datasets/DataSetsImpl.java index 2c987fe1..f79bdfbb 100644 --- a/src/main/java/fr/insee/rmes/services/datasets/DataSetsImpl.java +++ b/src/main/java/fr/insee/rmes/services/datasets/DataSetsImpl.java @@ -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; @@ -26,6 +26,7 @@ import org.springframework.web.client.RestClient; import java.util.*; + @Service public class DataSetsImpl extends RdfService implements DataSetsServices { @@ -117,7 +118,6 @@ public ResponseEntity patchDataset(String datasetId, PatchDatasetDTO pat return httpPatchRequest(token,patchDataset,datasetId, user); } - protected ResponseEntity httpPatchRequest(String token, PatchDatasetDTO body, String datasetId, Optional user){ Optional id = user.map(User::id); @@ -164,6 +164,24 @@ protected DataSetModelSwagger findDataSetModelSwagger(String id) throws RmesExce } } + protected IdLabel labelInformation(String id,String label1,String label2){ + List listOfLangContent = null; + + 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 keys = jsonObject.keys(); while (keys.hasNext()) { @@ -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 creatorUris = List.of(catalogue_result.getString("creators").split(",")); - List 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 creatorUris =List.of(creatorUrisSet.getString("names").split(",")); + List creator = getCreator(creatorUris,idCatalogResult); reponse.setCreator(creator); } //récupération du subtitle @@ -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 @@ -389,7 +414,6 @@ protected void testPresenceVariablePuisAjout(DataSetModelSwagger reponse, JSONOb List spatialResolutionList = getSpatialResolution(urisSpatialResolution); reponse.setSpatialResolution(spatialResolutionList); } - } @Override @@ -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()); @@ -501,21 +524,32 @@ public Distributions[] getDataSetDistributionsById(String id) throws RmesExcepti return distributionReponse; } + private List getCreator(List creatorUris,String idCatalogue) throws RmesException { + List listCreatorUrisWithoutExternalSpace= new ArrayList<>(); + for (String s : creatorUris) {listCreatorUrisWithoutExternalSpace.add(s.trim());} + List listWithoutDuplicates = new ArrayList<>(new LinkedHashSet<>(listCreatorUrisWithoutExternalSpace)); - private List getCreator(List creatorUris) throws RmesException { + List identifiers = new ArrayList<>(); List 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 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 getWasGeneratedBy(List operationStat) throws RmesException { List wasGeneratedBy = new ArrayList<>(); for (String s : operationStat){ @@ -600,11 +634,19 @@ private List getThemeModelSwaggerS(JSONObject dataSetId) thro } } - protected List constructLangContent(String elementLg1, String elementLg2) { return List.of(LangContent.lg1(elementLg1), LangContent.lg2(elementLg2)); } + protected List constructLangContent1(String elementLg1) { + return List.of(LangContent.lg1(elementLg1)); + } + + protected List constructLangContent2(String elementLg2) { + return List.of(LangContent.lg2(elementLg2)); + } + + protected List constructLangContentList(String stringListLg, String lg) { List listLg= List.of(stringListLg.split(",")); List rep = new ArrayList<>(); @@ -615,7 +657,6 @@ protected List constructLangContentList(String stringListLg, String return rep; } - private IdLabel constructIdLabel(String id, String labelLg1, String labelLg2) { List langContentList = constructLangContent(labelLg1,labelLg2); return new IdLabel(id,langContentList); @@ -626,7 +667,6 @@ private WasDerivedFrom constructWasDerivedFrom(List 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); diff --git a/src/main/resources/queries/dataSets/getDatasetById/getDataSetByIdCreator.ftlh b/src/main/resources/queries/dataSets/getDatasetById/getDataSetByIdCreator.ftlh index 7cbda3bd..86eb57ac 100644 --- a/src/main/resources/queries/dataSets/getDatasetById/getDataSetByIdCreator.ftlh +++ b/src/main/resources/queries/dataSets/getDatasetById/getDataSetByIdCreator.ftlh @@ -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 . diff --git a/src/main/resources/queries/dataSets/getDatasetById/getDataSetByIdCreatorInformation.ftlh b/src/main/resources/queries/dataSets/getDatasetById/getDataSetByIdCreatorInformation.ftlh new file mode 100644 index 00000000..dee7fae6 --- /dev/null +++ b/src/main/resources/queries/dataSets/getDatasetById/getDataSetByIdCreatorInformation.ftlh @@ -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}') .} +} \ No newline at end of file diff --git a/src/test/java/fr/insee/rmes/services/datasets/DataSetsImplTest.java b/src/test/java/fr/insee/rmes/services/datasets/DataSetsImplTest.java index d6323071..daadaf45 100644 --- a/src/test/java/fr/insee/rmes/services/datasets/DataSetsImplTest.java +++ b/src/test/java/fr/insee/rmes/services/datasets/DataSetsImplTest.java @@ -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; @@ -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"); + 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 creatorUris = List.of(" Insee ","Ined ","Ine d","Ined"); + List creatorUrisWithoutExternalSpaces = new ArrayList<>(); + for (String s : creatorUris) {creatorUrisWithoutExternalSpaces.add(s.trim());} + List 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); @@ -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); @@ -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) @@ -248,8 +292,6 @@ void testConstructLangContentList(){ assertThat(actual_1).isEqualTo(expected_1); assertThat(actual_2).isEqualTo(expected_2); - } - - + } \ No newline at end of file