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,35 @@
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package fr.insee.rmes.bauhaus_services.datasets;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.insee.rmes.bauhaus_services.Constants;
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 org.apache.http.HttpStatus;
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;

import java.io.IOException;

@Service
public class DatasetServiceImpl extends RdfService implements DatasetService {
private static final String IO_EXCEPTION = "IOException";

@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 = deserializeBody(body);
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 = deserializeBody(body);
dataset.setId(generateNextId());
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();
}

private Dataset deserializeBody(String body) throws RmesException {
ObjectMapper mapper = new ObjectMapper();
EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved
mapper.configure(
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Dataset dataset;
try {
dataset = mapper.readValue(body, Dataset.class);
} catch (IOException e) {
EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved
throw new RmesException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), IO_EXCEPTION);
}
return dataset;
}

private String generateNextId() throws RmesException {
EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved
String prefix = "jd";
JSONObject json = repoGestion.getResponseAsObject(DatasetQueries.lastDatasetId());
if (json.isEmpty()) {
return prefix + "1000";
}
String id = json.getString(Constants.ID);
if (id.equals(Constants.UNDEFINED)) {
EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved
return prefix + "1000";
}
return prefix + (Integer.parseInt(id) + 1);
}

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.addTripleString(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