From 1417616107bf2822e5d39616f5b70b5f896de735 Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Tue, 19 Nov 2024 13:58:29 +0000 Subject: [PATCH] feat: review --- .../operations/series/SeriesPublication.java | 38 ++++++++++++------- .../series/OpSeriesQueriesTest.java | 6 +-- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesPublication.java b/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesPublication.java index 86ad9d657..6092bc01c 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesPublication.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesPublication.java @@ -11,6 +11,7 @@ import fr.insee.rmes.exceptions.RmesNotFoundException; import fr.insee.rmes.persistance.ontologies.DCTERMS; import fr.insee.rmes.persistance.sparql_queries.operations.series.OpSeriesQueries; +import fr.insee.rmes.utils.JSONUtils; import org.eclipse.rdf4j.model.Model; import org.eclipse.rdf4j.model.Resource; import org.eclipse.rdf4j.model.Statement; @@ -31,6 +32,11 @@ public SeriesPublication(ParentUtils ownersUtils) { this.ownersUtils = ownersUtils; } + private static void checkIfSeriesExist(String id, RepositoryResult statements) throws RmesNotFoundException { + if (!statements.hasNext()) { + throw new RmesNotFoundException(ErrorCodes.SERIES_UNKNOWN_ID, "Series not found", id); + } + } public void publishSeries(String id, JSONObject series) throws RmesException { String familyId = series.getJSONObject(Constants.FAMILY).getString(Constants.ID); String status = ownersUtils.getValidationStatus(familyId); @@ -48,14 +54,13 @@ public void publishSeries(String id, JSONObject series) throws RmesException { RepositoryConnection con = repoGestion.getConnection(); RepositoryResult statements = repoGestion.getStatements(con, resource); + checkIfSeriesExist(id, statements); + RepositoryResult hasPartStatements = repoGestion.getHasPartStatements(con, resource); RepositoryResult replacesStatements = repoGestion.getReplacesStatements(con, resource); RepositoryResult isReplacedByStatements = repoGestion.getIsReplacedByStatements(con, resource); try { - if (!statements.hasNext()) { - throw new RmesNotFoundException(ErrorCodes.SERIES_UNKNOWN_ID, "Series not found", id); - } while (statements.hasNext()) { Statement st = statements.next(); String pred = RdfUtils.toString(st.getPredicate()); @@ -92,17 +97,7 @@ public void publishSeries(String id, JSONObject series) throws RmesException { /** * We have to query all published operations linked to this series and publish all of them */ - JSONArray operations = repoGestion.getResponseAsArray(OpSeriesQueries.getPublishedOperationsForSeries(resource.toString())); - for (var i = 0; i < operations.length(); i++) { - JSONObject operation = operations.getJSONObject(i); - String iri = operation.getString("operation"); - model.add( - publicationUtils.tranformBaseURIToPublish(resource), - DCTERMS.HAS_PART, - publicationUtils.tranformBaseURIToPublish(RdfUtils.createIRI(iri)), - RdfUtils.operationsGraph() - ); - } + addOperationsWhoHavePartWithToModel(resource, model); } finally { @@ -115,6 +110,21 @@ public void publishSeries(String id, JSONObject series) throws RmesException { } + + private void addOperationsWhoHavePartWithToModel(Resource resource, Model model) throws RmesException { + JSONArray operations = repoGestion.getResponseAsArray(OpSeriesQueries.getPublishedOperationsForSeries(resource.toString())); + JSONUtils.stream(operations) + .map(operation -> operation.getString("operation")) + .forEach(iri -> { + model.add( + publicationUtils.tranformBaseURIToPublish(resource), + DCTERMS.HAS_PART, + publicationUtils.tranformBaseURIToPublish(RdfUtils.createIRI(iri)), + RdfUtils.operationsGraph() + ); + }); + } + public void addStatementsToModel(Model model, RepositoryResult statements) { while (statements.hasNext()) { Statement statement = statements.next(); diff --git a/src/test/java/fr/insee/rmes/testcontainers/queries/sparql_queries/operations/series/OpSeriesQueriesTest.java b/src/test/java/fr/insee/rmes/testcontainers/queries/sparql_queries/operations/series/OpSeriesQueriesTest.java index 9cf4033f9..d468e67d1 100644 --- a/src/test/java/fr/insee/rmes/testcontainers/queries/sparql_queries/operations/series/OpSeriesQueriesTest.java +++ b/src/test/java/fr/insee/rmes/testcontainers/queries/sparql_queries/operations/series/OpSeriesQueriesTest.java @@ -7,6 +7,7 @@ import fr.insee.rmes.exceptions.RmesException; import fr.insee.rmes.persistance.sparql_queries.operations.series.OpSeriesQueries; import fr.insee.rmes.testcontainers.queries.WithGraphDBContainer; +import fr.insee.rmes.utils.JSONUtils; import org.json.JSONArray; import org.json.JSONObject; import org.junit.jupiter.api.BeforeAll; @@ -135,10 +136,7 @@ void should_return_all_series() throws Exception { OpSeriesQueries.setConfig(new ConfigStub()); JSONArray result = repositoryGestion.getResponseAsArray(OpSeriesQueries.seriesWithSimsQuery()); assertEquals(174, result.length()); - - for (var i = 0; i < result.length(); i++){ - assertNotNull(result.getJSONObject(i).getString("iri")); - } + JSONUtils.stream(result).forEach(object -> assertNotNull(object.getString("iri"))); } @Test