Project

General

Profile

1 50937 alessia.ba
package eu.dnetlib.parthenos.jrr;
2
3
import java.io.IOException;
4 51287 alessia.ba
import java.net.URISyntaxException;
5 51321 alessia.ba
import javax.annotation.PostConstruct;
6 50937 alessia.ba
7
import eu.dnetlib.parthenos.CRM;
8 51321 alessia.ba
import eu.dnetlib.parthenos.CRMdig;
9 50937 alessia.ba
import eu.dnetlib.parthenos.CRMpe;
10
import eu.dnetlib.parthenos.catalogue.CatalogueRegistrator;
11
import eu.dnetlib.parthenos.publisher.ParthenosPublisherException;
12 51276 alessia.ba
import eu.dnetlib.parthenos.rdf.RecordParserHelper;
13 51429 alessia.ba
import org.apache.commons.io.IOUtils;
14 50937 alessia.ba
import org.apache.commons.lang3.StringUtils;
15
import org.apache.commons.logging.Log;
16
import org.apache.commons.logging.LogFactory;
17 51321 alessia.ba
import org.apache.jena.ontology.OntModel;
18
import org.apache.jena.ontology.OntModelSpec;
19 51096 alessia.ba
import org.apache.jena.rdf.model.*;
20 50937 alessia.ba
import org.apache.jena.vocabulary.RDF;
21
import org.springframework.beans.factory.annotation.Autowired;
22
import org.springframework.stereotype.Component;
23
24 51663 alessia.ba
25 50937 alessia.ba
/**
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 51321 alessia.ba
	private OntModel baseModel;
38 50937 alessia.ba
39
	@Autowired
40
	private CatalogueRegistrator catalogueRegistrator;
41 55561 alessia.ba
	//@Autowired
42
	//private GCubeResourceRegistrator gCubeResourceRegistrator;
43 50937 alessia.ba
	@Autowired
44 51276 alessia.ba
	private RecordParserHelper recordParserHelper;
45 50937 alessia.ba
46 51321 alessia.ba
	@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 57139 alessia.ba
	//No need to pass the RDF inside an OAI record: use register(String, String, String) to explicitely pass the needed information.
55
	@Deprecated
56 51767 alessia.ba
	public void register(final String record)
57 55561 alessia.ba
			throws ParthenosPublisherException, IOException, URISyntaxException, InterruptedException {
58 51276 alessia.ba
		String id = recordParserHelper.getObjIdentifier(record);
59 51709 alessia.ba
		String datasourceName = recordParserHelper.getDatasourceName(record);
60 56986 alessia.ba
		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 51696 alessia.ba
		//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 56986 alessia.ba
		log.debug("REGISTERING ON JRR: "+objIdentifier);
68 51321 alessia.ba
		InfModel model = loadBaseModel();
69 51429 alessia.ba
		model.read(IOUtils.toInputStream(rdfRecord), CRMpe.NS, "RDF/XML");
70 56986 alessia.ba
		register(model, objIdentifier, datasourceName);
71 50937 alessia.ba
	}
72
73 51321 alessia.ba
74 51767 alessia.ba
	protected void register(final Model model, final String resourceURI, final String datasourceName)
75
			throws ParthenosPublisherException, IOException, URISyntaxException, InterruptedException {
76 51696 alessia.ba
		Resource rdfResource = model.getResource(resourceURI);
77 52248 alessia.ba
		if (rdfResource == null) {
78
			log.error("UNEXPECTED NULL rdfResource with resourceURI " + resourceURI + ". I am skipping it, but you should check!");
79
		} else {
80 51696 alessia.ba
			//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 54681 alessia.ba
			////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 51709 alessia.ba
			if (rdfResource.hasProperty(RDF.type, CRM.E39_Actor)) registerRDFResource(rdfResource, CRM.E39_Actor, datasourceName);
84 51696 alessia.ba
			else {
85 52248 alessia.ba
				if (rdfResource.hasProperty(RDF.type, CRMpe.PE35_Project)) registerRDFResource(rdfResource, CRMpe.PE35_Project, datasourceName);
86 51696 alessia.ba
				else {
87 53694 alessia.ba
					if (rdfResource.hasProperty(RDF.type, CRM.E29_Design_or_Procedure)) registerRDFResource(rdfResource, CRM.E29_Design_or_Procedure, datasourceName);
88 51696 alessia.ba
					else {
89 53694 alessia.ba
						if (rdfResource.hasProperty(RDF.type, CRMdig.D14_Software)) registerRDFResource(rdfResource, CRMdig.D14_Software, datasourceName);
90 52248 alessia.ba
						else {
91 53694 alessia.ba
							if (rdfResource.hasProperty(RDF.type, CRMpe.PE18_Dataset)) registerRDFResource(rdfResource, CRMpe.PE18_Dataset, datasourceName);
92 52248 alessia.ba
							else {
93
								if (rdfResource.hasProperty(RDF.type, CRM.E78_Collection)) registerRDFResource(rdfResource, CRM.E78_Collection, datasourceName);
94
								else {
95 53694 alessia.ba
									if (rdfResource.hasProperty(RDF.type, CRMpe.PE1_Service)) registerRDFResource(rdfResource, CRMpe.PE1_Service, datasourceName);
96 52248 alessia.ba
									else {
97
										log.debug("Skipping " + resourceURI + " because of its type");
98
									}
99
								}
100
							}
101
						}
102 51696 alessia.ba
					}
103
				}
104 50937 alessia.ba
			}
105
		}
106
	}
107
108 52754 alessia.ba
	protected boolean registerRDFResource(final Resource rdfResource, final Resource type, final String datasourceName)
109 51767 alessia.ba
			throws ParthenosPublisherException, IOException, URISyntaxException, InterruptedException {
110 50937 alessia.ba
		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 52754 alessia.ba
				String uuid = registerOnCatalogue(rdfResource, type, datasourceName);
118 51663 alessia.ba
				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 50937 alessia.ba
			}
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 52754 alessia.ba
	protected String registerOnCatalogue(final Resource rdfResource, final Resource type, final String datasourceName)
138 51767 alessia.ba
			throws ParthenosPublisherException, IOException, URISyntaxException, InterruptedException {
139 52754 alessia.ba
		return catalogueRegistrator.register(rdfResource, type, datasourceName);
140 50937 alessia.ba
	}
141
142 55561 alessia.ba
	/*
143 50937 alessia.ba
	protected void registerOnRegistry(final Resource rdfResource, final String uuid, final Resource type)
144
			throws ParthenosPublisherException, ResourceRegistryException, IOException {
145
			gCubeResourceRegistrator.register(rdfResource, uuid, type);
146 51696 alessia.ba
	}
147 55561 alessia.ba
	*/
148 50937 alessia.ba
149 51696 alessia.ba
	protected InfModel loadBaseModel() {
150
		return ModelFactory.createRDFSModel(baseModel);
151 50937 alessia.ba
	}
152
153 51696 alessia.ba
154 50937 alessia.ba
}