Project

General

Profile

1
package eu.dnetlib.parthenos.registry;
2

    
3
import java.io.IOException;
4
import java.io.StringWriter;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.Set;
8

    
9
import com.fasterxml.jackson.core.JsonFactory;
10
import com.fasterxml.jackson.core.JsonGenerator;
11
import com.google.common.collect.Lists;
12
import com.google.common.collect.Maps;
13
import com.google.common.collect.Sets;
14
import eu.dnetlib.parthenos.CRM;
15
import eu.dnetlib.parthenos.CRMdig;
16
import eu.dnetlib.parthenos.CRMpe;
17
import org.apache.commons.io.IOUtils;
18
import org.apache.commons.lang3.StringUtils;
19
import org.apache.commons.logging.Log;
20
import org.apache.commons.logging.LogFactory;
21
import org.apache.jena.assembler.AssemblerHelp;
22
import org.apache.jena.assembler.exceptions.AmbiguousSpecificTypeException;
23
import org.apache.jena.ontology.OntModel;
24
import org.apache.jena.ontology.OntModelSpec;
25
import org.apache.jena.rdf.model.InfModel;
26
import org.apache.jena.rdf.model.ModelFactory;
27
import org.apache.jena.rdf.model.ResIterator;
28
import org.apache.jena.rdf.model.Resource;
29
import org.apache.jena.vocabulary.RDF;
30
import org.springframework.beans.factory.annotation.Autowired;
31
import org.springframework.stereotype.Component;
32

    
33
/**
34
 * Created by Alessia Bardi on 26/09/2017.
35
 *
36
 * @author Alessia Bardi
37
 */
38
@Component
39
public class GCubeResourceGenerator {
40

    
41
	private static final Log log = LogFactory.getLog(GCubeResourceGenerator.class);
42

    
43
	private OntModel baseModel;
44

    
45
	@Autowired
46
	private FacetWriter facetWriter;
47

    
48
	public GCubeResourceGenerator() {
49
		final String url = "http://www.w3.org/TR/REC-rdf-syntax/example14.nt";
50
		baseModel = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_TRANS_INF);
51
		baseModel.read(CRMpe.RDFS_URL);
52
		baseModel.read(CRM.RDFS_URL);
53
		baseModel.read(CRMdig.RDFS_URL);
54
	}
55

    
56
	protected InfModel loadBaseModel(){
57
		return ModelFactory.createRDFSModel(baseModel);
58
	}
59

    
60
	/**
61
	 * Generates the Gcube ER instances (Entity [Resource/Facet] and Relation) as json strings to be registered from a given RDF record serialised in RDF/XML PLAIN format.
62
	 * @param rdfRecord RDF record in RDF/XML PLAIN format
63
	 * @return an Iterable of json strings or an empty Iterable if the record is blank
64
	 */
65
	public Iterable<String> getGCubeER(final String rdfRecord) throws IOException {
66
		List<String> jsonResources = Lists.newArrayList();
67
		//This map contains the pair (rdf:about, registryUUID) registered so far, useful I guess to create relationships between resources/facets.
68
		Map<String,String> resourceIdentifiers = Maps.newHashMap();
69
		if (StringUtils.isBlank(rdfRecord)) {
70
			log.warn("Got empty record");
71
			return Lists.newArrayList();
72
		}
73

    
74
		InfModel model = loadBaseModel();
75
		model.read(IOUtils.toInputStream(rdfRecord, "UTF-8"), CRMpe.NS);
76

    
77
		JsonFactory jsonFactory = new JsonFactory();
78
		Set<String> uriProcessed = Sets.newHashSet();
79

    
80
		//let's start with services
81
		ResIterator iter = model.listResourcesWithProperty(RDF.type, CRMpe.PE1_Service);
82
		while (iter.hasNext()) {
83
			Resource res = iter.nextResource();
84
			String resourceURI = res.getURI();
85
			if (!uriProcessed.contains(resourceURI)) {
86
				log.debug("Processing "+resourceURI);
87
				Resource specificType = findSpecificType(res, CRMpe.PE1_Service);
88
				StringWriter sw = new StringWriter();
89
				JsonGenerator jg = jsonFactory.createGenerator(sw);
90
				jg.writeStartObject();
91

    
92
				jg.writeStringField("@class", specificType.getURI());
93

    
94
				//******THE FACETS *******//
95
				jg.writeArrayFieldStart("consistsOf");
96
				//list of facets
97

    
98
				facetWriter.writeIdentifierFacet(jg, resourceURI);
99
				facetWriter.writeInfoFacet(jg, res);
100
				facetWriter.writeEventFacet(jg);
101
				facetWriter.writeRightsFacet(jg, res);
102
				facetWriter.writeContactReferenceFacet(jg, res);
103
				facetWriter.writeDesignatedAccessPointFacet(jg, res);
104

    
105
				jg.writeEndArray();
106

    
107
				// ******* RELATIONSHIPS *****//
108
				jg.writeArrayFieldStart("isRelatedto");
109

    
110
				jg.writeEndArray();
111

    
112

    
113
				jg.writeEndObject();
114
				jg.close();
115
				String json = sw.toString();
116
				log.debug(json);
117
				jsonResources.add(json);
118
				uriProcessed.add(resourceURI);
119
			} else {
120
				log.debug(resourceURI+" already processed, now skipping it");
121
			}
122
		}
123
		return jsonResources;
124
	}
125

    
126
	/**
127
	 * Finds the most specific type of res.
128
	 * @param res Resource you want to find the most specific type
129
	 * @param fallbackType Resource representing the type to return if there is no type or if we get AmbiguousSpecificTypeException
130
	 * @return Resource: the most specific type, if any. fallbackType otherwise
131
	 */
132
	protected Resource findSpecificType(final Resource res, final Resource fallbackType){
133
		Resource specType = fallbackType;
134
		try{
135
			specType = AssemblerHelp.findSpecificType(res, fallbackType);
136
		}catch(AmbiguousSpecificTypeException e){
137
			log.warn(res.getURI()+": "+e.getMessage());
138
		}
139
		return specType;
140
	}
141

    
142
	public FacetWriter getFacetWriter() {
143
		return facetWriter;
144
	}
145

    
146
	public void setFacetWriter(final FacetWriter facetWriter) {
147
		this.facetWriter = facetWriter;
148
	}
149
}
(2-2/4)