337 lines
13 KiB
Java
337 lines
13 KiB
Java
package eu.dnetlib.ariadneplus.reader;
|
|
|
|
import eu.dnetlib.ariadneplus.elasticsearch.BulkUpload;
|
|
import eu.dnetlib.ariadneplus.reader.ResourceManager;
|
|
import eu.dnetlib.ariadneplus.reader.json.ParseRDFJSON;
|
|
import org.eclipse.rdf4j.model.Model;
|
|
import org.eclipse.rdf4j.model.Statement;
|
|
import org.eclipse.rdf4j.query.GraphQuery;
|
|
import org.eclipse.rdf4j.query.GraphQueryResult;
|
|
import org.eclipse.rdf4j.query.QueryLanguage;
|
|
import org.eclipse.rdf4j.query.QueryResults;
|
|
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 org.eclipse.rdf4j.rio.RDFWriter;
|
|
import org.eclipse.rdf4j.rio.Rio;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.io.StringWriter;
|
|
import java.util.Calendar;
|
|
|
|
@Service
|
|
public class RunSPARQLQueryService {
|
|
|
|
@Value("${sparql.query:undefined}")
|
|
private String query;
|
|
|
|
@Value("${repository.url:undefined}")
|
|
private String repository_url;
|
|
|
|
private RepositoryConnection connection;
|
|
private RemoteRepositoryManager manager;
|
|
private Repository repository;
|
|
|
|
private ParseRDFJSON parser;
|
|
private ResourceManager resourceManager;
|
|
private BulkUpload bulkUpload;
|
|
|
|
private static String username = null;
|
|
private static String pwd = null;
|
|
private static String graphDBUrl = null;
|
|
private static String graphDBRepository = null;
|
|
|
|
public void setupConnection(String username, String pwd, String graphDbUrl, String graphDbRepository) {
|
|
setUsername(username);
|
|
setPwd(pwd);
|
|
setGraphDBUrl(graphDbUrl);
|
|
setGraphDBRepository(graphDbRepository);
|
|
}
|
|
|
|
private void openConnection(){
|
|
manager = new RemoteRepositoryManager(getGraphDBUrl());
|
|
manager.init();
|
|
manager.setUsernameAndPassword(getUsername(), getPwd());
|
|
repository = manager.getRepository(getGraphDBRepository());
|
|
connection = repository.getConnection();
|
|
|
|
}
|
|
|
|
private void closeConnection(){
|
|
connection.close();
|
|
repository.shutDown();
|
|
manager.shutDown();
|
|
}
|
|
|
|
public String executeQueryGraph(){
|
|
query = "PREFIX aocat: <https://www.ariadne-infrastructure.eu/resource/ao/cat/1.1/> \n" +
|
|
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n" +
|
|
"PREFIX skos: <http://www.w3.org/2004/02/skos/core#> \n" +
|
|
"PREFIX aoprop: <https://www.ariadne-infrastructure.eu/property/> \n" +
|
|
"PREFIX onto: <http://www.ontotext.com/>\n" +
|
|
"PREFIX ariadneplus: <https://ariadne-infrastructure.eu/aocat/>\n" +
|
|
"PREFIX time: <http://www.w3.org/2006/time#>\n" +
|
|
"\n" +
|
|
"CONSTRUCT { \n" +
|
|
"%s aoprop:identifier %s . \n" +
|
|
"%s aoprop:originalId ?originalId . \n" +
|
|
"%s aoprop:issued ?issued .\n" +
|
|
"%s aoprop:modified ?modified .\n" +
|
|
"%s aoprop:partOf ?partOf .\n" +
|
|
"%s aoprop:creator ?creator . \n" +
|
|
"?creator aoprop:name ?creatorName .\n" +
|
|
"?creator aoprop:email ?creatorEmail . \n" +
|
|
"%s aoprop:contributor ?contributor . \n" +
|
|
"?contributor aoprop:name ?contributorName .\n" +
|
|
"?contributor aoprop:email ?contributorEmail .\n" +
|
|
"%s aoprop:legalResponsible ?legalResponsible . \n" +
|
|
"?legalResponsible aoprop:name ?legalResponsibleName .\n" +
|
|
"?legalResponsible aoprop:email ?legalResponsibleEmail . \n" +
|
|
"%s aoprop:owner ?owner . \n" +
|
|
"?owner aoprop:name ?ownerName .\n" +
|
|
"?owner aoprop:email ?ownerEmail . \n" +
|
|
"%s aoprop:publisher ?publisher . \n" +
|
|
"?publisher aoprop:name ?publisherName . \n" +
|
|
"?publisher aoprop:email ?publisherEmail . \n" +
|
|
"%s aoprop:accessPolicy ?accessPolicy . \n" +
|
|
"%s aoprop:accessRights ?accessRights .\n" +
|
|
"%s aoprop:landingPage ?landingPage .\n" +
|
|
"%s aoprop:spatialRegion ?spatialRegion . \n" +
|
|
"?spatialRegion aoprop:placeName ?spatialPlaceName .\n" +
|
|
"?spatialRegion aoprop:spatialCoordinateSystem ?spatialCoordinateSystem . \n" +
|
|
"%s aoprop:spatialRegionPoint ?spatialRegionPoint . \n" +
|
|
"?spatialRegionPoint aoprop:lat ?spatialLocationLat . \n" +
|
|
"?spatialRegionPoint aoprop:lon ?spatialLocationLon .\n" +
|
|
"%s aoprop:spatialRegionBox ?spatialRegionBox . \n" +
|
|
"?spatialRegionBox aoprop:boxMaxLat ?spatialLocationBBMaxLat .\n" +
|
|
"?spatialRegionBox aoprop:boxMaxLon ?spatialLocationBBMaxLon .\n" +
|
|
"?spatialRegionBox aoprop:boxMinLat ?spatialLocationBBMinLat .\n" +
|
|
"?spatialRegionBox aoprop:boxMinLon ?spatialLocationBBMinLon .\n" +
|
|
"%s aoprop:uri ?temporal .\n" +
|
|
"?temporal aoprop:periodName ?temporalPeriodName .\n" +
|
|
"?temporal aoprop:from ?temporalFrom .\n" +
|
|
"?temporal aoprop:until ?temporalUntil .\n" +
|
|
"%s aoprop:uri ?temporalNative .\n" +
|
|
"?temporalNative aoprop:periodName ?temporalNativePeriodName .\n" +
|
|
"%s aoprop:archeologicalResourceType ?archeologicalResourceType . \n" +
|
|
"?archeologicalResourceType aoprop:name ?archeologicalResourceTypeName . \n" +
|
|
"%s aoprop:resourceType ?resourceType . \n" +
|
|
"%s aoprop:nativeSubject ?nativeSubject . \n" +
|
|
"?nativeSubject aoprop:prefLabel ?nativeSubjectPrefLabel .\n" +
|
|
"?nativeSubject aoprop:rdfAbout ?nativeSubject .\n" +
|
|
"%s aoprop:derivedSubject ?derivedSubject .\n" +
|
|
"?derivedSubject aoprop:prefLabel ?derivedSubjectPrefLabel .\n" +
|
|
"?derivedSubject aoprop:source \"Getty AAT\" .\n" +
|
|
"%s aoprop:aatSubjects ?derivedSubject .\n" +
|
|
"?derivedSubject aoprop:id ?derivedSubject . \n" +
|
|
"?derivedSubject aoprop:label ?derivedSubjectPrefLabel .\n" +
|
|
"?derivedSubject aoprop:lang ?aatSubjectsLang .\n" +
|
|
"%s aoprop:title ?title . \n" +
|
|
"%s aoprop:description ?description . \n" +
|
|
"%s aoprop:language ?language . \n" +
|
|
"}\n" +
|
|
"from <https://ariadne-infrastructure.eu/api_________::ariadne_plus::ads::aat>\n" +
|
|
"from <https://ariadne-infrastructure.eu/ariadneplus::ads::aatplus>\n" +
|
|
"from <https://ariadne-infrastructure.eu/ariadneplus::ads::periodo>\n" +
|
|
"from <https://ariadne-infrastructure.eu/ariadneplus::ads::periodoplus>\n" +
|
|
"from <https://ariadne-infrastructure.eu/api_________::ariadne_plus::ads::271>\n" +
|
|
"where { \n" +
|
|
" \t%s\taocat:has_language / skos:prefLabel ?language .\n" +
|
|
" \t%s\taocat:has_original_id ?originalId .\n" +
|
|
" \t%s aocat:is_part_of ?partOf .\n" +
|
|
" \t%s\taocat:has_creator ?creator .\n" +
|
|
" ?creator aocat:has_name ?creatorName .\n" +
|
|
"\t %s aocat:has_title ?title .\n" +
|
|
" \t%s aocat:has_type / skos:prefLabel ?resourceType .\t\n" +
|
|
" \t%s aocat:has_native_subject ?nativeSubject .\n" +
|
|
" ?nativeSubject skos:prefLabel ?nativeSubjectPrefLabel .\n" +
|
|
" optional {\n" +
|
|
"\t\t\t%s aocat:has_derived_subject ?derivedSubject .\n" +
|
|
" \t?derivedSubject skos:prefLabel ?derivedSubjectPrefLabel .\n" +
|
|
" }\n" +
|
|
" optional {\n" +
|
|
"\t ?creator aocat:has_email ?creatorEmail .\n" +
|
|
" \t}\n" +
|
|
" optional {\n" +
|
|
" %s aocat:has_description ?description .\n" +
|
|
" }\n" +
|
|
" optional {\n" +
|
|
" %s aocat:has_access_policy / rdfs:label ?accessPolicy . \n" +
|
|
" }\n" +
|
|
" optional {\n" +
|
|
" \t%s aocat:has_landing_page / rdfs:label ?landingPage .\n" +
|
|
" }\n" +
|
|
" \toptional {\n" +
|
|
" \t%s aocat:has_temporal_coverage ?temporalNative .\n" +
|
|
" \t?temporalNative aocat:has_native_period / skos:prefLabel ?temporalNativePeriodName .\n" +
|
|
" }\n" +
|
|
" optional {\n" +
|
|
" \t%s aocat:has_temporal_coverage ?temporal .\n" +
|
|
" \t?temporal aocat:has_period / skos:prefLabel ?temporalPeriodName .\n" +
|
|
" \toptional {\n" +
|
|
" ?temporal aocat:from ?temporalFrom .\n" +
|
|
" ?temporal aocat:until ?temporalUntil .\n" +
|
|
" \t}\n" +
|
|
" }\n" +
|
|
" \t\n" +
|
|
" \t{ \n" +
|
|
"\t\tselect * \n" +
|
|
" where {\n" +
|
|
" %s aocat:is_part_of ?collection .\n" +
|
|
" %s aocat:was_issued ?issued .\n" +
|
|
" %s aocat:was_modified ?modified .\n" +
|
|
" %s aocat:has_contributor ?contributor .\n" +
|
|
" ?contributor aocat:has_name ?contributorName .\n" +
|
|
" %s aocat:has_responsible ?legalResponsible .\n" +
|
|
" ?legalResponsible aocat:has_name ?legalResponsibleName .\n" +
|
|
" %s aocat:has_owner ?owner .\n" +
|
|
" ?owner aocat:has_name ?ownerName .\n" +
|
|
" %s aocat:has_publisher ?publisher . \n" +
|
|
" ?publisher aocat:has_name ?publisherName .\n" +
|
|
" %s aocat:has_access_rights ?accessRights .\n" +
|
|
" %s aocat:has_ARIADNE_subject ?archeologicalResourceType . \n" +
|
|
" ?archeologicalResourceType skos:prefLabel ?archeologicalResourceTypeName .\n" +
|
|
" optional {\n" +
|
|
" ?contributor aocat:has_email ?contributorEmail .\n" +
|
|
" }\n" +
|
|
" optional {\n" +
|
|
" ?legalResponsible aocat:has_email ?legalResponsibleEmail .\n" +
|
|
" }\n" +
|
|
" optional {\n" +
|
|
" ?owner aocat:has_email ?ownerEmail . \n" +
|
|
" }\n" +
|
|
" optional {\n" +
|
|
" ?publisher aocat:has_email ?publisherEmail .\n" +
|
|
" }\n" +
|
|
" \t}\n" +
|
|
" \t}\n" +
|
|
" \n" +
|
|
" \t{\n" +
|
|
" select * \n" +
|
|
" where {\n" +
|
|
" \t%s\taocat:has_spatial_coverage ?spatialRegion .\n" +
|
|
" ?spatialRegion aocat:has_place_name ?spatialPlaceName .\n" +
|
|
" optional {\n" +
|
|
" ?spatialRegion aocat:has_coordinate_system ?spatialCoordinateSystem .\n" +
|
|
" }\n" +
|
|
" optional {\n" +
|
|
" %s\taocat:has_spatial_coverage ?spatialRegionPoint .\n" +
|
|
" ?spatialRegionPoint aocat:has_latitude ?spatialLocationLat ; \n" +
|
|
" \t\t aocat:has_longitude ?spatialLocationLon . \n" +
|
|
" }\n" +
|
|
" optional {\n" +
|
|
" %s\taocat:has_spatial_coverage ?spatialRegionBox .\n" +
|
|
" ?spatialRegionBox aocat:has_bounding_box_max_lat ?spatialLocationBBMaxLat ; \n" +
|
|
" \t\t aocat:has_bounding_box_max_lon ?spatialLocationBBMaxLon ;\n" +
|
|
" \t\t aocat:has_bounding_box_min_lat ?spatialLocationBBMinLat ;\n" +
|
|
" \t aocat:has_bounding_box_min_lon ?spatialLocationBBMinLon ;\n" +
|
|
" }\n" +
|
|
" }\t\n" +
|
|
" \t}\n" +
|
|
" BIND (lang(?derivedSubjectPrefLabel) as ?aatSubjectsLang)\n" +
|
|
"}";
|
|
|
|
if (query.equals("undefined"))
|
|
return null;
|
|
query = query.replaceAll("%s", "<https://ariadne-infrastructure.eu/aocat/Resource/02E4F4B5-24B7-3AD7-B460-CFA8B1F0BD1F>");
|
|
openConnection();
|
|
StringWriter recordWriter = null;
|
|
Model resultsModel = null;
|
|
String jsonRecord = null;
|
|
try {
|
|
System.out.println("Start connection Time: "+Calendar.getInstance().getTime().toString());
|
|
GraphQuery graphQuery = connection.prepareGraphQuery(QueryLanguage.SPARQL, query);
|
|
GraphQueryResult graphQueryResult = graphQuery.evaluate();
|
|
resultsModel = QueryResults.asModel(graphQueryResult);
|
|
graphQueryResult.close();
|
|
System.out.println("End connection Time: "+Calendar.getInstance().getTime().toString());
|
|
System.out.println("count statements: " + resultsModel.size());
|
|
recordWriter = new StringWriter();
|
|
RDFWriter rdfRecordWriter = Rio.createWriter(RDFFormat.RDFJSON, recordWriter);
|
|
Rio.write(resultsModel, rdfRecordWriter);
|
|
System.out.println("RDF > json record: "+recordWriter.toString());
|
|
parser.parse(recordWriter.toString());
|
|
resourceManager.manage(parser);
|
|
bulkUpload.index(resourceManager);
|
|
} catch(Exception e){
|
|
e.printStackTrace();
|
|
} finally{
|
|
closeConnection();
|
|
if (resultsModel!=null) {
|
|
resultsModel.clear();
|
|
}
|
|
}
|
|
return jsonRecord;
|
|
}
|
|
|
|
private void dumpModel(Model model) {
|
|
System.out.print(" [ dump model ] ");
|
|
for (Statement stmt: model) {
|
|
System.out.println(stmt.toString());
|
|
}
|
|
}
|
|
|
|
public ParseRDFJSON getParser() {
|
|
return parser;
|
|
}
|
|
|
|
|
|
public void setParser(ParseRDFJSON parser) {
|
|
this.parser = parser;
|
|
}
|
|
|
|
|
|
public ResourceManager getResourceManager() {
|
|
return resourceManager;
|
|
}
|
|
|
|
|
|
public void setResourceManager(ResourceManager resourceManager) {
|
|
this.resourceManager = resourceManager;
|
|
}
|
|
|
|
|
|
public BulkUpload getBulkUpload() {
|
|
return bulkUpload;
|
|
}
|
|
|
|
|
|
public void setBulkUpload(BulkUpload bulkUpload) {
|
|
this.bulkUpload = bulkUpload;
|
|
}
|
|
|
|
public static String getUsername() {
|
|
return username;
|
|
}
|
|
|
|
public static String getPwd() {
|
|
return pwd;
|
|
}
|
|
|
|
public static String getGraphDBUrl() {
|
|
return graphDBUrl;
|
|
}
|
|
|
|
public static String getGraphDBRepository() {
|
|
return graphDBRepository;
|
|
}
|
|
|
|
public static void setUsername(String username) {
|
|
RunSPARQLQueryService.username = username;
|
|
}
|
|
|
|
public static void setPwd(String pwd) {
|
|
RunSPARQLQueryService.pwd = pwd;
|
|
}
|
|
|
|
public static void setGraphDBUrl(String graphDBUrl) {
|
|
RunSPARQLQueryService.graphDBUrl = graphDBUrl;
|
|
}
|
|
|
|
public static void setGraphDBRepository(String graphDBRepository) {
|
|
RunSPARQLQueryService.graphDBRepository = graphDBRepository;
|
|
}
|
|
}
|