Skip to content

Commit

Permalink
Merge pull request #190 from vincentmigot/jena-insert-sensor-profile
Browse files Browse the repository at this point in the history
Jena #4 - Use Jena for Sensor Profiles INSERT and DELETE
  • Loading branch information
annetireau authored Jan 11, 2019
2 parents 85aa7c8 + 1c88863 commit 0125a78
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ public int getLastId() {
BindingSet bindingSet = result.next();
uri = bindingSet.getValue(URI).stringValue();
}

if (uri == null) {
return 0;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

import java.util.ArrayList;
import java.util.List;
import org.apache.jena.arq.querybuilder.UpdateBuilder;
import org.apache.jena.graph.Node;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.update.UpdateRequest;
import org.apache.jena.vocabulary.RDF;
import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.query.MalformedQueryException;
import org.eclipse.rdf4j.query.QueryEvaluationException;
Expand All @@ -30,12 +38,10 @@
import phis2ws.service.ontologies.Rdf;
import phis2ws.service.ontologies.Rdfs;
import phis2ws.service.ontologies.Vocabulary;
import phis2ws.service.resources.dto.rdfResourceDefinition.PropertyDTO;
import phis2ws.service.resources.dto.SensorProfileDTO;
import phis2ws.service.resources.dto.rdfResourceDefinition.PropertyPostDTO;
import phis2ws.service.utils.POSTResultsReturn;
import phis2ws.service.utils.sparql.SPARQLQueryBuilder;
import phis2ws.service.utils.sparql.SPARQLUpdateBuilder;
import phis2ws.service.view.brapi.Status;
import phis2ws.service.view.model.phis.Ask;
import phis2ws.service.view.model.phis.Property;
Expand Down Expand Up @@ -172,20 +178,28 @@ public POSTResultsReturn check(List<SensorProfileDTO> sensorProfiles) {
* }
* }
*/
private SPARQLUpdateBuilder prepareInsertQuery(SensorProfile sensorProfile) {
SPARQLUpdateBuilder query = new SPARQLUpdateBuilder();
private UpdateRequest prepareInsertQuery(SensorProfile sensorProfile) {
UpdateBuilder spql = new UpdateBuilder();

query.appendGraphURI(Contexts.SENSORS.toString());
Node graph = NodeFactory.createURI(Contexts.SENSORS.toString());
Resource sensorProfileUri = ResourceFactory.createResource(sensorProfile.getUri());

for (Property property : sensorProfile.getProperties()) {
org.apache.jena.rdf.model.Property propertyRelation = ResourceFactory.createProperty(property.getRelation());

if (property.getRdfType() != null) {
query.appendTriplet(sensorProfile.getUri(), property.getRelation(), property.getValue(), null);
query.appendTriplet(property.getValue(), Rdf.RELATION_TYPE.toString(), property.getRdfType(), null);
Node propertyValue = NodeFactory.createURI(property.getValue());
spql.addInsert(graph, sensorProfileUri, propertyRelation, propertyValue);
spql.addInsert(graph,propertyValue, RDF.type, property.getRdfType());
} else {
query.appendTriplet(sensorProfile.getUri(), property.getRelation(), "\"" + property.getValue() + "\"", null);
Literal propertyValue = ResourceFactory.createStringLiteral(property.getValue());
spql.addInsert(graph, sensorProfileUri, propertyRelation, propertyValue);
}
}

UpdateRequest query = spql.buildRequest();
LOGGER.debug(SPARQL_SELECT_QUERY + " " + query.toString());

return query;
}

Expand All @@ -207,7 +221,7 @@ private POSTResultsReturn insert(List<SensorProfileDTO> sensorsProfiles) {

this.getConnection().begin();
for (SensorProfileDTO sensorProfileDTO : sensorsProfiles) {
SPARQLUpdateBuilder query = prepareInsertQuery(sensorProfileDTO.createObjectFromDTO());
UpdateRequest query = prepareInsertQuery(sensorProfileDTO.createObjectFromDTO());
Update prepareUpdate = getConnection().prepareUpdate(QueryLanguage.SPARQL, query.toString());
prepareUpdate.execute();

Expand Down

0 comments on commit 0125a78

Please sign in to comment.