Skip to content

Commit

Permalink
feat: be able to define multiple wasGeneratedBy for dataset (#703)
Browse files Browse the repository at this point in the history
* feat: be able to define multiple wasGeneratedBy for dataset

* feat: add missing file

* feat: add unit test

* feat: add unit test

* feat: define ID directly in the sparql query

* fix: solve review

* fix: test
  • Loading branch information
EmmanuelDemey authored Jul 26, 2024
1 parent 0ae0b3f commit d2321a9
Show file tree
Hide file tree
Showing 21 changed files with 732 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.insee.rmes.bauhaus_services;

import fr.insee.rmes.config.swagger.model.IdLabelTwoLangs;
import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.model.operations.Indicator;
import fr.insee.rmes.model.operations.Operation;
Expand Down Expand Up @@ -43,8 +42,6 @@ public interface OperationsService {

String getSeriesJsonByID(String id) throws RmesException;

IdLabelTwoLangs getSeriesLabelByID(String id) throws RmesException;

String getSeriesWithSims() throws RmesException;

String getSeriesWithStamp(String stamp) throws RmesException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ private static String getDatasetArrays(String path, String datasetsGraph, String
return FreeMarkerUtils.buildRequest(ROOT_DIRECTORY, path, params);
}

public static String getDatasetWasGeneratedIris(String id, String datasetsGraph) throws RmesException {
return getDatasetArrays("getDatasetWasGeneratedIris.ftlh", datasetsGraph, id);
}

public static String getDatasetCreators(String id, String datasetsGraph) throws RmesException {
return getDatasetArrays("getDatasetCreators.ftlh", datasetsGraph, id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public Dataset getDatasetByID(String id) throws RmesException {
dataset.remove(THEME);

getMultipleTripletsForObject(dataset, "creators", DatasetQueries.getDatasetCreators(id, getDatasetsGraph()), CREATOR);

getMultipleTripletsForObject(dataset, "wasGeneratedIRIs", DatasetQueries.getDatasetWasGeneratedIris(id, getDatasetsGraph()), "iri");
IRI catalogRecordIRI = RdfUtils.createIRI(getCatalogRecordBaseUri() + "/" + id);
getMultipleTripletsForObject(dataset, "spacialResolutions", DatasetQueries.getDatasetSpacialResolutions(id, getDatasetsGraph()), "spacialResolution");
getMultipleTripletsForObject(dataset, "statisticalUnit", DatasetQueries.getDatasetStatisticalUnits(id, getDatasetsGraph()), "statisticalUnit");
Expand Down Expand Up @@ -186,9 +186,6 @@ private String update(String datasetId, Dataset dataset) throws RmesException {
if(ValidationStatus.VALIDATED.toString().equalsIgnoreCase(dataset.getValidationState())){
dataset.setValidationState(ValidationStatus.MODIFIED.toString());
}
if(dataset.getIdSerie() != null){
dataset.setIdSerie(RdfUtils.seriesIRI(dataset.getIdSerie()).toString());
}

if(dataset.getCatalogRecord() == null){
dataset.setCatalogRecord(new CatalogRecord());
Expand All @@ -213,10 +210,6 @@ public String create(String body) throws RmesException {
dataset.setId(idGenerator.generateNextId());
dataset.setValidationState(ValidationStatus.UNPUBLISHED.toString());

if(dataset.getIdSerie() != null){
dataset.setIdSerie(RdfUtils.seriesIRI(dataset.getIdSerie()).toString());
}

if(dataset.getCatalogRecord() == null){
dataset.setCatalogRecord(new CatalogRecord());
}
Expand Down Expand Up @@ -472,7 +465,10 @@ private void persistDataset(Dataset dataset) throws RmesException {
this.persistStatisticsInformations(datasetIri, dataset, model, graph);

RdfUtils.addTripleString(datasetIri, INSEE.VALIDATION_STATE, dataset.getValidationState(), model, graph);
RdfUtils.addTripleUri(datasetIri, PROV.WAS_GENERATED_BY, dataset.getIdSerie(), model, graph);

if(dataset.getWasGeneratedIRIs() != null) {
dataset.getWasGeneratedIRIs().forEach(iri -> RdfUtils.addTripleUri(datasetIri, PROV.WAS_GENERATED_BY, iri, model, graph));
}

if(dataset.getThemes() != null){
dataset.getThemes().forEach(theme -> RdfUtils.addTripleUri(datasetIri, DCAT.THEME, theme, model, graph));
Expand Down Expand Up @@ -517,8 +513,9 @@ private void validate(Dataset dataset) throws RmesException {
if (dataset.getAltIdentifier() != null && !ALT_IDENTIFIER_PATTERN.matcher(dataset.getAltIdentifier()).matches()) {
throw new RmesBadRequestException("The property altIdentifier contains forbidden characters");
}
if(!this.seriesUtils.isSeriesExist(dataset.getIdSerie())){
throw new RmesBadRequestException("The series does not exist");

if(!this.seriesUtils.isSeriesAndOperationsExist(dataset.getWasGeneratedIRIs())){
throw new RmesBadRequestException("Some series or operations do not exist");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import fr.insee.rmes.bauhaus_services.operations.series.SeriesUtils;
import fr.insee.rmes.bauhaus_services.rdf_utils.QueryUtils;
import fr.insee.rmes.bauhaus_services.rdf_utils.RdfService;
import fr.insee.rmes.config.swagger.model.IdLabelTwoLangs;
import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.external.services.export.XDocReport;
import fr.insee.rmes.model.operations.Indicator;
Expand Down Expand Up @@ -127,10 +126,6 @@ public Series getSeriesByID(String id) throws RmesException {
return seriesUtils.getSeriesById(id,EncodingType.MARKDOWN);
}

@Override
public IdLabelTwoLangs getSeriesLabelByID(String id) throws RmesException {
return seriesUtils.getSeriesLabelById(id);
}

/**
* Return the series in a JSONObject encoding in markdown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public JSONObject getSeriesJsonById(String id, EncodingType encode) throws RmesE
if (EncodingType.MARKDOWN.equals(encode)) {
XhtmlToMarkdownUtils.convertJSONObject(series);
}
series.put(Constants.ID, id);

addSeriesOperations(id, series);
addSeriesFamily(id, series);
addSeriesLinks(id, series);
Expand Down Expand Up @@ -508,8 +508,9 @@ public String setSeriesValidation(String id) throws RmesException {
return id;
}

public boolean isSeriesExist(String iri) throws RmesException {
return repositoryGestion.getResponseAsBoolean(OpSeriesQueries.checkIfSeriesExists(iri));
public boolean isSeriesAndOperationsExist(List<String> iris) throws RmesException {
var length = repositoryGestion.getResponseAsArray(OpSeriesQueries.checkIfSeriesExists(iris)).length();
return length == iris.size();
}

}
10 changes: 5 additions & 5 deletions src/main/java/fr/insee/rmes/model/dataset/Dataset.java
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public void setSpacialResolutions(List<String> spacialResolutions) {
private String cautionLg1;
private String cautionLg2;
private String disseminationStatus;
private String idSerie;
private List<String> wasGeneratedIRIs;
private List<String> themes;
private String validationState;

Expand Down Expand Up @@ -351,12 +351,12 @@ public void setDisseminationStatus(String disseminationStatus) {
this.disseminationStatus = disseminationStatus;
}

public String getIdSerie() {
return idSerie;
public List<String> getWasGeneratedIRIs() {
return wasGeneratedIRIs;
}

public void setIdSerie(String idSerie) {
this.idSerie = idSerie;
public void setWasGeneratedIRIs(List<String> wasGeneratedIds) {
this.wasGeneratedIRIs = wasGeneratedIds;
}

public List<String> getThemes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,12 @@ public static String checkPrefLabelUnicity(String id, String label, String lang)
return FreeMarkerUtils.buildRequest("operations/", "checkFamilyPrefLabelUnicity.ftlh", params);
}

public static String operationsQuery() {
return "SELECT DISTINCT ?id ?label (group_concat(?altLabelLg1;separator=' || ') as ?altLabel) \n"
+ "WHERE { GRAPH <"+config.getOperationsGraph()+"> { \n"
+ "?operation a insee:StatisticalOperation . \n"
+ "?operation skos:prefLabel ?label . \n"
+ "FILTER (lang(?label) = '" + config.getLg1() + "') \n"
+ "BIND(STRAFTER(STR(?operation),'/operations/operation/') AS ?id) . \n"
+ "OPTIONAL{?operation skos:altLabel ?altLabelLg1 . "
+ "FILTER (lang(?altLabelLg1) = '" + config.getLg1() + "')}\n"
+ "}} \n"
+ "GROUP BY ?id ?label \n"
+ "ORDER BY ?label ";
public static String operationsQuery() throws RmesException {
HashMap<String, Object> params = new HashMap<>();
params.put("OPERATIONS_GRAPH", config.getOperationsGraph());
params.put("LG1", config.getLg1());
params.put("LG2", config.getLg2());
return FreeMarkerUtils.buildRequest("operations/", "getOperations.ftlh", params);
}

public static String operationQuery(String id){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import org.eclipse.rdf4j.model.IRI;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class OpSeriesQueries extends GenericQueries{

private OpSeriesQueries() {
Expand Down Expand Up @@ -191,11 +193,9 @@ public static String seriesQuery() throws RmesException {
return buildSeriesRequest("getSeriesQuery.ftlh", params);
}

public static String checkIfSeriesExists(String iri) throws RmesException {
public static String checkIfSeriesExists(List<String> iris) throws RmesException {
if (params==null) {initParams();}
params.put(URI_SERIES, iri);
params.put(URI_SERIES, iris);
return buildSeriesRequest("checkIfSeriesExists.ftlh", params);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@RestController
Expand All @@ -34,7 +33,6 @@ public class DatasetResources {
final DatasetService datasetService;

public DatasetResources(DatasetService datasetService) {

this.datasetService = datasetService;
}

Expand Down
7 changes: 1 addition & 6 deletions src/main/resources/request/dataset/getDataset.ftlh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ SELECT ?id ?altIdentifier ?labelLg1 ?labelLg2 ?subTitleLg1 ?subTitleLg2 ?accrual
?disseminationStatus ?processStep ?archiveUnit
?type ?dataStructure ?timeSeriesNumber ?observationNumber ?spacialCoverage ?spacialTemporal ?temporalResolution
?temporalCoverageStartDate ?temporalCoverageEndDate (datatype(?temporalCoverageEndDate) AS ?temporalCoverageDataType)
?descriptionLg1 ?descriptionLg2 ?idSerie ?theme
?descriptionLg1 ?descriptionLg2 ?theme
?validationState ?abstractLg1 ?abstractLg2 ?cautionLg1 ?cautionLg2
?catalogRecordCreated ?catalogRecordUpdated ?catalogRecordCreator ?catalogRecordContributor

Expand Down Expand Up @@ -81,11 +81,6 @@ WHERE {
<#include "getDatasetIncludes/generalManagment.ftlh">
<#include "getDatasetIncludes/statisticsInformations.ftlh">

OPTIONAL {
?uri prov:wasGeneratedBy ?wasGeneratedBy .
BIND(STRAFTER(STR(?wasGeneratedBy),'/operations/serie/') AS ?idSerie) .
}

OPTIONAL {
?uri dcat:theme ?theme .
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SELECT ?iri
FROM <${DATASET_GRAPH}>
WHERE {
?dataset dcterms:identifier '${ID}' ;
prov:wasGeneratedBy ?iri .
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
SELECT ?id ?prefLabelLg1 ?prefLabelLg2 ?abstractLg1 ?abstractLg2 ?validationState ?created ?modified
SELECT ('${ID}' AS ?id) ?prefLabelLg1 ?prefLabelLg2 ?abstractLg1 ?abstractLg2 ?validationState ?created ?modified
WHERE {
GRAPH <${OPERATIONS_GRAPH}> {
?family skos:prefLabel ?prefLabelLg1 .
FILTER(STRENDS(STR(?family),'/operations/famille/${ID}')) .
BIND(STRAFTER(STR(?family),'/famille/') AS ?id) .

FILTER (lang(?prefLabelLg1) = '${LG1}') .

Expand Down
17 changes: 17 additions & 0 deletions src/main/resources/request/operations/getOperations.ftlh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
SELECT DISTINCT ?iri ?seriesIri ?id ?label (group_concat(?altLabelLg1;separator=' || ') as ?altLabel)
WHERE {
GRAPH <${OPERATIONS_GRAPH}> {
?iri a insee:StatisticalOperation .
?iri skos:prefLabel ?label .
FILTER (lang(?label) = '${LG1}')
BIND(STRAFTER(STR(?iri),'/operations/operation/') AS ?id) .

?seriesIri dcterms:hasPart ?iri .
OPTIONAL{
?iri skos:altLabel ?altLabelLg1 .
FILTER (lang(?altLabelLg1) = '${LG2}')
}
}
}
GROUP BY ?iri ?seriesIri ?id ?label
ORDER BY ?label
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
ASK
SELECT ?uri
FROM <${OPERATIONS_GRAPH}>
WHERE {
<${URI_SERIES}> rdf:type ?type .

<#list URI_SERIES as URI_SERIE>
{
<${URI_SERIE}> rdf:type ?type .
BIND('${URI_SERIE}' AS ?uri) .
}
<#sep> UNION </#sep>
</#list>
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SELECT ?id ?prefLabelLg1 ?prefLabelLg2 ?created ?modified ?altLabelLg1 ?altLabelLg2 ?abstractLg1 ?abstractLg2
SELECT ('${ID}' AS ?id) ?prefLabelLg1 ?prefLabelLg2 ?created ?modified ?altLabelLg1 ?altLabelLg2 ?abstractLg1 ?abstractLg2
?historyNoteLg1 ?historyNoteLg2 ?idSims ?typeCode ?typeList ?accrualPeriodicityCode ?accrualPeriodicityList ?validationState
WHERE {
FILTER(STRENDS(STR(?series),'/operations/serie/${ID}')) .
Expand Down
14 changes: 7 additions & 7 deletions src/main/resources/request/operations/series/getSeriesQuery.ftlh
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
SELECT DISTINCT ?id ?label (group_concat(?altLabelLg1;separator=' || ') as ?altLabel)
SELECT DISTINCT ?iri ?id ?label (group_concat(?altLabelLg1;separator=' || ') as ?altLabel)
<#if withSims = "true"> ?idSims
</#if>
WHERE {
GRAPH <${OPERATIONS_GRAPH}> {
?series a insee:StatisticalOperationSeries .
?series skos:prefLabel ?label .
?iri a insee:StatisticalOperationSeries .
?iri skos:prefLabel ?label .
FILTER (lang(?label) = '${LG1}')
BIND(STRAFTER(STR(?series),'/operations/serie/') AS ?id) .
BIND(STRAFTER(STR(?iri),'/operations/serie/') AS ?id) .
OPTIONAL{
?series skos:altLabel ?altLabelLg1 .
?iri skos:altLabel ?altLabelLg1 .
FILTER (lang(?altLabelLg1) = '${LG1}')
}
<#if withSims = "true">
OPTIONAL{
?report rdf:type sdmx-mm:MetadataReport .
?report sdmx-mm:target ?series
?report sdmx-mm:target ?iri
BIND(STRAFTER(STR(?report),'/rapport/') AS ?idSims) .
}
</#if>

}
}
GROUP BY ?id ?label <#if withSims = "true"> ?idSims</#if>
GROUP BY ?iri ?id ?label <#if withSims = "true"> ?idSims</#if>
ORDER BY ?label
Loading

0 comments on commit d2321a9

Please sign in to comment.