Project

General

Profile

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

    
55
	//No need to pass the RDF inside an OAI record: use register(String, String, String) to explicitely pass the needed information.
56
	@Deprecated
57
	public void register(final String record)
58
			throws ParthenosPublisherException, IOException, URISyntaxException, InterruptedException {
59
		String id = recordParserHelper.getObjIdentifier(record);
60
		String datasourceName = recordParserHelper.getDatasourceName(record);
61
		String rdfRecord = recordParserHelper.getRDF(record);
62
		register(rdfRecord, id, datasourceName);
63
	}
64

    
65
	public int purgeFromCatalogue(int bulkSize) throws ParthenosPublisherException {
66
		return catalogueRegistrator.purgeAll(bulkSize);
67
	}
68

    
69
	public void register(final String rdfRecord, final String objIdentifier, final String datasourceName)
70
			throws ParthenosPublisherException, IOException, URISyntaxException, InterruptedException {
71
		//objIdentifier contains the subject URI used to get the RDF: that is the only resource we have to register when processing this rdf file!
72
		log.debug("REGISTERING ON JRR: "+objIdentifier);
73
		InfModel model = loadBaseModel();
74
		model.read(IOUtils.toInputStream(rdfRecord), CRMpe.NS, "RDF/XML");
75
		register(model, objIdentifier, datasourceName);
76
	}
77

    
78

    
79
	protected void register(final Model model, final String resourceURI, final String datasourceName)
80
			throws ParthenosPublisherException, IOException, URISyntaxException, InterruptedException {
81
		Resource rdfResource = model.getResource(resourceURI);
82
		if (rdfResource == null) {
83
			log.error("UNEXPECTED NULL rdfResource with resourceURI " + resourceURI + ". I am skipping it, but you should check!");
84
		} else {
85
			//call the correct register method based on the resource type
86
			//we skip everything that is not Software, Actor, Service, Dataset, Curation Plan and Project: other entities are in fact source of metadata for them
87
			////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)
88
			if (rdfResource.hasProperty(RDF.type, CRM.E39_Actor)) registerRDFResource(rdfResource, CRM.E39_Actor, datasourceName);
89
			else {
90
				if (rdfResource.hasProperty(RDF.type, CRMpe.PE35_Project)) registerRDFResource(rdfResource, CRMpe.PE35_Project, datasourceName);
91
				else {
92
					if (rdfResource.hasProperty(RDF.type, CRM.E29_Design_or_Procedure)) registerRDFResource(rdfResource, CRM.E29_Design_or_Procedure, datasourceName);
93
					else {
94
						if (rdfResource.hasProperty(RDF.type, CRMdig.D14_Software)) registerRDFResource(rdfResource, CRMdig.D14_Software, datasourceName);
95
						else {
96
							if (rdfResource.hasProperty(RDF.type, CRMpe.PE18_Dataset)) registerRDFResource(rdfResource, CRMpe.PE18_Dataset, datasourceName);
97
							else {
98
								if (rdfResource.hasProperty(RDF.type, CRM.E78_Collection)) registerRDFResource(rdfResource, CRM.E78_Collection, datasourceName);
99
								else {
100
									if (rdfResource.hasProperty(RDF.type, CRMpe.PE1_Service)) registerRDFResource(rdfResource, CRMpe.PE1_Service, datasourceName);
101
									else {
102
										log.debug("Skipping " + resourceURI + " because of its type");
103
									}
104
								}
105
							}
106
						}
107
					}
108
				}
109
			}
110
		}
111
	}
112

    
113
	protected boolean registerRDFResource(final Resource rdfResource, final Resource type, final String datasourceName)
114
			throws ParthenosPublisherException, IOException, URISyntaxException, InterruptedException {
115
		String resURI = rdfResource.getURI();
116
		if (!resURI.startsWith("http")) {
117
			//this is something George said: if it has no http URI, then it is not to be considered relevant by itself
118
			log.info("Resource " + resURI + " skipped: URI does not start with http");
119
			return false;
120
		}
121
		else {
122
				String uuid = registerOnCatalogue(rdfResource, type, datasourceName);
123
				if(StringUtils.isNotBlank(uuid)){
124
					//TODO: let's skip the registration on the registry for now.
125
					//registerOnRegistry(rdfResource, uuid, type);
126
					return true;
127
				}
128
				else{
129
					log.warn("Got blank uuid when registering "+resURI+": skipping registration on the registry");
130
					return false;
131
				}
132
			}
133
		}
134

    
135
	/**
136
	 * Register resource of the given type on the catalogue
137
	 * @param rdfResource
138
	 * @param type
139
	 * @return the catalogue uuid
140
	 * @throws IOException
141
	 */
142
	protected String registerOnCatalogue(final Resource rdfResource, final Resource type, final String datasourceName)
143
			throws ParthenosPublisherException, IOException, URISyntaxException, InterruptedException {
144
		return catalogueRegistrator.register(rdfResource, type, datasourceName);
145
	}
146

    
147
	/*
148
	protected void registerOnRegistry(final Resource rdfResource, final String uuid, final Resource type)
149
			throws ParthenosPublisherException, ResourceRegistryException, IOException {
150
			gCubeResourceRegistrator.register(rdfResource, uuid, type);
151
	}
152
	*/
153

    
154
	protected InfModel loadBaseModel() {
155
		return ModelFactory.createRDFSModel(baseModel);
156
	}
157

    
158

    
159

    
160

    
161
}
(1-1/4)