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
	public void register(final String record)
55
			throws ParthenosPublisherException, IOException, URISyntaxException, InterruptedException {
56
		String id = recordParserHelper.getObjIdentifier(record);
57
		String datasourceName = recordParserHelper.getDatasourceName(record);
58
		//objIdentifier contains the subject URI used to get the RDF: that is the only resource we have to register when processing this rdf file!
59
		log.debug("REGISTERING ON JRR: "+id);
60
		String rdfRecord = recordParserHelper.getRDF(record);
61
		InfModel model = loadBaseModel();
62
		model.read(IOUtils.toInputStream(rdfRecord), CRMpe.NS, "RDF/XML");
63
		register(model, id, datasourceName);
64
	}
65

    
66

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

    
101
	protected boolean registerRDFResource(final Resource rdfResource, final Resource type, final String datasourceName)
102
			throws ParthenosPublisherException, IOException, URISyntaxException, InterruptedException {
103
		String resURI = rdfResource.getURI();
104
		if (!resURI.startsWith("http")) {
105
			//this is something George said: if it has no http URI, then it is not to be considered relevant by itself
106
			log.info("Resource " + resURI + " skipped: URI does not start with http");
107
			return false;
108
		}
109
		else {
110
				String uuid = registerOnCatalogue(rdfResource, type, datasourceName);
111
				if(StringUtils.isNotBlank(uuid)){
112
					//TODO: let's skip the registration on the registry for now.
113
					//registerOnRegistry(rdfResource, uuid, type);
114
					return true;
115
				}
116
				else{
117
					log.warn("Got blank uuid when registering "+resURI+": skipping registration on the registry");
118
					return false;
119
				}
120
			}
121
		}
122

    
123
	/**
124
	 * Register resource of the given type on the catalogue
125
	 * @param rdfResource
126
	 * @param type
127
	 * @return the catalogue uuid
128
	 * @throws IOException
129
	 */
130
	protected String registerOnCatalogue(final Resource rdfResource, final Resource type, final String datasourceName)
131
			throws ParthenosPublisherException, IOException, URISyntaxException, InterruptedException {
132
		return catalogueRegistrator.register(rdfResource, type, datasourceName);
133
	}
134

    
135
	/*
136
	protected void registerOnRegistry(final Resource rdfResource, final String uuid, final Resource type)
137
			throws ParthenosPublisherException, ResourceRegistryException, IOException {
138
			gCubeResourceRegistrator.register(rdfResource, uuid, type);
139
	}
140
	*/
141

    
142
	protected InfModel loadBaseModel() {
143
		return ModelFactory.createRDFSModel(baseModel);
144
	}
145

    
146

    
147
}
(1-1/3)