2019-12-12 12:58:30 +01:00
|
|
|
package eu.dnetlib.ariadneplus.graphdb;
|
|
|
|
|
2019-12-13 14:53:15 +01:00
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
2019-12-12 12:58:30 +01:00
|
|
|
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;
|
2019-12-13 14:53:15 +01:00
|
|
|
import org.eclipse.rdf4j.model.Statement;
|
2019-12-12 12:58:30 +01:00
|
|
|
import org.eclipse.rdf4j.model.ValueFactory;
|
|
|
|
import org.eclipse.rdf4j.repository.Repository;
|
|
|
|
import org.eclipse.rdf4j.repository.RepositoryConnection;
|
|
|
|
import org.eclipse.rdf4j.repository.manager.RemoteRepositoryManager;
|
|
|
|
import org.eclipse.rdf4j.rio.RDFFormat;
|
|
|
|
|
|
|
|
import eu.dnetlib.ariadneplus.publisher.AriadnePlusPublisherException;
|
|
|
|
import eu.dnetlib.ariadneplus.rdf.RecordParserHelper;
|
|
|
|
import net.sf.saxon.s9api.SaxonApiException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author enrico.ottonello
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class GraphDBClient {
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(GraphDBClient.class);
|
|
|
|
|
2019-12-13 14:53:15 +01:00
|
|
|
public static final String PROVENANCE_NS = "http://www.d-net.research-infrastructures.eu/provenance/";
|
|
|
|
|
2019-12-12 12:58:30 +01:00
|
|
|
private RecordParserHelper recordParserHelper;
|
|
|
|
private String graphDBServerUrl;
|
2019-12-13 14:53:15 +01:00
|
|
|
private String graphDBBaseURI;
|
|
|
|
private String writerUser;
|
|
|
|
private String writerPwd;
|
2019-12-16 14:46:42 +01:00
|
|
|
private String repository;
|
2019-12-13 14:53:15 +01:00
|
|
|
|
2019-12-12 12:58:30 +01:00
|
|
|
protected GraphDBClient(final RecordParserHelper recordParserHelper,
|
2019-12-16 14:46:42 +01:00
|
|
|
final String graphDBServerUrl, final String graphDBBaseURI, final String writerUser, final String writerPwd, final String repository) {
|
2019-12-12 12:58:30 +01:00
|
|
|
this.recordParserHelper = recordParserHelper;
|
|
|
|
this.graphDBServerUrl = graphDBServerUrl;
|
2019-12-13 14:53:15 +01:00
|
|
|
this.graphDBBaseURI = graphDBBaseURI;
|
|
|
|
this.writerUser = writerUser;
|
|
|
|
this.writerPwd = writerPwd;
|
2019-12-16 14:46:42 +01:00
|
|
|
this.repository = repository;
|
2019-12-12 12:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public long feed(final String record) throws AriadnePlusPublisherException{
|
|
|
|
try {
|
|
|
|
String objIdentifier = recordParserHelper.getObjIdentifier(record);
|
|
|
|
if (StringUtils.isBlank(objIdentifier)) {
|
|
|
|
log.warn("Got record with no objIdentifier -- skipping");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
log.debug("init connection to graphDBServerUrl " + this.graphDBServerUrl);
|
|
|
|
RemoteRepositoryManager manager = new RemoteRepositoryManager(this.graphDBServerUrl);
|
|
|
|
manager.init();
|
2019-12-13 14:53:15 +01:00
|
|
|
manager.setUsernameAndPassword(getWriterUser(), getWriterPwd());
|
2019-12-12 12:58:30 +01:00
|
|
|
log.debug("manager init");
|
2019-12-16 14:46:42 +01:00
|
|
|
Repository repository = manager.getRepository(getRepository());
|
2019-12-12 12:58:30 +01:00
|
|
|
ValueFactory factory = repository.getValueFactory();
|
|
|
|
String dsInterface = recordParserHelper.getDatasourceApi(record);
|
|
|
|
IRI graph = factory.createIRI(dsInterface);
|
|
|
|
try (RepositoryConnection con = repository.getConnection()) {
|
|
|
|
log.debug("connection established");
|
|
|
|
con.begin();
|
|
|
|
String recordURI = getRecordURI(objIdentifier, dsInterface);
|
|
|
|
log.debug("Trying to adding record with recordURI " + recordURI + " into graph " + graph);
|
|
|
|
con.add(IOUtils.toInputStream(getRDFBlock(record), "UTF-8"), recordURI, RDFFormat.RDFXML, graph);
|
|
|
|
con.commit();
|
|
|
|
log.debug("statement added");
|
|
|
|
con.close();
|
|
|
|
}
|
|
|
|
catch (RDF4JException e) {
|
|
|
|
log.error("error adding statement ...", e);
|
|
|
|
}
|
|
|
|
repository.shutDown();
|
|
|
|
manager.shutDown();
|
|
|
|
log.debug("manager shutDown");
|
|
|
|
return 1;
|
|
|
|
}catch(Throwable e){
|
|
|
|
log.error(e);
|
|
|
|
throw new AriadnePlusPublisherException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 14:53:15 +01:00
|
|
|
|
|
|
|
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();
|
2019-12-16 14:46:42 +01:00
|
|
|
manager.setUsernameAndPassword(getWriterUser(), getWriterPwd());
|
2019-12-13 14:53:15 +01:00
|
|
|
log.debug("manager init");
|
2019-12-16 14:46:42 +01:00
|
|
|
Repository repository = manager.getRepository(getRepository());
|
2019-12-13 14:53:15 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2019-12-12 12:58:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all triples in named graphs collected from the given api
|
|
|
|
* @param api the id of the API
|
|
|
|
* @return the number of triples deleted from the named graphs associated to the given api
|
|
|
|
*/
|
|
|
|
public long drop(final String api){
|
|
|
|
// Model prov = null;
|
|
|
|
// //look for all named graphs associated to the api
|
|
|
|
// Resource rApi = ResourceFactory.createResource(defaultBaseURI + api);
|
|
|
|
// long deletedTriples = 0;
|
|
|
|
// final ResIterator resIterator = prov.listSubjectsWithProperty(COLL_FROM, rApi);
|
|
|
|
// while (resIterator.hasNext()) {
|
|
|
|
// Resource namedGraphURI = resIterator.nextResource();
|
|
|
|
// //delete all triples belonging to the r named graph
|
|
|
|
// deletedTriples += dropNamedGraph(namedGraphURI.getURI());
|
|
|
|
// //delete the named graph from the provenance graph
|
|
|
|
// prov.removeAll(namedGraphURI, null, null);
|
|
|
|
// }
|
|
|
|
// //delete the api from the provenance graph
|
|
|
|
// prov.removeAll(null, null, rApi);
|
|
|
|
// prov.removeAll(rApi, null, null);
|
|
|
|
// prov.close();
|
|
|
|
// return deletedTriples;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private long dropNamedGraph(String namedGraphURI){
|
|
|
|
// Model namedGraph = null;
|
|
|
|
// long deletedTriples = namedGraph.size();
|
|
|
|
// namedGraph.removeAll();
|
|
|
|
// namedGraph.close();
|
|
|
|
// return deletedTriples;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getRecordURI(final String objIdentifier, final String datasourceApi) {
|
|
|
|
return "/" + datasourceApi + "/" + objIdentifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
public RecordParserHelper getRecordParserHelper() {
|
|
|
|
return recordParserHelper;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setRecordParserHelper(final RecordParserHelper recordParserHelper) {
|
|
|
|
this.recordParserHelper = recordParserHelper;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setDefaultBaseURI(final String defaultBaseURI) {
|
|
|
|
this.graphDBServerUrl = defaultBaseURI;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getRDFBlock(final String record) throws SaxonApiException{
|
|
|
|
recordParserHelper.init();
|
|
|
|
try {
|
|
|
|
if (StringUtils.isBlank(record)) {
|
|
|
|
log.warn("Got empty record");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
String objIdentifier = recordParserHelper.getObjIdentifier(record);
|
|
|
|
if (StringUtils.isBlank(objIdentifier)) {
|
|
|
|
log.warn("Got record with no objIdentifier -- skipping");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
log.debug(objIdentifier);
|
|
|
|
String rdfBlock = recordParserHelper.getRDF(record);
|
|
|
|
if (StringUtils.isBlank(rdfBlock)) {
|
|
|
|
log.warn("Missing rdf:RDF in record with objIdentifier " + objIdentifier);
|
|
|
|
}
|
|
|
|
return rdfBlock;
|
|
|
|
}catch(Throwable e){
|
|
|
|
log.error(e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 14:53:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-12-16 14:46:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
public String getRepository() {
|
|
|
|
return repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setRepository(String repository) {
|
|
|
|
this.repository = repository;
|
|
|
|
}
|
2019-12-12 12:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// strQuery =
|
|
|
|
// "SELECT ?name FROM DEFAULT WHERE {" +
|
|
|
|
// "?s <http://xmlns.com/foaf/0.1/name> ?name .}";
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// public void queryTest(){
|
|
|
|
// RemoteRepositoryManager manager = new RemoteRepositoryManager(GRAPHDB_SERVER_URL);
|
|
|
|
// manager.init();
|
|
|
|
// logger.debug("manager init");
|
|
|
|
// Repository repository = manager.getRepository("PersonData");
|
|
|
|
// try (RepositoryConnection con = repository.getConnection()) {
|
|
|
|
// logger.debug("connection established");
|
|
|
|
// query(con);
|
|
|
|
// logger.debug("query success");
|
|
|
|
// }
|
|
|
|
// catch (RDF4JException e) {
|
|
|
|
// logger.error("error adding statement ...", e);
|
|
|
|
// }
|
|
|
|
// manager.shutDown();
|
|
|
|
// logger.debug("manager shutDown");
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// private void query(RepositoryConnection repositoryConnection) {
|
|
|
|
// TupleQuery tupleQuery = repositoryConnection.prepareTupleQuery(QueryLanguage.SPARQL, strQuery);
|
|
|
|
// TupleQueryResult result = null;
|
|
|
|
// try {
|
|
|
|
// result = tupleQuery.evaluate();
|
|
|
|
// int count = 0;
|
|
|
|
// while (result.hasNext()) {
|
|
|
|
// BindingSet bindingSet = result.next();
|
|
|
|
//
|
|
|
|
// SimpleLiteral name = (SimpleLiteral)bindingSet.getValue("name");
|
|
|
|
// logger.info("name = " + name.stringValue());
|
|
|
|
// count++;
|
|
|
|
// }
|
|
|
|
// logger.info("Entries found: ", count);
|
|
|
|
// }
|
|
|
|
// catch (QueryEvaluationException qee) {
|
|
|
|
// logger.error(WTF_MARKER, qee.getStackTrace().toString(), qee);
|
|
|
|
// } finally {
|
|
|
|
// result.close();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|