1
|
package eu.dnetlib.repo.manager.service;
|
2
|
|
3
|
import com.fasterxml.jackson.databind.JsonNode;
|
4
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
5
|
import eu.dnetlib.domain.data.Repository;
|
6
|
import eu.dnetlib.repo.manager.shared.BrokerException;
|
7
|
import eu.dnetlib.repo.manager.shared.Term;
|
8
|
import eu.dnetlib.repo.manager.shared.Tuple;
|
9
|
import eu.dnetlib.repo.manager.shared.broker.*;
|
10
|
import org.json.JSONException;
|
11
|
import org.springframework.beans.factory.annotation.Autowired;
|
12
|
import org.springframework.beans.factory.annotation.Value;
|
13
|
import org.springframework.core.ParameterizedTypeReference;
|
14
|
import org.springframework.http.*;
|
15
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
16
|
import org.springframework.stereotype.Service;
|
17
|
import org.springframework.util.LinkedMultiValueMap;
|
18
|
import org.springframework.util.MultiValueMap;
|
19
|
import org.springframework.web.client.RestClientException;
|
20
|
import org.springframework.web.client.RestTemplate;
|
21
|
import org.springframework.web.util.UriComponents;
|
22
|
import org.springframework.web.util.UriComponentsBuilder;
|
23
|
|
24
|
import javax.annotation.PostConstruct;
|
25
|
import java.io.IOException;
|
26
|
import java.io.InputStream;
|
27
|
import java.net.URL;
|
28
|
import java.util.*;
|
29
|
|
30
|
@Service("brokerService")
|
31
|
public class BrokerServiceImpl implements BrokerService {
|
32
|
|
33
|
@Autowired
|
34
|
private RepositoryServiceImpl repoAPI;
|
35
|
@Value("${services.broker.url}:${services.broker.port}/${services.broker.api}${services.broker.openaire}")
|
36
|
private String openairePath;
|
37
|
@Value("${services.broker.url}:${services.broker.port}/${services.broker.api}")
|
38
|
private String apiPath;
|
39
|
@Value("${topic_types.url}")
|
40
|
private String topicsURL;
|
41
|
|
42
|
private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
|
43
|
.getLogger(BrokerServiceImpl.class);
|
44
|
|
45
|
@Autowired
|
46
|
RestTemplate restTemplate ;
|
47
|
|
48
|
private HttpHeaders httpHeaders;
|
49
|
|
50
|
private HashMap<String,Term> topics = new HashMap<String, Term>();
|
51
|
|
52
|
@Autowired
|
53
|
private EmailUtils emailUtils;
|
54
|
|
55
|
@PostConstruct
|
56
|
private void initDnetTopicsMap() {
|
57
|
|
58
|
httpHeaders = new HttpHeaders();
|
59
|
httpHeaders.set("Content-Type", "application/json");
|
60
|
|
61
|
LOGGER.debug("Init dnet topics!");
|
62
|
try (InputStream is = new URL(topicsURL).openStream() ){
|
63
|
ObjectMapper mapper = new ObjectMapper();
|
64
|
JsonNode root = mapper.readTree(is);
|
65
|
for (JsonNode term : root.path("terms") )
|
66
|
topics.put(term.path("code").textValue(), parseTerm(term));
|
67
|
} catch (IOException e) {
|
68
|
LOGGER.debug("Exception on initDnetTopicsMap" , e);
|
69
|
emailUtils.reportException(e);
|
70
|
}
|
71
|
}
|
72
|
|
73
|
private Term parseTerm(JsonNode term) {
|
74
|
return new Term(term.path("englishName").textValue(),term.path("nativeName").textValue(),
|
75
|
term.path("encoding").textValue(),term.path("code").textValue());
|
76
|
}
|
77
|
|
78
|
|
79
|
@Override
|
80
|
public DatasourcesBroker getDatasourcesOfUser(String user,String includeShared,String includeByOthers) throws JSONException {
|
81
|
|
82
|
DatasourcesBroker ret = new DatasourcesBroker();
|
83
|
try {
|
84
|
ret.setDatasourcesOfUser(getDatasourcesOfUserType(getRepositoriesOfUser(user)));
|
85
|
//TODO fix bug when values are true
|
86
|
if (Boolean.parseBoolean(includeShared)) {
|
87
|
List<String> sharedDatasourceIds = new ArrayList<String>();
|
88
|
ret.setSharedDatasources(getDatasourcesOfUserType(getRepositoriesByIds(sharedDatasourceIds)));
|
89
|
}
|
90
|
|
91
|
if (Boolean.parseBoolean(includeByOthers)) {
|
92
|
ret.setDatasourcesOfOthers(getDatasourcesOfUserType(getRepositoriesOfUser(user)));
|
93
|
}
|
94
|
} catch (BrokerException e) {
|
95
|
LOGGER.debug("Exception on getDatasourcesOfUser" , e);
|
96
|
emailUtils.reportException(e);
|
97
|
}
|
98
|
|
99
|
return ret;
|
100
|
}
|
101
|
|
102
|
@Override
|
103
|
public List<BrowseEntry> getTopicsForDatasource(String datasourceName) throws BrokerException {
|
104
|
final String service = "/topicsForDatasource";
|
105
|
|
106
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
|
107
|
.queryParam("ds", datasourceName);
|
108
|
|
109
|
ResponseEntity<List<BrowseEntry>> resp;
|
110
|
try {
|
111
|
resp = restTemplate.exchange(
|
112
|
builder.build().encode().toUri(),
|
113
|
HttpMethod.GET,
|
114
|
null,
|
115
|
new ParameterizedTypeReference<List<BrowseEntry>>() {
|
116
|
});
|
117
|
} catch (RestClientException e) {
|
118
|
LOGGER.debug("Exception on getTopicsForDatasource" , e);
|
119
|
emailUtils.reportException(e);
|
120
|
throw new BrokerException(e);
|
121
|
}
|
122
|
|
123
|
return resp.getBody();
|
124
|
}
|
125
|
|
126
|
@Override
|
127
|
public EventsPage advancedShowEvents(String page,
|
128
|
String size,
|
129
|
AdvQueryObject advQueryObject) throws BrokerException, JSONException ,IOException {
|
130
|
|
131
|
final String service = "/events/{page}/{pageSize}";
|
132
|
|
133
|
Map<String, Long> uriParams = new HashMap<>();
|
134
|
uriParams.put("page", Long.parseLong(page));
|
135
|
uriParams.put("pageSize", Long.parseLong(size));
|
136
|
|
137
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service);
|
138
|
|
139
|
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
|
140
|
advQueryObject.setPage(Long.parseLong(page));
|
141
|
HttpEntity<AdvQueryObject> entity = new HttpEntity<>(advQueryObject, httpHeaders);
|
142
|
ResponseEntity<EventsPage> resp;
|
143
|
try {
|
144
|
resp = restTemplate.exchange(
|
145
|
builder.buildAndExpand(uriParams).encode().toUri(),
|
146
|
HttpMethod.POST,
|
147
|
entity,
|
148
|
new ParameterizedTypeReference<EventsPage>() {
|
149
|
}
|
150
|
);
|
151
|
} catch (RestClientException e) {
|
152
|
LOGGER.debug("Exception on advancedShowEvents" , e);
|
153
|
emailUtils.reportException(e);
|
154
|
throw new BrokerException(e);
|
155
|
}
|
156
|
return resp.getBody();
|
157
|
|
158
|
|
159
|
}
|
160
|
|
161
|
|
162
|
private List<Tuple<BrowseEntry, String>> getDatasourcesOfUserType(List<Repository> repositories) throws BrokerException {
|
163
|
|
164
|
List<Tuple<BrowseEntry, String>> entries = new ArrayList<>();
|
165
|
for (Repository repo : repositories) {
|
166
|
BrowseEntry temp = new BrowseEntry();
|
167
|
temp.setValue(repo.getOfficialName());
|
168
|
temp.setSize(new Long(0));
|
169
|
for (BrowseEntry e : getTopicsForDatasource(repo.getOfficialName())) {
|
170
|
temp.setSize(temp.getSize() + e.getSize());
|
171
|
}
|
172
|
Tuple<BrowseEntry, String> tup = new Tuple<>(temp, repo.getLogoUrl());
|
173
|
entries.add(tup);
|
174
|
}
|
175
|
|
176
|
// sort the collection by the second field of the tuple which is size
|
177
|
Collections.sort(entries, new Comparator<Tuple<BrowseEntry, String>>() {
|
178
|
@Override
|
179
|
public int compare(Tuple<BrowseEntry, String> e1, Tuple<BrowseEntry, String> e2) {
|
180
|
return (int) (e2.getFirst().getSize().longValue() - e1.getFirst().getSize().longValue());
|
181
|
}
|
182
|
});
|
183
|
|
184
|
return entries;
|
185
|
}
|
186
|
|
187
|
private List<Repository> getRepositoriesOfUser(String userEmail) throws JSONException {
|
188
|
|
189
|
int page = 0;
|
190
|
int size = 50;
|
191
|
List<Repository> rs ;
|
192
|
List<Repository> resultSet = new ArrayList<>();
|
193
|
|
194
|
while (true){
|
195
|
rs = repoAPI.getRepositoriesOfUser(userEmail, String.valueOf(page), String.valueOf(size));
|
196
|
resultSet.addAll(rs);
|
197
|
page+=1;
|
198
|
if(rs.size() == 0) break;
|
199
|
}
|
200
|
return resultSet;
|
201
|
}
|
202
|
|
203
|
private List<Repository> getRepositoriesByIds(List<String> sharedDatasourceIds) {
|
204
|
return null;
|
205
|
}
|
206
|
|
207
|
@Override
|
208
|
public EventsPage showEvents(String datasourceName,
|
209
|
String topic,
|
210
|
String page,
|
211
|
String size) throws BrokerException, JSONException {
|
212
|
|
213
|
final String service = "/events";
|
214
|
|
215
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
|
216
|
.queryParam("ds", datasourceName)
|
217
|
.queryParam("topic", topic)
|
218
|
.path("/{page}/{size}/");
|
219
|
|
220
|
ResponseEntity<EventsPage> resp;
|
221
|
try {
|
222
|
resp = restTemplate.exchange(
|
223
|
builder.build().expand(page, size).encode().toUri(),
|
224
|
HttpMethod.GET,
|
225
|
null,
|
226
|
new ParameterizedTypeReference<EventsPage>() {
|
227
|
});
|
228
|
} catch (RestClientException e) {
|
229
|
LOGGER.debug("Exception on showEvents" , e);
|
230
|
emailUtils.reportException(e);
|
231
|
throw new BrokerException(e);
|
232
|
}
|
233
|
return resp.getBody();
|
234
|
}
|
235
|
|
236
|
@Override
|
237
|
public Map<String, List<SimpleSubscriptionDesc>> getSimpleSubscriptionsOfUser(String userEmail)
|
238
|
throws BrokerException {
|
239
|
|
240
|
final String service = "/subscriptions";
|
241
|
|
242
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
|
243
|
.queryParam("email", userEmail);
|
244
|
|
245
|
LOGGER.debug(builder.build().encode().toUri());
|
246
|
ResponseEntity<Map<String, List<SimpleSubscriptionDesc>>> resp;
|
247
|
try {
|
248
|
resp = restTemplate.exchange(
|
249
|
builder.build().encode().toUri(),
|
250
|
HttpMethod.GET,
|
251
|
null,
|
252
|
new ParameterizedTypeReference<Map<String, List<SimpleSubscriptionDesc>>>() {
|
253
|
});
|
254
|
} catch (RestClientException e) {
|
255
|
LOGGER.debug("Exception on getSimpleSubscriptionsOfUser" , e);
|
256
|
emailUtils.reportException(e);
|
257
|
throw new BrokerException(e);
|
258
|
}
|
259
|
return resp.getBody();
|
260
|
}
|
261
|
|
262
|
@Override
|
263
|
public Subscription subscribe(OpenaireSubscription obj) throws BrokerException {
|
264
|
final String service = "/subscribe";
|
265
|
|
266
|
//build the uri params
|
267
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service);
|
268
|
|
269
|
HttpEntity<OpenaireSubscription> entity = new HttpEntity<>(obj, httpHeaders);
|
270
|
|
271
|
//create new template engine
|
272
|
RestTemplate template = new RestTemplate();
|
273
|
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
274
|
ResponseEntity<Subscription> resp;
|
275
|
try {
|
276
|
//communicate with endpoint
|
277
|
resp = restTemplate.exchange(
|
278
|
builder.build().encode().toUri(),
|
279
|
HttpMethod.POST,
|
280
|
entity,
|
281
|
new ParameterizedTypeReference<Subscription>() {
|
282
|
});
|
283
|
} catch (RestClientException e) {
|
284
|
LOGGER.debug("Exception on OpenaireSubscription" , e);
|
285
|
emailUtils.reportException(e);
|
286
|
throw new BrokerException(e);
|
287
|
}
|
288
|
|
289
|
return resp.getBody();
|
290
|
}
|
291
|
|
292
|
@Override
|
293
|
public ResponseEntity<Object> unsubscribe(String subscriptionId) throws BrokerException {
|
294
|
final String service = "/subscriptions/" + subscriptionId;
|
295
|
|
296
|
//build the uri params
|
297
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service);
|
298
|
|
299
|
try {
|
300
|
//communicate with endpoint
|
301
|
restTemplate.exchange(
|
302
|
builder.build().encode().toUri(),
|
303
|
HttpMethod.DELETE,
|
304
|
null,
|
305
|
new ParameterizedTypeReference<Void>() {
|
306
|
});
|
307
|
} catch (RestClientException e) {
|
308
|
LOGGER.debug("Exception on unsubscribe" , e);
|
309
|
emailUtils.reportException(e);
|
310
|
throw new BrokerException(e);
|
311
|
}
|
312
|
return new ResponseEntity<>("OK",HttpStatus.OK);
|
313
|
}
|
314
|
|
315
|
@Override
|
316
|
public Subscription getSubscription( String subscriptionId) throws BrokerException {
|
317
|
final String service = "/subscriptions/" + subscriptionId;
|
318
|
|
319
|
//build the uri params
|
320
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service);
|
321
|
|
322
|
ResponseEntity<Subscription> resp;
|
323
|
try {
|
324
|
//communicate with endpoint
|
325
|
resp = restTemplate.exchange(
|
326
|
builder.build().encode().toUri(),
|
327
|
HttpMethod.GET,
|
328
|
null,
|
329
|
new ParameterizedTypeReference<Subscription>() {
|
330
|
});
|
331
|
} catch (RestClientException e) {
|
332
|
LOGGER.debug("Exception on getSubscription" , e);
|
333
|
emailUtils.reportException(e);
|
334
|
throw new BrokerException(e);
|
335
|
}
|
336
|
return resp.getBody();
|
337
|
}
|
338
|
|
339
|
@Override
|
340
|
public Map<String, Term> getDnetTopics() throws BrokerException {
|
341
|
return topics;
|
342
|
}
|
343
|
|
344
|
@Override
|
345
|
public EventsPage getNotificationsBySubscriptionId(String subscriptionId,
|
346
|
String page,
|
347
|
String size) throws BrokerException {
|
348
|
|
349
|
UriComponents uriComponents = UriComponentsBuilder
|
350
|
.fromHttpUrl(openairePath + "/notifications/")
|
351
|
.path("/{id}/{page}/{size}/")
|
352
|
.build().expand(subscriptionId,page, size).encode();
|
353
|
|
354
|
ResponseEntity<EventsPage> resp;
|
355
|
try {
|
356
|
resp = restTemplate.exchange(
|
357
|
uriComponents.toUri(),
|
358
|
HttpMethod.GET,
|
359
|
null,
|
360
|
new ParameterizedTypeReference<EventsPage>() {
|
361
|
});
|
362
|
} catch (RestClientException e) {
|
363
|
LOGGER.debug("Exception on getNotificationsBySubscriptionId" , e);
|
364
|
emailUtils.reportException(e);
|
365
|
throw new BrokerException(e);
|
366
|
}
|
367
|
return resp.getBody();
|
368
|
}
|
369
|
|
370
|
//@Override
|
371
|
public Map<String, List<Subscription>> getSubscriptionsOfUser(String userEmail)
|
372
|
throws BrokerException {
|
373
|
|
374
|
Map<String, List<SimpleSubscriptionDesc>> simpleSubs = getSimpleSubscriptionsOfUser(userEmail);
|
375
|
Map<String,List<Subscription>> subs = new HashMap<>();
|
376
|
List<Subscription> subscriptions = null;
|
377
|
|
378
|
for(String s:simpleSubs.keySet()){
|
379
|
List<SimpleSubscriptionDesc> simpleSubscriptionDescs = simpleSubs.get(s);
|
380
|
for(SimpleSubscriptionDesc simpleSubscriptionDesc : simpleSubscriptionDescs) {
|
381
|
subscriptions = new ArrayList<>();
|
382
|
subscriptions.add(getSubscription(simpleSubscriptionDesc.getId()));
|
383
|
}
|
384
|
subs.put(s,subscriptions);
|
385
|
}
|
386
|
return subs;
|
387
|
}
|
388
|
|
389
|
|
390
|
}
|