1
|
package eu.dnetlib.parthenos.jrr;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.net.URISyntaxException;
|
5
|
import javax.annotation.PostConstruct;
|
6
|
|
7
|
import eu.dnetlib.parthenos.CRM;
|
8
|
import eu.dnetlib.parthenos.CRMdig;
|
9
|
import eu.dnetlib.parthenos.CRMpe;
|
10
|
import eu.dnetlib.parthenos.catalogue.CatalogueRegistrator;
|
11
|
import eu.dnetlib.parthenos.publisher.ParthenosPublisherException;
|
12
|
import eu.dnetlib.parthenos.rdf.RecordParserHelper;
|
13
|
import org.apache.commons.io.IOUtils;
|
14
|
import org.apache.commons.lang3.StringUtils;
|
15
|
import org.apache.commons.logging.Log;
|
16
|
import org.apache.commons.logging.LogFactory;
|
17
|
import org.apache.jena.ontology.OntModel;
|
18
|
import org.apache.jena.ontology.OntModelSpec;
|
19
|
import org.apache.jena.rdf.model.*;
|
20
|
import org.apache.jena.vocabulary.RDF;
|
21
|
import org.springframework.beans.factory.annotation.Autowired;
|
22
|
import org.springframework.stereotype.Component;
|
23
|
|
24
|
|
25
|
/**
|
26
|
* Created by Alessia Bardi on 25/02/2018.
|
27
|
*
|
28
|
* publish on Gcube registry and catalogue.
|
29
|
*
|
30
|
*
|
31
|
* @author Alessia Bardi
|
32
|
*/
|
33
|
@Component
|
34
|
public class JRRPublisher {
|
35
|
|
36
|
private static final Log log = LogFactory.getLog(JRRPublisher.class);
|
37
|
private OntModel baseModel;
|
38
|
|
39
|
@Autowired
|
40
|
private CatalogueRegistrator catalogueRegistrator;
|
41
|
//@Autowired
|
42
|
//private GCubeResourceRegistrator gCubeResourceRegistrator;
|
43
|
@Autowired
|
44
|
private RecordParserHelper recordParserHelper;
|
45
|
|
46
|
@PostConstruct
|
47
|
public void init(){
|
48
|
baseModel = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_TRANS_INF);
|
49
|
baseModel.read(CRMpe.RDFS_URL);
|
50
|
baseModel.read(CRM.RDFS_URL);
|
51
|
baseModel.read(CRMdig.RDFS_URL);
|
52
|
}
|
53
|
|
54
|
//No need to pass the RDF inside an OAI record: use register(String, String, String) to explicitely pass the needed information.
|
55
|
@Deprecated
|
56
|
public void register(final String record)
|
57
|
throws ParthenosPublisherException, IOException, URISyntaxException, InterruptedException {
|
58
|
String id = recordParserHelper.getObjIdentifier(record);
|
59
|
String datasourceName = recordParserHelper.getDatasourceName(record);
|
60
|
String rdfRecord = recordParserHelper.getRDF(record);
|
61
|
register(rdfRecord, id, datasourceName);
|
62
|
}
|
63
|
|
64
|
public void register(final String rdfRecord, final String objIdentifier, final String datasourceName)
|
65
|
throws ParthenosPublisherException, IOException, URISyntaxException, InterruptedException {
|
66
|
//objIdentifier contains the subject URI used to get the RDF: that is the only resource we have to register when processing this rdf file!
|
67
|
log.debug("REGISTERING ON JRR: "+objIdentifier);
|
68
|
InfModel model = loadBaseModel();
|
69
|
model.read(IOUtils.toInputStream(rdfRecord), CRMpe.NS, "RDF/XML");
|
70
|
register(model, objIdentifier, datasourceName);
|
71
|
}
|
72
|
|
73
|
|
74
|
protected void register(final Model model, final String resourceURI, final String datasourceName)
|
75
|
throws ParthenosPublisherException, IOException, URISyntaxException, InterruptedException {
|
76
|
Resource rdfResource = model.getResource(resourceURI);
|
77
|
if (rdfResource == null) {
|
78
|
log.error("UNEXPECTED NULL rdfResource with resourceURI " + resourceURI + ". I am skipping it, but you should check!");
|
79
|
} else {
|
80
|
//call the correct register method based on the resource type
|
81
|
//we skip everything that is not Software, Actor, Service, Dataset, Curation Plan and Project: other entities are in fact source of metadata for them
|
82
|
////base types are {Project, Service, Actors, Datasets, Software, Physical Collections, Standards} i.e. {PE35_Project, PE1_Service, E39_Actor, PE18_Dataset, D14_Software, E78_Collection, E29_Design_or_Procedure)
|
83
|
if (rdfResource.hasProperty(RDF.type, CRM.E39_Actor)) registerRDFResource(rdfResource, CRM.E39_Actor, datasourceName);
|
84
|
else {
|
85
|
if (rdfResource.hasProperty(RDF.type, CRMpe.PE35_Project)) registerRDFResource(rdfResource, CRMpe.PE35_Project, datasourceName);
|
86
|
else {
|
87
|
if (rdfResource.hasProperty(RDF.type, CRM.E29_Design_or_Procedure)) registerRDFResource(rdfResource, CRM.E29_Design_or_Procedure, datasourceName);
|
88
|
else {
|
89
|
if (rdfResource.hasProperty(RDF.type, CRMdig.D14_Software)) registerRDFResource(rdfResource, CRMdig.D14_Software, datasourceName);
|
90
|
else {
|
91
|
if (rdfResource.hasProperty(RDF.type, CRMpe.PE18_Dataset)) registerRDFResource(rdfResource, CRMpe.PE18_Dataset, datasourceName);
|
92
|
else {
|
93
|
if (rdfResource.hasProperty(RDF.type, CRM.E78_Collection)) registerRDFResource(rdfResource, CRM.E78_Collection, datasourceName);
|
94
|
else {
|
95
|
if (rdfResource.hasProperty(RDF.type, CRMpe.PE1_Service)) registerRDFResource(rdfResource, CRMpe.PE1_Service, datasourceName);
|
96
|
else {
|
97
|
log.debug("Skipping " + resourceURI + " because of its type");
|
98
|
}
|
99
|
}
|
100
|
}
|
101
|
}
|
102
|
}
|
103
|
}
|
104
|
}
|
105
|
}
|
106
|
}
|
107
|
|
108
|
protected boolean registerRDFResource(final Resource rdfResource, final Resource type, final String datasourceName)
|
109
|
throws ParthenosPublisherException, IOException, URISyntaxException, InterruptedException {
|
110
|
String resURI = rdfResource.getURI();
|
111
|
if (!resURI.startsWith("http")) {
|
112
|
//this is something George said: if it has no http URI, then it is not to be considered relevant by itself
|
113
|
log.info("Resource " + resURI + " skipped: URI does not start with http");
|
114
|
return false;
|
115
|
}
|
116
|
else {
|
117
|
String uuid = registerOnCatalogue(rdfResource, type, datasourceName);
|
118
|
if(StringUtils.isNotBlank(uuid)){
|
119
|
//TODO: let's skip the registration on the registry for now.
|
120
|
//registerOnRegistry(rdfResource, uuid, type);
|
121
|
return true;
|
122
|
}
|
123
|
else{
|
124
|
log.warn("Got blank uuid when registering "+resURI+": skipping registration on the registry");
|
125
|
return false;
|
126
|
}
|
127
|
}
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
* Register resource of the given type on the catalogue
|
132
|
* @param rdfResource
|
133
|
* @param type
|
134
|
* @return the catalogue uuid
|
135
|
* @throws IOException
|
136
|
*/
|
137
|
protected String registerOnCatalogue(final Resource rdfResource, final Resource type, final String datasourceName)
|
138
|
throws ParthenosPublisherException, IOException, URISyntaxException, InterruptedException {
|
139
|
return catalogueRegistrator.register(rdfResource, type, datasourceName);
|
140
|
}
|
141
|
|
142
|
/*
|
143
|
protected void registerOnRegistry(final Resource rdfResource, final String uuid, final Resource type)
|
144
|
throws ParthenosPublisherException, ResourceRegistryException, IOException {
|
145
|
gCubeResourceRegistrator.register(rdfResource, uuid, type);
|
146
|
}
|
147
|
*/
|
148
|
|
149
|
protected InfModel loadBaseModel() {
|
150
|
return ModelFactory.createRDFSModel(baseModel);
|
151
|
}
|
152
|
|
153
|
|
154
|
}
|