Project

General

Profile

1
package eu.dnetlib.parthenos.catalogue;
2

    
3
import java.io.BufferedOutputStream;
4
import java.io.ByteArrayOutputStream;
5
import java.io.IOException;
6
import java.net.URISyntaxException;
7
import java.util.Iterator;
8
import java.util.List;
9
import java.util.Set;
10

    
11
import com.fasterxml.jackson.core.JsonEncoding;
12
import com.fasterxml.jackson.core.JsonFactory;
13
import com.fasterxml.jackson.core.JsonGenerator;
14
import com.google.common.base.Joiner;
15
import com.google.common.collect.Iterators;
16
import com.google.common.collect.Lists;
17
import com.google.common.collect.Sets;
18
import eu.dnetlib.parthenos.CRM;
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.rdf.model.RDFNode;
25
import org.apache.jena.rdf.model.Resource;
26
import org.apache.jena.rdf.model.StmtIterator;
27
import org.apache.jena.vocabulary.RDF;
28
import org.springframework.beans.factory.annotation.Autowired;
29
import org.springframework.stereotype.Component;
30

    
31
/**
32
 * Created by Alessia Bardi on 21/11/2017.
33
 *
34
 * @author Alessia Bardi
35
 */
36
@Component
37
public class CatalogueRegistrator {
38

    
39
	private static final Log log = LogFactory.getLog(CatalogueRegistrator.class);
40
	private final String PARTHENOS_BASE_URL = "http://parthenos.d4science.org";
41

    
42
	@Autowired
43
	private ResourceReader resourceReader;
44

    
45
	@Autowired
46
	private CatalogueAPIClient catalogueAPIClient;
47

    
48
	public String register(final Resource resource, final Resource type, final String datasourceName)
49
			throws IOException, ParthenosPublisherException, URISyntaxException, InterruptedException {
50
		String resURI = resource.getURI();
51
		log.debug(String.format("Catalogue --> Processing resource : %s with type: %s from source: %s", resURI, type.getLocalName(), datasourceName));
52
		String resCatName = catalogueAPIClient.getNameForCatalogue(resURI.substring(resURI.lastIndexOf("handle/") + 7));
53
		if(catalogueAPIClient.isRegistered(resCatName)){
54
			log.debug(resCatName+ " is already registered");
55
			String json = getJson(type, resource, resCatName, datasourceName);
56
			catalogueAPIClient.doUpdate(json, resCatName);
57
		}
58
		else {
59
			//resource not yet registered
60
			String json = getJson(type, resource, resCatName, datasourceName);
61
			if(!catalogueAPIClient.doRegister(json, resCatName)){
62
				log.warn(String.format("%s could not be registered even the second time, giving up", resURI));
63
				return null;
64
			}
65
		}
66
		log.debug(String.format("%s registered on the catalogue with name: %s", resURI, resCatName));
67
		return resCatName;
68
	}
69

    
70
	protected boolean purge(final String resCatName) throws URISyntaxException, ParthenosPublisherException {
71
		return catalogueAPIClient.purgeItem(resCatName);
72
	}
73

    
74
	public int purgeAll(final int bulkSize) throws ParthenosPublisherException {
75
		return catalogueAPIClient.purgeAll(bulkSize);
76
	}
77

    
78

    
79

    
80
	protected String getJson(final Resource type, final Resource resource, final String resNameForCatalogue, final String datasourceName)
81
			throws IOException, ParthenosPublisherException {
82
		switch (type.getLocalName()) {
83
		case "E29_Design_or_Procedure":
84
			return getJsonForDesignProcedure(resource, resNameForCatalogue, datasourceName);
85
		case "D14_Software":
86
			return getJsonForSoftware(resource, resNameForCatalogue, datasourceName);
87
		case "PE35_Project":
88
				return getJsonForProject(resource, resNameForCatalogue, datasourceName);
89
		case "PE1_Service":
90
			return getJsonForService(resource, resNameForCatalogue, datasourceName);
91
		case "E39_Actor":
92
			return getJsonForActor(resource, resNameForCatalogue, datasourceName);
93
		case "PE18_Dataset":
94
			return getJsonForDataset(resource, resNameForCatalogue, datasourceName);
95
		case "E78_Collection":
96
			return getJsonForCollection(resource, resNameForCatalogue, datasourceName);
97
		default:
98
			throw new IllegalArgumentException(String.format("Type " + type.getLocalName() + " not supported"));
99
		}
100
	}
101

    
102
	protected String getJsonForProject(final Resource res, final String resNameForCatalogue, final String datasourceName)
103
			throws IOException, ParthenosPublisherException {
104
		JsonFactory jsonFactory = new JsonFactory();
105
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
106
		BufferedOutputStream bos = new BufferedOutputStream(out);
107
		JsonGenerator jg = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
108
		jg.writeStartObject();
109
		writeCommonFields(jg, res, resNameForCatalogue, datasourceName);
110
		//Only RI_Project are linked to RIs that must be used as groups
111
		List<String> maintainer_RIs = Lists.newArrayList(resourceReader.getMaintainersLabels(res));
112
		if(maintainer_RIs.size() > 0) {
113
			jg.writeArrayFieldStart("groups");
114
			for(String ri : maintainer_RIs) {
115
				String group = CKANUtils.getCkanGroup(ri);
116
				if(StringUtils.isNotBlank(group)) {
117
					jg.writeStartObject();
118
					jg.writeStringField("name", group);
119
					jg.writeEndObject();
120
				}
121
			}
122
			jg.writeEndArray();
123
		}
124

    
125
		jg.writeStringField("maintainer", Joiner.on(", ").join(resourceReader.getMaintainersLabels(res)));
126
		//TODO: it should be better to identify email contacts rather than generic contact labels of maintainer
127
		//jg.writeStringField("maintainer_email", Joiner.on(", ").join(resourceReader.getMaintainerContacts(res)));
128

    
129
		jg.writeArrayFieldStart("extras");
130
		addExtra(jg, "system:type", CKANUtils.Project_type);
131
		//specific class
132
		addExtra(jg, "instance of", resourceReader.findSpecificType(res, CRM.E7_Activity).getLocalName());
133
		if (res.getURI().startsWith(PARTHENOS_BASE_URL))
134
			addExtra(jg, "Parthenos URL", res.getURI());
135
		else addExtra(jg, "URL", res.getURI());
136
		addIdentifiers(res, jg);
137

    
138
		addExtra(jg, "started on", resourceReader.getStartTime(res));
139
		int idx = 1;
140
		Iterator<String> maintainers = resourceReader.getMaintainerUrls(res);
141
		while(maintainers.hasNext()){
142
			addExtra(jg, String.format("maintaining team (%d)", idx), maintainers.next());
143
			idx++;
144
		}
145
		//addExtra(jg, "maintaining team", Joiner.on(", ").join(resourceReader.getMaintainerUrls(res)));
146
		idx = 1;
147
		Iterator<String> services = resourceReader.getOfferedServiceUrls(res);
148
		while(services.hasNext()){
149
			addExtra(jg, String.format("offers (%d)", idx), services.next());
150
			idx++;
151
		}
152
		//addExtra(jg, "offers", Joiner.on(", ").join(resourceReader.getOfferedServiceUrls(res)));
153

    
154
		jg.writeEndArray(); //end extras
155

    
156
		jg.writeEndObject();
157
		jg.close();
158
		return out.toString("UTF-8");
159
	}
160

    
161
	protected String getJsonForService(final Resource res, final String resNameForCatalogue, final String datasourceName)
162
			throws IOException, ParthenosPublisherException {
163
		JsonFactory jsonFactory = new JsonFactory();
164
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
165
		BufferedOutputStream bos = new BufferedOutputStream(out);
166
		JsonGenerator jg = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
167
		jg.writeStartObject();
168
		writeCommonFields(jg, res, resNameForCatalogue, datasourceName);
169
		jg.writeStringField("maintainer", Joiner.on(", ").join(resourceReader.getMaintainersLabels(res)));
170
		//TODO: it should be better to identify email contacts rather than generic contact labels of maintainer
171
		//jg.writeStringField("maintainer_email", Joiner.on(", ").join(resourceReader.getMaintainerContacts(res)));
172

    
173
		jg.writeArrayFieldStart("extras");
174
		addExtra(jg, "system:type", CKANUtils.Service_type);
175
		//specific class
176
		addExtra(jg, "instance of", resourceReader.findSpecificType(res, CRM.E7_Activity).getLocalName());
177
		if (res.getURI().startsWith(PARTHENOS_BASE_URL))
178
			addExtra(jg, "Parthenos URL", res.getURI());
179
		else addExtra(jg, "URL", res.getURI());
180
		addIdentifiers(res, jg);
181
		addExtra(jg, "competence", Joiner.on(", ").join(resourceReader.getCompetences(res)));
182
		addExtra(jg, "activity type", Joiner.on(", ").join(resourceReader.getActivityTypes(res)));
183
		//condition of use (Rights)
184
		addExtra(jg, "condition of use", resourceReader.getConditionOfUse(res));
185
		int idx = 1;
186
		Iterator<String> contacts = Iterators.concat(resourceReader.getResourceDirectContactPointsURI(res), resourceReader.getProviderContactPoints(res));
187
		while(contacts.hasNext()){
188
			addExtra(jg, String.format("contact points (%d)", idx), contacts.next());
189
			idx++;
190
		}
191
		//addExtra(jg, "contact points", Joiner.on(", ").join(Iterators.concat(resourceReader.getResourceDirectContactPointsURI(res), resourceReader.getProviderContactPoints(res))));
192
		idx = 1;
193
		Iterator<String> providers = resourceReader.getProviderUris(res);
194
		while(providers.hasNext()){
195
			addExtra(jg, String.format("provided by (%d)", idx), providers.next());
196
			idx++;
197
		}
198
		//addExtra(jg, "provided by", Joiner.on(", ").join(resourceReader.getProviderUris(res)));
199
		idx = 1;
200
		Iterator<String> points = resourceReader.getAccessPoints(res);
201
		while(points.hasNext()){
202
			addExtra(jg, String.format("online access point (%d)", idx), points.next());
203
			idx++;
204
		}
205
		//addExtra(jg, "online access point", Joiner.on(", ").join(resourceReader.getAccessPoints(res)));
206
		addExtra(jg, "protocol", Joiner.on(", ").join(resourceReader.getProtocols(res)));
207
		idx = 1;
208
		Iterator<String> delivers = resourceReader.getDeliversOnRequest(res);
209
		while(delivers.hasNext()){
210
			addExtra(jg, String.format("delivers on request (%d)", idx), delivers.next());
211
			idx++;
212
		}
213
		//addExtra(jg, "delivers on request", Joiner.on(", ").join(resourceReader.getDeliversOnRequest(res)));
214
		idx = 1;
215
		Iterator<String> runs = resourceReader.getRunsOnRequest(res);
216
		while(runs.hasNext()){
217
			addExtra(jg, String.format("runs on request (%d)", idx), runs.next());
218
			idx++;
219
		}
220
		//addExtra(jg, "runs on request", Joiner.on(", ").join(resourceReader.getRunsOnRequest(res)));
221
		idx = 1;
222
		Iterator<String> hosts = resourceReader.getHostedStuff(res);
223
		while(hosts.hasNext()){
224
			addExtra(jg, String.format("hosts (%d)", idx), hosts.next());
225
			idx++;
226
		}
227
		//addExtra(jg, "hosts", Joiner.on(", ").join(resourceReader.getHostedStuff(res)));
228
		idx = 1;
229
		Iterator<String> curates = resourceReader.getCuratedObjects(res);
230
		while(curates.hasNext()){
231
			addExtra(jg, String.format("curates (%d)", idx), curates.next());
232
			idx++;
233
		}
234
		//addExtra(jg, "curates", Joiner.on(", ").join(resourceReader.getCuratedObjects(res)));
235
		addExtra(jg, "declared begin/end of operation", Joiner.on(", ").join(resourceReader.getDeclarativeTimes(res)));
236
		addExtra(jg, "availability", resourceReader.getAvailability(res));
237
		idx = 1;
238
		Iterator<String> plans = resourceReader.getCurationPlans(res);
239
		while(plans.hasNext()){
240
			addExtra(jg, String.format("uses curation plan (%d)", idx), plans.next());
241
			idx++;
242
		}
243
		//addExtra(jg, "uses curation plan", Joiner.on(", ").join(resourceReader.getCurationPlans(res)));
244
		addExtra(jg, "time of service", Joiner.on(", ").join(resourceReader.getDeclarativeTimes(res)));
245
		//TODO: where to get it?
246
		//addExtra(jg, "last confirmation", "");
247
		//TODO: where to get it?
248
		//addExtra(jg, "date of registration", "");
249

    
250
		jg.writeEndArray(); //end extras
251

    
252
		jg.writeEndObject();
253
		jg.close();
254
		return out.toString("UTF-8");
255
	}
256

    
257
	protected String getJsonForActor(final Resource res, final String resNameForCatalogue, final String datasourceName)
258
			throws IOException, ParthenosPublisherException {
259
		JsonFactory jsonFactory = new JsonFactory();
260
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
261
		BufferedOutputStream bos = new BufferedOutputStream(out);
262
		JsonGenerator jg = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
263
		jg.writeStartObject();
264
		writeCommonFields(jg, res, resNameForCatalogue, datasourceName);
265

    
266
		jg.writeArrayFieldStart("extras");
267
		addExtra(jg, "system:type", CKANUtils.Actor_type);
268
		//specific class
269
		addExtra(jg, "instance of", resourceReader.findSpecificType(res, CRM.E39_Actor).getLocalName());
270
		if (res.getURI().startsWith(PARTHENOS_BASE_URL))
271
			addExtra(jg, "Parthenos URL", res.getURI());
272
		else addExtra(jg, "URL", res.getURI());
273
		addIdentifiers(res, jg);
274
		addExtra(jg, "has type", Joiner.on(", ").join(resourceReader.getHasTypeLabels(res)));
275
		int idx = 1;
276
		Iterator<String> hasMembers = resourceReader.getMemberUrls(res);
277
		while(hasMembers.hasNext()){
278
			addExtra(jg, String.format("has member (%d)", idx), hasMembers.next());
279
			idx++;
280
		}
281
		//addExtra(jg, "has member", Joiner.on(", ").join(resourceReader.getMemberUrls(res)));
282
		idx = 1;
283
		Iterator<String> isMembers = resourceReader.isMemberOf(res);
284
		while(isMembers.hasNext()){
285
			addExtra(jg, String.format("is member of (%d)", idx), isMembers.next());
286
			idx++;
287
		}
288
		//addExtra(jg, "is member of", Joiner.on(", ").join(resourceReader.isMemberOf(res)));
289
		idx = 1;
290
		Iterator<String> provides = resourceReader.getProvidedServiceUrls(res);
291
		while(provides.hasNext()){
292
			addExtra(jg, String.format("provides (%d)", idx), provides.next());
293
			idx++;
294
		}
295
		//addExtra(jg, "provides", Joiner.on(", ").join(resourceReader.getProvidedServiceUrls(res)));
296

    
297
		idx = 1;
298
		String contactPoints = "";
299
		StmtIterator it = res.listProperties(CRM.P76_has_contact_point);
300
		while(it.hasNext()) {
301
			Resource cp = it.next().getResource();
302
			Resource cpType = cp.getPropertyResourceValue(CRM.P2_has_type);
303
			String cpTypeLabel = resourceReader.getLabel(cpType);
304
			String cpLabel = resourceReader.getLabel(cp);
305
			if (StringUtils.isNotBlank(cpLabel)) {
306
				if (StringUtils.isNotBlank(cpTypeLabel)) {
307
					addExtra(jg,String.format("contact point (%d) - %s ", idx, cpTypeLabel), cpLabel );
308
					idx++;
309
					//contactPoints += cpTypeLabel + ": ";
310
				}
311
				else{
312
					addExtra(jg,String.format("contact point (%d)", idx), cpLabel );
313
					idx++;
314
					//contactPoints += cpLabel + "; ";
315
				}
316

    
317
			}
318
			else{
319
				addExtra(jg,String.format("contact point (%d)", idx), cp.getURI());
320
				idx++;
321
			}
322
		}
323
	//	addExtra(jg,"contact points", contactPoints );
324
		idx = 1;
325
		Iterator<String> maintains = resourceReader.getMaintainedUrls(res);
326
		while(maintains.hasNext()){
327
			addExtra(jg, String.format("maintains (%d)", idx), maintains.next());
328
			idx++;
329
		}
330
		//addExtra(jg, "maintains", Joiner.on(", ").join(resourceReader.getMaintainedUrls(res)));
331

    
332
		jg.writeEndArray();
333

    
334
		jg.writeEndObject();
335
		jg.close();
336
		return out.toString("UTF-8");
337
	}
338

    
339
	protected String getJsonForDataset(final Resource res, final String resNameForCatalogue, final String datasourceName)
340
			throws IOException, ParthenosPublisherException {
341
		JsonFactory jsonFactory = new JsonFactory();
342
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
343
		BufferedOutputStream bos = new BufferedOutputStream(out);
344
		JsonGenerator jg = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
345
		jg.writeStartObject();
346
		writeCommonFields(jg, res, resNameForCatalogue, datasourceName);
347

    
348
		jg.writeArrayFieldStart("extras");
349
		addExtra(jg, "system:type", CKANUtils.Dataset_type);
350
		//specific class
351
		addExtra(jg, "instance of", resourceReader.findSpecificType(res, CRM.E70_Thing).getLocalName());
352
		if (res.getURI().startsWith(PARTHENOS_BASE_URL)) {
353
			addExtra(jg, "Parthenos URL", res.getURI());
354
		}
355
		else addExtra(jg, "URL", res.getURI());
356
		addIdentifiers(res, jg);
357
		addExtra(jg, "has type", Joiner.on(", ").join(resourceReader.getHasTypeLabels(res)));
358
		int idx = 1;
359
		Iterator<String> isPartOf = resourceReader.getIsPartOfUrls(res);
360
		while(isPartOf.hasNext()){
361
			addExtra(jg, String.format("is part of (%d)", idx), isPartOf.next());
362
			idx++;
363
		}
364
		//addExtra(jg, "is part of", Joiner.on(", ").join(resourceReader.getIsPartOfUrls(res)));
365
		idx = 1;
366
		Iterator<String> hasPart = resourceReader.getHasPartUrls(res);
367
		while(hasPart.hasNext()){
368
			addExtra(jg, String.format("has part (%d)", idx), hasPart.next());
369
			idx++;
370
		}
371
		//addExtra(jg, "has part", Joiner.on(", ").join(resourceReader.getHasPartUrls(res)));
372
		idx = 1;
373
		Iterator<String> curators = resourceReader.getCuratorUrls(res);
374
		while(curators.hasNext()){
375
			addExtra(jg, String.format("curated by (%d)", idx), curators.next());
376
			idx++;
377
		}
378
		//addExtra(jg, "curated by", Joiner.on(", ").join(resourceReader.getCuratorUrls(res)));
379
		idx = 1;
380
		Iterator<String> curationplans = resourceReader.getResourceCuratorCurationPlans(res);
381
		while(curationplans.hasNext()){
382
			addExtra(jg, String.format("curation plan (%d)", idx), curationplans.next());
383
			idx++;
384
		}
385
		//addExtra(jg, "curation plan", Joiner.on(", ").join(resourceReader.getResourceCuratorCurationPlans(res)));
386
		idx = 1;
387
		Iterator<String> hostedbys = resourceReader.getHostedBys(res);
388
		while(hostedbys.hasNext()){
389
			addExtra(jg, String.format("hosted by (%d)", idx), hostedbys.next());
390
			idx++;
391
		}
392
		//addExtra(jg, "hosted by", Joiner.on(", ").join(resourceReader.getHostedBys(res)));
393
		addExtra(jg, "encoding type", Joiner.on(", ").join(resourceReader.getEncodings(res)));
394
		idx = 1;
395
		Iterator<String> creators = resourceReader.getCreatorsURIs(res);
396
		while(creators.hasNext()){
397
			addExtra(jg, String.format("creator (%d)", idx), creators.next());
398
			idx++;
399
		}
400
		//addExtra(jg, "creator", Joiner.on(", ").join(resourceReader.getCreatorsURIs(res)));
401
		idx = 1;
402
		Iterator<String> sw = resourceReader.getUsedSoftware(res);
403
		while(sw.hasNext()){
404
			addExtra(jg, String.format("used software (%d)", idx), sw.next());
405
			idx++;
406
		}
407

    
408
		addExtra(jg, "subject", Joiner.on(", ").join(resourceReader.getSubjects(res)));
409
		addExtra(jg, "temporal coverage", Joiner.on(", ").join(resourceReader.getTemporalCoverages(res)));
410
		addExtra(jg, "spatial coverage", Joiner.on(", ").join(resourceReader.getSpatialCoverages(res)));
411
		idx = 1;
412
		Iterator<String> usedbys = resourceReader.getUsedBy(res);
413
		while(usedbys.hasNext()){
414
			addExtra(jg, String.format("used by (%d)", idx), usedbys.next());
415
			idx++;
416
		}
417
		//addExtra(jg, "used by", Joiner.on(", ").join(resourceReader.getUsedBy(res)));
418
		addExtra(jg, "languages", Joiner.on(", ").join(resourceReader.getLanguages(res)));
419
		idx = 1;
420
		Iterator<String> metadata = resourceReader.getMetadata(res);
421
		while(metadata.hasNext()){
422
			addExtra(jg, String.format("has metadata (%d)", idx), metadata.next());
423
			idx++;
424
		}
425
		//addExtra(jg, "has metadata", Joiner.on(", ").join(resourceReader.getMetadata(res)));
426
		idx = 1;
427
		Iterator<String> metadataFor = resourceReader.getDescribedDataset(res);
428
		while(metadataFor.hasNext()){
429
			addExtra(jg, String.format("is metadata for (%d)", idx), metadataFor.next());
430
			idx++;
431
		}
432
		//addExtra(jg, "is metadata for", Joiner.on(", ").join(resourceReader.getDescribedDataset(res)));
433
		idx = 1;
434
		Iterator<String> snaphsots = resourceReader.getSnapshots(res);
435
		while(snaphsots.hasNext()){
436
			addExtra(jg, String.format("has snapshot (%d)", idx), snaphsots.next());
437
			idx++;
438
		}
439
		//addExtra(jg, "has snapshot", Joiner.on(", ").join(resourceReader.getSnapshots(res)));
440
		idx = 1;
441
		Iterator<String> issnaphsots = resourceReader.getIsSnapshotOfs(res);
442
		while(issnaphsots.hasNext()){
443
			addExtra(jg, String.format("is snapshot of (%d)", idx), issnaphsots.next());
444
			idx++;
445
		}
446
		//addExtra(jg, "is snapshot of", Joiner.on(", ").join(resourceReader.getIsSnapshotOfs(res)));
447

    
448
		jg.writeEndArray();
449

    
450
		jg.writeEndObject();
451
		jg.close();
452
		return out.toString("UTF-8");
453
	}
454

    
455
	protected String getJsonForSoftware(final Resource res, final String resNameForCatalogue, final String datasourceName)
456
			throws IOException, ParthenosPublisherException {
457
		JsonFactory jsonFactory = new JsonFactory();
458
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
459
		BufferedOutputStream bos = new BufferedOutputStream(out);
460
		JsonGenerator jg = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
461
		jg.writeStartObject();
462
		writeCommonFields(jg, res, resNameForCatalogue, datasourceName);
463

    
464
		jg.writeArrayFieldStart("extras");
465
		addExtra(jg, "system:type", CKANUtils.Software_type);
466
		//specific class
467
		addExtra(jg, "instance of", resourceReader.findSpecificType(res, CRM.E70_Thing).getLocalName());
468
		if (res.getURI().startsWith(PARTHENOS_BASE_URL)) {
469
			addExtra(jg, "Parthenos URL", res.getURI());
470
		}
471
		else addExtra(jg, "URL", res.getURI());
472
		addIdentifiers(res, jg);
473
		addExtra(jg, "subject", Joiner.on(", ").join(resourceReader.getSubjects(res)));
474
		addExtra(jg, "has type", Joiner.on(", ").join(resourceReader.getHasTypeLabels(res)));
475
		int idx = 1;
476
		Iterator<String> hosted = resourceReader.getHostedBys(res);
477
		while(hosted.hasNext()){
478
			addExtra(jg, String.format("hosted by (%d)", idx), hosted.next());
479
			idx++;
480
		}
481
		//addExtra(jg, "hosted by", Joiner.on(", ").join(resourceReader.getHostedBys(res)));
482
		idx = 1;
483
		Iterator<String> curated = resourceReader.getCuratorUrls(res);
484
		while(curated.hasNext()){
485
			addExtra(jg, String.format("curated by (%d)", idx), curated.next());
486
			idx++;
487
		}
488
		//addExtra(jg, "curated by", Joiner.on(", ").join(resourceReader.getCuratorUrls(res)));
489
		idx = 1;
490
		Iterator<String> hasSnapshot = resourceReader.getSnapshots(res);
491
		while(hasSnapshot.hasNext()){
492
			addExtra(jg, String.format("has snapshot (%d)", idx), hasSnapshot.next());
493
			idx++;
494
		}
495
		//addExtra(jg, "has snapshot", Joiner.on(", ").join(resourceReader.getSnapshots(res)));
496
		idx = 1;
497
		Iterator<String> isSnapshot = resourceReader.getIsSnapshotOfs(res);
498
		while(isSnapshot.hasNext()){
499
			addExtra(jg, String.format("is snapshot (%d)", idx), isSnapshot.next());
500
			idx++;
501
		}
502
		//addExtra(jg, "is snapshot of", Joiner.on(", ").join(resourceReader.getIsSnapshotOfs(res)));
503
		idx = 1;
504
		Iterator<String> isPart = resourceReader.getIsPartOfUrls(res);
505
		while(isPart.hasNext()){
506
			addExtra(jg, String.format("is part of (%d)", idx), isPart.next());
507
			idx++;
508
		}
509
		//addExtra(jg, "is part of", Joiner.on(", ").join(resourceReader.getIsPartOfUrls(res)));
510
		idx = 1;
511
		Iterator<String> hasPart = resourceReader.getHasPartUrls(res);
512
		while(hasPart.hasNext()){
513
			addExtra(jg, String.format("has part (%d)", idx), hasPart.next());
514
			idx++;
515
		}
516
		//addExtra(jg, "has part", Joiner.on(", ").join(resourceReader.getHasPartUrls(res)));
517
		idx = 1;
518
		Iterator<String> hasRelease = resourceReader.getHasReleases(res);
519
		while(hasRelease.hasNext()){
520
			addExtra(jg, String.format("has release (%d)", idx), hasRelease.next());
521
			idx++;
522
		}
523
		//addExtra(jg, "has release", Joiner.on(", ").join(resourceReader.getHasReleases(res)));
524
		idx = 1;
525
		Iterator<String> isRelease = resourceReader.getIsReleaseOfs(res);
526
		while(isRelease.hasNext()){
527
			addExtra(jg, String.format("is release (%d)", idx), isRelease.next());
528
			idx++;
529
		}
530
		//addExtra(jg, "is release of", Joiner.on(", ").join(resourceReader.getIsReleaseOfs(res)));
531
		idx = 1;
532
		Iterator<String> used = resourceReader.getUsedBy(res);
533
		while(used.hasNext()){
534
			addExtra(jg, String.format("used by (%d)", idx), used.next());
535
			idx++;
536
		}
537
		//addExtra(jg, "used by", Joiner.on(", ").join(resourceReader.getUsedBy(res)));
538
		addExtra(jg, "creation time", resourceReader.getFirstCreationTime(res));
539

    
540
		jg.writeEndArray();
541

    
542
		jg.writeEndObject();
543
		jg.close();
544
		return out.toString("UTF-8");
545
	}
546

    
547
	protected String getJsonForCollection(final Resource res, final String resNameForCatalogue, final String datasourceName)
548
			throws IOException, ParthenosPublisherException {
549
		JsonFactory jsonFactory = new JsonFactory();
550
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
551
		BufferedOutputStream bos = new BufferedOutputStream(out);
552
		JsonGenerator jg = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
553
		jg.writeStartObject();
554
		writeCommonFields(jg, res, resNameForCatalogue, datasourceName);
555

    
556
		jg.writeArrayFieldStart("extras");
557
		addExtra(jg, "system:type", CKANUtils.Collection_type);
558
		//specific class
559
		addExtra(jg, "instance of", resourceReader.findSpecificType(res, CRM.E70_Thing).getLocalName());
560
		if (res.getURI().startsWith(PARTHENOS_BASE_URL)) {
561
			addExtra(jg, "Parthenos URL", res.getURI());
562
		}
563
		else addExtra(jg, "URL", res.getURI());
564
		addIdentifiers(res, jg);
565
		addExtra(jg, "has type", Joiner.on(", ").join(resourceReader.getHasTypeLabels(res)));
566
		addExtra(jg, "subject", Joiner.on(", ").join(resourceReader.getSubjects(res)));
567
		addExtra(jg, "temporal coverage", Joiner.on(", ").join(resourceReader.getTemporalCoverages(res)));
568
		addExtra(jg, "spatial coverage", Joiner.on(", ").join(resourceReader.getSpatialCoverages(res)));
569
		int idx = 1;
570
		Iterator<String> hasPart = resourceReader.getHasPartUrls(res);
571
		while(hasPart.hasNext()){
572
			addExtra(jg, String.format("has part (%d)", idx), hasPart.next());
573
			idx++;
574
		}
575
		//addExtra(jg, "has part", Joiner.on(", ").join(resourceReader.getHasPartUrls(res)));
576
		idx = 1;
577
		Iterator<String> hosted = resourceReader.getHostedBys(res);
578
		while(hosted.hasNext()){
579
			addExtra(jg, String.format("hosted by (%d)", idx), hosted.next());
580
			idx++;
581
		}
582
		//addExtra(jg, "hosted by", Joiner.on(", ").join(resourceReader.getHostedBys(res)));
583
		idx = 1;
584
		Iterator<String> curated = resourceReader.getCuratorUrls(res);
585
		while(curated.hasNext()){
586
			addExtra(jg, String.format("curated by (%d)", idx), curated.next());
587
			idx++;
588
		}
589
		//addExtra(jg, "curated by", Joiner.on(", ").join(resourceReader.getCuratorUrls(res)));
590
		idx = 1;
591
		Iterator<String> creators = resourceReader.getCreatorsURIs(res);
592
		while(creators.hasNext()){
593
			addExtra(jg, String.format("creator (%d)", idx), creators.next());
594
			idx++;
595
		}
596
		//addExtra(jg, "creator", Joiner.on(", ").join(resourceReader.getCreatorsURIs(res)));
597
		addExtra(jg, "languages", Joiner.on(", ").join(resourceReader.getLanguages(res)));
598

    
599
		jg.writeEndArray();
600

    
601
		jg.writeEndObject();
602
		jg.close();
603
		return out.toString("UTF-8");
604
	}
605

    
606

    
607
	protected String getJsonForDesignProcedure(final Resource res, final String resNameForCatalogue, final String datasourceName)
608
			throws IOException, ParthenosPublisherException {
609
		JsonFactory jsonFactory = new JsonFactory();
610
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
611
		BufferedOutputStream bos = new BufferedOutputStream(out);
612
		JsonGenerator jg = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
613
		jg.writeStartObject();
614
		writeCommonFields(jg, res, resNameForCatalogue, datasourceName);
615

    
616
		jg.writeArrayFieldStart("extras");
617
		addExtra(jg, "system:type", CKANUtils.DesignOrProcedure_type);
618
		//specific class
619
		addExtra(jg, "instance of", resourceReader.findSpecificType(res, CRM.E29_Design_or_Procedure).getLocalName());
620
		if (res.getURI().startsWith(PARTHENOS_BASE_URL)) {
621
			addExtra(jg, "Parthenos URL", res.getURI());
622
		}
623
		else addExtra(jg, "URL", res.getURI());
624
		addIdentifiers(res, jg);
625
		int idx = 1;
626
		Iterator<String> used = resourceReader.getUsedBy(res);
627
		while(used.hasNext()){
628
			addExtra(jg, String.format("used by (%d)", idx), used.next());
629
			idx++;
630
		}
631
		//addExtra(jg, "used by", Joiner.on(", ").join(resourceReader.getUsedBy(res)));
632
		//TODO: add additional metadata for E29_Design_or_Procedure, if any
633
		jg.writeEndArray();
634

    
635
		jg.writeEndObject();
636
		jg.close();
637
		return out.toString("UTF-8");
638
	}
639

    
640
	protected void addIdentifiers(final Resource res, final JsonGenerator jg ) throws IOException {
641
		/*
642
		 <${subjectURL}> crm:P1_is_identified_by ?IDRes .
643
		 ?IDRes a crm:E42_Identifier .
644
		 ?IDRes rdfs:label ?ID_label .
645
		 */
646
		StmtIterator it = res.listProperties(CRM.P1_is_identified_by);
647
		Set<String> ids = Sets.newHashSet();
648
		while(it.hasNext()){
649
			RDFNode obj = it.next().getObject();
650
			if(obj.isLiteral()) ids.add(obj.asLiteral().getLexicalForm());
651
			else {
652
				Resource id = (Resource) obj;
653
				if (id.hasProperty(RDF.type, CRM.E42_Identifier)) {
654
					ids.add(resourceReader.getLabel(id));
655
				}
656
			}
657
		}
658
		addExtra(jg, "ID", String.join(",", ids));
659
	}
660

    
661
	protected void addExtra(final JsonGenerator jg, final String key, final String value) throws IOException {
662
		if(StringUtils.isNotBlank(value)) {
663
			jg.writeStartObject();
664
			jg.writeStringField("key", key);
665
			jg.writeStringField("value", value);
666
			jg.writeEndObject();
667
		}
668
	}
669

    
670

    
671

    
672
	protected void writeCommonFields(final JsonGenerator jg, final Resource res, final String resNameForCatalogue, final String datasourceName)
673
			throws IOException, ParthenosPublisherException {
674
		String ckanOrg = CKANUtils.getCKanOrg(datasourceName);
675
		//id is available only for updates
676
		if(StringUtils.isBlank(ckanOrg)) throw new ParthenosPublisherException(String.format("Cannot register %s : blank ckan org for data source with name %s", resNameForCatalogue, datasourceName));
677
		//the owning organization, i.e. the data souce from which this resource has been collected from
678
		jg.writeStringField("owner_org", ckanOrg);
679
		jg.writeStringField("name", resNameForCatalogue);
680
		//default license
681
		jg.writeStringField("license_id", resourceReader.getCatalogueLicense(res).getId());
682
		String title = resourceReader.getTitle(res);
683
		if (StringUtils.isBlank(title))
684
			title = resNameForCatalogue;
685
		jg.writeStringField("title", title);
686
		//description
687
		jg.writeStringField("notes",Joiner.on(';').join(resourceReader.getDescriptions(res)));
688
		//the names of all superclasses of the entity
689
		jg.writeArrayFieldStart("tags");
690
		Iterator<String> classNames = resourceReader.getRDFClassNames(res);
691
		while (classNames.hasNext()) {
692
			jg.writeStartObject();
693
			jg.writeStringField("name", classNames.next());
694
			jg.writeEndObject();
695
		}
696
		jg.writeEndArray();
697
	}
698

    
699

    
700
	public ResourceReader getResourceReader() {
701
		return resourceReader;
702
	}
703

    
704
	public void setResourceReader(final ResourceReader resourceReader) {
705
		this.resourceReader = resourceReader;
706
	}
707

    
708
	public CatalogueAPIClient getCatalogueAPIClient() {
709
		return catalogueAPIClient;
710
	}
711

    
712
	public void setCatalogueAPIClient(final CatalogueAPIClient catalogueAPIClient) {
713
		this.catalogueAPIClient = catalogueAPIClient;
714
	}
715
}
(5-5/5)