Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/datasets distributions #416

Merged
merged 13 commits into from
Oct 23, 2023
16 changes: 15 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,27 @@
<artifactId>java-jwt</artifactId>
<version>${java-jwt.version}</version>
</dependency>




<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.5.1</version>
<scope>test</scope>
</dependency>

<!-- SLF4J / LOG4J -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package fr.insee.rmes.bauhaus_services.datasets;

import fr.insee.rmes.bauhaus_services.rdf_utils.FreeMarkerUtils;
import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.persistance.sparql_queries.GenericQueries;

import java.util.HashMap;

public class DatasetQueries extends GenericQueries {

private static final String ROOT_DIRECTORY = "dataset/";

public static String getDatasets() throws RmesException {
HashMap<String, Object> params = new HashMap<>();
params.put("DATASET_GRAPH", config.getDatasetsGraph());
params.put("LG1", config.getLg1());
return FreeMarkerUtils.buildRequest(ROOT_DIRECTORY, "getDatasets.ftlh", params);
}

public static String getDataset(String id) throws RmesException {
HashMap<String, Object> params = new HashMap<>();
params.put("DATASET_GRAPH", config.getDatasetsGraph());
params.put("LG1", config.getLg1());
params.put("LG2", config.getLg2());
params.put("ID", id);
return FreeMarkerUtils.buildRequest(ROOT_DIRECTORY, "getDataset.ftlh", params);
}

public static String lastDatasetId() throws RmesException {
HashMap<String, Object> params = new HashMap<>();
params.put("DATASET_GRAPH", config.getDatasetsGraph());

return FreeMarkerUtils.buildRequest(ROOT_DIRECTORY, "getLastDatasetId.ftlh", params);
}

public static String getThemes() throws RmesException {
HashMap<String, Object> params = new HashMap<>();
params.put("CONCEPTS_GRAPH", config.getBaseGraph() + config.getDatasetsThemeGraph());
params.put("THEME_TYPE", config.getBaseUriGestion() + config.getDatasetsThemeTypePrefix());
return FreeMarkerUtils.buildRequest(ROOT_DIRECTORY, "getThemes.ftlh", params);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package fr.insee.rmes.bauhaus_services.datasets;

import fr.insee.rmes.exceptions.RmesException;

public interface DatasetService {

String getDatasets() throws RmesException;

String getDatasetByID(String id) throws RmesException ;

String update(String datasetId, String body) throws RmesException;

String create(String body) throws RmesException;

String getDistributions(String id) throws RmesException;

String getThemes() throws RmesException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package fr.insee.rmes.bauhaus_services.datasets;

import fr.insee.rmes.bauhaus_services.distribution.DistributionQueries;
import fr.insee.rmes.bauhaus_services.operations.series.SeriesUtils;
import fr.insee.rmes.bauhaus_services.rdf_utils.RdfService;
import fr.insee.rmes.bauhaus_services.rdf_utils.RdfUtils;
import fr.insee.rmes.exceptions.RmesBadRequestException;
import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.model.ValidationStatus;
import fr.insee.rmes.model.dataset.Dataset;
import fr.insee.rmes.persistance.ontologies.INSEE;
import fr.insee.rmes.utils.DateUtils;
import fr.insee.rmes.utils.Deserializer;
import fr.insee.rmes.utils.IdGenerator;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.impl.LinkedHashModel;
import org.eclipse.rdf4j.model.vocabulary.DCAT;
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
import org.eclipse.rdf4j.model.vocabulary.PROV;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DatasetServiceImpl extends RdfService implements DatasetService {

@Autowired
EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved
SeriesUtils seriesUtils;

@Override
public String getDatasets() throws RmesException {
return this.repoGestion.getResponseAsArray(DatasetQueries.getDatasets()).toString();
}

@Override
public String getDatasetByID(String id) throws RmesException {
return this.repoGestion.getResponseAsObject(DatasetQueries.getDataset(id)).toString();
}

@Override
public String update(String datasetId, String body) throws RmesException {

Dataset dataset = Deserializer.deserializeBody(body, Dataset.class);
dataset.setId(datasetId);

if(ValidationStatus.VALIDATED.toString().equalsIgnoreCase(dataset.getValidationState())){
dataset.setValidationState(ValidationStatus.MODIFIED.toString());
}
if(dataset.getIdSerie() != null){
dataset.setIdSerie(RdfUtils.seriesIRI(dataset.getIdSerie()).toString());
}

this.validate(dataset);

dataset.setUpdated(DateUtils.getCurrentDate());

return this.persist(dataset);
}

@Override
public String create(String body) throws RmesException {
Dataset dataset = Deserializer.deserializeBody(body, Dataset.class);
dataset.setId(IdGenerator.generateNextId(repoGestion.getResponseAsObject(DatasetQueries.lastDatasetId()), "jd"));
dataset.setValidationState(ValidationStatus.UNPUBLISHED.toString());

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


this.validate(dataset);

dataset.setCreated(DateUtils.getCurrentDate());
dataset.setUpdated(dataset.getCreated());

return this.persist(dataset);
}

@Override
public String getDistributions(String id) throws RmesException {
return this.repoGestion.getResponseAsArray(DistributionQueries.getDatasetDistributions(id)).toString();
}

@Override
public String getThemes() throws RmesException {
return this.repoGestion.getResponseAsArray(DatasetQueries.getThemes()).toString();
}


private String persist(Dataset dataset) throws RmesException {
Resource graph = RdfUtils.datasetGraph();

IRI datasetIri = RdfUtils.datasetIRI(dataset.getId());

Model model = new LinkedHashModel();

model.add(datasetIri, DCTERMS.IDENTIFIER, RdfUtils.setLiteralString(dataset.getId()), graph);
model.add(datasetIri, RDF.TYPE, DCAT.DATASET, graph);
model.add(datasetIri, DCTERMS.TITLE, RdfUtils.setLiteralString(dataset.getLabelLg1(), config.getLg1()), graph);
model.add(datasetIri, DCTERMS.TITLE, RdfUtils.setLiteralString(dataset.getLabelLg2(), config.getLg2()), graph);
model.add(datasetIri, DCTERMS.CREATOR, RdfUtils.setLiteralString(dataset.getCreator()), graph);
model.add(datasetIri, DCTERMS.CONTRIBUTOR, RdfUtils.setLiteralString(dataset.getContributor()), graph);

RdfUtils.addTripleString(datasetIri, DCTERMS.DESCRIPTION, dataset.getDescriptionLg1(), config.getLg1(), model, graph);
RdfUtils.addTripleString(datasetIri, DCTERMS.DESCRIPTION, dataset.getDescriptionLg2(), config.getLg2(), model, graph);

RdfUtils.addTripleDateTime(datasetIri, DCTERMS.CREATED, dataset.getCreated(), model, graph);
RdfUtils.addTripleDateTime(datasetIri, DCTERMS.MODIFIED, dataset.getUpdated(), model, graph);

RdfUtils.addTripleUri(datasetIri, INSEE.DISSEMINATIONSTATUS, dataset.getDisseminationStatus(), model, graph);
RdfUtils.addTripleString(datasetIri, INSEE.VALIDATION_STATE, dataset.getValidationState(), model, graph);
RdfUtils.addTripleUri(datasetIri, PROV.WAS_GENERATED_BY, dataset.getIdSerie(), model, graph);
RdfUtils.addTripleUri(datasetIri, DCAT.THEME, dataset.getTheme(), model, graph);

JSONArray distributions = new JSONArray(this.getDistributions(dataset.getId()));

for(int i = 0; i < distributions.length(); i++) {
JSONObject distribution = distributions.getJSONObject(i);
if (distribution.has("id")) {
String id = distribution.getString("id");
RdfUtils.addTripleUri(datasetIri, DCAT.DISTRIBUTION, RdfUtils.distributionIRI(id), model, graph);
}
}

repoGestion.loadSimpleObject(datasetIri, model, null);

return dataset.getId();
}

private void validate(Dataset dataset) throws RmesException {
if (dataset.getLabelLg1() == null) {
throw new RmesBadRequestException("The property labelLg1 is required");
}
if (dataset.getLabelLg2() == null) {
throw new RmesBadRequestException("The property labelLg2 is required");
}
if (dataset.getCreator() == null) {
throw new RmesBadRequestException("The property creator is required");
}
if (dataset.getContributor() == null) {
throw new RmesBadRequestException("The property contributor is required");
}
if (dataset.getDisseminationStatus() == null) {
throw new RmesBadRequestException("The property disseminationStatus is required");
}
if(!this.seriesUtils.isSeriesExist(dataset.getIdSerie())){
throw new RmesBadRequestException("The series does not exist");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package fr.insee.rmes.bauhaus_services.distribution;

import fr.insee.rmes.bauhaus_services.rdf_utils.FreeMarkerUtils;
import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.persistance.sparql_queries.GenericQueries;

import java.util.HashMap;

public class DistributionQueries extends GenericQueries {

private static final String ROOT_DIRECTORY = "distribution/";

public static String getDistributions() throws RmesException {
HashMap<String, Object> params = new HashMap<>();
params.put("DATASET_GRAPH", config.getDistributionsGraph());
params.put("DATASET_ID", "");
params.put("LG1", config.getLg1());
params.put("LG2", config.getLg2());
return FreeMarkerUtils.buildRequest(ROOT_DIRECTORY, "getDistributions.ftlh", params);
}

public static String getDistribution(String id) throws RmesException {
HashMap<String, Object> params = new HashMap<>();
params.put("DATASET_GRAPH", config.getDistributionsGraph());
params.put("LG1", config.getLg1());
params.put("LG2", config.getLg2());
params.put("ID", id);
return FreeMarkerUtils.buildRequest(ROOT_DIRECTORY, "getDistribution.ftlh", params);
}

public static String getDatasetDistributions(String id) throws RmesException {
HashMap<String, Object> params = new HashMap<>();
params.put("DATASET_GRAPH", config.getDistributionsGraph());
params.put("LG1", config.getLg1());
params.put("LG2", config.getLg2());
params.put("DATASET_ID", id);
return FreeMarkerUtils.buildRequest(ROOT_DIRECTORY, "getDistributions.ftlh", params);

}

public static String lastDatasetId() throws RmesException {
HashMap<String, Object> params = new HashMap<>();
params.put("DATASET_GRAPH", config.getDistributionsGraph());
return FreeMarkerUtils.buildRequest(ROOT_DIRECTORY, "getLastDatasetId.ftlh", params);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fr.insee.rmes.bauhaus_services.distribution;

import fr.insee.rmes.exceptions.RmesException;

public interface DistributionService {

String getDistributions() throws RmesException;

String getDistributionByID(String id) throws RmesException;

String create(String body) throws RmesException;

String update(String id, String body) throws RmesException;
}
Loading