Skip to content

Commit

Permalink
684 complter la suppression dun jeu de donnes (#705)
Browse files Browse the repository at this point in the history
* feat: delete dataset completion

delete the catalogRecord associated with the dataset
delete any alternative identifier associated with the dataset
block deletion if a derived dataset exists

* feat: clean database after dataset deletion

* test: let see with Fabrice

* resolve conflicts

* refactor: delete unuseful code
  • Loading branch information
GtanSndil authored Jul 22, 2024
1 parent af2840b commit b02b290
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,37 @@ public static String getDatasetContributors(IRI iri, String datasetsGraph) throw
Map<String, Object> params = Map.of("GRAPH", datasetsGraph, "IRI", iri, "PREDICATE", "dc:contributor");
return FreeMarkerUtils.buildRequest("common/", "getContributors.ftlh", params);
}

public static String getDerivedDataset(String id, String datasetsGraph) throws RmesException {
HashMap<String, Object> params = new HashMap<>();
params.put(DATASET_GRAPH, datasetsGraph);
params.put("ID", id);
return FreeMarkerUtils.buildRequest(ROOT_DIRECTORY, "getDerivedDataset.ftlh", params);
}

public static String deleteTempWhiteNode(String id, String datasetsGraph) throws RmesException {
HashMap<String, Object> params = new HashMap<>();
params.put(DATASET_GRAPH, datasetsGraph);
params.put("ID", id);
return FreeMarkerUtils.buildRequest(ROOT_DIRECTORY, "deleteDatasetTemporalCoverageWhiteNode.ftlh", params);
}

public static String getDatasetDerivedFrom(String id, String datasetsGraph) throws RmesException {
HashMap<String, Object> params = new HashMap<>();
params.put(DATASET_GRAPH, datasetsGraph);
params.put("ID", id);
return FreeMarkerUtils.buildRequest(ROOT_DIRECTORY, "getDatasetDerivedFrom.ftlh", params);
}



public static String deleteDatasetQualifiedDerivationWhiteNode(String id, String datasetsGraph) throws RmesException {
HashMap<String, Object> params = new HashMap<>();
params.put(DATASET_GRAPH, datasetsGraph);
params.put("ID", id);
return FreeMarkerUtils.buildRequest(ROOT_DIRECTORY, "deleteDatasetQualifiedDerivationWhiteNode.ftlh", params);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import org.eclipse.rdf4j.model.BNode;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;


import static fr.insee.rmes.exceptions.ErrorCodes.DATASET_PATCH_INCORRECT_BODY;

@Service
Expand Down Expand Up @@ -277,31 +282,74 @@ public void patchDataset(String datasetId, PatchDataset patchDataset) throws Rme
@Override
public void deleteDatasetId(String datasetId) throws RmesException{
Dataset dataset = getDatasetByID(datasetId);
if (!isUnpublished(dataset)){
throw new RmesBadRequestException(ErrorCodes.DATASET_DELETE_ONLY_UNPUBLISHED, "Only unpublished datasets can be deleted");
if (isPublished(dataset)){
throw new RmesBadRequestException(ErrorCodes.DATASET_DELETE_ONLY_UNPUBLISHED, "Only unpublished datasets can be deleted");
}

if (hasDistribution(dataset)) {
throw new RmesBadRequestException(ErrorCodes.DATASET_DELETE_ONLY_WITHOUT_DISTRIBUTION, "Only dataset without any distribution can be deleted");
}

if (hasDerivedDataset(dataset)) {
throw new RmesBadRequestException(ErrorCodes.DATASET_DELETE_ONLY_WITHOUT_DERIVED_DATASET, "Only dataset without any derived dataset can be deleted");
}

IRI datasetIRI = RdfUtils.createIRI(getDatasetsBaseUri());
IRI graph = getDatasetIri(datasetId);
String datasetURI = getDatasetsBaseUri() + "/" + datasetId;
IRI catalogRecordIRI = RdfUtils.createIRI(getCatalogRecordBaseUri() + "/" + datasetId);
IRI datasetAdmsIri = RdfUtils.createIRI(getDatasetsAdmsBaseUri() + "/" + datasetId);

if (hasTemporalCoverage(dataset)){
deleteTemporalWhiteNode(datasetId);
}

if (isDerivedFromADataset(dataset)){
deleteQualifiedDerivationWhiteNode(datasetId);
}
repoGestion.deleteObject(RdfUtils.toURI(datasetURI));
repoGestion.deleteObject(catalogRecordIRI);
repoGestion.deleteObject(datasetAdmsIri);
repoGestion.deleteTripletByPredicate(datasetIRI, DCAT.DATASET, graph);

}

private boolean isUnpublished(Dataset dataset) {
return "Unpublished".equalsIgnoreCase(dataset.getValidationState());

private boolean isPublished(Dataset dataset) {
return !"Unpublished".equalsIgnoreCase(dataset.getValidationState());
}

private boolean hasDistribution(Dataset dataset) throws RmesException {
String datasetId = dataset.getId();
return !getDistributions(datasetId).equals("[]");
}

private boolean hasDerivedDataset(Dataset dataset) throws RmesException {
String datasetId = dataset.getId();
JSONObject datasetDerivation = this.repoGestion.getResponseAsObject(DatasetQueries.getDerivedDataset(datasetId, getDatasetsGraph()));
return (datasetDerivation.has("id"));
}

private boolean hasTemporalCoverage(Dataset dataset) {
return !(dataset.getTemporalCoverageDataType() == null);
}

private HttpStatus deleteTemporalWhiteNode(String id) throws RmesException {
HttpStatus result = repoGestion.executeUpdate(DatasetQueries.deleteTempWhiteNode(id, getDatasetsGraph()));
return result;
}

private boolean isDerivedFromADataset(Dataset dataset) throws RmesException {
String datasetId = dataset.getId();
JSONObject datasetDerivedFrom = this.repoGestion.getResponseAsObject(DatasetQueries.getDatasetDerivedFrom(datasetId, getDatasetsGraph()));
return (!datasetDerivedFrom.optString("wasDerivedFromS").isEmpty());
}

private HttpStatus deleteQualifiedDerivationWhiteNode(String id) throws RmesException {
HttpStatus result = repoGestion.executeUpdate(DatasetQueries.deleteDatasetQualifiedDerivationWhiteNode(id, getDatasetsGraph()));
return result;
}

private void persistCatalogRecord(Dataset dataset) throws RmesException {
Resource graph = RdfUtils.createIRI(getDatasetsGraph());
IRI catalogRecordIRI = RdfUtils.createIRI(getCatalogRecordBaseUri() + "/" + dataset.getId());
Expand Down
1 change: 1 addition & 0 deletions src/main/java/fr/insee/rmes/exceptions/ErrorCodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public class ErrorCodes {
public static final int DISTRIBUTION_DELETE_ONLY_UNPUBLISHED = 1203;
public static final int DATASET_DELETE_ONLY_UNPUBLISHED = 1203 ;
public static final int DATASET_DELETE_ONLY_WITHOUT_DISTRIBUTION = 1204 ;
public static final int DATASET_DELETE_ONLY_WITHOUT_DERIVED_DATASET = 1205 ;


/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DELETE WHERE {
GRAPH <http://rdf.insee.fr/graphes/catalogue>
{
?uri rdf:type dcat:Dataset .
?uri prov:qualifiedDerivation ?node .
?uri dcterms:identifier '${ID}' .
?node rdf:type prov:Derivation .
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DELETE WHERE {
GRAPH <${DATASET_GRAPH}>
{
?uri rdf:type dcat:Dataset .
?uri dcterms:temporal ?node .
?uri dcterms:identifier '${ID}' .
?node rdf:type dcterms:PeriodOfTime .
}
}
21 changes: 21 additions & 0 deletions src/main/resources/request/dataset/getDatasetDerivedFrom.ftlh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
SELECT DISTINCT
?id
?uri
(GROUP_CONCAT(DISTINCT ?wasDerivedFromS; separator=" , ") AS ?wasDerivedFromS)

FROM <http://rdf.insee.fr/graphes/catalogue>

WHERE {
?uri a dcat:Dataset ;
dcterms:identifier '${ID}' ;
dcterms:identifier ?id ;

OPTIONAL {?uri prov:wasDerivedFrom ?wasDerivedFrom .
?wasDerivedFrom dcterms:identifier ?wasDerivedFromS
} .


}
GROUP BY ?id ?uri


9 changes: 9 additions & 0 deletions src/main/resources/request/dataset/getDerivedDataset.ftlh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SELECT DISTINCT ?id
FROM <${DATASET_GRAPH}>
WHERE {
?uri a dcat:Dataset ;
dcterms:identifier ?id .
?uri prov:wasDerivedFrom ?wasDerivedFrom .
?wasDerivedFrom dcterms:identifier '${ID}' .
}
GROUP BY ?id
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class DatasetServiceImplTest {
}
]""";
public static final String EMPTY_ARRAY = "[]";
public static final String QUASI_EMPTY_OBJECT = "{\"uri\":\"\"}";

@Test
void shouldReturnDatasets() throws RmesException {
Expand Down Expand Up @@ -669,6 +670,7 @@ void shouldDeleteDataSet() throws RmesException{
"}\n" +
"]");
JSONArray empty_array = new JSONArray(EMPTY_ARRAY);
JSONObject quasi_empty_object = new JSONObject(QUASI_EMPTY_OBJECT);

String stringDatasetURI="http://bauhaus/catalogues/entreeCatalogue/idtest";
IRI datasetUri= RdfUtils.toURI(stringDatasetURI);
Expand All @@ -689,6 +691,12 @@ void shouldDeleteDataSet() throws RmesException{
distributionQueriesMock.when(() -> DistributionQueries.getDatasetDistributions(any(), any())).thenReturn("query3 ");
when(repositoryGestion.getResponseAsArray("query3 ")).thenReturn(empty_array);

datasetQueriesMock.when(() -> DatasetQueries.getDerivedDataset(any(), any())).thenReturn("query4 ");
when(repositoryGestion.getResponseAsObject("query4 ")).thenReturn(quasi_empty_object);

datasetQueriesMock.when(() -> DatasetQueries.getDatasetDerivedFrom(any(), any())).thenReturn("query5 ");
when(repositoryGestion.getResponseAsObject("query5 ")).thenReturn(quasi_empty_object);

rdfUtilsMock.when(() -> RdfUtils.createIRI(any(String.class))).thenReturn(datasetUri);
rdfUtilsMock.when(() -> RdfUtils.toURI(any(String.class))).thenReturn(datasetUri);

Expand All @@ -697,10 +705,10 @@ void shouldDeleteDataSet() throws RmesException{

datasetService.deleteDatasetId("idTest");

verify(repositoryGestion, times(1)).deleteObject(uriCaptor.capture());
verify(repositoryGestion, times(3)).deleteObject(uriCaptor.capture());
Assertions.assertEquals(datasetUri, uriCaptor.getValue());

verify(repositoryGestion, times(1)).deleteObject(datasetUri);
verify(repositoryGestion, times(3)).deleteObject(datasetUri);
verify(repositoryGestion, times(1)).deleteTripletByPredicate(any(IRI.class), eq(DCAT.DATASET), any(IRI.class));
}
}
Expand Down
Loading

0 comments on commit b02b290

Please sign in to comment.