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