new rest method to save datasourceApi information after records publishing on graphDB

This commit is contained in:
Enrico Ottonello 2019-12-13 14:53:15 +01:00
parent 2f5fb6fcb5
commit 3f8a4e9a46
5 changed files with 153 additions and 27 deletions

View File

@ -1,11 +1,14 @@
package eu.dnetlib.ariadneplus.graphdb;
import java.time.LocalDateTime;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.rdf4j.RDF4JException;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
@ -25,14 +28,21 @@ public class GraphDBClient {
private static final Log log = LogFactory.getLog(GraphDBClient.class);
public static final String PROVENANCE_NS = "http://www.d-net.research-infrastructures.eu/provenance/";
private RecordParserHelper recordParserHelper;
private String graphDBServerUrl;
private String graphDBBaseURI;
private String writerUser;
private String writerPwd;
protected GraphDBClient(final RecordParserHelper recordParserHelper,
final String graphDBServerUrl) {
final String graphDBServerUrl, final String graphDBBaseURI, final String writerUser, final String writerPwd) {
this.recordParserHelper = recordParserHelper;
this.graphDBServerUrl = graphDBServerUrl;
this.graphDBBaseURI = graphDBBaseURI;
this.writerUser = writerUser;
this.writerPwd = writerPwd;
}
public long feed(final String record) throws AriadnePlusPublisherException{
@ -45,7 +55,7 @@ public class GraphDBClient {
log.debug("init connection to graphDBServerUrl " + this.graphDBServerUrl);
RemoteRepositoryManager manager = new RemoteRepositoryManager(this.graphDBServerUrl);
manager.init();
manager.setUsernameAndPassword("writer", "writer01");
manager.setUsernameAndPassword(getWriterUser(), getWriterPwd());
log.debug("manager init");
Repository repository = manager.getRepository("ariadneprova");
ValueFactory factory = repository.getValueFactory();
@ -74,27 +84,49 @@ public class GraphDBClient {
}
}
//
// long feedProvenance(final String namedGraphURI, final String collectionDate, final String transformationDate, final String datasource, final String api) {
// Model md = null;
// Resource rApi = ResourceFactory.createResource(defaultBaseURI + api);
// Resource r = ResourceFactory.createResource(namedGraphURI);
// Statement stmApi =
// ResourceFactory.createStatement(rApi, IS_API_OF, ResourceFactory.createPlainLiteral(datasource));
// Statement stmCollFrom =
// ResourceFactory.createStatement(r, COLL_FROM, rApi);
// Statement stmCollDate = ResourceFactory
// .createStatement(r, COLL_IN_DATE, ResourceFactory.createTypedLiteral(collectionDate, XSDDatatype.XSDdateTime));
// Statement stmTransDate = ResourceFactory
// .createStatement(r, TRANS_IN_DATE, ResourceFactory.createTypedLiteral(transformationDate, XSDDatatype.XSDdateTime));
//
// //let's remove previous provenance statements for this resource:
// md.removeAll(r, null, null);
// //and add the new ones
// md.add(stmApi).add(stmCollFrom).add(stmCollDate).add(stmTransDate);
// md.close();
// return 3;
// }
public long feedProvenance(final String datasourceApi) throws AriadnePlusPublisherException {
try {
log.debug("init connection to graphDBServerUrl " + this.graphDBServerUrl);
RemoteRepositoryManager manager = new RemoteRepositoryManager(this.graphDBServerUrl);
manager.init();
manager.setUsernameAndPassword("writer", "writer01");
log.debug("manager init");
Repository repository = manager.getRepository("ariadneprova");
ValueFactory factory = repository.getValueFactory();
IRI IS_API_OF = factory.createIRI(PROVENANCE_NS, "isApiOf");
IRI INSERTED_IN_DATE = factory.createIRI(PROVENANCE_NS, "insertedInDate");
IRI rApi = factory.createIRI(getGraphDBBaseURI(), datasourceApi);
Statement stmApi = factory.createStatement(rApi, IS_API_OF, factory.createLiteral(datasourceApi));
LocalDateTime now = LocalDateTime.now();
Statement stmInsertedDate = factory.createStatement(rApi, INSERTED_IN_DATE, factory.createLiteral(now.toString()));
IRI datasourceApisGraph = factory.createIRI(getGraphDBBaseURI(), "datasourceApis");
try (RepositoryConnection con = repository.getConnection()) {
log.debug("connection established");
con.begin();
log.debug("Adding stmt " + stmApi.toString() + " into graph " + datasourceApisGraph.toString());
con.add(stmApi, datasourceApisGraph);
log.debug("Adding stmt " + stmInsertedDate.toString() + " into graph " + datasourceApisGraph.toString());
con.add(stmInsertedDate, datasourceApisGraph);
con.commit();
log.debug("statements added");
con.close();
}
catch (RDF4JException e) {
log.error("error adding statement ...", e);
throw new AriadnePlusPublisherException(e);
}
repository.shutDown();
manager.shutDown();
log.debug("manager shutDown");
return 200;
}
catch(Throwable e){
log.error(e);
throw new AriadnePlusPublisherException(e);
}
}
/**
* Delete all triples in named graphs collected from the given api
@ -170,6 +202,36 @@ public class GraphDBClient {
throw e;
}
}
public String getGraphDBBaseURI() {
return graphDBBaseURI;
}
public void setGraphDBBaseURI(String graphDBBaseURI) {
this.graphDBBaseURI = graphDBBaseURI;
}
public String getWriterUser() {
return writerUser;
}
public void setWriterUser(String writerUser) {
this.writerUser = writerUser;
}
public String getWriterPwd() {
return writerPwd;
}
public void setWriterPwd(String writerPwd) {
this.writerPwd = writerPwd;
}
}
//

View File

@ -19,13 +19,19 @@ public class GraphDBClientFactory {
private static final Log log = LogFactory.getLog(GraphDBClientFactory.class);
@Value("${graphdb.serverUrl}")
private String graphDBServerUrl;
@Value("${graphdb.baseURI}")
private String graphDBBaseURI;
@Value("${graphdb.writer.user}")
private String writerUser;
@Value("${graphdb.writer.pwd}")
private String writerPwd;
@Autowired
private RecordParserHelper recordParserHelper;
public GraphDBClient getGraphDBClient() {
log.debug("Creating GraphDBClient for "+graphDBServerUrl);
return new GraphDBClient(recordParserHelper, graphDBServerUrl);
return new GraphDBClient(recordParserHelper, graphDBServerUrl, graphDBBaseURI, writerUser, writerPwd);
}
public RecordParserHelper getRecordParserHelper() {
@ -45,4 +51,34 @@ public class GraphDBClientFactory {
public void setGraphDBServerUrl(String graphDBServerUrl) {
this.graphDBServerUrl = graphDBServerUrl;
}
public String getGraphDBBaseURI() {
return graphDBBaseURI;
}
public void setGraphDBBaseURI(String graphDBBaseURI) {
this.graphDBBaseURI = graphDBBaseURI;
}
public String getWriterUser() {
return writerUser;
}
public void setWriterUser(String writerUser) {
this.writerUser = writerUser;
}
public String getWriterPwd() {
return writerPwd;
}
public void setWriterPwd(String writerPwd) {
this.writerPwd = writerPwd;
}
}

View File

@ -37,7 +37,15 @@ public class AriadnePlusPublisherController {
}
getAriadnePlusPublisherHelper().publish(record, getTarget(ariadneplusTarget));
}
@RequestMapping(value = "/feedProvenance", method = RequestMethod.POST)
public void feedProvenance(@RequestParam final String datasourceApi, @RequestParam(required = false) String ariadneplusTarget) throws AriadnePlusPublisherException {
if (ariadneplusTarget==null) {
ariadneplusTarget = DEFAULT_TARGET_ENDPOINT;
}
getAriadnePlusPublisherHelper().feedProvenance(datasourceApi, getTarget(ariadneplusTarget));
}
@RequestMapping(value = "/unpublish", method = RequestMethod.GET)
public void unpublish(@RequestParam final String datasourceApi, @RequestParam(required = false) String ariadneplusTarget) throws AriadnePlusPublisherException {

View File

@ -35,6 +35,16 @@ public class AriadnePlusPublisherHelper {
}
public void feedProvenance(final String datasourceApi, final AriadnePlusTargets target) throws AriadnePlusPublisherException {
switch(target){
case GRAPHDB:
feedProvenance(datasourceApi);
break;
default: throw new AriadnePlusPublisherException("Target "+target+" not supported yet");
}
}
public long unpublish(final String datasourceInterface, final AriadnePlusTargets target) throws AriadnePlusPublisherException {
long res = 0;
switch(target){
@ -51,6 +61,12 @@ public class AriadnePlusPublisherHelper {
GraphDBClient graphDBClient = this.graphdbClientFactory.getGraphDBClient();
graphDBClient.feed(record);
}
private void feedProvenance(final String datasourceApi) throws AriadnePlusPublisherException {
log.debug("Feed Provenance on graphdb");
GraphDBClient graphDBClient = this.graphdbClientFactory.getGraphDBClient();
graphDBClient.feedProvenance(datasourceApi);
}
private long unpublishGraphDB(final String datasourceInterface) {
log.info("Unpublishing from graphdb "+datasourceInterface);

View File

@ -2,4 +2,8 @@ server.contextPath=/ariadneplus-graphdb
server.port=8281
graphdb.serverUrl=http://localhost:7200/
graphdb.writer.user=writer
graphdb.writer.pwd=writer01
graphdb.sparqlUrl = http://localhost:7200/sparql
graphdb.baseURI=https://ariadne-infrastructure.eu/