223 lines
7.5 KiB
Java
223 lines
7.5 KiB
Java
package eu.dnetlib.ariadneplus.graphdb;
|
|
|
|
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.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);
|
|
|
|
private RecordParserHelper recordParserHelper;
|
|
|
|
private String graphDBServerUrl;
|
|
|
|
protected GraphDBClient(final RecordParserHelper recordParserHelper,
|
|
final String graphDBServerUrl) {
|
|
this.recordParserHelper = recordParserHelper;
|
|
this.graphDBServerUrl = graphDBServerUrl;
|
|
}
|
|
|
|
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();
|
|
manager.setUsernameAndPassword("writer", "writer01");
|
|
log.debug("manager init");
|
|
Repository repository = manager.getRepository("ariadneprova");
|
|
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);
|
|
}
|
|
}
|
|
|
|
//
|
|
// 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;
|
|
// }
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// 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();
|
|
// }
|
|
// }
|
|
|
|
|