1
|
package eu.dnetlib.repo.manager.service;
|
2
|
|
3
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
4
|
import com.google.gson.JsonArray;
|
5
|
import com.google.gson.JsonElement;
|
6
|
import com.google.gson.JsonObject;
|
7
|
import eu.dnetlib.api.functionality.ValidatorServiceException;
|
8
|
import eu.dnetlib.domain.data.Repository;
|
9
|
import eu.dnetlib.domain.data.RepositoryInterface;
|
10
|
import eu.dnetlib.domain.enabling.Vocabulary;
|
11
|
import eu.dnetlib.domain.functionality.validator.JobForValidation;
|
12
|
import eu.dnetlib.repo.manager.domain.*;
|
13
|
import eu.dnetlib.repo.manager.domain.dto.Role;
|
14
|
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
|
15
|
import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService;
|
16
|
import eu.dnetlib.repo.manager.service.security.AaiUserRoleService;
|
17
|
import eu.dnetlib.repo.manager.service.security.AuthoritiesUpdater;
|
18
|
import eu.dnetlib.repo.manager.utils.Converter;
|
19
|
import gr.uoa.di.driver.enabling.vocabulary.VocabularyLoader;
|
20
|
import org.apache.commons.codec.digest.DigestUtils;
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.json.JSONArray;
|
23
|
import org.json.JSONException;
|
24
|
import org.json.JSONObject;
|
25
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
26
|
import org.springframework.beans.factory.annotation.Autowired;
|
27
|
import org.springframework.beans.factory.annotation.Value;
|
28
|
import org.springframework.context.annotation.Lazy;
|
29
|
import org.springframework.core.ParameterizedTypeReference;
|
30
|
import org.springframework.http.*;
|
31
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
32
|
import org.springframework.security.core.Authentication;
|
33
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
34
|
import org.springframework.security.core.context.SecurityContextHolder;
|
35
|
import org.springframework.stereotype.Service;
|
36
|
import org.springframework.web.client.HttpClientErrorException;
|
37
|
import org.springframework.web.client.RestClientException;
|
38
|
import org.springframework.web.client.RestTemplate;
|
39
|
import org.springframework.web.util.UriComponents;
|
40
|
import org.springframework.web.util.UriComponentsBuilder;
|
41
|
|
42
|
import javax.annotation.PostConstruct;
|
43
|
import java.io.IOException;
|
44
|
import java.sql.Timestamp;
|
45
|
import java.util.*;
|
46
|
import java.util.concurrent.ConcurrentHashMap;
|
47
|
import java.util.stream.Collectors;
|
48
|
|
49
|
@Service("repositoryService")
|
50
|
public class RepositoryServiceImpl implements RepositoryService {
|
51
|
|
52
|
private static final Logger LOGGER = Logger.getLogger(RepositoryServiceImpl.class);
|
53
|
|
54
|
private final AaiUserRoleService aaiUserRoleService;
|
55
|
private final AaiRegistryService registryCalls;
|
56
|
private final AuthoritiesUpdater authoritiesUpdater;
|
57
|
private final RestTemplate restTemplate;
|
58
|
private final VocabularyLoader vocabularyLoader;
|
59
|
private final PiWikService piWikService;
|
60
|
private final EmailUtils emailUtils;
|
61
|
private final ValidatorService validatorService;
|
62
|
|
63
|
@Value("${api.baseAddress}")
|
64
|
private String baseAddress;
|
65
|
|
66
|
@Value("${services.repo-manager.adminEmail}")
|
67
|
private String adminEmail;
|
68
|
|
69
|
@Value("${services.repomanager.usageStatisticsDiagramsBaseURL}")
|
70
|
private String usageStatisticsDiagramsBaseURL;
|
71
|
|
72
|
@Value("${services.repomanager.usageStatisticsNumbersBaseURL}")
|
73
|
private String usageStatisticsNumbersBaseURL;
|
74
|
|
75
|
|
76
|
private static final Map<String, List<String>> dataSourceClass = new HashMap<>();
|
77
|
private static final Map<String, String> invertedDataSourceClass = new HashMap<>();
|
78
|
|
79
|
|
80
|
private final String[] vocabularyNames = {"dnet:countries", "dnet:datasource_typologies", "dnet:compatibilityLevel"};
|
81
|
private final Map<String, Vocabulary> vocabularyMap = new ConcurrentHashMap<>();
|
82
|
private final Map<String, String> countriesMap = new HashMap<>();
|
83
|
private final Map<String, String> inverseCountriesMap = new HashMap<>();
|
84
|
|
85
|
private HttpHeaders httpHeaders;
|
86
|
|
87
|
@Autowired
|
88
|
public RepositoryServiceImpl(AaiUserRoleService aaiUserRoleService,
|
89
|
AaiRegistryService registryCalls,
|
90
|
AuthoritiesUpdater authoritiesUpdater,
|
91
|
VocabularyLoader vocabularyLoader, EmailUtils emailUtils,
|
92
|
RestTemplate restTemplate,
|
93
|
@Lazy ValidatorService validatorService,
|
94
|
@Lazy PiWikService piWikService) {
|
95
|
this.aaiUserRoleService = aaiUserRoleService;
|
96
|
this.registryCalls = registryCalls;
|
97
|
this.authoritiesUpdater = authoritiesUpdater;
|
98
|
this.vocabularyLoader = vocabularyLoader;
|
99
|
this.piWikService = piWikService;
|
100
|
this.emailUtils = emailUtils;
|
101
|
this.validatorService = validatorService;
|
102
|
this.restTemplate = restTemplate;
|
103
|
}
|
104
|
|
105
|
private String sendEmail() {
|
106
|
OIDCAuthenticationToken authenticationToken = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
|
107
|
return authenticationToken.getUserInfo().getEmail();
|
108
|
}
|
109
|
|
110
|
|
111
|
@PostConstruct
|
112
|
private void init() {
|
113
|
LOGGER.debug("Initialization method of repository api!");
|
114
|
LOGGER.debug("Updated version!");
|
115
|
|
116
|
dataSourceClass.put("opendoar", Arrays.asList("pubsrepository::institutional", "pubsrepository::thematic", "pubsrepository::unknown", "pubsrepository::mock"));
|
117
|
dataSourceClass.put("re3data", Collections.singletonList("datarepository::unknown"));
|
118
|
dataSourceClass.put("journal", Collections.singletonList("pubsrepository::journal"));
|
119
|
dataSourceClass.put("aggregator", Arrays.asList("aggregator::pubsrepository::institutional", "aggregator::pubsrepository::journals", "aggregator::datarepository", "aggregator::pubsrepository::unknown"));
|
120
|
|
121
|
invertedDataSourceClass.put("pubsrepository::institutional", "opendoar");
|
122
|
invertedDataSourceClass.put("pubsrepository::thematic", "opendoar");
|
123
|
invertedDataSourceClass.put("pubsrepository::unknown", "opendoar");
|
124
|
invertedDataSourceClass.put("pubsrepository::mock", "opendoar");
|
125
|
invertedDataSourceClass.put("datarepository::unknown", "re3data");
|
126
|
invertedDataSourceClass.put("pubsrepository::journal", "journal");
|
127
|
invertedDataSourceClass.put("aggregator::pubsrepository::institutional", "aggregator");
|
128
|
invertedDataSourceClass.put("aggregator::pubsrepository::journals", "aggregator");
|
129
|
invertedDataSourceClass.put("aggregator::datarepository", "aggregator");
|
130
|
invertedDataSourceClass.put("aggregator::pubsrepository::unknown", "aggregator");
|
131
|
|
132
|
|
133
|
httpHeaders = new HttpHeaders();
|
134
|
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
|
135
|
|
136
|
for (String vocName : vocabularyNames) {
|
137
|
vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT));
|
138
|
}
|
139
|
|
140
|
Country[] countries = getCountries();
|
141
|
for (Country c : countries) {
|
142
|
countriesMap.put(c.getName(), c.getCode());
|
143
|
inverseCountriesMap.put(c.getCode(), c.getName());
|
144
|
}
|
145
|
|
146
|
|
147
|
}
|
148
|
|
149
|
@Override
|
150
|
public Country[] getCountries() {
|
151
|
UriComponents uriComponents = UriComponentsBuilder
|
152
|
.fromHttpUrl(baseAddress + "/ds/countries")
|
153
|
.build().encode();
|
154
|
return restTemplate.getForObject(uriComponents.toUri(), Country[].class);
|
155
|
}
|
156
|
|
157
|
|
158
|
@Override
|
159
|
public List<RepositorySnippet> getRepositoriesByCountry(String country,
|
160
|
String mode,
|
161
|
Boolean managed) throws JSONException, IOException {
|
162
|
|
163
|
LOGGER.debug("Getting repositories by country!");
|
164
|
int page = 0;
|
165
|
int size = 100;
|
166
|
List<RepositorySnippet> resultSet = new ArrayList<>();
|
167
|
ObjectMapper mapper = new ObjectMapper();
|
168
|
|
169
|
String filterKey = "UNKNOWN";
|
170
|
if (mode.equalsIgnoreCase("opendoar"))
|
171
|
filterKey = "openaire____::opendoar";
|
172
|
else if (mode.equalsIgnoreCase("re3data"))
|
173
|
filterKey = "openaire____::re3data";
|
174
|
|
175
|
|
176
|
LOGGER.debug("Country code equals : " + country);
|
177
|
LOGGER.debug("Filter mode equals : " + filterKey);
|
178
|
|
179
|
UriComponents uriComponents = searchSnipperDatasource(String.valueOf(page), String.valueOf(size));
|
180
|
RequestFilter requestFilter = new RequestFilter();
|
181
|
requestFilter.setCountry(country);
|
182
|
requestFilter.setCollectedfrom(filterKey);
|
183
|
|
184
|
try {
|
185
|
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
|
186
|
JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
|
187
|
while (jsonArray.length() > 0) {
|
188
|
resultSet.addAll(mapper.readValue(String.valueOf(jsonArray),
|
189
|
mapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class)));
|
190
|
page += 1;
|
191
|
uriComponents = searchSnipperDatasource(String.valueOf(page), String.valueOf(size));
|
192
|
rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
|
193
|
jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
|
194
|
}
|
195
|
return resultSet;
|
196
|
} catch (Exception e) {
|
197
|
LOGGER.debug("Exception on getRepositoriesByCountry", e);
|
198
|
// emailUtils.reportException(e);
|
199
|
throw e;
|
200
|
}
|
201
|
}
|
202
|
|
203
|
public List<RepositorySnippet> searchRegisteredRepositories(String country, String typology, String englishName,
|
204
|
String officialName, String requestSortBy, String order, int page, int pageSize) throws Exception {
|
205
|
|
206
|
LOGGER.debug("Searching registered repositories");
|
207
|
|
208
|
List<RepositorySnippet> resultSet = new ArrayList<>();
|
209
|
ObjectMapper mapper = new ObjectMapper();
|
210
|
|
211
|
UriComponents uriComponents = searchRegisteredDatasource(requestSortBy, order, Integer.toString(page), Integer.toString(pageSize));
|
212
|
|
213
|
RequestFilter requestFilter = new RequestFilter();
|
214
|
requestFilter.setCountry(country);
|
215
|
requestFilter.setTypology(typology);
|
216
|
requestFilter.setOfficialname(officialName);
|
217
|
requestFilter.setEnglishname(englishName);
|
218
|
|
219
|
try {
|
220
|
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
|
221
|
JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
|
222
|
|
223
|
resultSet.addAll(mapper.readValue(String.valueOf(jsonArray), mapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class)));
|
224
|
|
225
|
return resultSet;
|
226
|
} catch (Exception e) {
|
227
|
LOGGER.error("Error searching registered datasources", e);
|
228
|
throw e;
|
229
|
}
|
230
|
}
|
231
|
|
232
|
private Repository updateRepositoryInfo(Repository r) throws JSONException {
|
233
|
|
234
|
/*
|
235
|
* from datasource class
|
236
|
* we get the datasource type form the inverted map
|
237
|
* */
|
238
|
r.setDatasourceType(getRepositoryType(r.getDatasourceClass()));
|
239
|
r.setInterfaces(this.getRepositoryInterface(r.getId()));
|
240
|
r.setPiwikInfo(piWikService.getPiwikSiteForRepo(r.getId()));
|
241
|
r.setCountryName(getCountryName(r.getCountryCode()));
|
242
|
return r;
|
243
|
}
|
244
|
|
245
|
|
246
|
private Collection<Repository> getRepositoriesByMode(String mode, List<Repository> rs) {
|
247
|
|
248
|
List<Repository> reps = new ArrayList<>();
|
249
|
for (Repository r : rs) {
|
250
|
if (r.getCollectedFrom() != null && r.getCollectedFrom().equals(mode))
|
251
|
reps.add(r);
|
252
|
|
253
|
}
|
254
|
return reps;
|
255
|
}
|
256
|
|
257
|
@Override
|
258
|
public List<Repository> getRepositoriesOfUser(String userEmail,
|
259
|
String page,
|
260
|
String size) throws JSONException {
|
261
|
|
262
|
LOGGER.debug("Retreiving repositories of user : " + userEmail);
|
263
|
UriComponents uriComponents = searchDatasource(page, size);
|
264
|
RequestFilter requestFilter = new RequestFilter();
|
265
|
// requestFilter.setRegisteredby(userEmail);
|
266
|
|
267
|
List<String> repoIds = getRepoIdsFromUserRoles(userEmail);
|
268
|
List<Repository> repos = new ArrayList<>();
|
269
|
|
270
|
try {
|
271
|
for (String repoId : repoIds) {
|
272
|
requestFilter.setId(repoId);
|
273
|
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
|
274
|
|
275
|
repos.addAll(Converter.jsonToRepositoryList(new JSONObject(rs)));
|
276
|
}
|
277
|
} catch (Exception e) {
|
278
|
LOGGER.debug("Exception on getRepositoriesOfUser", e);
|
279
|
emailUtils.reportException(e);
|
280
|
throw e;
|
281
|
}
|
282
|
|
283
|
for (Repository r : repos)
|
284
|
r.setPiwikInfo(piWikService.getPiwikSiteForRepo(r.getId()));
|
285
|
return repos;
|
286
|
}
|
287
|
|
288
|
@Override
|
289
|
public List<RepositorySnippet> getRepositoriesSnippetOfUser(String userEmail, String page, String size) throws IOException, JSONException {
|
290
|
|
291
|
// TODO: Step 3 - For each repo from the previous step call findByRepositoryId piwik_site to get the full info of repo
|
292
|
List<String> repoIds = getRepoIdsFromUserRoles(userEmail);
|
293
|
|
294
|
List<RepositorySnippet> resultSet = new ArrayList<>();
|
295
|
ObjectMapper mapper = new ObjectMapper();
|
296
|
|
297
|
// here page should be 0
|
298
|
UriComponents uriComponents = searchSnipperDatasource(page, size);
|
299
|
RequestFilter requestFilter = new RequestFilter();
|
300
|
|
301
|
try {
|
302
|
for (String repoId : repoIds) {
|
303
|
requestFilter.setId(repoId);
|
304
|
|
305
|
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
|
306
|
JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
|
307
|
resultSet.addAll(mapper.readValue(String.valueOf(jsonArray),
|
308
|
mapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class)));
|
309
|
}
|
310
|
} catch (Exception e) {
|
311
|
LOGGER.debug("Exception on getRepositoriesSnippetOfUser", e);
|
312
|
throw e;
|
313
|
}
|
314
|
|
315
|
LOGGER.debug("resultSet:" + resultSet);
|
316
|
resultSet.parallelStream().forEach(repositorySnippet -> {
|
317
|
repositorySnippet.setPiwikInfo(piWikService.getPiwikSiteForRepo(repositorySnippet.getId()));
|
318
|
});
|
319
|
return resultSet;
|
320
|
}
|
321
|
|
322
|
@Override
|
323
|
public RepositorySnippet getRepositorySnippetById(String id) throws JSONException, ResourceNotFoundException {
|
324
|
|
325
|
LOGGER.debug("Retreiving repositories with id : " + id);
|
326
|
RepositorySnippet repo = null;
|
327
|
UriComponents uriComponents = searchSnipperDatasource("0", "100");
|
328
|
RequestFilter requestFilter = new RequestFilter();
|
329
|
requestFilter.setId(id);
|
330
|
|
331
|
try {
|
332
|
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
|
333
|
JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
|
334
|
|
335
|
if (jsonArray.length() == 0)
|
336
|
throw new ResourceNotFoundException();
|
337
|
|
338
|
repo = Converter.jsonToRepositorySnippetObject(jsonArray.getJSONObject(0));
|
339
|
return repo;
|
340
|
} catch (JSONException e) {
|
341
|
LOGGER.debug("Exception on getRepositoryById", e);
|
342
|
emailUtils.reportException(e);
|
343
|
throw e;
|
344
|
}
|
345
|
|
346
|
}
|
347
|
|
348
|
@Override
|
349
|
public Repository getRepositoryById(String id) throws JSONException, ResourceNotFoundException {
|
350
|
|
351
|
LOGGER.debug("Retreiving repositories with id : " + id);
|
352
|
Repository repo = null;
|
353
|
UriComponents uriComponents = searchDatasource("0", "100");
|
354
|
RequestFilter requestFilter = new RequestFilter();
|
355
|
requestFilter.setId(id);
|
356
|
|
357
|
try {
|
358
|
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
|
359
|
JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
|
360
|
|
361
|
if (jsonArray.length() == 0)
|
362
|
throw new ResourceNotFoundException();
|
363
|
|
364
|
repo = Converter.jsonToRepositoryObject(jsonArray.getJSONObject(0));
|
365
|
return updateRepositoryInfo(repo);
|
366
|
} catch (JSONException e) {
|
367
|
LOGGER.debug("Exception on getRepositoryById", e);
|
368
|
emailUtils.reportException(e);
|
369
|
throw e;
|
370
|
}
|
371
|
|
372
|
}
|
373
|
|
374
|
|
375
|
@Override
|
376
|
public List<AggregationDetails> getRepositoryAggregations(String id, int from, int size) throws JSONException {
|
377
|
|
378
|
LOGGER.debug("Retreiving aggregations for repository with id : " + id);
|
379
|
UriComponents uriComponents = searchDatasource(from + "", size + "");
|
380
|
RequestFilter requestFilter = new RequestFilter();
|
381
|
requestFilter.setId(id);
|
382
|
|
383
|
List<AggregationDetails> aggregationHistory = new ArrayList<>();
|
384
|
|
385
|
try {
|
386
|
long start = System.currentTimeMillis();
|
387
|
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
|
388
|
long end = System.currentTimeMillis();
|
389
|
|
390
|
System.out.println("Aggregations request through rest template took " + (end - start) + "ms");
|
391
|
JSONObject repository = new JSONObject(rs);
|
392
|
|
393
|
if (repository.getJSONArray("datasourceInfo").length() == 0)
|
394
|
return aggregationHistory;
|
395
|
|
396
|
start = System.currentTimeMillis();
|
397
|
aggregationHistory.addAll(Converter.getAggregationHistoryFromJson(repository.getJSONArray("datasourceInfo").getJSONObject(0)));
|
398
|
end = System.currentTimeMillis();
|
399
|
|
400
|
System.out.println("Getting aggregations history from json " + (end - start) + "ms");
|
401
|
return aggregationHistory.size() == 0 ? aggregationHistory : aggregationHistory.stream()
|
402
|
.sorted(Comparator.comparing(AggregationDetails::getDate).reversed())
|
403
|
.limit(size)
|
404
|
.collect(Collectors.toList());
|
405
|
} catch (JSONException e) {
|
406
|
LOGGER.debug("Exception on getRepositoryAggregations", e);
|
407
|
emailUtils.reportException(e);
|
408
|
throw e;
|
409
|
}
|
410
|
|
411
|
}
|
412
|
|
413
|
@Override
|
414
|
public Map<String, List<AggregationDetails>> getRepositoryAggregationsByYear(String id) throws JSONException {
|
415
|
LOGGER.debug("Retreiving aggregations (by year) for repository with id : " + id);
|
416
|
UriComponents uriComponents = searchDatasource("0", "100");
|
417
|
RequestFilter requestFilter = new RequestFilter();
|
418
|
requestFilter.setId(id);
|
419
|
|
420
|
List<AggregationDetails> aggregationHistory = new ArrayList<>();
|
421
|
Map<String, List<AggregationDetails>> aggregationByYear = new HashMap<>();
|
422
|
try {
|
423
|
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
|
424
|
JSONObject repository = new JSONObject(rs);
|
425
|
|
426
|
if (repository.getJSONArray("datasourceInfo").length() == 0)
|
427
|
return aggregationByYear;
|
428
|
|
429
|
aggregationHistory.addAll(Converter.getAggregationHistoryFromJson(repository.getJSONArray("datasourceInfo").getJSONObject(0)));
|
430
|
return aggregationHistory.size() == 0 ? aggregationByYear : createYearMap(aggregationHistory);
|
431
|
|
432
|
} catch (JSONException e) {
|
433
|
LOGGER.debug("Exception on getRepositoryAggregations", e);
|
434
|
emailUtils.reportException(e);
|
435
|
throw e;
|
436
|
}
|
437
|
}
|
438
|
|
439
|
private Map<String, List<AggregationDetails>> createYearMap(List<AggregationDetails> aggregationHistory) {
|
440
|
Map<String, List<AggregationDetails>> aggregationByYear;
|
441
|
aggregationHistory = aggregationHistory.stream()
|
442
|
.sorted(Comparator.comparing(AggregationDetails::getDate).reversed())
|
443
|
.collect(Collectors.toList());
|
444
|
|
445
|
return aggregationHistory.stream()
|
446
|
.collect(Collectors.groupingBy(AggregationDetails::getYear));
|
447
|
}
|
448
|
|
449
|
|
450
|
@Override
|
451
|
public List<Repository> getRepositoriesByName(String name,
|
452
|
String page,
|
453
|
String size) throws JSONException {
|
454
|
|
455
|
LOGGER.debug("Retreiving repositories with official name : " + name);
|
456
|
UriComponents uriComponents = searchDatasource("0", "100");
|
457
|
RequestFilter requestFilter = new RequestFilter();
|
458
|
requestFilter.setOfficialname(name);
|
459
|
|
460
|
try {
|
461
|
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
|
462
|
List<Repository> repos = Converter.jsonToRepositoryList(new JSONObject(rs));
|
463
|
for (Repository r : repos)
|
464
|
updateRepositoryInfo(r);
|
465
|
return repos;
|
466
|
} catch (Exception e) {
|
467
|
LOGGER.debug("Exception on getRepositoriesByName", e);
|
468
|
emailUtils.reportException(e);
|
469
|
throw e;
|
470
|
}
|
471
|
|
472
|
}
|
473
|
|
474
|
@Override
|
475
|
public List<RepositoryInterface> getRepositoryInterface(String id) throws JSONException {
|
476
|
|
477
|
UriComponents uriComponents = UriComponentsBuilder
|
478
|
.fromHttpUrl(baseAddress + "/ds/api/")
|
479
|
.path("/{id}")
|
480
|
.build().expand(id).encode();
|
481
|
|
482
|
try {
|
483
|
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
484
|
return Converter.jsonToRepositoryInterfaceList(new JSONObject(rs));
|
485
|
} catch (Exception e) {
|
486
|
LOGGER.debug("Exception on getRepositoryInterface", e);
|
487
|
emailUtils.reportException(e);
|
488
|
throw e;
|
489
|
}
|
490
|
|
491
|
}
|
492
|
|
493
|
@Override
|
494
|
public Repository addRepository(String datatype,
|
495
|
Repository repository) throws Exception {
|
496
|
|
497
|
LOGGER.debug("storing " + datatype + " repository with id: " + repository.getId());
|
498
|
|
499
|
repository.setCountryCode(countriesMap.get(repository.getCountryName()));
|
500
|
repository.setActivationId(UUID.randomUUID().toString());
|
501
|
repository.setCollectedFrom("infrastruct_::openaire");
|
502
|
|
503
|
if (datatype.equals("journal")) {
|
504
|
repository.setId("openaire____::issn" + repository.getIssn());
|
505
|
repository.setNamespacePrefix("issn" + repository.getIssn());
|
506
|
this.storeRepository(repository, SecurityContextHolder.getContext().getAuthentication());
|
507
|
} else if (datatype.equals("aggregator")) {
|
508
|
repository.setId("openaire____::" + com.unboundid.util.Base64.encode(repository.getOfficialName()));
|
509
|
repository.setNamespacePrefix(DigestUtils.md5Hex(repository.getOfficialName()).substring(0, 12));
|
510
|
this.storeRepository(repository, SecurityContextHolder.getContext().getAuthentication());
|
511
|
} else {
|
512
|
this.latentUpdate(repository, SecurityContextHolder.getContext().getAuthentication());
|
513
|
}
|
514
|
|
515
|
// TODO: move the following code elsewhere (creation and assignment of role to user) ??
|
516
|
// Create new role
|
517
|
String newRoleName = aaiUserRoleService.getRoleIdByRepoId(repository.getId());
|
518
|
Role newRole = new Role(newRoleName, repository.getOfficialName());
|
519
|
Integer couId = null;
|
520
|
try {
|
521
|
couId = registryCalls.createRole(newRole);
|
522
|
} catch (HttpClientErrorException e) {
|
523
|
couId = registryCalls.getCouId(newRoleName);
|
524
|
if (couId == null) {
|
525
|
LOGGER.error(String.format("Could not create role '%s'", newRoleName), e);
|
526
|
}
|
527
|
} catch (Exception e) {
|
528
|
LOGGER.error(String.format("Could not create role '%s'", newRoleName), e);
|
529
|
throw e;
|
530
|
}
|
531
|
|
532
|
// Assign new role to the user that created it
|
533
|
Integer coPersonId = registryCalls.getCoPersonIdByIdentifier();
|
534
|
if (couId != null) {
|
535
|
Integer role = registryCalls.getRoleId(coPersonId, couId);
|
536
|
try {
|
537
|
registryCalls.assignMemberRole(coPersonId, couId, role);
|
538
|
|
539
|
// Add role to current user authorities
|
540
|
authoritiesUpdater.addRole(aaiUserRoleService.convertRepoIdToAuthority(repository.getId()));
|
541
|
} catch (Exception e) {
|
542
|
LOGGER.debug("Exception on assign role to user during add repository", e);
|
543
|
throw e;
|
544
|
}
|
545
|
|
546
|
}
|
547
|
|
548
|
|
549
|
return repository;
|
550
|
}
|
551
|
|
552
|
/* update method acting as add -> send email with registration topic/body*/
|
553
|
private Repository latentUpdate(Repository repository, Authentication authentication) throws Exception {
|
554
|
UriComponents uriComponents = UriComponentsBuilder
|
555
|
.fromHttpUrl(baseAddress + "/ds/update/")
|
556
|
.build()
|
557
|
.encode();
|
558
|
|
559
|
try {
|
560
|
String json_repository = Converter.repositoryObjectToJson(repository);
|
561
|
LOGGER.debug("JSON to add(update) -> " + json_repository);
|
562
|
|
563
|
HttpEntity<String> httpEntity = new HttpEntity<String>(json_repository, httpHeaders);
|
564
|
ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class);
|
565
|
|
566
|
if (responseEntity.getStatusCode().equals(HttpStatus.OK)) {
|
567
|
// emailUtils.sendUserRegistrationEmail(repository, authentication);
|
568
|
// emailUtils.sendAdminRegistrationEmail(repository, authentication);
|
569
|
} else
|
570
|
LOGGER.debug(responseEntity.getBody().toString());
|
571
|
|
572
|
return repository;
|
573
|
} catch (Exception e) {
|
574
|
LOGGER.debug("Exception on updateRepository", e);
|
575
|
emailUtils.reportException(e);
|
576
|
throw e;
|
577
|
}
|
578
|
|
579
|
|
580
|
}
|
581
|
|
582
|
@Override
|
583
|
public Repository updateRepository(Repository repository, Authentication authentication) throws Exception {
|
584
|
UriComponents uriComponents = UriComponentsBuilder
|
585
|
.fromHttpUrl(baseAddress + "/ds/update/")
|
586
|
.build()
|
587
|
.encode();
|
588
|
|
589
|
try {
|
590
|
String json_repository = Converter.repositoryObjectToJson(repository);
|
591
|
|
592
|
LOGGER.debug("JSON to update -> " + json_repository);
|
593
|
|
594
|
HttpEntity<String> httpEntity = new HttpEntity<String>(json_repository, httpHeaders);
|
595
|
ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity
|
596
|
, ResponseEntity.class);
|
597
|
|
598
|
if (responseEntity.getStatusCode().equals(HttpStatus.OK)) {
|
599
|
emailUtils.sendUserUpdateRepositoryInfoEmail(repository, authentication);
|
600
|
emailUtils.sendAdminUpdateRepositoryInfoEmail(repository, authentication);
|
601
|
} else
|
602
|
LOGGER.debug(responseEntity.getBody().toString());
|
603
|
|
604
|
return repository;
|
605
|
} catch (Exception e) {
|
606
|
LOGGER.debug("Exception on updateRepository", e);
|
607
|
emailUtils.reportException(e);
|
608
|
throw e;
|
609
|
}
|
610
|
}
|
611
|
|
612
|
private void storeRepository(Repository repository, Authentication authentication) throws Exception {
|
613
|
|
614
|
Date utilDate = new Date();
|
615
|
Timestamp date = new Timestamp(utilDate.getTime());
|
616
|
repository.setDateOfCollection(date);
|
617
|
repository.setAggregator("OPENAIRE");
|
618
|
repository.setCountryCode(countriesMap.get(repository.getCountryName()));
|
619
|
|
620
|
UriComponents uriComponents = UriComponentsBuilder
|
621
|
.fromHttpUrl(baseAddress + "/ds/add/")
|
622
|
.build()
|
623
|
.encode();
|
624
|
String json_repository = Converter.repositoryObjectToJson(repository);
|
625
|
HttpEntity<String> httpEntity = new HttpEntity<String>(json_repository, httpHeaders);
|
626
|
ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class);
|
627
|
|
628
|
if (responseEntity.getStatusCode().equals(HttpStatus.OK)) {
|
629
|
emailUtils.sendUserRegistrationEmail(repository, authentication);
|
630
|
emailUtils.sendAdminRegistrationEmail(repository, authentication);
|
631
|
} else {
|
632
|
LOGGER.debug(responseEntity.getBody().toString());
|
633
|
}
|
634
|
}
|
635
|
|
636
|
@Override
|
637
|
public void deleteRepositoryInterface(String id,
|
638
|
String registeredBy) {
|
639
|
UriComponents uriComponents = UriComponentsBuilder
|
640
|
.fromHttpUrl(baseAddress + "/ds/api/")
|
641
|
.path("/{id}")
|
642
|
.build().expand(id).encode();
|
643
|
LOGGER.debug(uriComponents.toUri());
|
644
|
restTemplate.delete(uriComponents.toUri());
|
645
|
}
|
646
|
|
647
|
@Override
|
648
|
public RepositoryInterface addRepositoryInterface(String datatype,
|
649
|
String repoId,
|
650
|
String registeredBy,
|
651
|
String comment, RepositoryInterface repositoryInterface) throws Exception {
|
652
|
try {
|
653
|
Repository e = this.getRepositoryById(repoId);
|
654
|
repositoryInterface = createRepositoryInterface(e, repositoryInterface, datatype);
|
655
|
String json_interface = Converter.repositoryInterfaceObjectToJson(e, repositoryInterface);
|
656
|
|
657
|
UriComponents uriComponents = UriComponentsBuilder
|
658
|
.fromHttpUrl(baseAddress + "/ds/api/add/")
|
659
|
.build()
|
660
|
.encode();
|
661
|
|
662
|
HttpEntity<String> httpEntity = new HttpEntity<>(json_interface, httpHeaders);
|
663
|
|
664
|
restTemplate.postForObject(uriComponents.toUri(), httpEntity, String.class);
|
665
|
|
666
|
emailUtils.sendAdminRegisterInterfaceEmail(e, comment, repositoryInterface, SecurityContextHolder.getContext().getAuthentication());
|
667
|
emailUtils.sendUserRegisterInterfaceEmail(e, comment, repositoryInterface, SecurityContextHolder.getContext().getAuthentication());
|
668
|
|
669
|
submitInterfaceValidation(e, registeredBy, repositoryInterface, false);
|
670
|
|
671
|
return repositoryInterface;
|
672
|
|
673
|
} catch (Exception e) {
|
674
|
LOGGER.error("Exception on addRepositoryInterface", e);
|
675
|
emailUtils.reportException(e);
|
676
|
throw e;
|
677
|
}
|
678
|
}
|
679
|
|
680
|
@Override
|
681
|
public RepositoryInterface updateRepositoryInterface(String repoId,
|
682
|
String registeredBy,
|
683
|
String comment, RepositoryInterface repositoryInterface) throws Exception {
|
684
|
|
685
|
this.updateBaseUrl(repoId, repositoryInterface.getId(), repositoryInterface.getBaseUrl());
|
686
|
this.updateCompliance(repoId, repositoryInterface.getId(), repositoryInterface.getCompliance());
|
687
|
this.updateValidationSet(repoId, repositoryInterface.getId(), repositoryInterface.getAccessSet());
|
688
|
|
689
|
Repository e = this.getRepositoryById(repoId);
|
690
|
emailUtils.sendAdminUpdateInterfaceEmail(e, comment, repositoryInterface, SecurityContextHolder.getContext().getAuthentication());
|
691
|
emailUtils.sendUserUpdateInterfaceEmail(e, comment, repositoryInterface, SecurityContextHolder.getContext().getAuthentication());
|
692
|
submitInterfaceValidation(getRepositoryById(repoId), registeredBy, repositoryInterface, true);
|
693
|
|
694
|
return repositoryInterface;
|
695
|
}
|
696
|
|
697
|
private void submitInterfaceValidation(Repository repo, String userEmail, RepositoryInterface iFace, boolean updateExisting) throws ValidatorServiceException {
|
698
|
JobForValidation job = new JobForValidation();
|
699
|
|
700
|
job.setActivationId(UUID.randomUUID().toString());
|
701
|
job.setAdminEmails(Collections.singletonList(this.adminEmail));
|
702
|
job.setBaseUrl(iFace.getBaseUrl());
|
703
|
job.setDatasourceId(repo.getId());
|
704
|
job.setDesiredCompatibilityLevel(iFace.getDesiredCompatibilityLevel());
|
705
|
job.setInterfaceId(iFace.getId());
|
706
|
job.setOfficialName(repo.getOfficialName());
|
707
|
job.setRepoType(repo.getDatasourceType());
|
708
|
job.setUserEmail(userEmail);
|
709
|
job.setValidationSet((iFace.getAccessSet().isEmpty() ? "none" : iFace.getAccessSet()));
|
710
|
job.setRecords(-1);
|
711
|
job.setRegistration(!updateExisting);
|
712
|
job.setUpdateExisting(updateExisting);
|
713
|
|
714
|
this.validatorService.submitJobForValidation(job);
|
715
|
}
|
716
|
|
717
|
private RepositoryInterface createRepositoryInterface(Repository repo, RepositoryInterface iFace, String datatype) {
|
718
|
|
719
|
iFace.setContentDescription("metadata");
|
720
|
iFace.setCompliance("UNKNOWN");
|
721
|
|
722
|
if (datatype.equals("re3data"))
|
723
|
iFace.setAccessFormat("oai_datacite");
|
724
|
else
|
725
|
iFace.setAccessFormat("oai_dc");
|
726
|
|
727
|
|
728
|
if (repo.getDatasourceClass() != null && !repo.getDatasourceClass().isEmpty())
|
729
|
iFace.setTypology(repo.getDatasourceClass());
|
730
|
else if (datatype.equalsIgnoreCase("journal"))
|
731
|
iFace.setTypology("pubsrepository::journal");
|
732
|
else if (datatype.equalsIgnoreCase("aggregator"))
|
733
|
iFace.setTypology("aggregator::pubsrepository::unknown");
|
734
|
else if (datatype.equalsIgnoreCase("opendoar"))
|
735
|
iFace.setTypology("pubsrepository::unknown");
|
736
|
else if (datatype.equalsIgnoreCase("re3data"))
|
737
|
iFace.setTypology("datarepository::unknown");
|
738
|
|
739
|
iFace.setRemovable(true);
|
740
|
iFace.setAccessProtocol("oai");
|
741
|
iFace.setMetadataIdentifierPath("//*[local-name()='header']/*[local-name()='identifier']");
|
742
|
iFace.setId("api_________::" + repo.getId() + "::" + UUID.randomUUID().toString().substring(0, 8));
|
743
|
if (iFace.getAccessSet() == null || iFace.getAccessSet().isEmpty()) {
|
744
|
LOGGER.debug("set is empty: " + iFace.getAccessSet());
|
745
|
iFace.removeAccessSet();
|
746
|
iFace.setAccessSet("none");
|
747
|
}
|
748
|
return iFace;
|
749
|
}
|
750
|
|
751
|
@Override
|
752
|
public List<String> getDnetCountries() {
|
753
|
LOGGER.debug("Getting dnet-countries!");
|
754
|
return Converter.readFile("countries.txt");
|
755
|
}
|
756
|
|
757
|
@Override
|
758
|
public List<String> getTypologies() {
|
759
|
return Converter.readFile("typologies.txt");
|
760
|
}
|
761
|
|
762
|
@Override
|
763
|
public List<Timezone> getTimezones() {
|
764
|
List<String> timezones = Converter.readFile("timezones.txt");
|
765
|
return Converter.toTimezones(timezones);
|
766
|
}
|
767
|
|
768
|
@Override
|
769
|
public List<String> getUrlsOfUserRepos(String userEmail,
|
770
|
String page,
|
771
|
String size) {
|
772
|
UriComponents uriComponents = UriComponentsBuilder
|
773
|
.fromHttpUrl(baseAddress + "/api/baseurl/")
|
774
|
.path("/{page}/{size}")
|
775
|
.build().expand(page, size).encode();
|
776
|
|
777
|
try {
|
778
|
RequestFilter requestFilter = new RequestFilter();
|
779
|
requestFilter.setRegisteredby(userEmail);
|
780
|
return Arrays.asList(restTemplate.postForObject(uriComponents.toUri(), requestFilter, String[].class));
|
781
|
} catch (Exception e) {
|
782
|
LOGGER.debug("Exception on addRepositoryInterface", e);
|
783
|
emailUtils.reportException(e);
|
784
|
throw e;
|
785
|
}
|
786
|
}
|
787
|
|
788
|
@Override
|
789
|
public List<String> getDatasourceVocabularies(String mode) {
|
790
|
|
791
|
List<String> resultSet = new ArrayList<>();
|
792
|
for (Map.Entry<String, String> entry : this.getVocabulary("dnet:datasource_typologies").getAsMap().entrySet()) {
|
793
|
if (mode.equalsIgnoreCase("aggregator")) {
|
794
|
if (entry.getKey().contains("aggregator"))
|
795
|
resultSet.add(entry.getValue());
|
796
|
} else if (mode.equalsIgnoreCase("journal")) {
|
797
|
if (entry.getKey().contains("journal"))
|
798
|
resultSet.add(entry.getValue());
|
799
|
} else if (mode.equalsIgnoreCase("opendoar")) {
|
800
|
if (entry.getKey().contains("pubsrepository"))
|
801
|
resultSet.add(entry.getValue());
|
802
|
} else if (mode.equalsIgnoreCase("re3data")) {
|
803
|
if (entry.getKey().contains("datarepository"))
|
804
|
resultSet.add(entry.getValue());
|
805
|
}
|
806
|
}
|
807
|
|
808
|
|
809
|
return resultSet;
|
810
|
}
|
811
|
|
812
|
private Vocabulary getVocabulary(String vocName) {
|
813
|
|
814
|
if (!vocabularyMap.containsKey(vocName)) {
|
815
|
vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT));
|
816
|
}
|
817
|
return vocabularyMap.get(vocName);
|
818
|
}
|
819
|
|
820
|
|
821
|
@Override
|
822
|
public Map<String, String> getCompatibilityClasses(String mode) {
|
823
|
|
824
|
LOGGER.debug("Getting compatibility classes for mode: " + mode);
|
825
|
Map<String, String> retMap = new HashMap<String, String>();
|
826
|
|
827
|
Map<String, String> compatibilityClasses = this.getVocabulary("dnet:compatibilityLevel").getAsMap();
|
828
|
boolean foundData = false;
|
829
|
for (Map.Entry<String, String> entry : compatibilityClasses.entrySet()) {
|
830
|
if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_ALL))
|
831
|
return compatibilityClasses;
|
832
|
else if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_RE3DATA)) {
|
833
|
if (entry.getKey().matches("^openaire[1-9].0_data$")) {
|
834
|
retMap.put(entry.getKey(), entry.getValue());
|
835
|
foundData = true;
|
836
|
}
|
837
|
} else {
|
838
|
if (entry.getKey().matches("^openaire[1-9].0$") || entry.getKey().equals("driver"))
|
839
|
retMap.put(entry.getKey(), entry.getValue());
|
840
|
}
|
841
|
}
|
842
|
|
843
|
//TODO TO BE REMOVED WHEN VOCABULARIES ARE UPDATED
|
844
|
if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_RE3DATA) && !foundData)
|
845
|
retMap.put("openaire2.0_data", "OpenAIRE Data (funded, referenced datasets)");
|
846
|
|
847
|
return retMap;
|
848
|
}
|
849
|
|
850
|
@Override
|
851
|
public Map<String, String> getDatasourceClasses(String mode) {
|
852
|
|
853
|
LOGGER.debug("Getting datasource classes for mode: " + mode);
|
854
|
|
855
|
Map<String, String> retMap = new HashMap<String, String>();
|
856
|
|
857
|
for (Map.Entry<String, String> entry : this.getVocabulary("dnet:datasource_typologies").getAsMap().entrySet()) {
|
858
|
if (mode.equalsIgnoreCase("aggregator")) {
|
859
|
if (entry.getKey().contains("aggregator"))
|
860
|
retMap.put(entry.getKey(), entry.getValue());
|
861
|
} else if (mode.equalsIgnoreCase("journal")) {
|
862
|
if (entry.getKey().contains("journal"))
|
863
|
retMap.put(entry.getKey(), entry.getValue());
|
864
|
} else if (mode.equalsIgnoreCase("opendoar")) {
|
865
|
if (entry.getKey().contains("pubsrepository"))
|
866
|
retMap.put(entry.getKey(), entry.getValue());
|
867
|
} else if (mode.equalsIgnoreCase("re3data")) {
|
868
|
if (entry.getKey().contains("datarepository"))
|
869
|
retMap.put(entry.getKey(), entry.getValue());
|
870
|
}
|
871
|
}
|
872
|
return filterResults(retMap, mode);
|
873
|
|
874
|
}
|
875
|
|
876
|
private Map<String, String> filterResults(Map<String, String> map, String mode) {
|
877
|
|
878
|
HashMap<String, String> filteredMap = new HashMap<>();
|
879
|
for (String key : map.keySet())
|
880
|
if (dataSourceClass.get(mode).contains(key))
|
881
|
filteredMap.put(key, map.get(key));
|
882
|
|
883
|
return filteredMap;
|
884
|
}
|
885
|
|
886
|
@Override
|
887
|
public String getCountryName(String countryCode) {
|
888
|
return inverseCountriesMap.get(countryCode);
|
889
|
}
|
890
|
|
891
|
@Override
|
892
|
public MetricsInfo getMetricsInfoForRepository(String repoId) throws RepositoryServiceException {
|
893
|
try {
|
894
|
|
895
|
MetricsInfo metricsInfo = new MetricsInfo();
|
896
|
metricsInfo.setDiagramsBaseURL(this.usageStatisticsDiagramsBaseURL);
|
897
|
metricsInfo.setMetricsNumbers(getMetricsNumbers(getOpenAIREId(repoId)));
|
898
|
return metricsInfo;
|
899
|
|
900
|
} catch (Exception e) {
|
901
|
LOGGER.error("Error while getting metrics info for repository: ", e);
|
902
|
emailUtils.reportException(e);
|
903
|
throw new RepositoryServiceException("General error", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
|
904
|
}
|
905
|
}
|
906
|
|
907
|
@Override
|
908
|
public Map<String, String> getListLatestUpdate(String mode) throws JSONException {
|
909
|
if (mode.equals("opendoar"))
|
910
|
return Collections.singletonMap("lastCollectionDate", getRepositoryInterface("openaire____::" + mode).get(0).getLastCollectionDate());
|
911
|
else
|
912
|
/*
|
913
|
* first api of re3data has null value on collection date
|
914
|
* */
|
915
|
return Collections.singletonMap("lastCollectionDate", getRepositoryInterface("openaire____::" + mode).get(1).getLastCollectionDate());
|
916
|
}
|
917
|
|
918
|
private void updateValidationSet(String repositoryId, String repositoryInterfaceId, String validationSet) throws Exception {
|
919
|
UriComponents uriComponents = UriComponentsBuilder
|
920
|
.fromHttpUrl(baseAddress + "/ds/api/oaiset")
|
921
|
.queryParam("dsId", repositoryId)
|
922
|
.queryParam("apiId", repositoryInterfaceId)
|
923
|
.queryParam("oaiSet", validationSet)
|
924
|
.build().encode();
|
925
|
restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, null, ResponseEntity.class);
|
926
|
|
927
|
}
|
928
|
|
929
|
|
930
|
private void updateBaseUrl(String repositoryId, String repositoryInterfaceId, String baseUrl) {
|
931
|
UriComponents uriComponents = UriComponentsBuilder
|
932
|
.fromHttpUrl(baseAddress + "/ds/api/baseurl")
|
933
|
.queryParam("dsId", repositoryId)
|
934
|
.queryParam("apiId", repositoryInterfaceId)
|
935
|
.queryParam("baseUrl", baseUrl)
|
936
|
.build().encode();
|
937
|
restTemplate.postForObject(uriComponents.toUri(), null, String.class);
|
938
|
}
|
939
|
|
940
|
private void updateCompliance(String repositoryId, String repositoryInterfaceId, String compliance) {
|
941
|
UriComponents uriComponents = UriComponentsBuilder
|
942
|
.fromHttpUrl(baseAddress + "/ds/api/compliance")
|
943
|
.queryParam("dsId", repositoryId)
|
944
|
.queryParam("apiId", repositoryInterfaceId)
|
945
|
.queryParam("compliance", compliance)
|
946
|
.build().encode();
|
947
|
restTemplate.postForObject(uriComponents.toUri(), null, String.class);
|
948
|
}
|
949
|
|
950
|
private MetricsNumbers getMetricsNumbers(String openAIREID) throws BrokerException {
|
951
|
|
952
|
//build the uri params
|
953
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(this.usageStatisticsNumbersBaseURL + openAIREID + "/clicks");
|
954
|
|
955
|
//create new template engine
|
956
|
RestTemplate template = new RestTemplate();
|
957
|
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
958
|
ResponseEntity<MetricsNumbers> resp;
|
959
|
try {
|
960
|
//communicate with endpoint
|
961
|
resp = template.exchange(
|
962
|
builder.build().encode().toUri(),
|
963
|
HttpMethod.GET,
|
964
|
null,
|
965
|
new ParameterizedTypeReference<MetricsNumbers>() {
|
966
|
});
|
967
|
} catch (RestClientException e) {
|
968
|
LOGGER.debug("Exception on getMetricsNumbers", e);
|
969
|
emailUtils.reportException(e);
|
970
|
throw e;
|
971
|
}
|
972
|
|
973
|
return resp.getBody();
|
974
|
}
|
975
|
|
976
|
private String getOpenAIREId(String repoId) {
|
977
|
|
978
|
if (repoId != null && repoId.contains("::")) {
|
979
|
return repoId.split("::")[0] + "::" + DigestUtils.md5Hex(repoId.split("::")[1]);
|
980
|
}
|
981
|
|
982
|
return null;
|
983
|
}
|
984
|
|
985
|
private UriComponents searchDatasource(String page, String size) {
|
986
|
|
987
|
return UriComponentsBuilder
|
988
|
.fromHttpUrl(baseAddress + "/ds/search/")
|
989
|
.path("/{page}/{size}/")
|
990
|
.queryParam("requestSortBy", "officialname")
|
991
|
.queryParam("order", "ASCENDING")
|
992
|
.build().expand(page, size).encode();
|
993
|
}
|
994
|
|
995
|
private UriComponents searchSnipperDatasource(String page, String size) {
|
996
|
|
997
|
return UriComponentsBuilder
|
998
|
.fromHttpUrl(baseAddress + "/ds/searchsnippet/")
|
999
|
.path("/{page}/{size}/")
|
1000
|
.queryParam("requestSortBy", "officialname")
|
1001
|
.queryParam("order", "ASCENDING")
|
1002
|
.build().expand(page, size).encode();
|
1003
|
}
|
1004
|
|
1005
|
private UriComponents searchRegisteredDatasource(String requestSortBy, String order, String page, String size) {
|
1006
|
|
1007
|
return UriComponentsBuilder
|
1008
|
.fromHttpUrl(baseAddress + "/ds/searchregistered/")
|
1009
|
.path("/{page}/{size}/")
|
1010
|
.queryParam("requestSortBy", requestSortBy)
|
1011
|
.queryParam("order", order)
|
1012
|
.build().expand(page, size).encode();
|
1013
|
}
|
1014
|
|
1015
|
private String getRepositoryType(String typology) {
|
1016
|
return invertedDataSourceClass.get(typology);
|
1017
|
}
|
1018
|
|
1019
|
private List<String> getRoleIdsFromUserRoles(String userEmail) {
|
1020
|
Integer coPersonId = registryCalls.getCoPersonIdByEmail(userEmail);
|
1021
|
JsonArray roles;
|
1022
|
ArrayList<String> roleIds = new ArrayList<>();
|
1023
|
ArrayList<Integer> couIds = new ArrayList<>();
|
1024
|
if (coPersonId != null) {
|
1025
|
roles = registryCalls.getRoles(coPersonId);
|
1026
|
for (JsonElement role : roles) {
|
1027
|
JsonObject object = role.getAsJsonObject();
|
1028
|
if (object.get("CouId") == null) {
|
1029
|
continue;
|
1030
|
}
|
1031
|
couIds.add(object.get("CouId").getAsInt());
|
1032
|
}
|
1033
|
|
1034
|
roleIds.addAll(registryCalls.getCouNames(couIds).values());
|
1035
|
|
1036
|
}
|
1037
|
return roleIds;
|
1038
|
}
|
1039
|
|
1040
|
private List<String> getRepoIdsFromUserRoles(String userEmail) {
|
1041
|
List<String> roleIds = getRoleIdsFromUserRoles(userEmail);
|
1042
|
List<String> repoIds = new ArrayList<>();
|
1043
|
for (String role : roleIds) {
|
1044
|
if (role.contains("datasource")) {
|
1045
|
// TODO: ask if checking for BETA prefix is required
|
1046
|
repoIds.add(role.replaceFirst(".*datasource\\.", "").replace("$", ":"));
|
1047
|
}
|
1048
|
}
|
1049
|
return repoIds;
|
1050
|
}
|
1051
|
}
|