1
|
package eu.dnetlib.repo.manager.service.controllers;
|
2
|
|
3
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
4
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
5
|
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
|
6
|
import eu.dnetlib.domain.enabling.Vocabulary;
|
7
|
import eu.dnetlib.repo.manager.service.utils.Converter;
|
8
|
import eu.dnetlib.domain.data.Repository;
|
9
|
import eu.dnetlib.domain.data.RepositoryInterface;
|
10
|
import gr.uoa.di.driver.enabling.vocabulary.VocabularyLoader;
|
11
|
import io.swagger.annotations.Api;
|
12
|
import org.apache.log4j.Logger;
|
13
|
import org.json.JSONArray;
|
14
|
import org.json.JSONException;
|
15
|
import org.json.JSONObject;
|
16
|
import org.springframework.beans.factory.annotation.Autowired;
|
17
|
import org.springframework.beans.factory.annotation.Value;
|
18
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
19
|
import org.springframework.stereotype.Component;
|
20
|
import org.springframework.web.bind.annotation.PathVariable;
|
21
|
import org.springframework.web.bind.annotation.RequestBody;
|
22
|
import org.springframework.web.bind.annotation.RequestParam;
|
23
|
import org.springframework.web.client.RestTemplate;
|
24
|
import org.springframework.web.util.UriComponents;
|
25
|
import org.springframework.web.util.UriComponentsBuilder;
|
26
|
import javax.annotation.PostConstruct;
|
27
|
import java.sql.Timestamp;
|
28
|
import java.text.Normalizer;
|
29
|
import java.util.*;
|
30
|
import java.util.concurrent.ConcurrentHashMap;
|
31
|
import eu.dnetlib.repo.manager.shared.*;
|
32
|
|
33
|
@Component
|
34
|
public class RepositoryApiImpl implements RepositoryApi {
|
35
|
|
36
|
@Value("${api.baseAddress}")
|
37
|
private String baseAddress;
|
38
|
|
39
|
private RestTemplate restTemplate = null;
|
40
|
|
41
|
private final String[] vocabularyNames = {"dnet:countries", "dnet:datasource_typologies", "dnet:compatibilityLevel"};
|
42
|
|
43
|
private static final Logger LOGGER = Logger.getLogger(RepositoryApiImpl.class);
|
44
|
|
45
|
@Autowired
|
46
|
private VocabularyLoader vocabularyLoader;
|
47
|
|
48
|
private Map<String, Vocabulary> vocabularyMap = new ConcurrentHashMap<String, Vocabulary>();
|
49
|
|
50
|
private Map<String, String> countriesMap = new HashMap<>();
|
51
|
private Map<String, String> inverseCountriesMap = new HashMap<>();
|
52
|
|
53
|
|
54
|
@PostConstruct
|
55
|
private void init() {
|
56
|
LOGGER.debug("Initialization method of repository api!");
|
57
|
|
58
|
restTemplate = new RestTemplate();
|
59
|
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
60
|
|
61
|
for (String vocName : vocabularyNames) {
|
62
|
vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT));
|
63
|
}
|
64
|
|
65
|
Country[] countries = getCountries();
|
66
|
for (Country c : countries) {
|
67
|
countriesMap.put(c.getName(), c.getCode());
|
68
|
inverseCountriesMap.put(c.getCode(), c.getName());
|
69
|
}
|
70
|
|
71
|
|
72
|
}
|
73
|
|
74
|
@Override
|
75
|
public List<String> testAggregations() throws JSONException {
|
76
|
|
77
|
int page = 0;
|
78
|
int size = 1000;
|
79
|
|
80
|
UriComponents uriComponents = UriComponentsBuilder
|
81
|
.fromHttpUrl(baseAddress + "/ds/list/")
|
82
|
.path("/{page}/{size}/")
|
83
|
.build().expand(page, size).encode();
|
84
|
|
85
|
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
86
|
List<String> ids = new ArrayList<>();
|
87
|
while (!rs.equals("[]")) {
|
88
|
|
89
|
ids.addAll(getIdsWithNonEmptyAggregations(rs));
|
90
|
|
91
|
LOGGER.debug("Checked " + (page + 1) * size + " records!");
|
92
|
|
93
|
page += 1;
|
94
|
uriComponents = UriComponentsBuilder
|
95
|
.fromHttpUrl(baseAddress + "/ds/list/")
|
96
|
.path("/{page}/{size}/")
|
97
|
.build().expand(page, size).encode();
|
98
|
rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
99
|
}
|
100
|
|
101
|
return ids;
|
102
|
}
|
103
|
|
104
|
private List<String> getIdsWithNonEmptyAggregations(String rs) throws JSONException {
|
105
|
|
106
|
JSONArray ids = new JSONArray(rs);
|
107
|
List<String> agg_ids = new ArrayList<>();
|
108
|
for (int i = 0; i < ids.length(); i++) {
|
109
|
String id = ids.getString(i);
|
110
|
Aggregations aggregations = getRepositoryAggregations(id);
|
111
|
if (aggregations.getAggregationHistory() != null)
|
112
|
agg_ids.add(id);
|
113
|
}
|
114
|
return agg_ids;
|
115
|
}
|
116
|
|
117
|
@Override
|
118
|
public Country[] getCountries() {
|
119
|
UriComponents uriComponents = UriComponentsBuilder
|
120
|
.fromHttpUrl(baseAddress + "/ds/countries")
|
121
|
.build().encode();
|
122
|
return restTemplate.getForObject(uriComponents.toUri(), Country[].class);
|
123
|
}
|
124
|
|
125
|
|
126
|
@Override
|
127
|
public List<Repository> getRepositoriesByCountry(@PathVariable("country") String country,
|
128
|
@PathVariable("mode") String mode) throws JSONException {
|
129
|
|
130
|
LOGGER.debug("Getting repositories by country!");
|
131
|
int page = 0;
|
132
|
int size = 100;
|
133
|
|
134
|
String countryCode = countriesMap.get(country);
|
135
|
String filterKey = "UNKNOWN";
|
136
|
if (mode.equalsIgnoreCase("opendoar")) {
|
137
|
filterKey = "openaire____::opendoar";
|
138
|
} else if (mode.equalsIgnoreCase("re3data")) {
|
139
|
filterKey = "openaire____::re3data";
|
140
|
} else if (mode.equalsIgnoreCase("jour_aggr")) {
|
141
|
filterKey = "infrastruct_::openaire";
|
142
|
}
|
143
|
|
144
|
LOGGER.debug("Country code equals : " + countryCode);
|
145
|
LOGGER.debug("Filter mode equals : " + filterKey);
|
146
|
UriComponents uriComponents = UriComponentsBuilder
|
147
|
.fromHttpUrl(baseAddress + "/ds/search/country/")
|
148
|
.path("/{page}/{size}/")
|
149
|
.queryParam("country", countryCode)
|
150
|
.build().expand(page, size).encode();
|
151
|
|
152
|
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
153
|
List<Repository> resultSet = new ArrayList<>();
|
154
|
while (!rs.equals("[]")) {
|
155
|
List<Repository> rep = Converter.jsonToRepositoryList(new JSONArray(rs));
|
156
|
|
157
|
Collection<Repository> repos = this.getRepositoriesByMode(filterKey, rep);
|
158
|
resultSet.addAll(repos);
|
159
|
|
160
|
page += 1;
|
161
|
uriComponents = UriComponentsBuilder
|
162
|
.fromHttpUrl(baseAddress + "/ds/search/country/")
|
163
|
.path("/{page}/{size}/")
|
164
|
.queryParam("country", countryCode)
|
165
|
.build().expand(page, size).encode();
|
166
|
rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
167
|
}
|
168
|
for (Repository r : resultSet)
|
169
|
r.setCountryName(getCountryName(r.getCountryCode()));
|
170
|
return resultSet;
|
171
|
}
|
172
|
|
173
|
|
174
|
private Collection<Repository> getRepositoriesByMode(String mode, List<Repository> rs) {
|
175
|
|
176
|
List<Repository> reps = new ArrayList<>();
|
177
|
for (Repository r : rs) {
|
178
|
if (r.getCollectedFrom().equals(mode))
|
179
|
reps.add(r);
|
180
|
}
|
181
|
return reps;
|
182
|
}
|
183
|
|
184
|
@Override
|
185
|
public List<Repository> getRepositoriesOfUser(@PathVariable("userEmail") String userEmail,
|
186
|
@PathVariable("page") String page,
|
187
|
@PathVariable("size") String size) throws JSONException {
|
188
|
|
189
|
LOGGER.debug("Retreiving repositories of user : " + userEmail );
|
190
|
UriComponents uriComponents = UriComponentsBuilder
|
191
|
.fromHttpUrl(baseAddress + "/ds/search/registeredby/")
|
192
|
.path("/{page}/{size}/")
|
193
|
.queryParam("registeredBy", userEmail)
|
194
|
.build().expand(page, size).encode();
|
195
|
|
196
|
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
197
|
List<Repository> repos = Converter.jsonToRepositoryList(new JSONArray(rs));
|
198
|
for (Repository r : repos){
|
199
|
// r.setInterfaces(this.getRepositoryInterface(r.getId()));
|
200
|
r.setCountryName(getCountryName(r.getCountryCode()));
|
201
|
}
|
202
|
|
203
|
return repos;
|
204
|
}
|
205
|
|
206
|
@Override
|
207
|
public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException {
|
208
|
|
209
|
UriComponents uriComponents = UriComponentsBuilder
|
210
|
.fromHttpUrl(baseAddress + "/ds/get/")
|
211
|
.path("/{id}/")
|
212
|
.build().expand(id).encode();
|
213
|
|
214
|
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
215
|
Repository repo = Converter.jsonToRepositoryObject(new JSONObject(rs));
|
216
|
//repo.setInterfaces(this.getRepositoryInterface(repo.getId()));
|
217
|
if (repo != null)
|
218
|
repo.setCountryName(getCountryName(repo.getCountryCode()));
|
219
|
return repo;
|
220
|
}
|
221
|
|
222
|
|
223
|
@Override
|
224
|
public Aggregations getRepositoryAggregations(@PathVariable("id") String id) throws JSONException {
|
225
|
|
226
|
UriComponents uriComponents = UriComponentsBuilder
|
227
|
.fromHttpUrl(baseAddress + "/ds/get/")
|
228
|
.path("/{id}/")
|
229
|
.build().expand(id).encode();
|
230
|
|
231
|
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
232
|
JSONObject repository = new JSONObject(rs);
|
233
|
|
234
|
Aggregations aggregations = new Aggregations();
|
235
|
|
236
|
try {
|
237
|
aggregations.setAggregationHistory(Converter.getAggregationHistoryFromJson(repository));
|
238
|
aggregations.setLastCollection(Converter.getLastCollectionFromJson(repository));
|
239
|
aggregations.setLastTransformation(Converter.getLastTransformationFromJson(repository));
|
240
|
return aggregations;
|
241
|
} catch (JSONException e) {
|
242
|
LOGGER.debug("JSON aggregation exception ", e);
|
243
|
throw e;
|
244
|
}
|
245
|
|
246
|
}
|
247
|
|
248
|
@Override
|
249
|
public List<Repository> getRepositoriesByName(@PathVariable("name") String name,
|
250
|
@PathVariable("page") String page,
|
251
|
@PathVariable("size") String size) throws JSONException {
|
252
|
UriComponents uriComponents = UriComponentsBuilder
|
253
|
.fromHttpUrl(baseAddress + "/ds/search/name/")
|
254
|
.path("/{page}/{size}")
|
255
|
.queryParam("name", name)
|
256
|
.build().expand(page, size).encode();
|
257
|
|
258
|
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
259
|
List<Repository> repos = Converter.jsonToRepositoryList(new JSONArray(rs));
|
260
|
for (Repository r : repos){
|
261
|
// r.setInterfaces(this.getRepositoryInterface(r.getId()));
|
262
|
r.setCountryName(getCountryName(r.getCountryCode()));
|
263
|
}
|
264
|
return repos;
|
265
|
}
|
266
|
|
267
|
@Override
|
268
|
public List<RepositoryInterface> getRepositoryInterface(@PathVariable("id") String id) throws JSONException {
|
269
|
|
270
|
UriComponents uriComponents = UriComponentsBuilder
|
271
|
.fromHttpUrl(baseAddress + "/ds/api/")
|
272
|
.path("/{id}/")
|
273
|
.build().expand(id).encode();
|
274
|
|
275
|
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
276
|
return Converter.jsonToRepositoryInterfaceList(new JSONArray(rs));
|
277
|
}
|
278
|
|
279
|
@Override
|
280
|
public void addRepository(@RequestBody String params) throws Exception {
|
281
|
|
282
|
JSONObject json_params = new JSONObject(params);
|
283
|
String datatype = json_params.getString("datatype");
|
284
|
String json_repository = json_params.getString("repository");
|
285
|
Repository repository = null;
|
286
|
ObjectMapper mapper = new ObjectMapper();
|
287
|
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
288
|
try {
|
289
|
LOGGER.debug("repository -> " + json_repository);
|
290
|
repository = mapper.readValue(json_repository, Repository.class);
|
291
|
} catch (Exception e) {
|
292
|
LOGGER.debug("Error parsing repository ", e);
|
293
|
throw e;
|
294
|
}
|
295
|
|
296
|
repository = this.setRepositoryFeatures(datatype,repository);
|
297
|
|
298
|
LOGGER.debug("storing " + datatype + " repository with id: " + repository.getId());
|
299
|
if (!datatype.equalsIgnoreCase("opendoar") && !datatype.equalsIgnoreCase("re3data")) {
|
300
|
if (datatype.equalsIgnoreCase("journal") || datatype.equalsIgnoreCase("aggregator")) {
|
301
|
LOGGER.debug("looking if " + datatype + " " + repository.getOfficialName() + " is already in datasources");
|
302
|
if (getRepositoryById(repository.getId()) != null) {
|
303
|
String retMessage = datatype + " '" + repository.getOfficialName() + "' is already in datasources.";
|
304
|
repository.getInterfaces().clear();
|
305
|
LOGGER.debug(retMessage);
|
306
|
} else {
|
307
|
LOGGER.debug(datatype + " " + repository.getOfficialName() + " is not in datasources. Inserting..");
|
308
|
this.storeRepository(repository);
|
309
|
}
|
310
|
}
|
311
|
} else {
|
312
|
this.updateRegisteredByValue(repository.getId(),repository.getRegisteredBy());
|
313
|
}
|
314
|
|
315
|
LOGGER.debug("Inserting Interfaces");
|
316
|
Iterator var11 = repository.getInterfaces().iterator();
|
317
|
|
318
|
while (var11.hasNext()) {
|
319
|
RepositoryInterface iFace = (RepositoryInterface) var11.next();
|
320
|
if (!iFace.getBaseUrl().isEmpty() && !iFace.getDesiredCompatibilityLevel().isEmpty()) {
|
321
|
if (iFace.getId() != null && !iFace.getId().isEmpty()) {
|
322
|
LOGGER.debug("updating iface..");
|
323
|
this.updateInterface(datatype,iFace);
|
324
|
LOGGER.debug("updated successfully");
|
325
|
} else {
|
326
|
LOGGER.debug("adding new iface..");
|
327
|
this.registerRepositoryInterface(repository.getId(),iFace,datatype);
|
328
|
}
|
329
|
}
|
330
|
}
|
331
|
}
|
332
|
|
333
|
private void updateRegisteredByValue(String id, String registeredBy) {
|
334
|
|
335
|
LOGGER.debug("Updating registered by value with : " + registeredBy );
|
336
|
UriComponents uriComponents = UriComponentsBuilder
|
337
|
.fromHttpUrl(baseAddress + "/ds/registeredby/")
|
338
|
.queryParam("dsId",id)
|
339
|
.queryParam("registeredBy", registeredBy)
|
340
|
.build()
|
341
|
.encode();
|
342
|
|
343
|
restTemplate.postForObject(uriComponents.toUri(), null,String.class);
|
344
|
}
|
345
|
|
346
|
private Repository setRepositoryFeatures(String datatype, Repository repository) {
|
347
|
|
348
|
//TODO update map
|
349
|
repository.setCountryCode(countriesMap.get(repository.getCountryName()));
|
350
|
|
351
|
repository.setActivationId(UUID.randomUUID().toString());
|
352
|
// repo.setRegisteredBy((String) session.get(LocalVocabularies.loggedInField));
|
353
|
|
354
|
if (datatype.equals("opendoar") || datatype.equals("re3data")) {
|
355
|
repository.setProvenanceActionClass("sysimport:crosswalk:entityregistry");
|
356
|
} else if (datatype.equals("journal")) {
|
357
|
repository.setProvenanceActionClass("user:insert");
|
358
|
repository.setCollectedFrom("infrastruct_::openaire");
|
359
|
if (repository.getIssn() != null && repository.getIssn().length() == 0)
|
360
|
repository.setIssn(com.unboundid.util.Base64.encode(repository.getOfficialName()).substring(0, 8));
|
361
|
repository.setId("openaire____::issn" + repository.getIssn());
|
362
|
repository.setNamespacePrefix("issn" + repository.getIssn());
|
363
|
} else if (datatype.equals("aggregator")) {
|
364
|
repository.setProvenanceActionClass("user:insert");
|
365
|
repository.setCollectedFrom("infrastruct_::openaire");
|
366
|
repository.setId("openaire____::" + com.unboundid.util.Base64.encode(repository.getOfficialName()));
|
367
|
repository.setNamespacePrefix(Normalizer.normalize(repository.getOfficialName().toLowerCase().replace(" ", "_"), Normalizer.Form.NFD).replaceAll("[^a-zA-Z0-9]", ""));
|
368
|
if (repository.getNamespacePrefix().length() > 12) {
|
369
|
repository.setNamespacePrefix(repository.getNamespacePrefix().substring(0, 12));
|
370
|
} else {
|
371
|
while (repository.getNamespacePrefix().length() < 12)
|
372
|
repository.setNamespacePrefix(repository.getNamespacePrefix().concat("_"));
|
373
|
}
|
374
|
}
|
375
|
return repository;
|
376
|
}
|
377
|
|
378
|
private void updateInterface(String datatype,RepositoryInterface iFace) {
|
379
|
//TODO call update base url
|
380
|
//((DatasourceManagerService) this.dmService.getService()).updateBaseUrl(repo.getId(), iFace.getId(), iFace.getBaseUrl());
|
381
|
|
382
|
if (!iFace.getAccessSet().isEmpty()) {
|
383
|
LOGGER.debug("set not empty: " + iFace.getAccessSet());
|
384
|
//TODO call update method for access params
|
385
|
// ((DatasourceManagerService) this.dmService.getService()).updateAccessParam(repo.getId(), iFace.getId(), "set", iFace.getAccessSet(), false);
|
386
|
} else {
|
387
|
//TODO call deleteAccessParamOrExtraField
|
388
|
//((DatasourceManagerService) this.dmService.getService()).deleteAccessParamOrExtraField(repo.getId(), iFace.getId(), "set");
|
389
|
}
|
390
|
//TODO update content description
|
391
|
//((DatasourceManagerService) this.dmService.getService()).updateContentDescription(repo.getId(), iFace.getId(), "metadata");
|
392
|
if (datatype.equals("re3data")) {
|
393
|
//TODO call update access params
|
394
|
// ((DatasourceManagerService) this.dmService.getService()).updateAccessParam(repo.getId(), iFace.getId(), "format", "oai_datacite", false);
|
395
|
iFace.setAccessFormat("oai_datacite");
|
396
|
} else {
|
397
|
//TODO call update access params
|
398
|
//((DatasourceManagerService) this.dmService.getService()).updateAccessParam(repo.getId(), iFace.getId(), "format", "oai_dc", false);
|
399
|
iFace.setAccessFormat("oai_dc");
|
400
|
}
|
401
|
|
402
|
}
|
403
|
|
404
|
private RepositoryInterface createRepositoryInterface(Repository repo, RepositoryInterface iFace, String datatype) {
|
405
|
|
406
|
iFace.setContentDescription("metadata");
|
407
|
iFace.setCompliance("UNKNOWN");
|
408
|
if (datatype.equals("re3data")) {
|
409
|
iFace.setAccessFormat("oai_datacite");
|
410
|
} else {
|
411
|
iFace.setAccessFormat("oai_dc");
|
412
|
}
|
413
|
|
414
|
if (repo.getDatasourceClass() != null && !repo.getDatasourceClass().isEmpty()) {
|
415
|
iFace.setTypology(repo.getDatasourceClass());
|
416
|
} else if (datatype.equalsIgnoreCase("journal")) {
|
417
|
iFace.setTypology("pubsrepository::journal");
|
418
|
} else if (datatype.equalsIgnoreCase("aggregator")) {
|
419
|
iFace.setTypology("aggregator::pubsrepository::unknown");
|
420
|
} else if (datatype.equalsIgnoreCase("opendoar")) {
|
421
|
iFace.setTypology("pubsrepository::unknown");
|
422
|
} else if (datatype.equalsIgnoreCase("re3data")) {
|
423
|
iFace.setTypology("datarepository::unknown");
|
424
|
}
|
425
|
|
426
|
iFace.setRemovable(true);
|
427
|
iFace.setAccessProtocol("oai");
|
428
|
iFace.setMetadataIdentifierPath("//*[local-name()='header']/*[local-name()='identifier']");
|
429
|
iFace.setId("api_________::" + repo.getId() + "::" + UUID.randomUUID().toString().substring(0, 8));
|
430
|
if (iFace.getAccessSet().isEmpty()) {
|
431
|
LOGGER.debug("set is empty: " + iFace.getAccessSet());
|
432
|
iFace.removeAccessSet();
|
433
|
}
|
434
|
return iFace;
|
435
|
}
|
436
|
|
437
|
private void storeRepository(Repository repository) throws JSONException {
|
438
|
|
439
|
Date utilDate = new Date();
|
440
|
Timestamp date = new Timestamp(utilDate.getTime());
|
441
|
repository.setDateOfCollection(date);
|
442
|
repository.setAggregator("OPENAIRE");
|
443
|
|
444
|
UriComponents uriComponents = UriComponentsBuilder
|
445
|
.fromHttpUrl(baseAddress + "/ds/add/")
|
446
|
.build()
|
447
|
.encode();
|
448
|
try {
|
449
|
LOGGER.debug("Repository equals : " + Converter.repositoryObjectToJson(repository));
|
450
|
}catch (Exception e){
|
451
|
LOGGER.debug("JSON exception ", e);
|
452
|
throw e;
|
453
|
}
|
454
|
restTemplate.postForObject(uriComponents.toUri(),Converter.repositoryObjectToJson(repository),String.class);
|
455
|
}
|
456
|
|
457
|
@Override
|
458
|
public void deleteRepositoryInterface(@PathVariable("id") String id){
|
459
|
UriComponents uriComponents = UriComponentsBuilder
|
460
|
.fromHttpUrl(baseAddress + "/ds/api/")
|
461
|
.path("/{id}/")
|
462
|
.build().expand(id).encode();
|
463
|
restTemplate.delete(uriComponents.toUri());
|
464
|
}
|
465
|
|
466
|
@Override
|
467
|
public RepositoryInterface addRepositoryInterface(@RequestBody String params) throws JSONException {
|
468
|
|
469
|
JSONObject json_params = new JSONObject(params);
|
470
|
String datatype = json_params.getString("datatype");
|
471
|
String repoId = json_params.getString("repoId");
|
472
|
String json_iFace = json_params.getString("iFace");
|
473
|
RepositoryInterface iFace = null;
|
474
|
|
475
|
ObjectMapper mapper = new ObjectMapper();
|
476
|
try {
|
477
|
iFace = mapper.readValue(json_iFace, RepositoryInterface.class);
|
478
|
} catch (Exception e1) {
|
479
|
LOGGER.debug("Error parsing repository interface ", e1);
|
480
|
}
|
481
|
|
482
|
return registerRepositoryInterface(repoId,iFace,datatype);
|
483
|
}
|
484
|
|
485
|
private RepositoryInterface registerRepositoryInterface(String repoId, RepositoryInterface iFace, String datatype) {
|
486
|
Repository e = null;
|
487
|
try {
|
488
|
e = this.getRepositoryById(repoId);
|
489
|
iFace = createRepositoryInterface(e,iFace,datatype);
|
490
|
|
491
|
LOGGER.debug("iFace equals : " + Converter.repositoryInterfaceObjectToJson(e,iFace));
|
492
|
|
493
|
UriComponents uriComponents = UriComponentsBuilder
|
494
|
.fromHttpUrl(baseAddress + "/ds/api/add/")
|
495
|
.build()
|
496
|
.encode();
|
497
|
String rs = restTemplate.postForObject(uriComponents.toUri(), Converter.repositoryInterfaceObjectToJson(e,iFace),String.class);
|
498
|
return Converter.jsonToRepositoryInterfaceObject(new JSONArray(rs).getJSONObject(0));
|
499
|
|
500
|
} catch (JSONException e1) {
|
501
|
LOGGER.debug("Error parsing json ",e1);
|
502
|
}
|
503
|
return null;
|
504
|
}
|
505
|
|
506
|
@Override
|
507
|
public List<String> getDnetCountries() {
|
508
|
LOGGER.debug("Getting dnet-countries!");
|
509
|
return Converter.readFile("countries.txt");
|
510
|
}
|
511
|
|
512
|
@Override
|
513
|
public List<String> getTypologies() {
|
514
|
return Converter.readFile("typologies.txt");
|
515
|
}
|
516
|
|
517
|
@Override
|
518
|
public List<Timezone> getTimezones() {
|
519
|
List<String> timezones = Converter.readFile("timezones.txt");
|
520
|
return Converter.toTimezones(timezones);
|
521
|
}
|
522
|
|
523
|
@Override
|
524
|
public String updateManagedStatus(@RequestParam(value = "id") String id,
|
525
|
@RequestParam(value = "managed") String managed) {
|
526
|
|
527
|
UriComponents uriComponents = UriComponentsBuilder
|
528
|
.fromHttpUrl(baseAddress + "/ds/manage/")
|
529
|
.queryParam("id",id)
|
530
|
.queryParam("managed",managed)
|
531
|
.build().encode();
|
532
|
|
533
|
return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
|
534
|
}
|
535
|
|
536
|
@Override
|
537
|
public String updateEnglishName(@RequestParam(value = "id") String id,
|
538
|
@RequestParam(value = "englishname") String englishName) {
|
539
|
|
540
|
UriComponents uriComponents = UriComponentsBuilder
|
541
|
.fromHttpUrl(baseAddress + "/ds/englishname/")
|
542
|
.queryParam("dsId",id)
|
543
|
.queryParam("englishname",englishName)
|
544
|
.build().encode();
|
545
|
return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
|
546
|
|
547
|
|
548
|
}
|
549
|
|
550
|
@Override
|
551
|
public String updateLatitude(@RequestParam(value = "id") String id,
|
552
|
@RequestParam(value = "managed") String latitude) {
|
553
|
|
554
|
UriComponents uriComponents = UriComponentsBuilder
|
555
|
.fromHttpUrl(baseAddress + "/ds/latitude/")
|
556
|
.queryParam("dsId",id)
|
557
|
.queryParam("latitude",latitude)
|
558
|
.build().encode();
|
559
|
return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
|
560
|
}
|
561
|
|
562
|
@Override
|
563
|
public String updateLongitude(@RequestParam(value = "id") String id,
|
564
|
@RequestParam(value = "longitude") String longitude) {
|
565
|
|
566
|
UriComponents uriComponents = UriComponentsBuilder
|
567
|
.fromHttpUrl(baseAddress + "/ds/longitude/")
|
568
|
.queryParam("dsId",id)
|
569
|
.queryParam("longitude",longitude)
|
570
|
.build().encode();
|
571
|
return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
|
572
|
}
|
573
|
|
574
|
@Override
|
575
|
public String updateOfficialName(@RequestParam(value = "id") String id,
|
576
|
@RequestParam(value = "officialname") String officialname) {
|
577
|
|
578
|
UriComponents uriComponents = UriComponentsBuilder
|
579
|
.fromHttpUrl(baseAddress + "/ds/officialname/")
|
580
|
.queryParam("dsId",id)
|
581
|
.queryParam("officialname",officialname)
|
582
|
.build().encode();
|
583
|
return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
|
584
|
}
|
585
|
|
586
|
@Override
|
587
|
public String updateTimezone(@RequestParam(value = "id") String id,
|
588
|
@RequestParam(value = "timezone") String timezone) {
|
589
|
UriComponents uriComponents = UriComponentsBuilder
|
590
|
.fromHttpUrl(baseAddress + "/ds/timezone")
|
591
|
.queryParam("dsId",id)
|
592
|
.queryParam("timezone",timezone)
|
593
|
.build().encode();
|
594
|
return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
|
595
|
}
|
596
|
|
597
|
@Override
|
598
|
public String updateTypology(@RequestParam(value = "id") String id,
|
599
|
@RequestParam(value = "typology") String typology) {
|
600
|
UriComponents uriComponents = UriComponentsBuilder
|
601
|
.fromHttpUrl(baseAddress + "/ds/typology")
|
602
|
.queryParam("dsId",id)
|
603
|
.queryParam("typology",typology)
|
604
|
.build().encode();
|
605
|
return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
|
606
|
}
|
607
|
|
608
|
@Override
|
609
|
public String updateLogoUrl(@RequestParam(value = "id") String id,
|
610
|
@RequestParam(value = "logoUrl") String logoUrl) {
|
611
|
UriComponents uriComponents = UriComponentsBuilder
|
612
|
.fromHttpUrl(baseAddress + "/ds/logourl")
|
613
|
.queryParam("dsId",id)
|
614
|
.queryParam("logourl",logoUrl)
|
615
|
.build().encode();
|
616
|
return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
|
617
|
}
|
618
|
|
619
|
@Override
|
620
|
public String updatePlatform(String id, String platform) {
|
621
|
UriComponents uriComponents = UriComponentsBuilder
|
622
|
.fromHttpUrl(baseAddress + "/ds/platform")
|
623
|
.queryParam("dsId",id)
|
624
|
.queryParam("platform",platform)
|
625
|
.build().encode();
|
626
|
return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
|
627
|
}
|
628
|
|
629
|
@Override
|
630
|
public List<String> getUrlsOfUserRepos(@PathVariable("user_email") String user_email,
|
631
|
@PathVariable("page") String page,
|
632
|
@PathVariable("size") String size) throws JSONException {
|
633
|
UriComponents uriComponents = UriComponentsBuilder
|
634
|
.fromHttpUrl(baseAddress + "/api/baseurl/")
|
635
|
.path("/{page}/{size}")
|
636
|
.queryParam("userEmail",user_email)
|
637
|
.build().expand(page,size).encode();
|
638
|
return Arrays.asList(restTemplate.getForObject(uriComponents.toUri(), String[].class));
|
639
|
}
|
640
|
|
641
|
@Override
|
642
|
public List<String> getDatasourceVocabularies(@PathVariable("mode") String mode) {
|
643
|
|
644
|
List<String> resultSet = new ArrayList<>();
|
645
|
for (Map.Entry<String, String> entry : this.getVocabulary("dnet:datasource_typologies").getAsMap().entrySet()) {
|
646
|
if (mode.equalsIgnoreCase("aggregator")) {
|
647
|
if (entry.getKey().contains("aggregator"))
|
648
|
resultSet.add(entry.getValue());
|
649
|
} else if (mode.equalsIgnoreCase("journal")) {
|
650
|
if (entry.getKey().contains("journal"))
|
651
|
resultSet.add(entry.getValue());
|
652
|
} else if (mode.equalsIgnoreCase("opendoar")) {
|
653
|
if (entry.getKey().contains("pubsrepository"))
|
654
|
resultSet.add(entry.getValue());
|
655
|
} else if (mode.equalsIgnoreCase("re3data")) {
|
656
|
if (entry.getKey().contains("datarepository"))
|
657
|
resultSet.add(entry.getValue());
|
658
|
}
|
659
|
}
|
660
|
|
661
|
|
662
|
return resultSet;
|
663
|
}
|
664
|
|
665
|
private Vocabulary getVocabulary(String vocName) {
|
666
|
|
667
|
if (!vocabularyMap.containsKey(vocName)) {
|
668
|
vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT));
|
669
|
}
|
670
|
return vocabularyMap.get(vocName);
|
671
|
}
|
672
|
|
673
|
|
674
|
@Override
|
675
|
public Map<String, String> getCompatibilityClasses(@PathVariable("mode") String mode) {
|
676
|
|
677
|
LOGGER.debug("Getting compatibility classes for mode: " + mode);
|
678
|
Map<String, String> retMap = new HashMap<String, String>();
|
679
|
|
680
|
Map<String, String> compatibilityClasses = this.getVocabulary("dnet:compatibilityLevel").getAsMap();
|
681
|
boolean foundData = false;
|
682
|
for (Map.Entry<String, String> entry : compatibilityClasses.entrySet()) {
|
683
|
if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_ALL))
|
684
|
return compatibilityClasses;
|
685
|
else if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_RE3DATA)) {
|
686
|
if (entry.getKey().matches("^openaire[1-9].0_data$")) {
|
687
|
retMap.put(entry.getKey(), entry.getValue());
|
688
|
foundData = true;
|
689
|
}
|
690
|
} else {
|
691
|
if (entry.getKey().matches("^openaire[1-9].0$") || entry.getKey().equals("driver"))
|
692
|
retMap.put(entry.getKey(), entry.getValue());
|
693
|
}
|
694
|
}
|
695
|
|
696
|
//TODO TO BE REMOVED WHEN VOCABULARIES ARE UPDATED
|
697
|
if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_RE3DATA) && !foundData)
|
698
|
retMap.put("openaire2.0_data", "OpenAIRE Data (funded, referenced datasets)");
|
699
|
|
700
|
return retMap;
|
701
|
}
|
702
|
|
703
|
@Override
|
704
|
public Map<String, String> getDatasourceClasses(@PathVariable("mode") String mode) {
|
705
|
|
706
|
LOGGER.debug("Getting datasource classes for mode: " + mode);
|
707
|
|
708
|
Map<String, String> retMap = new HashMap<String, String>();
|
709
|
|
710
|
for (Map.Entry<String, String> entry : this.getVocabulary("dnet:datasource_typologies").getAsMap().entrySet()) {
|
711
|
if (mode.equalsIgnoreCase("aggregator")) {
|
712
|
if (entry.getKey().contains("aggregator"))
|
713
|
retMap.put(entry.getKey(), entry.getValue());
|
714
|
} else if (mode.equalsIgnoreCase("journal")) {
|
715
|
if (entry.getKey().contains("journal"))
|
716
|
retMap.put(entry.getKey(), entry.getValue());
|
717
|
} else if (mode.equalsIgnoreCase("opendoar")) {
|
718
|
if (entry.getKey().contains("pubsrepository"))
|
719
|
retMap.put(entry.getKey(), entry.getValue());
|
720
|
} else if (mode.equalsIgnoreCase("re3data")) {
|
721
|
if (entry.getKey().contains("datarepository"))
|
722
|
retMap.put(entry.getKey(), entry.getValue());
|
723
|
}
|
724
|
}
|
725
|
return retMap;
|
726
|
|
727
|
}
|
728
|
|
729
|
@Override
|
730
|
public String getCountryName(String countryCode) {
|
731
|
return inverseCountriesMap.get(countryCode);
|
732
|
}
|
733
|
|
734
|
}
|