1
|
package eu.dnetlib.repo.manager.service.controllers;
|
2
|
|
3
|
import eu.dnetlib.domain.enabling.Vocabulary;
|
4
|
import eu.dnetlib.repo.manager.service.utils.Converter;
|
5
|
import eu.dnetlib.domain.data.Repository;
|
6
|
import eu.dnetlib.domain.data.RepositoryInterface;
|
7
|
import gr.uoa.di.driver.enabling.vocabulary.VocabularyLoader;
|
8
|
import org.apache.log4j.Logger;
|
9
|
import org.json.JSONArray;
|
10
|
import org.json.JSONException;
|
11
|
import org.json.JSONObject;
|
12
|
import org.springframework.beans.factory.annotation.Autowired;
|
13
|
import org.springframework.beans.factory.annotation.Value;
|
14
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
15
|
import org.springframework.stereotype.Component;
|
16
|
import org.springframework.web.bind.annotation.PathVariable;
|
17
|
import org.springframework.web.bind.annotation.RequestBody;
|
18
|
import org.springframework.web.bind.annotation.RequestParam;
|
19
|
import org.springframework.web.client.RestTemplate;
|
20
|
import org.springframework.web.util.UriComponents;
|
21
|
import org.springframework.web.util.UriComponentsBuilder;
|
22
|
import javax.annotation.PostConstruct;
|
23
|
import java.util.*;
|
24
|
import java.util.concurrent.ConcurrentHashMap;
|
25
|
import eu.dnetlib.repo.manager.shared.*;
|
26
|
|
27
|
@Component
|
28
|
public class RepositoryApiImpl implements RepositoryApi {
|
29
|
|
30
|
@Value("${api.baseAddress}")
|
31
|
private String baseAddress;
|
32
|
|
33
|
private RestTemplate restTemplate = null;
|
34
|
|
35
|
private final String[] vocabularyNames = {"dnet:countries", "dnet:datasource_typologies", "dnet:compatibilityLevel"};
|
36
|
|
37
|
private static final Logger LOGGER = Logger.getLogger(RepositoryApiImpl.class);
|
38
|
|
39
|
@Autowired
|
40
|
private VocabularyLoader vocabularyLoader;
|
41
|
|
42
|
private Map<String, Vocabulary> vocabularyMap = new ConcurrentHashMap<String, Vocabulary>();
|
43
|
|
44
|
|
45
|
@PostConstruct
|
46
|
private void init(){
|
47
|
restTemplate = new RestTemplate();
|
48
|
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
49
|
|
50
|
for (String vocName : vocabularyNames) {
|
51
|
vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT));
|
52
|
}
|
53
|
|
54
|
}
|
55
|
|
56
|
@Override
|
57
|
public Aggregations testAggregations() throws JSONException {
|
58
|
|
59
|
int page = 1;
|
60
|
int size = 1000;
|
61
|
|
62
|
UriComponents uriComponents = UriComponentsBuilder
|
63
|
.fromHttpUrl(baseAddress + "/ds/list/")
|
64
|
.path("/{page}/{size}/")
|
65
|
.build().expand(page,size).encode();
|
66
|
|
67
|
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
68
|
while(!rs.equals("[]")){
|
69
|
|
70
|
Aggregations aggregations = getFirstNonEmptyAggregation(rs);
|
71
|
if(aggregations != null)
|
72
|
return aggregations;
|
73
|
|
74
|
LOGGER.debug("Checked " + page*size + " records!");
|
75
|
|
76
|
page+=1;
|
77
|
uriComponents = UriComponentsBuilder
|
78
|
.fromHttpUrl(baseAddress + "/ds/list/")
|
79
|
.path("/{page}/{size}/")
|
80
|
.build().expand(page,size).encode();
|
81
|
rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
82
|
}
|
83
|
|
84
|
return null;
|
85
|
}
|
86
|
|
87
|
private Aggregations getFirstNonEmptyAggregation(String rs) throws JSONException {
|
88
|
|
89
|
JSONArray ids = new JSONArray(rs);
|
90
|
for(int i=0;i<ids.length();i++){
|
91
|
String id = ids.getString(i);
|
92
|
Aggregations aggregations = getRepositoryAggregations(id);
|
93
|
if(aggregations.getAggregationHistory() != null)
|
94
|
return aggregations;
|
95
|
}
|
96
|
return null;
|
97
|
}
|
98
|
|
99
|
@Override
|
100
|
public Country[] getCountries() {
|
101
|
UriComponents uriComponents = UriComponentsBuilder
|
102
|
.fromHttpUrl(baseAddress + "/ds/countries")
|
103
|
.build().encode();
|
104
|
return restTemplate.getForObject(uriComponents.toUri(),Country[].class);
|
105
|
}
|
106
|
|
107
|
|
108
|
@Override
|
109
|
public Set<String> getRepositoriesByCountry(@PathVariable("country") String country) throws JSONException {
|
110
|
|
111
|
|
112
|
int page = 1;
|
113
|
int size = 10;
|
114
|
String mode = "openaire____::opendoar";
|
115
|
|
116
|
UriComponents uriComponents = UriComponentsBuilder
|
117
|
.fromHttpUrl(baseAddress + "/ds/search/country/")
|
118
|
.path("/{page}/{size}/")
|
119
|
.queryParam("country",country)
|
120
|
.build().expand(page,size).encode();
|
121
|
|
122
|
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
123
|
Set<String> resultSet = new HashSet<>();
|
124
|
while(!rs.equals("[]")){
|
125
|
List<Repository> rep = Converter.jsonToRepositoryList(new JSONArray(rs));
|
126
|
|
127
|
Collection<String> repos = this.getRepositoriesByMode(mode,rep);
|
128
|
this.addRepos(resultSet,repos);
|
129
|
|
130
|
page+=1;
|
131
|
uriComponents = UriComponentsBuilder
|
132
|
.fromHttpUrl(baseAddress + "/ds/search/country/")
|
133
|
.path("/{page}/{size}/")
|
134
|
.queryParam("country",country)
|
135
|
.build().expand(page,size).encode();
|
136
|
rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
137
|
}
|
138
|
|
139
|
return resultSet;
|
140
|
}
|
141
|
|
142
|
private void addRepos(Set<String> resultSet, Collection<String> repos) {
|
143
|
for(String s : repos)
|
144
|
if(!resultSet.contains(s))
|
145
|
resultSet.add(s);
|
146
|
}
|
147
|
|
148
|
private Collection<String> getRepositoriesByMode(String mode, List<Repository> rs) {
|
149
|
|
150
|
List<String> reps = new ArrayList<>();
|
151
|
|
152
|
for(Repository r : rs)
|
153
|
if(r.getCollectedFrom().equals(mode))
|
154
|
reps.add(r.getOfficialName());
|
155
|
|
156
|
return reps;
|
157
|
}
|
158
|
|
159
|
@Override
|
160
|
public List<Repository> getRepositoriesOfUser(@PathVariable("userEmail") String userEmail,
|
161
|
@PathVariable("page") String page,
|
162
|
@PathVariable("size") String size) throws JSONException {
|
163
|
|
164
|
UriComponents uriComponents = UriComponentsBuilder
|
165
|
.fromHttpUrl(baseAddress + "/ds/search/email/")
|
166
|
.path("/{page}/{size}/")
|
167
|
.queryParam("contactemail",userEmail)
|
168
|
.build().expand(page,size).encode();
|
169
|
|
170
|
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
171
|
return Converter.jsonToRepositoryList(new JSONArray(rs));
|
172
|
}
|
173
|
|
174
|
@Override
|
175
|
public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException {
|
176
|
|
177
|
UriComponents uriComponents = UriComponentsBuilder
|
178
|
.fromHttpUrl(baseAddress + "/ds/get/")
|
179
|
.path("/{id}/")
|
180
|
.build().expand(id).encode();
|
181
|
|
182
|
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
183
|
return Converter.jsonToRepositoryObject(new JSONObject(rs));
|
184
|
}
|
185
|
|
186
|
@Override
|
187
|
public Aggregations getRepositoryAggregations(@PathVariable("id") String id) throws JSONException {
|
188
|
|
189
|
UriComponents uriComponents = UriComponentsBuilder
|
190
|
.fromHttpUrl(baseAddress + "/ds/get/")
|
191
|
.path("/{id}/")
|
192
|
.build().expand(id).encode();
|
193
|
|
194
|
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
195
|
JSONObject repository = new JSONObject(rs);
|
196
|
|
197
|
Aggregations aggregations = new Aggregations();
|
198
|
|
199
|
aggregations.setAggregationHistory(Converter.getAggregationHistoryFromJson(repository));
|
200
|
aggregations.setLastCollection(Converter.getLastCollectionFromJson(repository));
|
201
|
aggregations.setLastTransformation(Converter.getLastTransformationFromJson(repository));
|
202
|
return aggregations;
|
203
|
}
|
204
|
|
205
|
@Override
|
206
|
public List<Repository> getRepositoriesByName(@PathVariable("name") String name,
|
207
|
@PathVariable("page") String page,
|
208
|
@PathVariable("size") String size) throws JSONException {
|
209
|
UriComponents uriComponents = UriComponentsBuilder
|
210
|
.fromHttpUrl(baseAddress + "/ds/search/name/")
|
211
|
.path("/{page}/{size}")
|
212
|
.queryParam("name",name)
|
213
|
.build().expand(page,size).encode();
|
214
|
|
215
|
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
216
|
return Converter.jsonToRepositoryList(new JSONArray(rs));
|
217
|
}
|
218
|
|
219
|
@Override
|
220
|
public RepositoryInterface getRepositoyInterface(@PathVariable("id") String id) throws JSONException {
|
221
|
|
222
|
UriComponents uriComponents = UriComponentsBuilder
|
223
|
.fromHttpUrl(baseAddress + "/ds/service/")
|
224
|
.path("/{id}/")
|
225
|
.build().expand(id).encode();
|
226
|
|
227
|
String rs = restTemplate.getForObject(uriComponents.toUri(),String.class);
|
228
|
return Converter.jsonToRepositoryInterfaceObject(new JSONArray(rs).getJSONObject(0));
|
229
|
}
|
230
|
|
231
|
@Override
|
232
|
public String addRepository(@RequestBody Repository repository) throws JSONException {
|
233
|
UriComponents uriComponents = UriComponentsBuilder
|
234
|
.fromHttpUrl(baseAddress + "/ds/add/")
|
235
|
.build()
|
236
|
.encode();
|
237
|
return restTemplate.postForObject(uriComponents.toUri(),Converter.repositoryObjectToJson(repository),String.class);
|
238
|
}
|
239
|
|
240
|
@Override
|
241
|
public String addRepositoryInterface(RepositoryInterface repositoryInterface) throws JSONException {
|
242
|
UriComponents uriComponents = UriComponentsBuilder
|
243
|
.fromHttpUrl(baseAddress + "/ds/service/add/")
|
244
|
.build()
|
245
|
.encode();
|
246
|
return restTemplate.postForObject(uriComponents.toUri(), Converter.repositoryInterfaceObjectToJson(repositoryInterface),String.class);
|
247
|
}
|
248
|
|
249
|
@Override
|
250
|
public List<String> getDnetCountries() {
|
251
|
return Converter.readFile("countries.txt");
|
252
|
}
|
253
|
|
254
|
@Override
|
255
|
public List<String> getTypologies() {
|
256
|
return Converter.readFile("typologies.txt");
|
257
|
}
|
258
|
|
259
|
@Override
|
260
|
public List<Timezone> getTimezones() {
|
261
|
List<String> timezones = Converter.readFile("timezones.txt");
|
262
|
return Converter.toTimezones(timezones);
|
263
|
}
|
264
|
|
265
|
@Override
|
266
|
public String updateManagedStatus(@RequestParam(value = "id") String id,
|
267
|
@RequestParam(value = "managed") String managed) {
|
268
|
|
269
|
UriComponents uriComponents = UriComponentsBuilder
|
270
|
.fromHttpUrl(baseAddress + "/ds/manage/")
|
271
|
.queryParam("id",id)
|
272
|
.queryParam("managed",managed)
|
273
|
.build().encode();
|
274
|
|
275
|
return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
|
276
|
}
|
277
|
|
278
|
@Override
|
279
|
public String updateEnglishName(@RequestParam(value = "id") String id,
|
280
|
@RequestParam(value = "englishname") String englishName) {
|
281
|
|
282
|
UriComponents uriComponents = UriComponentsBuilder
|
283
|
.fromHttpUrl(baseAddress + "/ds/manage/")
|
284
|
.queryParam("dsId",id)
|
285
|
.queryParam("englishname",englishName)
|
286
|
.build().encode();
|
287
|
return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
|
288
|
|
289
|
|
290
|
}
|
291
|
|
292
|
@Override
|
293
|
public String updateLatitude(@RequestParam(value = "id") String id,
|
294
|
@RequestParam(value = "managed") String latitude) {
|
295
|
|
296
|
UriComponents uriComponents = UriComponentsBuilder
|
297
|
.fromHttpUrl(baseAddress + "/ds/manage/")
|
298
|
.queryParam("dsId",id)
|
299
|
.queryParam("latitude",latitude)
|
300
|
.build().encode();
|
301
|
return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
|
302
|
}
|
303
|
|
304
|
@Override
|
305
|
public String updateLongitude(@RequestParam(value = "id") String id,
|
306
|
@RequestParam(value = "longitude") String longitude) {
|
307
|
|
308
|
UriComponents uriComponents = UriComponentsBuilder
|
309
|
.fromHttpUrl(baseAddress + "/ds/manage/")
|
310
|
.queryParam("dsId",id)
|
311
|
.queryParam("longitude",longitude)
|
312
|
.build().encode();
|
313
|
return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
|
314
|
}
|
315
|
|
316
|
@Override
|
317
|
public String updateOfficialName(@RequestParam(value = "id") String id,
|
318
|
@RequestParam(value = "officialname") String officialname) {
|
319
|
|
320
|
UriComponents uriComponents = UriComponentsBuilder
|
321
|
.fromHttpUrl(baseAddress + "/ds/manage/")
|
322
|
.queryParam("dsId",id)
|
323
|
.queryParam("officialname",officialname)
|
324
|
.build().encode();
|
325
|
return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
|
326
|
}
|
327
|
|
328
|
@Override
|
329
|
public List<String> getUrlsOfUserRepos(@PathVariable("user_email") String user_email,
|
330
|
@PathVariable("page") String page,
|
331
|
@PathVariable("size") String size) throws JSONException {
|
332
|
UriComponents uriComponents = UriComponentsBuilder
|
333
|
.fromHttpUrl(baseAddress + "/api/baseurl/")
|
334
|
.path("/{page}/{size}")
|
335
|
.queryParam("userEmail",user_email)
|
336
|
.build().expand(page,size).encode();
|
337
|
return Arrays.asList(restTemplate.getForObject(uriComponents.toUri(), String[].class));
|
338
|
}
|
339
|
|
340
|
@Override
|
341
|
public List<String> getDatasourceVocabularies(@PathVariable("mode") String mode) {
|
342
|
|
343
|
List<String> resultSet = new ArrayList<>();
|
344
|
for (Map.Entry<String, String> entry : this.getVocabulary("dnet:datasource_typologies").getAsMap().entrySet()) {
|
345
|
if (mode.equalsIgnoreCase("aggregator")) {
|
346
|
if (entry.getKey().contains("aggregator"))
|
347
|
resultSet.add(entry.getValue());
|
348
|
} else if (mode.equalsIgnoreCase("journal")) {
|
349
|
if (entry.getKey().contains("journal"))
|
350
|
resultSet.add(entry.getValue());
|
351
|
} else if (mode.equalsIgnoreCase("opendoar")) {
|
352
|
if (entry.getKey().contains("pubsrepository"))
|
353
|
resultSet.add(entry.getValue());
|
354
|
} else if (mode.equalsIgnoreCase("re3data")) {
|
355
|
if (entry.getKey().contains("datarepository"))
|
356
|
resultSet.add(entry.getValue());
|
357
|
}
|
358
|
}
|
359
|
|
360
|
|
361
|
return resultSet;
|
362
|
}
|
363
|
|
364
|
private Vocabulary getVocabulary(String vocName) {
|
365
|
|
366
|
if (!vocabularyMap.containsKey(vocName)) {
|
367
|
vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT));
|
368
|
}
|
369
|
return vocabularyMap.get(vocName);
|
370
|
}
|
371
|
|
372
|
}
|