Project

General

Profile

1
package eu.dnetlib.parthenos.registry;
2

    
3
import java.io.BufferedOutputStream;
4
import java.io.ByteArrayOutputStream;
5
import java.io.IOException;
6
import java.util.List;
7
import java.util.Map;
8
import javax.annotation.PostConstruct;
9

    
10
import com.fasterxml.jackson.core.JsonEncoding;
11
import com.fasterxml.jackson.core.JsonFactory;
12
import com.fasterxml.jackson.core.JsonGenerator;
13
import com.google.common.collect.Lists;
14
import eu.dnetlib.parthenos.CRM;
15
import eu.dnetlib.parthenos.CRMdig;
16
import eu.dnetlib.parthenos.CRMpe;
17
import eu.dnetlib.parthenos.jrr.ParthenosRegistryRel;
18
import eu.dnetlib.parthenos.jrr.ParthenosRegistryResource;
19
import eu.dnetlib.parthenos.publisher.ParthenosPublisherException;
20
import eu.dnetlib.parthenos.rdf.ResourceReader;
21
import org.apache.commons.lang3.StringUtils;
22
import org.apache.commons.logging.Log;
23
import org.apache.commons.logging.LogFactory;
24
import org.apache.jena.ext.com.google.common.collect.Maps;
25
import org.apache.jena.rdf.model.Property;
26
import org.apache.jena.rdf.model.Resource;
27
import org.apache.jena.rdf.model.StmtIterator;
28
import org.apache.jena.vocabulary.RDF;
29
import org.gcube.common.authorization.client.Constants;
30
import org.gcube.common.authorization.library.AuthorizationEntry;
31
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
32
import org.gcube.common.scope.api.ScopeProvider;
33
import org.gcube.informationsystem.model.entity.facet.IdentifierFacet;
34
import org.gcube.informationsystem.model.relation.IsIdentifiedBy;
35
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
36
import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException;
37
import org.gcube.informationsystem.resourceregistry.client.ParthenosRegistryClientSetter;
38
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
39
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
40
import org.gcube.informationsystem.resourceregistry.publisher.ParthenosRegistryPublisherSetter;
41
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher;
42
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory;
43
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.beans.factory.annotation.Value;
45
import org.springframework.stereotype.Component;
46

    
47
/**
48
 * Created by Alessia Bardi on 26/09/2017.
49
 *
50
 * TODO: when the mappings have a definitive URI generation policy then we need to be able to "upsert" (Luca will prepare new method for me).
51
 * When generating relationships, it could be the case that the linked entity was generated from another file, so we have to ask
52
 * the registry if it already exists a resource with a given URI. We cannot do it now because different entities with the same fake UUID are
53
 * generated by the mappings, now. See also https://support.d4science.org/issues/9820#change-56673
54
 *
55
 * @author Alessia Bardi
56
 */
57
@Component
58
public class GCubeResourceRegistrator {
59

    
60
	private static final Log log = LogFactory.getLog(GCubeResourceRegistrator.class);
61

    
62
	@Value("${gcube.registry.baseurl}")
63
	private String registryBaseURL;
64
	@Value("${gcube.registry.application.token}")
65
	private String applicationToken;
66

    
67
	@Autowired
68
	private FacetWriter facetWriter;
69
	@Autowired
70
	private RelWriter relWriter;
71
	@Autowired
72
	private ResourceReader resourceReader;
73

    
74
	private ResourceRegistryPublisher resourceRegistryPublisher;
75
	private ResourceRegistryClient resourceRegistryClient;
76

    
77

    
78
	@PostConstruct
79
	public void init() throws Exception {
80
		ParthenosRegistryClientSetter.forceToURL(getRegistryBaseURL());
81
		ParthenosRegistryPublisherSetter.forceToURL(getRegistryBaseURL());
82
		log.info("Registry URL forced to " + getRegistryBaseURL());
83
		if (StringUtils.isNotBlank(getApplicationToken())) {
84
			GCubeResourceRegistrator.setContext(getApplicationToken());
85
			log.info("GCube Context set");
86
		} else {
87
			log.fatal("GCube context cannot be configured without a value in gcube.registry.application.token");
88
		}
89
		this.resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
90
		this.resourceRegistryClient = ResourceRegistryClientFactory.create();
91
	}
92

    
93
	protected static String getCurrentScope(String token) throws Exception {
94
		AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
95
		String context = authorizationEntry.getContext();
96
		log.info("Context of token " + token + " is: " + context);
97
		return context;
98
	}
99

    
100
	protected static void setContext(String token) throws Exception {
101
		SecurityTokenProvider.instance.set(token);
102
		ScopeProvider.instance.set(getCurrentScope(token));
103
	}
104

    
105
	public int unregister(final String datasourceApi) {
106
		//TODO: implement me
107
		return -1;
108
	}
109

    
110

    
111
	public void register(final Resource rdfResource, final String uuid, final Resource type) throws IOException, ResourceRegistryException, ParthenosPublisherException {
112
		String resURI = rdfResource.getURI();
113
		log.debug("Processing resource " + resURI + " for registration on JRR");
114
		JsonFactory jsonFactory = new JsonFactory();
115
		ParthenosRegistryResource prr = null;
116
		List<ParthenosRegistryRel> rels = Lists.newArrayList();
117
		switch(type.getLocalName()){
118
		case "E7_Activity":
119
			if(rdfResource.hasProperty(RDF.type, CRMpe.PE1_Service)) {
120
				prr = processService(rdfResource, jsonFactory, uuid);
121
			}
122
			else{
123
				if(rdfResource.hasProperty(RDF.type, CRMpe.PE35_Project)){
124
					prr = processProject(rdfResource, jsonFactory, uuid);
125
				}
126
			}
127
			break;
128
		case "E39_Actor":
129
			prr = processActor(rdfResource, jsonFactory, uuid);
130
			break;
131
		case "E70_Thing":
132
			if(rdfResource.hasProperty(RDF.type, CRMdig.D1_Digital_Object)) {
133
				prr = processDigitalObject(rdfResource, jsonFactory, uuid);
134
			}
135
			//TODO: include PE32_Curated_Thing and E19_PhysicalObject?
136
			break;
137
		case "E55_Type":
138
			if(rdfResource.hasProperty(RDF.type, CRMpe.PE36_Competency_Type)) {
139
				prr = processBasicResource(rdfResource, CRMpe.PE36_Competency_Type, jsonFactory, uuid);
140
			}
141
			else{
142
				if(rdfResource.hasProperty(RDF.type, CRMpe.PE37_Protocol_Type))
143
					prr = processBasicResource(rdfResource, CRMpe.PE37_Protocol_Type, jsonFactory, uuid);
144
			}
145
			//TODO: need other E55_Types?
146
			break;
147
		case "E29_Design_or_Procedure":
148
			prr = processBasicResource(rdfResource, CRM.E29_Design_or_Procedure, jsonFactory, uuid);
149
			break;
150
		default:
151
			throw new IllegalArgumentException(String.format("Type "+type.getLocalName()+" not supported"));
152
		}
153
		if(prr != null) {
154
			this.resourceRegistryPublisher.createResource(prr.getRegistryType(), prr.getJson());
155
			rels = getResourceRels(rdfResource, prr);
156
			int registeredRels = 0;
157
			for(ParthenosRegistryRel r : rels){
158
				//TODO: handle exception
159
				String res = this.resourceRegistryPublisher.createIsRelatedTo(r.getRelName(), r.getJson());
160
				//TODO: handle negative response
161
				if(StringUtils.isNotBlank(res)) registeredRels++;
162
			}
163
			log.debug(String.format("Registered %d/%d relationships for uri %s, uuid %s", registeredRels, rels.size(), resURI, uuid));
164
		}
165
		else{
166
			log.warn("Could not register on registry: "+resURI);
167
		}
168

    
169
	}
170

    
171

    
172
	protected ParthenosRegistryResource processService(final Resource res, final JsonFactory jsonFactory, final String uuid) throws IOException {
173
		//ensure we are not registering generic PE1_Service because the class is abstract and the registry complaints.
174
		//We only want specific types of services.
175
		if (res.hasProperty(RDF.type, CRMpe.PE8_E_Service)
176
				|| res.hasProperty(RDF.type, CRMpe.PE2_Hosting_Service)
177
				|| res.hasProperty(RDF.type, CRMpe.PE3_Curating_Service)) {
178
			final ByteArrayOutputStream out = new ByteArrayOutputStream();
179
			BufferedOutputStream bos = new BufferedOutputStream(out);
180
			JsonGenerator jg = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
181
			jg.writeStartObject();
182
			//it should never happen to get PE1_Service as a specific type
183
			String specificType = resourceReader.findSpecificType(res, CRMpe.PE1_Service).getLocalName();
184
			writeCommon(res, specificType, jg, uuid);
185
			//******THE FACETS *******//
186
			jg.writeArrayFieldStart("consistsOf");
187
			//list of facets
188
			writeCommonFacets(res, jg);
189
			facetWriter.writeEventFacet(jg);
190
			facetWriter.writeRightsFacet(jg, res);
191
			if (res.hasProperty(CRMpe.PP2_provided_by)) {
192
				//TODO: shouldn't this be a rel to an actor?
193
				Resource provider = res.getPropertyResourceValue(CRMpe.PP2_provided_by);
194
				facetWriter.writeContactReferenceFacet(jg, provider);
195
			}
196
			facetWriter.writeDesignatedAccessPointFacet(jg, res);
197
			jg.writeEndArray();
198
			jg.writeEndObject();
199
			jg.close();
200
			String json = out.toString("UTF-8");
201
			log.debug(json);
202
			return new ParthenosRegistryResource().setJson(json).setType(specificType).setUuid(uuid);
203

    
204
		}
205
		else{
206
			log.warn(String.format("Skipping resource: %s It is not an instance of a specific service"));
207
			return null;
208
		}
209
	}
210

    
211
	protected ParthenosRegistryResource processProject(final Resource res, final JsonFactory jsonFactory, final String uuid) throws IOException {
212
		String specificType = resourceReader.findSpecificType(res, CRMpe.PE35_Project).getLocalName();
213
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
214
		BufferedOutputStream bos = new BufferedOutputStream(out);
215
		JsonGenerator jg = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
216
		jg.writeStartObject();
217

    
218
		writeCommon(res, specificType, jg, uuid);
219
		jg.writeArrayFieldStart("consistsOf");
220
		writeCommonFacets(res, jg);
221

    
222
		jg.writeEndArray();
223
		jg.writeEndObject();
224
		jg.close();
225
		String json = out.toString("UTF-8");
226
		log.debug(json);
227
		return new ParthenosRegistryResource().setJson(json).setType(specificType).setUuid(uuid);
228
	}
229

    
230
	protected ParthenosRegistryResource processActor(final Resource res, final JsonFactory jsonFactory, final String uuid) throws IOException {
231
		String specificType = resourceReader.findSpecificType(res, CRM.E39_Actor).getLocalName();
232
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
233
		BufferedOutputStream bos = new BufferedOutputStream(out);
234
		JsonGenerator jg = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
235
		jg.writeStartObject();
236

    
237
		writeCommon(res, specificType, jg, uuid);
238
		//******THE FACETS *******//
239
		jg.writeArrayFieldStart("consistsOf");
240
		//list of facets
241
		writeCommonFacets(res, jg);
242
		facetWriter.writeContactReferenceFacet(jg, res);
243

    
244
		jg.writeEndArray();
245
		jg.writeEndObject();
246
		jg.close();
247
		String json = out.toString("UTF-8");
248
		log.debug(json);
249
		return new ParthenosRegistryResource().setJson(json).setType(specificType).setUuid(uuid);
250
	}
251

    
252
	protected ParthenosRegistryResource processDigitalObject(final Resource res, final JsonFactory jsonFactory, final String uuid) throws IOException {
253
		String specificType = resourceReader.findSpecificType(res, CRMdig.D1_Digital_Object).getLocalName();
254
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
255
		BufferedOutputStream bos = new BufferedOutputStream(out);
256
		JsonGenerator jg = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
257
		jg.writeStartObject();
258

    
259
		writeCommon(res, specificType, jg, uuid);
260
		//******THE FACETS *******//
261
		jg.writeArrayFieldStart("consistsOf");
262
		//list of facets
263
		writeCommonFacets(res, jg);
264
		facetWriter.writeTemporalCoverageFacet(jg, res);
265
		facetWriter.writeRightsFacet(jg, res);
266

    
267
		jg.writeEndArray();
268
		jg.writeEndObject();
269
		jg.close();
270
		String json = out.toString("UTF-8");
271
		log.debug(json);
272
		return new ParthenosRegistryResource().setUuid(uuid).setType(specificType).setJson(json);
273
	}
274

    
275
	protected ParthenosRegistryResource processBasicResource(final Resource res, final Resource basicType, final JsonFactory jsonFactory, final String uuid) throws IOException {
276
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
277
		BufferedOutputStream bos = new BufferedOutputStream(out);
278
		JsonGenerator jg = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
279
		jg.writeStartObject();
280
		String specificType = resourceReader.findSpecificType(res, basicType).getLocalName();
281
		writeCommon(res, specificType, jg, uuid);
282
		//******THE FACETS *******//
283
		jg.writeArrayFieldStart("consistsOf");
284
		writeCommonFacets(res, jg);
285
		jg.writeEndArray();
286
		jg.writeEndObject();
287
		jg.close();
288
		String json = out.toString("UTF-8");
289
		log.debug(json);
290
		return new ParthenosRegistryResource().setJson(json).setType(specificType).setUuid(uuid);
291
	}
292

    
293
	protected List<ParthenosRegistryRel> getResourceRels(final Resource resource, final ParthenosRegistryResource prr) throws IOException, ResourceRegistryException, InvalidQueryException, ParthenosPublisherException {
294
		JsonFactory jsonFactory = new JsonFactory();
295
		List<ParthenosRegistryRel> jsonRels = Lists.newArrayList();
296

    
297
		if(resource.hasProperty(RDF.type, CRMpe.PE1_Service)) {
298
			jsonRels.addAll(getRels(resource, prr, CRM.P129_is_about, jsonFactory));
299
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP2_provided_by, jsonFactory));
300
			//TODO: IsRelatedTo the contact person of the provider. Interpret '"Follow path of service ‘Provided by’ and switch E39 for E21: E21- >p76->E51"
301
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP45_has_competency, jsonFactory));
302
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP4_hosts_object, jsonFactory));
303
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP31_uses_curation_plan, jsonFactory));
304
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP32_curates, jsonFactory));
305
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP6_hosts_digital_object, jsonFactory));
306
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP7_hosts_software_object, jsonFactory));
307
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP8_hosts_dataset, jsonFactory));
308
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP29_uses_access_protocol, jsonFactory));
309
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP47_has_protocol_type, jsonFactory));
310
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP46_brokers_access_to, jsonFactory));
311
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP11_curates_volatile_digital_object, jsonFactory));
312
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP12_curates_volatile_software, jsonFactory));
313
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP13_curates_volatile_dataset, jsonFactory));
314
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP15_delivers_on_request, jsonFactory));
315
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP45_has_competency, jsonFactory));
316
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP1i_is_currently_offered_by, CRMpe.PP1_currently_offers, jsonFactory));
317
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP46i_has_access_brokered_by, CRMpe.PP46_brokers_access_to, jsonFactory));
318
		}
319
		if(resource.hasProperty(RDF.type, CRMdig.D1_Digital_Object)){
320
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP6i_is_digital_object_hosted_by, CRMpe.PP6_hosts_digital_object, jsonFactory));
321
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP18i_is_digital_object_part_of, CRMpe.PP18_has_digital_object_part, jsonFactory));
322
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP39i_has_metadata, CRMpe.PP39_is_metadata_for, jsonFactory));
323
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP41i_is_indexed_by, CRMpe.PP41_is_index_of, jsonFactory));
324
		}
325
		if(resource.hasProperty(RDF.type, CRMpe.PE18_Dataset)) {
326
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP23i_is_dataset_part_of, CRMpe.PP23_has_dataset_part, jsonFactory));
327
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP8i_is_dataset_hosted_by, CRMpe.PP8_hosts_dataset, jsonFactory));
328
		}
329
		if(resource.hasProperty(RDF.type, CRMdig.D14_Software)) {
330
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP14i_is_run_by, CRMpe.PP14_runs_on_request, jsonFactory));
331
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP15i_is_delivered_by, CRMpe.PP15_delivers_on_request, jsonFactory));
332
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP21i_is_software_part_of, CRMpe.PP21_has_software_part, jsonFactory));
333
			//TODO: is this correct? Service --> uses access protocol --> Software ?
334
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP29i_is_access_protocol_used_by, CRMpe.PP29_uses_access_protocol, jsonFactory));
335
		}
336
		if(resource.hasProperty(RDF.type, CRMpe.PE19_Persistent_Digital_Object)) {
337
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP16_has_persistent_digital_object_part, jsonFactory));
338
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP16i_is_persistent_digital_object_part_of, CRMpe.PP16_has_persistent_digital_object_part, jsonFactory));
339
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP17i_is_snapshot_of, CRMpe.PP17_has_snapshot, jsonFactory));
340
		}
341
		if(resource.hasProperty(RDF.type, CRMpe.PE20_Volatile_Digital_Object)) {
342
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP17_has_snapshot, jsonFactory));
343
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP18_has_digital_object_part, jsonFactory));
344
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP11i_is_volatile_digital_object_curated_by, CRMpe.PP11_curates_volatile_digital_object, jsonFactory));
345
		}
346
		if(resource.hasProperty(RDF.type, CRMpe.PE21_Persistent_Software)) {
347
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP19_has_persistent_software_part, jsonFactory));
348
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP19i_is_persistent_software_part_of, CRMpe.PP19_has_persistent_software_part, jsonFactory));
349
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP22i_is_release_of, CRMpe.PP22_has_release, jsonFactory));
350
		}
351
		if(resource.hasProperty(RDF.type, CRMpe.PE22_Persistent_Dataset)) {
352
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP20_has_persistent_dataset_part, jsonFactory));
353
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP39_is_metadata_for, jsonFactory));
354
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP20i_is_persistent_dataset_part_of, CRMpe.PP20_has_persistent_dataset_part, jsonFactory));
355
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP24i_is_dataset_snapshot_of, CRMpe.PP24_has_dataset_snapshot, jsonFactory));
356
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP40i_is_deprecated_by, CRMpe.PP40_created_successor_of, jsonFactory));
357
		}
358
		if(resource.hasProperty(RDF.type, CRMpe.PE23_Volatile_Software)) {
359
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP21_has_software_part, jsonFactory));
360
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP22_has_release, jsonFactory));
361
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP12i_is_volatile_software_curated_by, CRMpe.PP12_curates_volatile_software, jsonFactory));
362
		}
363
		if(resource.hasProperty(RDF.type, CRMpe.PE24_Volatile_Dataset)) {
364
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP23_has_dataset_part, jsonFactory));
365
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP24_has_dataset_snapshot, jsonFactory));
366
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP41_is_index_of, jsonFactory));
367
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP13i_is_volatile_dataset_curated_by, CRMpe.PP13_curates_volatile_dataset, jsonFactory));
368
		}
369
		if(resource.hasProperty(RDF.type, CRMpe.PE26_RI_Project)) {
370
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP25_has_maintaining_RI, jsonFactory));
371
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP1_currently_offers, jsonFactory));
372
		}
373
		if(resource.hasProperty(RDF.type, CRMpe.PE35_Project)) {
374
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP43_supported_project_activity, jsonFactory));
375
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP44_has_maintaining_team, jsonFactory));
376
		}
377
		if(resource.hasProperty(RDF.type, CRM.E39_Actor)) {
378
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP2i_provides, CRMpe.PP2_provided_by, jsonFactory));
379
		}
380
		if(resource.hasProperty(RDF.type, CRMpe.PE34_Team)) {
381
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP44i_is_maintaining_team_of, CRMpe.PP44_has_maintaining_team, jsonFactory));
382
		}
383
		if(resource.hasProperty(RDF.type, CRM.E70_Thing)) {
384
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP4i_is_object_hosted_by, CRMpe.PP4_hosts_object, jsonFactory));
385
		}
386
		if(resource.hasProperty(RDF.type, CRMpe.PE25_RI_Consortium)) {
387
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP25i_is_maintaining_RI_of, CRMpe.PP25_has_maintaining_RI, jsonFactory));
388
		}
389
		if(resource.hasProperty(RDF.type, CRMpe.PE28_Curation_Plan)) {
390
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP31i_is_curation_plan_used_by, CRMpe.PP31_uses_curation_plan, jsonFactory));
391
		}
392
		if(resource.hasProperty(RDF.type, CRMpe.PE32_Curated_Thing)) {
393
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP32i_is_curated_by, CRMpe.PP32_curates, jsonFactory));
394
		}
395
		if(resource.hasProperty(RDF.type, CRM.E65_Creation)) {
396
			jsonRels.addAll(getRels(resource, prr, CRMpe.PP40_created_successor_of, jsonFactory));
397
		}
398
		if(resource.hasProperty(RDF.type, CRM.E7_Activity)) {
399
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP43i_is_project_activity_supported_by, CRMpe.PP43_supported_project_activity, jsonFactory));
400
		}
401
		if(resource.hasProperty(RDF.type, CRMpe.PE36_Competency_Type)) {
402
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP45i_is_competency_of, CRMpe.PP45_has_competency, jsonFactory));
403
		}
404
		if(resource.hasProperty(RDF.type, CRMpe.PE37_Protocol_Type)) {
405
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP47i_is_protocol_type_of, CRMpe.PP47_has_protocol_type, jsonFactory));
406
		}
407
		if(resource.hasProperty(RDF.type, CRMpe.PE38_Schema)) {
408
			jsonRels.addAll(getInverseRels(resource, prr, CRMpe.PP47i_is_protocol_type_of, CRMpe.PP47_has_protocol_type, jsonFactory));
409
		}
410
		return jsonRels;
411
	}
412

    
413
	protected List<ParthenosRegistryRel> getRels(final Resource subject, final ParthenosRegistryResource subjectPrr, final Property rel, final JsonFactory jsonFactory) throws IOException, InvalidQueryException, ParthenosPublisherException, ResourceRegistryException {
414
		List<ParthenosRegistryRel> rels = Lists.newArrayList();
415
		StmtIterator it = subject.listProperties(rel);
416
		while (it.hasNext()) {
417
			Resource obj = it.nextStatement().getResource();
418
			if (obj == null) throw new ParthenosPublisherException(String.format("No target resource available for %s --> %s", subject.getURI(), rel.getURI()));
419
			ParthenosRegistryResource target = findInRegistry(obj.getURI());
420
			if(target == null){
421
				log.debug(String.format("Rel: %s - %s - %s: target is not yet registered", subject.getURI(), rel.getLocalName(), obj.getURI()));
422
			}
423
			else{
424
				rels.add(getRel(subjectPrr, rel, target, jsonFactory));
425
			}
426
		}
427
		return rels;
428
	}
429

    
430
	protected ParthenosRegistryRel getRel(final ParthenosRegistryResource subjectPrr, final Property rel, final ParthenosRegistryResource targetPrr, final JsonFactory jsonFactory ) throws IOException {
431
		String relJson = relWriter.writeRelationship(jsonFactory, rel.getLocalName(), subjectPrr.getUuid(), subjectPrr.getRegistryType(), targetPrr.getUuid(), targetPrr.getRegistryType());
432
		return new ParthenosRegistryRel().json(relJson).relName(rel.getLocalName());
433
	}
434

    
435
	protected List<ParthenosRegistryRel> getInverseRels(final Resource resource, final ParthenosRegistryResource resourcePRR, final Property rel, final Property inverseRel, final JsonFactory jsonFactory) throws IOException, InvalidQueryException, ResourceRegistryException {
436
		List<ParthenosRegistryRel> rels = Lists.newArrayList();
437
		StmtIterator it = resource.listProperties(rel);
438
		while (it.hasNext()) {
439
			Resource obj = it.nextStatement().getResource();
440
			String objURI = obj.getURI();
441
			ParthenosRegistryResource registryRes = findInRegistry(objURI);
442
			if (registryRes != null) {
443
				rels.add(getRel(registryRes, inverseRel, resourcePRR, jsonFactory));
444
			}
445
		}
446
		return rels;
447
	}
448

    
449
	/**
450
	 * Write the common properties: header, class for all resources
451
	 *
452
	 * @param resource  input Resource.
453
	 * @param className name of the class for the registry
454
	 * @param jg        JsonGenerator to write with.
455
	 * @param uuid      uuid of the resource
456
	 */
457
	protected void writeCommon(final Resource resource, final String className, final JsonGenerator jg, final String uuid)
458
			throws IOException {
459
		jg.writeObjectFieldStart("header");
460
		jg.writeStringField("uuid", uuid);
461
		jg.writeEndObject();
462
		jg.writeStringField("@class", StringUtils.remove(className, '-'));
463
	}
464

    
465
	/**
466
	 * Write the common facets: identifier and info facets
467
	 *
468
	 * @param res
469
	 * @param jg
470
	 */
471
	protected void writeCommonFacets(final Resource res, final JsonGenerator jg) throws IOException {
472
		facetWriter.writeIdentifierFacet(jg, res.getURI());
473
		facetWriter.writeP1Facets(jg, res);
474
		facetWriter.writeInfoFacet(jg, res);
475
	}
476

    
477
	public ParthenosRegistryResource findInRegistry(final String uri) throws ResourceRegistryException {
478
		Map<String, Object> queryMap = Maps.newHashMap();
479
		queryMap.put("value", uri);
480
		List<org.gcube.informationsystem.model.entity.Resource> resources = resourceRegistryClient.getFilteredResources(
481
				org.gcube.informationsystem.model.entity.Resource.class, IsIdentifiedBy.class, IdentifierFacet.class, true, queryMap);
482
		if(resources.isEmpty()) return null;
483
		org.gcube.informationsystem.model.entity.Resource resource = resources.get(0);
484
		if(resources.size() > 1){
485
			log.warn("More than one resource in registry with uri "+uri+" -- considering the first, with uuid: "+resource.getHeader().getUUID());
486
		}
487
		//build PRR from res, need to have type and uuid
488
		//TODO: check the type is what we want
489
		ParthenosRegistryResource prr = new ParthenosRegistryResource().setUuid(resource.getHeader().getUUID().toString()).setType(resource.getClass().getSimpleName())
490
		log.debug(prr.toString());
491
		return prr;
492
	}
493

    
494

    
495
	public FacetWriter getFacetWriter() {
496
		return facetWriter;
497
	}
498

    
499
	public void setFacetWriter(final FacetWriter facetWriter) {
500
		this.facetWriter = facetWriter;
501
	}
502

    
503
	public String getApplicationToken() {
504
		return applicationToken;
505
	}
506

    
507
	public void setApplicationToken(final String applicationToken) {
508
		this.applicationToken = applicationToken;
509
	}
510

    
511
	public String getRegistryBaseURL() {
512
		return registryBaseURL;
513
	}
514

    
515
	public void setRegistryBaseURL(final String registryBaseURL) {
516
		this.registryBaseURL = registryBaseURL;
517
	}
518

    
519
	public RelWriter getRelWriter() {
520
		return relWriter;
521
	}
522

    
523
	public void setRelWriter(final RelWriter relWriter) {
524
		this.relWriter = relWriter;
525
	}
526

    
527
	public ResourceRegistryPublisher getResourceRegistryPublisher() {
528
		return resourceRegistryPublisher;
529
	}
530

    
531
	public void setResourceRegistryPublisher(final ResourceRegistryPublisher resourceRegistryPublisher) {
532
		this.resourceRegistryPublisher = resourceRegistryPublisher;
533
	}
534

    
535
	public ResourceRegistryClient getResourceRegistryClient() {
536
		return resourceRegistryClient;
537
	}
538

    
539
	public void setResourceRegistryClient(final ResourceRegistryClient resourceRegistryClient) {
540
		this.resourceRegistryClient = resourceRegistryClient;
541
	}
542

    
543
	public ResourceReader getResourceReader() {
544
		return resourceReader;
545
	}
546

    
547
	public void setResourceReader(final ResourceReader resourceReader) {
548
		this.resourceReader = resourceReader;
549
	}
550
}
(2-2/5)