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.URI;
|
7
|
import java.util.Iterator;
|
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.publisher.ParthenosPublisherException;
|
18
|
import eu.dnetlib.parthenos.rdf.ResourceReader;
|
19
|
import org.apache.commons.lang3.StringUtils;
|
20
|
import org.apache.commons.logging.Log;
|
21
|
import org.apache.commons.logging.LogFactory;
|
22
|
import org.apache.http.client.utils.URIBuilder;
|
23
|
import org.apache.jena.ontology.OntModel;
|
24
|
import org.apache.jena.ontology.OntModelSpec;
|
25
|
import org.apache.jena.rdf.model.ModelFactory;
|
26
|
import org.apache.jena.rdf.model.Resource;
|
27
|
import org.springframework.beans.factory.annotation.Autowired;
|
28
|
import org.springframework.beans.factory.annotation.Value;
|
29
|
import org.springframework.http.*;
|
30
|
import org.springframework.stereotype.Component;
|
31
|
import org.springframework.web.client.RestTemplate;
|
32
|
|
33
|
import static eu.dnetlib.parthenos.CRM.*;
|
34
|
|
35
|
/**
|
36
|
* Created by Alessia Bardi on 21/11/2017.
|
37
|
*
|
38
|
* @author Alessia Bardi
|
39
|
*/
|
40
|
@Component
|
41
|
public class CatalogueRegistrator {
|
42
|
|
43
|
private static final Log log = LogFactory.getLog(CatalogueRegistrator.class);
|
44
|
|
45
|
@Value("${gcube.catalogue.baseurl}")
|
46
|
private String baseURL;
|
47
|
@Value("${gcube.registry.application.token}")
|
48
|
private String applicationToken;
|
49
|
|
50
|
@Autowired
|
51
|
private ResourceReader resourceReader;
|
52
|
|
53
|
@Autowired
|
54
|
private RestTemplate jrrRestTemplate;
|
55
|
|
56
|
|
57
|
public String register(final Resource resource, final Resource type) throws IOException, ParthenosPublisherException {
|
58
|
String resURI = resource.getURI();
|
59
|
log.debug(String.format("Catalogue --> Processing resource : %s with type: %s", resURI, type.getLocalName()));
|
60
|
String resCatName = resURI.substring(resURI.lastIndexOf("/") + 1);
|
61
|
String json;
|
62
|
switch(type.getLocalName()){
|
63
|
case "E7_Activity":
|
64
|
json = getJsonForActivity(resource, resCatName);
|
65
|
break;
|
66
|
case "E39_Actor":
|
67
|
json = getJsonForActor(resource, resCatName);
|
68
|
break;
|
69
|
case "E70_Thing":
|
70
|
json = getJsonForThing(resource, resCatName);
|
71
|
break;
|
72
|
//TODO: handle more tyopes, see JRRPublisher
|
73
|
default:
|
74
|
throw new IllegalArgumentException(String.format("Type "+type.getLocalName()+" not supported"));
|
75
|
}
|
76
|
String uuid = doRegister(json, resCatName);
|
77
|
log.debug(String.format("%s registered on the catalogue with uuid: %s", resURI, uuid));
|
78
|
return uuid;
|
79
|
}
|
80
|
|
81
|
|
82
|
protected String getJsonForActivity(final Resource res, final String resNameForCatalogue) throws IOException {
|
83
|
JsonFactory jsonFactory = new JsonFactory();
|
84
|
final ByteArrayOutputStream out = new ByteArrayOutputStream();
|
85
|
BufferedOutputStream bos = new BufferedOutputStream(out);
|
86
|
JsonGenerator jg = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
|
87
|
jg.writeStartObject();
|
88
|
writeCommonFields(jg, res, resNameForCatalogue);
|
89
|
//TODO: from which linked E39_Actor?
|
90
|
jg.writeStringField("maintainer", "");
|
91
|
jg.writeStringField("maintainer_email", "");
|
92
|
//the names of all superclasses of the entity
|
93
|
jg.writeArrayFieldStart("tags");
|
94
|
Iterator<String> classNames = resourceReader.getRDFClassNames(res);
|
95
|
while (classNames.hasNext()) {
|
96
|
jg.writeStartObject();
|
97
|
jg.writeStringField("name", classNames.next());
|
98
|
jg.writeEndObject();
|
99
|
}
|
100
|
jg.writeEndArray();
|
101
|
//RI from which the entity has been collected, the source from which the RI collected the entity (if available).
|
102
|
//TODO: from which linked E39_Actors?
|
103
|
jg.writeArrayFieldStart("groups");
|
104
|
jg.writeStartObject();
|
105
|
jg.writeStringField("name", "");
|
106
|
jg.writeEndObject();
|
107
|
jg.writeStartObject();
|
108
|
jg.writeStringField("name", "");
|
109
|
jg.writeEndObject();
|
110
|
jg.writeEndArray();
|
111
|
|
112
|
jg.writeArrayFieldStart("extras");
|
113
|
addExtra(jg, "system:type", E7_Activity.getLocalName());
|
114
|
//specific class
|
115
|
addExtra(jg, "instance of", resourceReader.findSpecificType(res, E7_Activity).getLocalName());
|
116
|
if (res.getURI().startsWith("http://parthenos.d4science.org"))
|
117
|
addExtra(jg, "Parthenos URL", res.getURI());
|
118
|
addExtra(jg, "competence", res.getURI());
|
119
|
//TODO: from which linked E39_Actors? Should be the same as for groups above
|
120
|
addExtra(jg, "provided by", "");
|
121
|
//TODO: get begin/end of operation from PP42_has_declarative_time
|
122
|
addExtra(jg, "declared begin/end of operation", "");
|
123
|
//TODO: where to get it?
|
124
|
addExtra(jg, "last confirmation", "");
|
125
|
//TODO: where to get it?
|
126
|
addExtra(jg, "date of registration", "");
|
127
|
addExtra(jg, "availability", resourceReader.getAvailability(res));
|
128
|
addExtra(jg, "condition of use", resourceReader.getConditionOfUse(res));
|
129
|
//TODO: where to get the contact person
|
130
|
//Resource contactPerson = null;
|
131
|
//jg.writeStringField("contact person", resourceReader.getTitle(contactPerson));
|
132
|
//TODO: where to get it?
|
133
|
addExtra(jg, "activity type", "");
|
134
|
// can't add multiple fields with same name, hence skipping
|
135
|
//jg.writeStringField("hosts", "");
|
136
|
//TODO: where to get it?
|
137
|
addExtra(jg, "online access point", "");
|
138
|
//TODO: where to get it?
|
139
|
addExtra(jg, "authorization", "");
|
140
|
//TODO: where to get it?
|
141
|
addExtra(jg, "protocol", "");
|
142
|
//can't add multiple fields with same name, hence skipping
|
143
|
//addExtra(jg,"curates", "");
|
144
|
//TODO: where to get it?
|
145
|
addExtra(jg, "runs on request", "");
|
146
|
//TODO: where to get it?
|
147
|
addExtra(jg, "delivers on request", "");
|
148
|
jg.writeEndArray(); //end extras
|
149
|
|
150
|
jg.writeEndObject();
|
151
|
jg.close();
|
152
|
return out.toString("UTF-8");
|
153
|
}
|
154
|
protected String getJsonForActor(final Resource res, final String resNameForCatalogue) throws IOException {
|
155
|
JsonFactory jsonFactory = new JsonFactory();
|
156
|
final ByteArrayOutputStream out = new ByteArrayOutputStream();
|
157
|
BufferedOutputStream bos = new BufferedOutputStream(out);
|
158
|
JsonGenerator jg = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
|
159
|
jg.writeStartObject();
|
160
|
writeCommonFields(jg, res, resNameForCatalogue);
|
161
|
|
162
|
jg.writeArrayFieldStart("extras");
|
163
|
addExtra(jg, "system:type", E39_Actor.getLocalName());
|
164
|
//specific class
|
165
|
addExtra(jg, "instance of", resourceReader.findSpecificType(res, E39_Actor).getLocalName());
|
166
|
if (res.getURI().startsWith("http://parthenos.d4science.org"))
|
167
|
addExtra(jg, "Parthenos URL", res.getURI());
|
168
|
jg.writeEndArray();
|
169
|
|
170
|
jg.writeEndObject();
|
171
|
jg.close();
|
172
|
return out.toString("UTF-8");
|
173
|
}
|
174
|
protected String getJsonForThing(final Resource res, final String resNameForCatalogue) throws IOException {
|
175
|
JsonFactory jsonFactory = new JsonFactory();
|
176
|
final ByteArrayOutputStream out = new ByteArrayOutputStream();
|
177
|
BufferedOutputStream bos = new BufferedOutputStream(out);
|
178
|
JsonGenerator jg = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
|
179
|
jg.writeStartObject();
|
180
|
writeCommonFields(jg, res, resNameForCatalogue);
|
181
|
|
182
|
jg.writeArrayFieldStart("extras");
|
183
|
addExtra(jg, "system:type", E70_Thing.getLocalName());
|
184
|
//specific class
|
185
|
addExtra(jg, "instance of", resourceReader.findSpecificType(res, E70_Thing).getLocalName());
|
186
|
if (res.getURI().startsWith("http://parthenos.d4science.org")) {
|
187
|
addExtra(jg, "Parthenos URL", res.getURI());
|
188
|
}
|
189
|
//TODO: things include digital objects, software, datasets, schema. Guess we should know what to add here.
|
190
|
jg.writeEndArray();
|
191
|
|
192
|
jg.writeEndObject();
|
193
|
jg.close();
|
194
|
return out.toString("UTF-8");
|
195
|
}
|
196
|
|
197
|
protected void addExtra(final JsonGenerator jg, final String key, final String value) throws IOException {
|
198
|
jg.writeStartObject();
|
199
|
jg.writeStringField("key", key);
|
200
|
jg.writeStringField("value", value);
|
201
|
jg.writeEndObject();
|
202
|
}
|
203
|
|
204
|
protected void writeCommonFields(final JsonGenerator jg, final Resource res, final String resNameForCatalogue) throws IOException {
|
205
|
//end of URI
|
206
|
jg.writeStringField("name", resNameForCatalogue);
|
207
|
//default license
|
208
|
jg.writeStringField("license_id", "notspecified");
|
209
|
String title = resourceReader.getTitle(res);
|
210
|
if (StringUtils.isBlank(title))
|
211
|
title = resNameForCatalogue;
|
212
|
jg.writeStringField("title", title);
|
213
|
//description
|
214
|
jg.writeStringField("notes", resourceReader.getDescription(res));
|
215
|
}
|
216
|
|
217
|
/**
|
218
|
* @param json
|
219
|
* @param resCatName
|
220
|
* @return the uuid assigned by the catalogue to the registered resource
|
221
|
* @throws ParthenosPublisherException
|
222
|
*/
|
223
|
private String doRegister(final String json, final String resCatName) throws ParthenosPublisherException {
|
224
|
//TODO: implement me
|
225
|
log.debug(String.format("Catalogue --> Registering %s : %s", resCatName, json));
|
226
|
//if resCatName is already registered --> delete it and then saves it again
|
227
|
HttpEntity<String> entity = new HttpEntity<String>(json, getHeaders());
|
228
|
try {
|
229
|
URI uri = new URIBuilder(getBaseURL()).addParameter("gcube-token", getApplicationToken()).build();
|
230
|
ResponseEntity<String> res = jrrRestTemplate.<String>exchange(uri, HttpMethod.POST, entity, String.class);
|
231
|
//TODO: I guess response is a json, need to check the format, parse it to get the uuid of the registered resource
|
232
|
String uuid = res.getBody();
|
233
|
return uuid;
|
234
|
} catch (Throwable t) {
|
235
|
throw new ParthenosPublisherException(t);
|
236
|
}
|
237
|
}
|
238
|
|
239
|
private HttpHeaders getHeaders() {
|
240
|
HttpHeaders headers = new HttpHeaders();
|
241
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
242
|
headers.setAccept(Lists.newArrayList(MediaType.APPLICATION_JSON));
|
243
|
//headers.set("gcube-token", getApplicationToken() );
|
244
|
return headers;
|
245
|
}
|
246
|
|
247
|
|
248
|
public String getBaseURL() {
|
249
|
return baseURL;
|
250
|
}
|
251
|
|
252
|
public void setBaseURL(final String baseURL) {
|
253
|
this.baseURL = baseURL;
|
254
|
}
|
255
|
|
256
|
public String getApplicationToken() {
|
257
|
return applicationToken;
|
258
|
}
|
259
|
|
260
|
public void setApplicationToken(final String applicationToken) {
|
261
|
this.applicationToken = applicationToken;
|
262
|
}
|
263
|
|
264
|
public ResourceReader getResourceReader() {
|
265
|
return resourceReader;
|
266
|
}
|
267
|
|
268
|
public void setResourceReader(final ResourceReader resourceReader) {
|
269
|
this.resourceReader = resourceReader;
|
270
|
}
|
271
|
}
|