1
|
package eu.dnetlib.repo.manager.server.services;
|
2
|
|
3
|
import eu.dnetlib.domain.data.Repository;
|
4
|
import eu.dnetlib.gwt.server.service.SpringGwtRemoteServiceServlet;
|
5
|
import eu.dnetlib.repo.manager.client.services.BrokerService;
|
6
|
import eu.dnetlib.repo.manager.service.controllers.BrokerApi;
|
7
|
import eu.dnetlib.repo.manager.service.controllers.RepositoryApi;
|
8
|
import eu.dnetlib.repo.manager.shared.BrokerException;
|
9
|
import eu.dnetlib.repo.manager.shared.RepositoryServiceException;
|
10
|
import eu.dnetlib.repo.manager.shared.Tuple;
|
11
|
import eu.dnetlib.repo.manager.shared.broker.*;
|
12
|
import eu.dnetlib.repos.RepoApi;
|
13
|
import org.apache.log4j.Logger;
|
14
|
import org.json.JSONException;
|
15
|
import org.json.JSONObject;
|
16
|
import org.springframework.beans.factory.annotation.Autowired;
|
17
|
import org.springframework.beans.factory.annotation.Value;
|
18
|
import org.springframework.core.ParameterizedTypeReference;
|
19
|
import org.springframework.http.HttpEntity;
|
20
|
import org.springframework.http.HttpMethod;
|
21
|
import org.springframework.http.ResponseEntity;
|
22
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
23
|
import org.springframework.stereotype.Service;
|
24
|
import org.springframework.util.LinkedMultiValueMap;
|
25
|
import org.springframework.util.MultiValueMap;
|
26
|
import org.springframework.web.client.RestClientException;
|
27
|
import org.springframework.web.client.RestTemplate;
|
28
|
import org.springframework.web.util.UriComponentsBuilder;
|
29
|
|
30
|
import javax.servlet.ServletConfig;
|
31
|
import javax.servlet.ServletException;
|
32
|
import java.util.*;
|
33
|
|
34
|
/**
|
35
|
* Created by stefanos on 10/26/16.
|
36
|
*/
|
37
|
@SuppressWarnings("serial")
|
38
|
@Service("brokerService")
|
39
|
public class BrokerServiceImpl extends SpringGwtRemoteServiceServlet implements BrokerService {
|
40
|
|
41
|
private static final Logger LOGGER = Logger
|
42
|
.getLogger(BrokerServiceImpl.class);
|
43
|
|
44
|
@Autowired
|
45
|
private RepoApi repoAPI;
|
46
|
|
47
|
|
48
|
@Autowired
|
49
|
private RepositoryApi repositoryApi;
|
50
|
@Autowired
|
51
|
private BrokerApi brokerApi;
|
52
|
|
53
|
|
54
|
@Override
|
55
|
public void init(ServletConfig config) throws ServletException {
|
56
|
super.init(config);
|
57
|
LOGGER.info("broker service init");
|
58
|
}
|
59
|
|
60
|
@Value("${services.broker.url}:${services.broker.port}/${services.broker.api}${services.broker.openaire}")
|
61
|
private String openairePath;
|
62
|
@Value("${services.broker.url}:${services.broker.port}/${services.broker.api}")
|
63
|
private String apiPath;
|
64
|
|
65
|
/**
|
66
|
* @param datasourceName the name of the data source
|
67
|
* @return a list of BrowseEvent entries for that datasource
|
68
|
* @throws BrokerException containing the error code from the server
|
69
|
* @author stefanos
|
70
|
*/
|
71
|
@Override
|
72
|
public List<BrowseEntry> getTopicsForDatasource(String datasourceName) throws BrokerException {
|
73
|
return brokerApi.getTopicsForDatasource(datasourceName);
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* @param datasourceName the name of the datasource
|
78
|
* @param topic the name of the topic to filter
|
79
|
* @param page the page number
|
80
|
* @return an Events page with a constant 50 max number of entires
|
81
|
* @throws BrokerException containing the error code from the server
|
82
|
*/
|
83
|
@Override
|
84
|
public EventsPage showEvents(String datasourceName, String topic, long page) throws BrokerException{
|
85
|
JSONObject params = new JSONObject();
|
86
|
try {
|
87
|
params.append("datasourceName",datasourceName);
|
88
|
params.append("topic",topic);
|
89
|
params.append("page",String.valueOf(page));
|
90
|
return brokerApi.showEvents(params.toString());
|
91
|
} catch (JSONException e) {
|
92
|
LOGGER.debug("Error on show events",e);
|
93
|
}
|
94
|
return null;
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* @param advQueryObject a pojo class containing the filter parameters
|
99
|
* @param page the number of the page
|
100
|
* @param pageSize the page size
|
101
|
* @return
|
102
|
* @throws BrokerException
|
103
|
*/
|
104
|
@Override
|
105
|
public EventsPage advancedShowEvents(AdvQueryObject advQueryObject, long page, long pageSize) throws BrokerException {
|
106
|
|
107
|
JSONObject jsonObject = new JSONObject();
|
108
|
try {
|
109
|
jsonObject.append("page",String.valueOf(page));
|
110
|
jsonObject.append("pagesize",String.valueOf(pageSize));
|
111
|
jsonObject.append("advQueryObject",advQueryObject);
|
112
|
return brokerApi.advancedShowEvents(jsonObject.toString());
|
113
|
} catch (JSONException e) {
|
114
|
LOGGER.debug("Error on advanced show events",e);
|
115
|
}
|
116
|
return null;
|
117
|
}
|
118
|
|
119
|
@Override
|
120
|
public DatasourcesBroker getDatasourcesOfUser(String userEmail, boolean includeShared, boolean includeByOthers)
|
121
|
throws BrokerException {
|
122
|
|
123
|
/*JSONObject params = new JSONObject();
|
124
|
try {
|
125
|
params.append("userEmail",userEmail);
|
126
|
params.append("includeShared",includeShared);
|
127
|
params.append("includeByOthers",includeByOthers);
|
128
|
return brokerApi.getDatasourcesOfUser(params.toString());
|
129
|
} catch (JSONException e) {
|
130
|
LOGGER.debug("Error on get datasources of user",e);
|
131
|
}
|
132
|
return null;*/
|
133
|
DatasourcesBroker ret = new DatasourcesBroker();
|
134
|
try {
|
135
|
LOGGER.debug("In getDatasourcesOfUser");
|
136
|
|
137
|
//set for user
|
138
|
ret.setDatasourcesOfUser(getDatasourcesOfUserType(this.repoAPI.getRepositoriesOfUser(userEmail, false)));
|
139
|
|
140
|
//set for shared
|
141
|
if (includeShared) {
|
142
|
//TODO whatever nikonas was saying
|
143
|
List<String> sharedDatasourceIds = new ArrayList<String>();
|
144
|
ret.setSharedDatasources(getDatasourcesOfUserType(this.repoAPI.getReposByIds(sharedDatasourceIds)));
|
145
|
}
|
146
|
|
147
|
//set others
|
148
|
if (includeByOthers) {
|
149
|
ret.setDatasourcesOfOthers(getDatasourcesOfUserType(this.repoAPI.getRepositoriesOfUser(userEmail, true)));
|
150
|
}
|
151
|
|
152
|
} catch (RepositoryServiceException e) {
|
153
|
e.printStackTrace();
|
154
|
throw new BrokerException(e);
|
155
|
} catch (Exception e) {
|
156
|
e.printStackTrace();
|
157
|
}
|
158
|
return ret;
|
159
|
}
|
160
|
|
161
|
@Override
|
162
|
public Map<String, List<SimpleSubscriptionDesc>> getSubscriptionsOfUser(String userEmail) throws BrokerException {
|
163
|
|
164
|
|
165
|
final String service = "/subscriptions";
|
166
|
|
167
|
//build the uri params
|
168
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
|
169
|
.queryParam("email", userEmail);
|
170
|
|
171
|
//create new template engine
|
172
|
RestTemplate template = new RestTemplate();
|
173
|
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
174
|
ResponseEntity<Map<String, List<SimpleSubscriptionDesc>>> resp;
|
175
|
try {
|
176
|
//communicate with endpoint
|
177
|
resp = template.exchange(
|
178
|
builder.build().encode().toUri(),
|
179
|
HttpMethod.GET,
|
180
|
null,
|
181
|
new ParameterizedTypeReference<Map<String, List<SimpleSubscriptionDesc>>>() {
|
182
|
});
|
183
|
} catch (RestClientException e) {
|
184
|
throw new BrokerException(e);
|
185
|
}
|
186
|
|
187
|
return resp.getBody();
|
188
|
}
|
189
|
|
190
|
@Override
|
191
|
public Subscription subscribe(OpenaireSubscription obj) throws BrokerException {
|
192
|
final String service = "/subscribe";
|
193
|
|
194
|
//build the uri params
|
195
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service);
|
196
|
|
197
|
//Header info
|
198
|
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
|
199
|
headers.add("Content-Type", "application/json");
|
200
|
HttpEntity<OpenaireSubscription> entity = new HttpEntity<>(obj, headers);
|
201
|
|
202
|
//create new template engine
|
203
|
RestTemplate template = new RestTemplate();
|
204
|
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
205
|
ResponseEntity<Subscription> resp;
|
206
|
try {
|
207
|
//communicate with endpoint
|
208
|
resp = template.exchange(
|
209
|
builder.build().encode().toUri(),
|
210
|
HttpMethod.POST,
|
211
|
entity,
|
212
|
new ParameterizedTypeReference<Subscription>() {
|
213
|
});
|
214
|
} catch (RestClientException e) {
|
215
|
throw new BrokerException(e);
|
216
|
}
|
217
|
|
218
|
return resp.getBody();
|
219
|
}
|
220
|
|
221
|
@Override
|
222
|
public void unsubscribe(String subscriptionId) throws BrokerException {
|
223
|
final String service = "/subscriptions/" + subscriptionId;
|
224
|
|
225
|
//build the uri params
|
226
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service);
|
227
|
|
228
|
//create new template engine
|
229
|
RestTemplate template = new RestTemplate();
|
230
|
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
231
|
try {
|
232
|
//communicate with endpoint
|
233
|
template.exchange(
|
234
|
builder.build().encode().toUri(),
|
235
|
HttpMethod.DELETE,
|
236
|
null,
|
237
|
new ParameterizedTypeReference<Void>() {
|
238
|
});
|
239
|
} catch (RestClientException e) {
|
240
|
throw new BrokerException(e);
|
241
|
}
|
242
|
}
|
243
|
|
244
|
@Override
|
245
|
public Subscription getSubscription(String subscriptionId) throws BrokerException {
|
246
|
final String service = "/subscriptions/" + subscriptionId;
|
247
|
|
248
|
//build the uri params
|
249
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service);
|
250
|
|
251
|
//create new template engine
|
252
|
RestTemplate template = new RestTemplate();
|
253
|
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
254
|
ResponseEntity<Subscription> resp;
|
255
|
try {
|
256
|
//communicate with endpoint
|
257
|
resp = template.exchange(
|
258
|
builder.build().encode().toUri(),
|
259
|
HttpMethod.GET,
|
260
|
null,
|
261
|
new ParameterizedTypeReference<Subscription>() {
|
262
|
});
|
263
|
} catch (RestClientException e) {
|
264
|
throw new BrokerException(e);
|
265
|
}
|
266
|
return resp.getBody();
|
267
|
}
|
268
|
|
269
|
@Override
|
270
|
public void unsubscribe(List<String> subscriptionIds) throws BrokerException {
|
271
|
for (String subscriptionId : subscriptionIds) {
|
272
|
unsubscribe(subscriptionId);
|
273
|
}
|
274
|
}
|
275
|
|
276
|
/**
|
277
|
* Helper class to aggregate the datasources topic sizes by datasource name.
|
278
|
*
|
279
|
* @param repositories to be aggregated
|
280
|
* @return a List of BrowseEntry with the official name and the number of topics for each repo and logo url
|
281
|
* @throws BrokerException
|
282
|
*/
|
283
|
private List<Tuple<BrowseEntry, String>> getDatasourcesOfUserType(List<Repository> repositories) throws BrokerException {
|
284
|
//get user entries
|
285
|
LOGGER.debug("getDatasourcesOfUserType : " + repositories.size());
|
286
|
List<Tuple<BrowseEntry, String>> entries = new ArrayList<>();
|
287
|
for (Repository repo : repositories) {
|
288
|
BrowseEntry temp = new BrowseEntry();
|
289
|
temp.setValue(repo.getOfficialName());
|
290
|
temp.setSize(new Long(0));
|
291
|
for (BrowseEntry e : this.getTopicsForDatasource(repo.getOfficialName())) {
|
292
|
temp.setSize(temp.getSize() + e.getSize());
|
293
|
}
|
294
|
Tuple<BrowseEntry, String> tup = new Tuple<>(temp, repo.getLogoUrl());
|
295
|
entries.add(tup);
|
296
|
}
|
297
|
|
298
|
// sort the collection by the second field of the tuple which is size
|
299
|
Collections.sort(entries, new Comparator<Tuple<BrowseEntry, String>>() {
|
300
|
@Override
|
301
|
public int compare(Tuple<BrowseEntry, String> e1, Tuple<BrowseEntry, String> e2) {
|
302
|
return (int) (e2.getFirst().getSize().longValue() - e1.getFirst().getSize().longValue());
|
303
|
}
|
304
|
});
|
305
|
|
306
|
return entries;
|
307
|
}
|
308
|
}
|