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