Skip to content

Commit

Permalink
feat: review
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey committed Nov 19, 2024
1 parent b93a7ec commit 1417616
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +32,11 @@ public SeriesPublication(ParentUtils ownersUtils) {
this.ownersUtils = ownersUtils;
}

private static void checkIfSeriesExist(String id, RepositoryResult<Statement> 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);
Expand All @@ -48,14 +54,13 @@ public void publishSeries(String id, JSONObject series) throws RmesException {
RepositoryConnection con = repoGestion.getConnection();
RepositoryResult<Statement> statements = repoGestion.getStatements(con, resource);

checkIfSeriesExist(id, statements);

RepositoryResult<Statement> hasPartStatements = repoGestion.getHasPartStatements(con, resource);
RepositoryResult<Statement> replacesStatements = repoGestion.getReplacesStatements(con, resource);
RepositoryResult<Statement> 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());
Expand Down Expand Up @@ -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 {
Expand All @@ -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<Statement> statements) {
while (statements.hasNext()) {
Statement statement = statements.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1417616

Please sign in to comment.