Project

General

Profile

« Previous | Next » 

Revision 49324

feeding the registry with resources and relationships

View differences:

modules/dnet-parthenos-publisher/trunk/src/main/java/eu/dnetlib/parthenos/registry/GCubeResourceGenerator.java
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
	@Autowired
48
	private RelWriter relWriter;
49

  
50
	public GCubeResourceGenerator() {
51
		baseModel = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_TRANS_INF);
52
		baseModel.read(CRMpe.RDFS_URL);
53
		baseModel.read(CRM.RDFS_URL);
54
		baseModel.read(CRMdig.RDFS_URL);
55
	}
56

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

  
61
	/**
62
	 * 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.
63
	 * @param rdfRecord RDF record in RDF/XML PLAIN format
64
	 * @return an Iterable of json strings or an empty Iterable if the record is blank
65
	 */
66
	public Iterable<String> getGCubeER(final String rdfRecord) throws IOException {
67
		List<String> jsonResources = Lists.newArrayList();
68
		//This map contains the pair (rdf:about, registryUUID) registered so far, useful I guess to create relationships between resources/facets.
69
		Map<String,String> resourceIdentifiers = Maps.newHashMap();
70
		if (StringUtils.isBlank(rdfRecord)) {
71
			log.warn("Got empty record");
72
			return Lists.newArrayList();
73
		}
74

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

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

  
81
		//TODO: many things in common in the code below: re-factor it!
82

  
83

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

  
97
				//TODO: generate header with uuid for all type of entities, so we can also generate the rels. Need a map for uri --> uuid.
98
				jg.writeStringField("@class", specificType.getLocalName());
99

  
100
				//******THE FACETS *******//
101
				jg.writeArrayFieldStart("consistsOf");
102
				//list of facets
103
				//TODO: write P1 identifier facet following the existing links (for all types)
104
				facetWriter.writeIdentifierFacet(jg, resourceURI);
105
				facetWriter.writeInfoFacet(jg, res);
106
				facetWriter.writeEventFacet(jg);
107
				facetWriter.writeRightsFacet(jg, res);
108
				if(res.hasProperty(CRMpe.PP2_provided_by)){
109
					//TODO: shouldn't this be a rel to an actor?
110
					Resource provider = res.getPropertyResourceValue(CRMpe.PP2_provided_by);
111
					facetWriter.writeContactReferenceFacet(jg, provider);
112
				}
113

  
114
				facetWriter.writeDesignatedAccessPointFacet(jg, res);
115

  
116
				jg.writeEndArray();
117

  
118
				// ******* RELATIONSHIPS ***** ??? or separately after we got the ids of everything?//
119
				//jg.writeArrayFieldStart("isRelatedto");
120
				//jg.writeEndArray();
121

  
122
				jg.writeEndObject();
123
				jg.close();
124
				String json = sw.toString();
125
				log.debug(json);
126
				jsonResources.add(json);
127
				uriProcessed.add(resourceURI);
128
			} else {
129
				log.debug(resourceURI+" already processed, now skipping it");
130
			}
131
		}
132

  
133
		log.debug("Processing digital objects (include dataset and software)");
134
		//and then the datasets
135
		ResIterator iterDo = model.listResourcesWithProperty(RDF.type, CRMdig.D1_Digital_Object);
136
		while (iterDo.hasNext()) {
137
			Resource res = iterDo.nextResource();
138
			String resourceURI = res.getURI();
139
			if (!uriProcessed.contains(resourceURI)) {
140
				log.debug("Processing " + resourceURI);
141
				Resource specificType = findSpecificType(res, CRMdig.D1_Digital_Object);
142
				StringWriter sw = new StringWriter();
143
				JsonGenerator jg = jsonFactory.createGenerator(sw);
144
				jg.writeStartObject();
145

  
146
				jg.writeStringField("@class", specificType.getLocalName());
147
				//******THE FACETS *******//
148
				jg.writeArrayFieldStart("consistsOf");
149
				//list of facets
150
				facetWriter.writeIdentifierFacet(jg, resourceURI);
151
				facetWriter.writeInfoFacet(jg, res);
152
				facetWriter.writeTemporalCoverageFacet(jg, res);
153
				facetWriter.writeRightsFacet(jg, res);
154

  
155
				jg.writeEndArray();
156
				jg.writeEndObject();
157
				jg.close();
158
				String json = sw.toString();
159
				log.debug(json);
160
				jsonResources.add(json);
161
				uriProcessed.add(resourceURI);
162

  
163
			} else {
164
				log.debug(resourceURI + " already processed, now skipping it");
165
			}
166
		}
167

  
168
			log.debug("Processing actors");
169
			//and then the datasets
170
			ResIterator iterActors = model.listResourcesWithProperty(RDF.type, CRM.E39_Actor);
171
			while (iterActors.hasNext()) {
172
				Resource res = iterActors.nextResource();
173
				String resourceURI = res.getURI();
174
				if (!uriProcessed.contains(resourceURI)) {
175
					log.debug("Processing " + resourceURI);
176
					Resource specificType = findSpecificType(res, CRM.E39_Actor);
177
					StringWriter sw = new StringWriter();
178
					JsonGenerator jg = jsonFactory.createGenerator(sw);
179
					jg.writeStartObject();
180

  
181
					jg.writeStringField("@class", specificType.getLocalName());
182
					//******THE FACETS *******//
183
					jg.writeArrayFieldStart("consistsOf");
184
					//list of facets
185
					facetWriter.writeIdentifierFacet(jg, resourceURI);
186
					facetWriter.writeContactReferenceFacet(jg, res);
187

  
188
					jg.writeEndArray();
189
					jg.writeEndObject();
190
					jg.close();
191
					String json = sw.toString();
192
					log.debug(json);
193
					jsonResources.add(json);
194
					uriProcessed.add(resourceURI);
195

  
196
				} else {
197
					log.debug(resourceURI + " already processed, now skipping it");
198
				}
199
			}
200
		log.debug("Processing software");
201
		ResIterator iterSw = model.listResourcesWithProperty(RDF.type, CRMdig.D14_Software);
202
		while (iterSw.hasNext()) {
203
			Resource res = iterSw.nextResource();
204
			String resourceURI = res.getURI();
205
			if (!uriProcessed.contains(resourceURI)) {
206
				log.debug("Processing " + resourceURI);
207
				Resource specificType = findSpecificType(res, CRMdig.D14_Software);
208
				StringWriter sw = new StringWriter();
209
				JsonGenerator jg = jsonFactory.createGenerator(sw);
210
				jg.writeStartObject();
211

  
212
				jg.writeStringField("@class", specificType.getLocalName());
213
				//******THE FACETS *******//
214
				jg.writeArrayFieldStart("consistsOf");
215
				//list of facets
216
				facetWriter.writeIdentifierFacet(jg, resourceURI);
217
				facetWriter.writeInfoFacet(jg, res);
218

  
219
				jg.writeEndArray();
220
				jg.writeEndObject();
221
				jg.close();
222
				String json = sw.toString();
223
				log.debug(json);
224
				jsonResources.add(json);
225
				uriProcessed.add(resourceURI);
226

  
227
			} else {
228
				log.debug(resourceURI + " already processed, now skipping it");
229
			}
230
		}
231
		log.debug("Processing projects");
232
		//and then the projects
233
		ResIterator iterProjects = model.listResourcesWithProperty(RDF.type, CRMpe.PE35_Project);
234
		while (iterProjects.hasNext()) {
235
			Resource res = iterProjects.nextResource();
236
			String resourceURI = res.getURI();
237
			if (!uriProcessed.contains(resourceURI)) {
238
				log.debug("Processing " + resourceURI);
239
				Resource specificType = findSpecificType(res, CRMpe.PE35_Project);
240
				StringWriter sw = new StringWriter();
241
				JsonGenerator jg = jsonFactory.createGenerator(sw);
242
				jg.writeStartObject();
243

  
244
				jg.writeStringField("@class", specificType.getLocalName());
245
				//******THE FACETS *******//
246
				jg.writeArrayFieldStart("consistsOf");
247
				//list of facets
248
				facetWriter.writeIdentifierFacet(jg, resourceURI);
249
				facetWriter.writeInfoFacet(jg, res);
250

  
251
				jg.writeEndArray();
252
				jg.writeEndObject();
253
				jg.close();
254
				String json = sw.toString();
255
				log.debug(json);
256
				jsonResources.add(json);
257
				uriProcessed.add(resourceURI);
258

  
259
			} else {
260
				log.debug(resourceURI + " already processed, now skipping it");
261
			}
262
		}
263
		return jsonResources;
264
	}
265

  
266

  
267

  
268

  
269
	/**
270
	 * Finds the most specific type of res.
271
	 * @param res Resource you want to find the most specific type
272
	 * @param fallbackType Resource representing the type to return if there is no type or if we get AmbiguousSpecificTypeException
273
	 * @return Resource: the most specific type, if any. fallbackType otherwise
274
	 */
275
	protected Resource findSpecificType(final Resource res, final Resource fallbackType){
276
		Resource specType = fallbackType;
277
		try{
278
			specType = AssemblerHelp.findSpecificType(res, fallbackType);
279
		}catch(AmbiguousSpecificTypeException e){
280
			log.warn(res.getURI()+": "+e.getMessage());
281
		}
282
		return specType;
283
	}
284

  
285
	public FacetWriter getFacetWriter() {
286
		return facetWriter;
287
	}
288

  
289
	public void setFacetWriter(final FacetWriter facetWriter) {
290
		this.facetWriter = facetWriter;
291
	}
292
}
modules/dnet-parthenos-publisher/trunk/test/main/java/eu/dnetlib/parthenos/registry/FacetWriterTest.java
48 48
	 */
49 49
	@Test
50 50
	public void testWriteContactReferenceFacet() throws Exception {
51
		GCubeResourceGenerator generator = new GCubeResourceGenerator();
51
		GCubeResourceRegistrator generator = new GCubeResourceRegistrator();
52 52
		JsonFactory jsonFactory = new JsonFactory();
53 53
		StringWriter sw = new StringWriter();
54 54
		JsonGenerator jg = jsonFactory.createGenerator(sw);
modules/dnet-parthenos-publisher/trunk/test/main/java/eu/dnetlib/parthenos/registry/RelWriterTest.java
1
package eu.dnetlib.parthenos.registry;
2

  
3
import java.io.IOException;
4

  
5
import com.fasterxml.jackson.core.JsonFactory;
6
import org.junit.Test;
7

  
8
/**
9
 * Created by Alessia Bardi on 06/10/2017.
10
 *
11
 * @author Alessia Bardi
12
 */
13
public class RelWriterTest {
14

  
15
	private RelWriter relWriter = new RelWriter();
16

  
17
	@Test
18
	public void test() throws IOException {
19
		String rel = relWriter.writeRelationship(new JsonFactory(), "theType", "uuid1", "typeSource", "uuid2", "typeTarget");
20
		System.out.println(rel);
21
	}
22
}
modules/dnet-parthenos-publisher/trunk/test/main/java/eu/dnetlib/parthenos/registry/GCubeResourceGeneratorTest.java
21 21

  
22 22
	private static String recordPath = "eu/dnetlib/parthenos/registry/sample1.rdf";
23 23
	private static String baseURI = "http://parthenos.d4science.org/CRMext/CRMpe.rdfs/";
24
	private static GCubeResourceGenerator generator = new GCubeResourceGenerator();
24
	private static GCubeResourceRegistrator generator = new GCubeResourceRegistrator();
25 25
	private static InfModel baseModel;
26 26

  
27 27
	static{
......
37 37

  
38 38
	@Test
39 39
	public void testLoadBaseModel() {
40
		GCubeResourceGenerator generator1 = new GCubeResourceGenerator();
40
		GCubeResourceRegistrator generator1 = new GCubeResourceRegistrator();
41 41
		InfModel model = generator1.loadBaseModel();
42 42
		//let's snapshot to a normal model to write all statements
43 43
		Model snapshot = ModelFactory.createDefaultModel();
modules/dnet-parthenos-publisher/trunk/dnet-parthenos-publisher.iml
96 96
    <orderEntry type="library" name="Maven: org.codehaus.woodstox:woodstox-core-asl:4.4.1" level="project" />
97 97
    <orderEntry type="library" name="Maven: org.noggit:noggit:0.6" level="project" />
98 98
    <orderEntry type="library" name="Maven: org.slf4j:jcl-over-slf4j:1.7.25" level="project" />
99
    <orderEntry type="module" module-name="parthenos-entities" />
99
    <orderEntry type="library" name="Maven: org.gcube.information-system:parthenos-entities:0.0.1-SNAPSHOT" level="project" />
100 100
    <orderEntry type="library" name="Maven: org.gcube.information-system:information-system-model:1.6.0-SNAPSHOT" level="project" />
101 101
    <orderEntry type="library" name="Maven: org.gcube.information-system:gcube-resources:1.6.0-SNAPSHOT" level="project" />
102 102
    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
103 103
    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.9" level="project" />
104
    <orderEntry type="module" module-name="resource-registry-publisher" />
104
    <orderEntry type="library" name="Maven: org.gcube.information-system:resource-registry-publisher:1.5.0-SNAPSHOT" level="project" />
105 105
    <orderEntry type="library" name="Maven: org.gcube.common:authorization-client:2.0.1-4.6.1-134803" level="project" />
106 106
    <orderEntry type="library" name="Maven: org.gcube.common:common-authorization:2.0.2-4.6.1-144378" level="project" />
107 107
    <orderEntry type="library" name="Maven: org.gcube.core:common-configuration-scanner:1.0.0-4.6.1-144261" level="project" />
......
111 111
    <orderEntry type="library" name="Maven: org.gcube.resources.discovery:discovery-client:1.0.1-4.6.1-125857" level="project" />
112 112
    <orderEntry type="library" name="Maven: org.gcube.resources:common-gcore-resources:1.3.3-4.6.1-144114" level="project" />
113 113
    <orderEntry type="library" name="Maven: org.gcube.core:common-gcore-stubs:1.2.2-4.6.1-132342" level="project" />
114
    <orderEntry type="library" name="Maven: org.gcube.information-system:resource-registry-api:1.6.0-SNAPSHOT" level="project" />
114
    <orderEntry type="library" name="Maven: org.gcube.information-system:resource-registry-api:1.7.0-SNAPSHOT" level="project" />
115 115
    <orderEntry type="library" name="Maven: org.gcube.information-system:resource-registry-client:1.5.0-SNAPSHOT" level="project" />
116 116
  </component>
117 117
</module>
modules/dnet-parthenos-publisher/trunk/src/main/java/eu/dnetlib/parthenos/registry/FacetWriter.java
5 5
import com.fasterxml.jackson.core.JsonGenerator;
6 6
import eu.dnetlib.parthenos.CRM;
7 7
import eu.dnetlib.parthenos.CRMpe;
8
import org.apache.commons.lang3.StringUtils;
8 9
import org.apache.jena.rdf.model.Resource;
9 10
import org.apache.jena.rdf.model.Statement;
10 11
import org.apache.jena.rdf.model.StmtIterator;
11 12
import org.apache.jena.vocabulary.RDF;
12 13
import org.apache.jena.vocabulary.RDFS;
13
import org.gcube.informationsystem.model.entity.facet.IdentifierFacet.IdentificationType;
14 14
import org.springframework.stereotype.Component;
15 15

  
16 16
/**
......
21 21
@Component
22 22
public class FacetWriter {
23 23

  
24
	public void writeP1Facet(final JsonGenerator jg, final String value, final IdentificationType type) throws IOException {
24
	//final IdentificationType type
25
	public void writeP1Facet(final JsonGenerator jg, final String value) throws IOException {
25 26
		jg.writeStartObject();
26 27
		jg.writeStringField("@class", "P1_is_identified_by");
27 28
		jg.writeObjectFieldStart("target");
28 29
		jg.writeStringField("@class", "IdentifierFacet");
29 30
		jg.writeStringField("value", value);
30
		jg.writeStringField("type", type.name());
31
		//jg.writeStringField("type", type.name());
31 32
		jg.writeEndObject();
32 33
		jg.writeEndObject();
33 34
	}
......
38 39
		jg.writeObjectFieldStart("target");
39 40
		jg.writeStringField("@class", "IdentifierFacet");
40 41
		jg.writeStringField("value", identifier);
41
		jg.writeStringField("type", IdentificationType.URI.name());
42
		//jg.writeStringField("type", IdentificationType.URI.name());
42 43
		jg.writeEndObject();
43 44
		jg.writeEndObject();
44 45
	}
45 46

  
47
	public void writeP1Facets(final JsonGenerator jg, final Resource res) throws IOException {
48
		if(res.hasProperty(CRM.P1_is_identified_by)){
49
			StmtIterator idRelsIt = res.listProperties(CRM.P1_is_identified_by);
50
			while(idRelsIt.hasNext()){
51
				Resource target = idRelsIt.nextStatement().getResource();
52
				String label = getLabelFromRDFResource(target);
53
				if(StringUtils.isNotBlank(label)) {
54
					writeP1Facet(jg, label);
55
				}
56
			}
57
		}
58
	}
59

  
46 60
	public void writeInfoFacet(final JsonGenerator jg, final Resource res) throws IOException {
47 61
		jg.writeStartObject();
62
		jg.writeStringField("@class", "consistsOf");
63
		jg.writeObjectFieldStart("target");
48 64
		jg.writeStringField("@class", "PE_Info_Facet");
49
		jg.writeObjectFieldStart("target");
50 65
		jg.writeStringField("title", getTitleFromRDFResource(res));
51 66
		jg.writeStringField("description", getDescriptionFromRDFResource(res));
52 67
		//new object comp + field value + schema="noSchema"
53
		jg.writeStringField("competence", getCompetenceFromRDFResource(res));
68
		String competence = getCompetenceFromRDFResource(res);
69
		if(StringUtils.isNotBlank(competence)) {
70
			writeValueSchema(jg, "competence", competence, "noSchema");
71
		}
54 72
		//TODO: uncomment this when George adds the rel to the model, see method getAvailabilityFromRDFResource below
55
		//jg.writeStringField("availability", getAvailabilityFromRDFResource(res));
73
		//String availability = getAvailabilityFromRDFResource(res);
74
//		if(StringUtils.isNotBlank(availability)) {
75
//			writeValueSchema(jg, "availability", availability, "noSchema");
76
//		}
56 77
		jg.writeEndObject();
57 78
		jg.writeEndObject();
58 79
	}
59 80

  
81
	protected void writeValueSchema(final JsonGenerator jg, final String fieldName, final String value, final String schema) throws IOException {
82
		jg.writeObjectFieldStart(fieldName);
83
		jg.writeStringField("value", value);
84
		jg.writeStringField("schema", schema);
85
		jg.writeEndObject();
86
	}
87

  
60 88
	public void writeEventFacet(final JsonGenerator jg) {
61 89
		//TODO: implement me. get begin/end of operation from PP42_has_declarative_time
62 90
	}
modules/dnet-parthenos-publisher/trunk/src/main/java/eu/dnetlib/parthenos/registry/RegistryClientFactory.java
35 35
	@Autowired
36 36
	private SaxonHelper saxonHelper;
37 37
	@Autowired
38
	private GCubeResourceGenerator resourceGenerator;
38
	private GCubeResourceRegistrator resourceGenerator;
39 39

  
40 40
	public RegistryClient getRegistryClient() throws ParthenosPublisherException {
41 41
		log.debug("Creating RegistryClient for "+registryBaseURL);
......
101 101
		this.defaultBaseURI = defaultBaseURI;
102 102
	}
103 103

  
104
	public GCubeResourceGenerator getResourceGenerator() {
104
	public GCubeResourceRegistrator getResourceGenerator() {
105 105
		return resourceGenerator;
106 106
	}
107 107

  
108
	public void setResourceGenerator(final GCubeResourceGenerator resourceGenerator) {
108
	public void setResourceGenerator(final GCubeResourceRegistrator resourceGenerator) {
109 109
		this.resourceGenerator = resourceGenerator;
110 110
	}
111 111
}
modules/dnet-parthenos-publisher/trunk/src/main/java/eu/dnetlib/parthenos/registry/RelWriter.java
1 1
package eu.dnetlib.parthenos.registry;
2 2

  
3
import java.io.IOException;
4
import java.io.StringWriter;
5

  
6
import com.fasterxml.jackson.core.JsonFactory;
7
import com.fasterxml.jackson.core.JsonGenerator;
3 8
import org.springframework.stereotype.Component;
4 9

  
5 10
/**
......
10 15
@Component
11 16
public class RelWriter {
12 17

  
18
	protected String writeRelationship(final JsonFactory jsonFactory, final String relType, final String sourceUUID, final String sourceType, final String targetUUID, final String targetType)
19
			throws IOException {
20
		StringWriter sw = new StringWriter();
21
		JsonGenerator jg = jsonFactory.createGenerator(sw);
22
		jg.writeStartObject();
23
		jg.writeStringField("@class", relType);
24
		jg.writeObjectFieldStart("source");
25
		jg.writeObjectFieldStart("header");
26
		jg.writeStringField("uuid", sourceUUID);
27
		jg.writeEndObject();
28
		jg.writeStringField("@class", sourceType);
29
		jg.writeEndObject();
30
		jg.writeObjectFieldStart("target");
31
		jg.writeObjectFieldStart("header");
32
		jg.writeStringField("uuid", targetUUID);
33
		jg.writeEndObject();
34
		jg.writeStringField("@class", targetType);
35
		jg.writeEndObject();
36
		jg.close();
37
		return sw.toString();
38
	}
13 39
}
modules/dnet-parthenos-publisher/trunk/src/main/java/eu/dnetlib/parthenos/registry/GCubeResourceRegistrator.java
1
package eu.dnetlib.parthenos.registry;
2

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

  
9
import com.fasterxml.jackson.core.JsonFactory;
10
import com.fasterxml.jackson.core.JsonGenerator;
11
import com.google.common.collect.Maps;
12
import com.google.common.collect.Sets;
13
import eu.dnetlib.parthenos.CRM;
14
import eu.dnetlib.parthenos.CRMdig;
15
import eu.dnetlib.parthenos.CRMpe;
16
import eu.dnetlib.parthenos.publisher.ParthenosPublisherException;
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.*;
26
import org.apache.jena.vocabulary.RDF;
27
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
28
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
29
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
30
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher;
31
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory;
32
import org.springframework.beans.factory.annotation.Autowired;
33
import org.springframework.stereotype.Component;
34

  
35
/**
36
 * Created by Alessia Bardi on 26/09/2017.
37
 *
38
 * @author Alessia Bardi
39
 */
40
@Component
41
public class GCubeResourceRegistrator {
42

  
43
	private static final Log log = LogFactory.getLog(GCubeResourceRegistrator.class);
44

  
45
	private OntModel baseModel;
46

  
47
	@Autowired
48
	private FacetWriter facetWriter;
49
	@Autowired
50
	private RelWriter relWriter;
51

  
52
	private ResourceRegistryPublisher resourceRegistryPublisher;
53
	private ResourceRegistryClient resourceRegistryClient;
54

  
55
	public GCubeResourceRegistrator() {
56
		this.resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
57
		this.resourceRegistryClient = ResourceRegistryClientFactory.create();
58
		baseModel = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_TRANS_INF);
59
		baseModel.read(CRMpe.RDFS_URL);
60
		baseModel.read(CRM.RDFS_URL);
61
		baseModel.read(CRMdig.RDFS_URL);
62
	}
63

  
64
	public int unregister(final String datasourceApi){
65
		//TODO: implement me
66
		return -1;
67
	}
68

  
69
	/**
70
	 * Registers resources and relationships into the Parthenos Registry
71
	 *
72
	 * @param rdfRecord RDF record in RDF/XML PLAIN format
73
	 */
74
	public void register(final String rdfRecord, final String objIdentifier) throws IOException, ResourceRegistryException, ParthenosPublisherException {
75
		if (StringUtils.isBlank(rdfRecord)) {
76
			log.warn("Got empty record "+objIdentifier);
77
		} else {
78
			InfModel model = loadBaseModel();
79
			model.read(IOUtils.toInputStream(rdfRecord, "UTF-8"), CRMpe.NS);
80

  
81
			JsonFactory jsonFactory = new JsonFactory();
82
			Set<String> uriProcessed = Sets.newHashSet();
83
			/* Maps URI to ParthenosRegistryResource (with uuids) */
84
			Map<String, ParthenosRegistryResource> idMap = Maps.newHashMap();
85

  
86
			/* Resource */
87
			int registeredServices = processServices(model, uriProcessed, jsonFactory, idMap);
88
			int registeredDigitalObjects = processDigitalObjects(model, uriProcessed, jsonFactory, idMap);
89
			int registeredActors = processActors(model, uriProcessed, jsonFactory, idMap);
90
			int registeredProjects = processProjects(model, uriProcessed, jsonFactory, idMap);
91

  
92
			int total = registeredActors+registeredDigitalObjects+registeredProjects+registeredServices;
93
			log.debug(String.format("Registered %d resources from record with objIdentifier %s", total, objIdentifier));
94

  
95
			/* Relationships */
96
			int relTotal = 0;
97
			for(String uri : uriProcessed){
98
				relTotal += processRelationships(model, uri, idMap);
99
			}
100
			log.debug(String.format("Registered %d relationships from record with objIdentifier %s", relTotal, objIdentifier));
101
		}
102
	}
103

  
104
	private int processRelationships(final InfModel model, final String uri, final Map<String, ParthenosRegistryResource> idMap)
105
			throws IOException, ResourceRegistryException, ParthenosPublisherException {
106
		Resource subject = model.getResource(uri);
107
		String uuid = idMap.get(uri).getUuid();
108
		JsonFactory jsonFactory = new JsonFactory();
109
		int relCount = 0;
110

  
111
		if(subject.hasProperty(RDF.type, CRMpe.PE1_Service)){
112
			StmtIterator itOfferedBy = subject.listProperties(CRMpe.PP1i_is_currently_offered_by);
113
			while(itOfferedBy.hasNext()){
114
				Resource project = itOfferedBy.nextStatement().getResource();
115
				ParthenosRegistryResource source = idMap.get(uri);
116
				ParthenosRegistryResource projectRes = idMap.get(project.getURI());
117
				String relJson = relWriter.writeRelationship(jsonFactory, "PP1_currently_offers", projectRes.getUuid(), projectRes.getType(), source.getUuid(), source.getType());
118
				this.resourceRegistryPublisher.createIsRelatedTo("PP1_currently_offers", relJson);
119
				relCount++;
120
			}
121
			StmtIterator itProvidedBy = subject.listProperties(CRMpe.PP2_provided_by);
122
			while(itProvidedBy.hasNext()){
123
				Resource provider = itProvidedBy.nextStatement().getResource();
124
				ParthenosRegistryResource prr = idMap.get(uri);
125
				ParthenosRegistryResource providerRes = idMap.get(provider.getURI());
126
				String relJson = relWriter.writeRelationship(jsonFactory, "PP2_provided_by", prr.getUuid(), prr.getType(), providerRes.getUuid(), providerRes.getType());
127
				this.resourceRegistryPublisher.createIsRelatedTo("PP2_provided_by", relJson);
128
				relCount++;
129
				//TODO: IsRelatedTo the contact person of the provider. Interpret '"Follow path of service ‘Provided by’ and switch E39 for E21: E21- >p76->E51""
130
			}
131
			relCount += registerRelationship(CRMpe.PP45_has_competency, subject, idMap, jsonFactory);
132
			relCount += registerRelationship(CRMpe.PP4_hosts_object, subject, idMap, jsonFactory);
133
			relCount += registerRelationship(CRMpe.PP31_uses_curation_plan, subject, idMap, jsonFactory);
134
			relCount += registerRelationship(CRMpe.PP32_curates, subject, idMap, jsonFactory);
135
			relCount += registerRelationship(CRMpe.PP6_hosts_digital_object, subject, idMap, jsonFactory);
136
			relCount += registerRelationship(CRMpe.PP7_hosts_software_object, subject, idMap, jsonFactory);
137
			relCount += registerRelationship(CRMpe.PP8_hosts_dataset, subject, idMap, jsonFactory);
138
			relCount += registerRelationship(CRMpe.PP29_uses_access_protocol, subject, idMap, jsonFactory);
139
			relCount += registerRelationship(CRMpe.PP47_has_protocol_type, subject, idMap, jsonFactory);
140
			relCount += registerRelationship(CRMpe.PP48_uses_protocol_parameter, subject, idMap, jsonFactory);
141
			relCount += registerRelationship(CRMpe.PP46_brokers_access_to, subject, idMap, jsonFactory);
142
			relCount += registerRelationship(CRMpe.PP11_curates_volatile_digital_object, subject, idMap, jsonFactory);
143
			relCount += registerRelationship(CRMpe.PP12_curates_volatile_software, subject, idMap, jsonFactory);
144
			relCount += registerRelationship(CRMpe.PP13_curates_volatile_dataset, subject, idMap, jsonFactory);
145
			relCount += registerRelationship(CRMpe.PP15_delivers_on_request, subject, idMap, jsonFactory);
146
			relCount += registerRelationship(CRMpe.PP46_brokers_access_to, subject, idMap, jsonFactory);
147
		}
148
		if(subject.hasProperty(RDF.type, CRMpe.PE18_Dataset)){
149
			StmtIterator itPartOf = subject.listProperties(CRMpe.PP23i_is_dataset_part_of);
150
			while(itPartOf.hasNext()){
151
				Resource obj = itPartOf.nextStatement().getResource();
152
				ParthenosRegistryResource prr = idMap.get(uri);
153
				ParthenosRegistryResource objres = idMap.get(obj.getURI());
154
				String relJson = relWriter.writeRelationship(jsonFactory, "PP23_has_dataset_part", objres.getUuid(), objres.getType(), prr.getUuid(), prr.getType());
155
				this.resourceRegistryPublisher.createIsRelatedTo("PP23_has_dataset_part", relJson);
156
				relCount++;
157
			}
158
			//TODO: this is in the registry model but I am not sure what's going to happen...
159
			StmtIterator itIsAbout = subject.listProperties(CRM.P129_is_about);
160
			while(itIsAbout.hasNext()){
161
				Resource obj = itIsAbout.nextStatement().getResource();
162
				if(obj.hasProperty(RDF.type, CRM.E55_Type)) {
163
					ParthenosRegistryResource prr = idMap.get(uri);
164
					ParthenosRegistryResource objres = idMap.get(obj.getURI());
165
					String relJson = relWriter.writeRelationship(jsonFactory, "P129_is_about", prr.getUuid(), prr.getType(), objres.getUuid(), objres.getType());
166
					this.resourceRegistryPublisher.createIsRelatedTo("P129_is_about", relJson);
167
					relCount++;
168
				}
169
			}
170
		}
171
		if(subject.hasProperty(RDF.type, CRMpe.PE19_Persistent_Digital_Object)){
172
			relCount += registerRelationship(CRMpe.PP16_has_persistent_digital_object_part, subject, idMap, jsonFactory);
173
		}
174
		if(subject.hasProperty(RDF.type, CRMpe.PE20_Volatile_Digital_Object)){
175
			relCount += registerRelationship(CRMpe.PP17_has_snapshot, subject, idMap, jsonFactory);
176
			relCount += registerRelationship(CRMpe.PP18_has_digital_object_part, subject, idMap, jsonFactory);
177
		}
178
		if(subject.hasProperty(RDF.type, CRMpe.PE21_Persistent_Software)){
179
			relCount += registerRelationship(CRMpe.PP19_has_persistent_software_part, subject, idMap, jsonFactory);
180
		}
181
		if(subject.hasProperty(RDF.type, CRMpe.PE22_Persistent_Dataset)){
182
			relCount += registerRelationship(CRMpe.PP20_has_persistent_dataset_part, subject, idMap, jsonFactory);
183
			relCount += registerRelationship(CRMpe.PP39_is_metadata_for, subject, idMap, jsonFactory);
184
		}
185
		if(subject.hasProperty(RDF.type, CRMpe.PE23_Volatile_Software)){
186
			relCount += registerRelationship(CRMpe.PP21_has_software_part, subject, idMap, jsonFactory);
187
			relCount += registerRelationship(CRMpe.PP22_has_release, subject, idMap, jsonFactory);
188
		}
189
		if(subject.hasProperty(RDF.type, CRMpe.PE24_Volatile_Dataset)){
190
			relCount += registerRelationship(CRMpe.PP23_has_dataset_part, subject, idMap, jsonFactory);
191
			relCount += registerRelationship(CRMpe.PP24_has_dataset_snapshot, subject, idMap, jsonFactory);
192
			relCount += registerRelationship(CRMpe.PP41_is_index_of, subject, idMap, jsonFactory);
193
		}
194
		if(subject.hasProperty(RDF.type, CRMpe.PE26_RI_Project)){
195
			relCount += registerRelationship(CRMpe.PP25_has_maintaining_RI, subject, idMap, jsonFactory);
196
		}
197
		if(subject.hasProperty(RDF.type, CRMpe.PE35_Project)){
198
			relCount += registerRelationship(CRMpe.PP43_supported_project_activity,  subject, idMap, jsonFactory);
199
			relCount += registerRelationship(CRMpe.PP44_has_maintaining_team, subject, idMap, jsonFactory);
200
		}
201
		log.debug(String.format("Registered %d relationships for uri %s, uuid %s", relCount, uri, uuid));
202
		return relCount;
203
	}
204

  
205
	protected int registerRelationship(final Property rel, final Resource subject, final Map<String,ParthenosRegistryResource> idMap, final JsonFactory jsonFactory)
206
			throws IOException, ResourceRegistryException, ParthenosPublisherException {
207
		int relCount = 0;
208
		StmtIterator it = subject.listProperties(rel);
209
		while(it.hasNext()){
210
			Resource obj = it.nextStatement().getResource();
211
			if(obj == null) throw new ParthenosPublisherException(String.format("No target resource available for %s --> %s", subject.getURI(), rel.getURI()));
212
			ParthenosRegistryResource source = idMap.get(subject.getURI());
213
			ParthenosRegistryResource target = idMap.get(obj.getURI());
214
			String relJson = relWriter.writeRelationship(jsonFactory, rel.getLocalName(), source.getUuid(), source.getType(), target.getUuid(), target.getType());
215
			this.resourceRegistryPublisher.createIsRelatedTo(rel.getLocalName(), relJson);
216
			relCount++;
217
		}
218
		return relCount;
219
	}
220

  
221

  
222
	protected InfModel loadBaseModel() {
223
		return ModelFactory.createRDFSModel(baseModel);
224
	}
225

  
226
	protected int processServices(final InfModel model, final Set<String> uriProcessed, final JsonFactory jsonFactory, final Map<String, ParthenosRegistryResource> idMap)
227
			throws IOException, ResourceRegistryException {
228
		log.debug("Processing services");
229
		int count = 0;
230
		int total = 0;
231
		ResIterator iter = model.listResourcesWithProperty(RDF.type, CRMpe.PE1_Service);
232
		while (iter.hasNext()) {
233
			total++;
234
			Resource res = iter.nextResource();
235
			String resourceURI = res.getURI();
236
			if (!uriProcessed.contains(resourceURI)) {
237
				ParthenosRegistryResource prr = processService(res, jsonFactory, idMap);
238
				this.resourceRegistryPublisher.createResource(prr.getType(), prr.getJson());
239
				uriProcessed.add(resourceURI);
240
				idMap.put(resourceURI, prr);
241
				count++;
242
			} else {
243
				log.debug(resourceURI + " already processed, now skipping it");
244
			}
245
		}
246
		log.debug(String.format("Registered %d/%d services", count, total));
247
		return count;
248

  
249
	}
250

  
251
	private ParthenosRegistryResource processService(final Resource res, final JsonFactory jsonFactory, final Map<String, ParthenosRegistryResource> idMap) throws IOException {
252
		log.debug("Processing " + res.getURI());
253

  
254
		StringWriter sw = new StringWriter();
255
		JsonGenerator jg = jsonFactory.createGenerator(sw);
256
		jg.writeStartObject();
257
		String specificType = findSpecificType(res, CRMpe.PE1_Service).getLocalName();
258
		String uuid = writeCommon(res, specificType, jg, idMap);
259
		//******THE FACETS *******//
260
		jg.writeArrayFieldStart("consistsOf");
261
		//list of facets
262
		writeCommonFacets(res, jg);
263
		facetWriter.writeEventFacet(jg);
264
		facetWriter.writeRightsFacet(jg, res);
265
		if (res.hasProperty(CRMpe.PP2_provided_by)) {
266
			//TODO: shouldn't this be a rel to an actor?
267
			Resource provider = res.getPropertyResourceValue(CRMpe.PP2_provided_by);
268
			facetWriter.writeContactReferenceFacet(jg, provider);
269
		}
270
		facetWriter.writeDesignatedAccessPointFacet(jg, res);
271
		jg.writeEndArray();
272
		jg.writeEndObject();
273
		jg.close();
274
		String json = sw.toString();
275
		log.debug(json);
276
		return new ParthenosRegistryResource().setJson(json).setType(specificType).setUuid(uuid);
277
	}
278

  
279
	/**
280
	 * Write the common properties: header, class for all resources
281
	 *
282
	 * @param resource  input Resource.
283
	 * @param className name of the class for the registry
284
	 * @param jg        JsonGenerator to write with.
285
	 * @param idMap     map of the already assigned UUID
286
	 * @return the uuid of the resource
287
	 */
288
	protected String writeCommon(final Resource resource, final String className, final JsonGenerator jg, final Map<String, ParthenosRegistryResource> idMap) throws IOException {
289
		String uuid;
290
		if (idMap.containsKey(resource.getURI())) {
291
			uuid = idMap.get(resource.getURI()).getUuid();
292
		} else {
293
			uuid = UUID.randomUUID().toString();
294
			idMap.put(resource.getURI(), new ParthenosRegistryResource().setUuid(uuid));
295
		}
296
		jg.writeStartObject();
297
		jg.writeObjectFieldStart("header");
298
		jg.writeStringField("uuid", uuid);
299
		jg.writeEndObject();
300
		jg.writeStringField("@class", className);
301
		return uuid;
302
	}
303

  
304
	/**
305
	 * Write the common facets: identifier and info facets
306
	 *
307
	 * @param res
308
	 * @param jg
309
	 */
310
	protected void writeCommonFacets(final Resource res, final JsonGenerator jg) throws IOException {
311
		facetWriter.writeIdentifierFacet(jg, res.getURI());
312
		facetWriter.writeP1Facets(jg, res);
313
		facetWriter.writeInfoFacet(jg, res);
314
	}
315

  
316
	protected int processDigitalObjects(final InfModel model, final Set<String> uriProcessed, final JsonFactory jsonFactory, final Map<String, ParthenosRegistryResource> idMap)
317
			throws IOException, ResourceRegistryException {
318
		log.debug("Processing digital objects (include dataset and software)");
319
		int count = 0;
320
		int total = 0;
321
		ResIterator iterDo = model.listResourcesWithProperty(RDF.type, CRMdig.D1_Digital_Object);
322
		while (iterDo.hasNext()) {
323
			total++;
324
			Resource res = iterDo.nextResource();
325
			String resourceURI = res.getURI();
326
			if (!uriProcessed.contains(resourceURI)) {
327
				ParthenosRegistryResource prr = processDigitalObject(res, jsonFactory, idMap);
328
				this.resourceRegistryPublisher.createResource(prr.getType(), prr.getJson());
329
				uriProcessed.add(resourceURI);
330
				idMap.put(resourceURI, prr);
331
				count++;
332
			} else {
333
				log.debug(resourceURI + " already processed, now skipping it");
334
			}
335
		}
336
		log.debug(String.format("Registered %d/%d digital objects", count, total));
337
		return count;
338
	}
339

  
340
	protected ParthenosRegistryResource processDigitalObject(final Resource res, final JsonFactory jsonFactory, final Map<String, ParthenosRegistryResource> idMap)
341
			throws IOException {
342
		log.debug("Processing " + res.getURI());
343
		String specificType = findSpecificType(res, CRMdig.D1_Digital_Object).getLocalName();
344
		StringWriter sw = new StringWriter();
345
		JsonGenerator jg = jsonFactory.createGenerator(sw);
346
		jg.writeStartObject();
347

  
348
		String uuid = writeCommon(res, specificType, jg, idMap);
349
		//******THE FACETS *******//
350
		jg.writeArrayFieldStart("consistsOf");
351
		//list of facets
352
		writeCommonFacets(res, jg);
353
		facetWriter.writeTemporalCoverageFacet(jg, res);
354
		facetWriter.writeRightsFacet(jg, res);
355

  
356
		jg.writeEndArray();
357
		jg.writeEndObject();
358
		jg.close();
359
		String json = sw.toString();
360
		log.debug(json);
361
		return new ParthenosRegistryResource().setUuid(uuid).setType(specificType).setJson(json);
362
	}
363

  
364
	protected int processActors(final InfModel model, Set<String> uriProcessed, final JsonFactory jsonFactory, final Map<String, ParthenosRegistryResource> idMap)
365
			throws IOException {
366
		log.debug("Processing actors");
367
		int count = 0;
368
		int total = 0;
369
		ResIterator iterActors = model.listResourcesWithProperty(RDF.type, CRM.E39_Actor);
370
		while (iterActors.hasNext()) {
371
			total++;
372
			Resource res = iterActors.nextResource();
373
			String resourceURI = res.getURI();
374
			if (!uriProcessed.contains(resourceURI)) {
375
				ParthenosRegistryResource prr = processActor(res, jsonFactory, idMap);
376
				uriProcessed.add(resourceURI);
377
				idMap.put(resourceURI, prr);
378
				count++;
379

  
380
			} else {
381
				log.debug(resourceURI + " already processed, now skipping it");
382
			}
383
		}
384
		log.debug(String.format("Registered %d/%d actors", count, total));
385
		return count;
386
	}
387

  
388
	protected ParthenosRegistryResource processActor(final Resource r, final JsonFactory jsonFactory, final Map<String, ParthenosRegistryResource> idMap) throws IOException {
389
		log.debug("Processing " + r.getURI());
390
		String specificType = findSpecificType(r, CRM.E39_Actor).getLocalName();
391
		StringWriter sw = new StringWriter();
392
		JsonGenerator jg = jsonFactory.createGenerator(sw);
393
		jg.writeStartObject();
394

  
395
		String uuid = writeCommon(r, specificType, jg, idMap);
396
		//******THE FACETS *******//
397
		jg.writeArrayFieldStart("consistsOf");
398
		//list of facets
399
		writeCommonFacets(r, jg);
400
		facetWriter.writeContactReferenceFacet(jg, r);
401

  
402
		jg.writeEndArray();
403
		jg.writeEndObject();
404
		jg.close();
405
		String json = sw.toString();
406
		log.debug(json);
407
		return new ParthenosRegistryResource().setJson(json).setType(specificType).setUuid(uuid);
408
	}
409

  
410
	protected int processProjects(final InfModel model, Set<String> uriProcessed, final JsonFactory jsonFactory, final Map<String, ParthenosRegistryResource> idMap)
411
			throws IOException, ResourceRegistryException {
412
		log.debug("Processing projects");
413
		int count = 0;
414
		int total = 0;
415
		ResIterator iterProjects = model.listResourcesWithProperty(RDF.type, CRMpe.PE35_Project);
416
		while (iterProjects.hasNext()) {
417
			total++;
418
			Resource res = iterProjects.nextResource();
419
			String resourceURI = res.getURI();
420
			if (!uriProcessed.contains(resourceURI)) {
421
				ParthenosRegistryResource prr = processProject(res, jsonFactory, idMap);
422
				this.resourceRegistryPublisher.createResource(prr.getType(), prr.getJson());
423
				uriProcessed.add(resourceURI);
424
				idMap.put(resourceURI, prr);
425
				count++;
426
			} else {
427
				log.debug(resourceURI + " already processed, now skipping it");
428
			}
429
		}
430
		log.debug(String.format("Registered %d/%d projects", count, total));
431
		return count;
432
	}
433

  
434
	protected ParthenosRegistryResource processProject(final Resource res, final JsonFactory jsonFactory, final Map<String, ParthenosRegistryResource> idMap) throws IOException {
435
		log.debug("Processing " + res.getURI());
436
		String specificType = findSpecificType(res, CRMpe.PE35_Project).getLocalName();
437
		StringWriter sw = new StringWriter();
438
		JsonGenerator jg = jsonFactory.createGenerator(sw);
439
		jg.writeStartObject();
440

  
441
		String uuid = writeCommon(res, specificType, jg, idMap);
442
		jg.writeArrayFieldStart("consistsOf");
443
		writeCommonFacets(res, jg);
444

  
445
		jg.writeEndArray();
446
		jg.writeEndObject();
447
		jg.close();
448
		String json = sw.toString();
449
		log.debug(json);
450
		return new ParthenosRegistryResource().setJson(json).setType(specificType).setUuid(uuid);
451
	}
452

  
453
	/**
454
	 * Finds the most specific type of res.
455
	 *
456
	 * @param res          Resource you want to find the most specific type
457
	 * @param fallbackType Resource representing the type to return if there is no type or if we get AmbiguousSpecificTypeException
458
	 * @return Resource: the most specific type, if any. fallbackType otherwise
459
	 */
460
	protected Resource findSpecificType(final Resource res, final Resource fallbackType) {
461
		Resource specType = fallbackType;
462
		try {
463
			specType = AssemblerHelp.findSpecificType(res, fallbackType);
464
		} catch (AmbiguousSpecificTypeException e) {
465
			log.warn(res.getURI() + ": " + e.getMessage());
466
		}
467
		return specType;
468
	}
469

  
470
	public FacetWriter getFacetWriter() {
471
		return facetWriter;
472
	}
473

  
474
	public void setFacetWriter(final FacetWriter facetWriter) {
475
		this.facetWriter = facetWriter;
476
	}
477
}
modules/dnet-parthenos-publisher/trunk/src/main/java/eu/dnetlib/parthenos/registry/ParthenosRegistryResource.java
1
package eu.dnetlib.parthenos.registry;
2

  
3
/**
4
 * Created by Alessia Bardi on 06/10/2017.
5
 *
6
 * @author Alessia Bardi
7
 */
8
public class ParthenosRegistryResource {
9

  
10
	private String uuid;
11
	private String type;
12
	private String json;
13

  
14
	public String getUuid() {
15
		return uuid;
16
	}
17

  
18
	public ParthenosRegistryResource setUuid(final String uuid) {
19
		this.uuid = uuid;
20
		return this;
21
	}
22

  
23
	public String getType() {
24
		return type;
25
	}
26

  
27
	public ParthenosRegistryResource setType(final String type) {
28
		this.type = type;
29
		return this;
30
	}
31

  
32
	public String getJson() {
33
		return json;
34
	}
35

  
36
	public ParthenosRegistryResource setJson(final String json) {
37
		this.json = json;
38
		return this;
39
	}
40
}
modules/dnet-parthenos-publisher/trunk/src/main/java/eu/dnetlib/parthenos/registry/RegistryClient.java
29 29
	private static final String DRI_NAMESPACE_URI = "http://www.driver-repository.eu/namespace/dri";
30 30
	private static final String RDF_NAMESPACE_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
31 31

  
32
	private GCubeResourceGenerator resourceGenerator;
32
	private GCubeResourceRegistrator gCubeResourceRegistrator;
33 33
	private SaxonHelper saxonHelper;
34 34
	private XPathSelector xpathSelectorObjIdentifier;
35
	private XPathSelector xpathSelectorCollectionDate;
36
	private XPathSelector xpathSelectorTransformationDate;
37
	private XPathSelector xpathSelectorDatasourceName;
38
	private XPathSelector xpathSelectorDatasourceApi;
35
	//	private XPathSelector xpathSelectorCollectionDate;
36
	//	private XPathSelector xpathSelectorTransformationDate;
37
	//	private XPathSelector xpathSelectorDatasourceName;
38
	//	private XPathSelector xpathSelectorDatasourceApi;
39 39
	private XPathSelector xpathSelectorRDF;
40 40

  
41 41
	private ResourceRegistryPublisher resourceRegistryPublisher;
......
43 43
	private String registryBaseURL;
44 44
	private String defaultBaseURI;
45 45

  
46
	protected RegistryClient(final String registryBaseURL, final SaxonHelper saxonHelper, final String defaultBaseURI, final GCubeResourceGenerator resourceGenerator)
46
	protected RegistryClient(final String registryBaseURL,
47
			final SaxonHelper saxonHelper,
48
			final String defaultBaseURI,
49
			final GCubeResourceRegistrator gCubeResourceRegistrator)
47 50
			throws ParthenosPublisherException {
48 51
		this.registryBaseURL = registryBaseURL;
49 52
		this.saxonHelper = saxonHelper;
50 53
		this.defaultBaseURI = defaultBaseURI;
51 54
		this.resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
52 55
		this.resourceRegistryClient = ResourceRegistryClientFactory.create();
53
		this.resourceGenerator = resourceGenerator;
56
		this.gCubeResourceRegistrator = gCubeResourceRegistrator;
54 57
		try {
55 58
			prepareXpathSelectors();
56
		}catch(SaxonApiException e){
59
		} catch (SaxonApiException e) {
57 60
			throw new ParthenosPublisherException(e);
58 61
		}
59 62
	}
60 63

  
61
	public int register(final String record) throws ParthenosPublisherException{
64
	public int unregister(final String datasourceInterface) {
65
		return gCubeResourceRegistrator.unregister(datasourceInterface);
66
	}
67

  
68
	public void register(final String record) throws ParthenosPublisherException {
62 69
		try {
63 70
			if (StringUtils.isBlank(record)) {
64 71
				log.warn("Got empty record");
65
				return 0;
66 72
			}
67 73
			String objIdentifier = extractFromRecord(record, xpathSelectorObjIdentifier);
68 74
			if (StringUtils.isBlank(objIdentifier)) {
69 75
				log.warn("Got record with no objIdentifier -- skipping");
70
				return 0;
71 76
			}
72
			//TODO: get the json strings to register
73
			Iterable<String> resources = resourceGenerator.getGCubeER(record);
74
			for(String res : resources){
75
				//resourceRegistryPublisher.
76
			}
77
			return 0;
78
		}catch(Throwable e){
77
			String recordRDF = extractFromRecord(record, xpathSelectorRDF);
78
			gCubeResourceRegistrator.register(recordRDF, objIdentifier);
79
		} catch (Throwable e) {
79 80
			log.error(e.getMessage());
80 81
			throw new ParthenosPublisherException(e);
81 82
		}
82 83
	}
83 84

  
84

  
85 85
	private void prepareXpathSelectors() throws SaxonApiException {
86 86
		Map<String, String> namespaces = Maps.newHashMap();
87 87
		namespaces.put("oai", OAI_NAMESPACE_URI);
88 88
		namespaces.put("dri", DRI_NAMESPACE_URI);
89 89
		namespaces.put("rdf", RDF_NAMESPACE_URI);
90 90
		xpathSelectorObjIdentifier = this.saxonHelper.help().prepareXPathSelector("//oai:header/dri:objIdentifier/text()", namespaces);
91
		xpathSelectorCollectionDate = this.saxonHelper.help().prepareXPathSelector("//oai:header/dri:dateOfCollection/text()", namespaces);
92
		xpathSelectorTransformationDate = this.saxonHelper.help().prepareXPathSelector("//oai:header/dri:dateOfTransformation/text()", namespaces);
93
		xpathSelectorDatasourceName = this.saxonHelper.help().prepareXPathSelector("//oai:header/dri:datasourcename/text()", namespaces);
94
		xpathSelectorDatasourceApi = this.saxonHelper.help().prepareXPathSelector("//oai:header/dri:datasourceapi/text()", namespaces);
91
		//xpathSelectorCollectionDate = this.saxonHelper.help().prepareXPathSelector("//oai:header/dri:dateOfCollection/text()", namespaces);
92
		//xpathSelectorTransformationDate = this.saxonHelper.help().prepareXPathSelector("//oai:header/dri:dateOfTransformation/text()", namespaces);
93
		//xpathSelectorDatasourceName = this.saxonHelper.help().prepareXPathSelector("//oai:header/dri:datasourcename/text()", namespaces);
94
		//xpathSelectorDatasourceApi = this.saxonHelper.help().prepareXPathSelector("//oai:header/dri:datasourceapi/text()", namespaces);
95 95
		xpathSelectorRDF = this.saxonHelper.help().prepareXPathSelector("//oai:metadata/rdf:RDF", namespaces);
96 96
	}
97 97

  
......
103 103
		}
104 104
	}
105 105

  
106

  
107 106
	public void setSaxonHelper(final SaxonHelper saxonHelper) {
108 107
		this.saxonHelper = saxonHelper;
109 108
	}
......
128 127
		return defaultBaseURI;
129 128
	}
130 129

  
131
	public GCubeResourceGenerator getResourceGenerator() {
132
		return resourceGenerator;
130
	public GCubeResourceRegistrator getgCubeResourceRegistrator() {
131
		return gCubeResourceRegistrator;
133 132
	}
134 133

  
135
	public void setResourceGenerator(final GCubeResourceGenerator resourceGenerator) {
136
		this.resourceGenerator = resourceGenerator;
134
	public void setgCubeResourceRegistrator(final GCubeResourceRegistrator gCubeResourceRegistrator) {
135
		this.gCubeResourceRegistrator = gCubeResourceRegistrator;
137 136
	}
138 137

  
139 138
	public ResourceRegistryPublisher getResourceRegistryPublisher() {
modules/dnet-parthenos-publisher/trunk/src/main/java/eu/dnetlib/parthenos/publisher/ParthenosPublisherHelper.java
64 64
		virtuosoClient.feed(record);
65 65
	}
66 66

  
67
	private long unpublishVirtuoso(final String datasourceInterface) throws ParthenosPublisherException {
68
		log.debug("Unublishing from virtuoso "+datasourceInterface);
69
		VirtuosoClient virtuosoClient = this.virtuosoClientFactory.getVirtuosoClient();
70
		return virtuosoClient.drop(datasourceInterface);
71
	}
72

  
67 73
	private void publishJRR(final String record) throws ParthenosPublisherException {
68 74
		log.debug("Publishing on Registry");
69 75
		RegistryClient registryClient = this.registryClientFactory.getRegistryClient();
70 76
		registryClient.register(record);
71 77
	}
72 78

  
73
	private long unpublishVirtuoso(final String datasourceInterface) throws ParthenosPublisherException {
74
		log.debug("Unublishing from virtuoso "+datasourceInterface);
75
		VirtuosoClient virtuosoClient = this.virtuosoClientFactory.getVirtuosoClient();
76
		return virtuosoClient.drop(datasourceInterface);
79
	private int unpublishJRR(final String datasourceInterface) throws ParthenosPublisherException {
80
		log.debug("Unublishing from registry "+datasourceInterface);
81
		RegistryClient registryClient = this.registryClientFactory.getRegistryClient();
82
		return registryClient.unregister(datasourceInterface);
77 83
	}
78 84

  
79 85

  
modules/dnet-parthenos-publisher/trunk/pom.xml
159 159
		<dependency>
160 160
			<groupId>org.gcube.information-system</groupId>
161 161
			<artifactId>parthenos-entities</artifactId>
162
			<version>LATEST</version>
162
			<version>0.0.1-SNAPSHOT</version>
163 163
		</dependency>
164 164
		<dependency>
165 165
			<groupId>org.gcube.information-system</groupId>
166 166
			<artifactId>resource-registry-publisher</artifactId>
167
			<version>LATEST</version>
167
			<version>1.5.0-SNAPSHOT</version>
168 168
		</dependency>
169 169
		<dependency>
170 170
			<groupId>org.gcube.information-system</groupId>
171 171
			<artifactId>resource-registry-client</artifactId>
172
			<version>LATEST</version>
172
			<version>1.5.0-SNAPSHOT</version>
173 173
		</dependency>
174 174
		<dependency>
175 175
			<groupId>org.gcube.common</groupId>
176 176
			<artifactId>authorization-client</artifactId>
177 177
			<version>LATEST</version>
178 178
		</dependency>
179
		<dependency>
180
			<groupId>org.gcube.information-system</groupId>
181
			<artifactId>resource-registry-api</artifactId>
182
			<version>RELEASE</version>
183
		</dependency>
179 184
	</dependencies>
180 185

  
181 186
	<build>

Also available in: Unified diff