1
|
package eu.dnetlib.repo.manager.service.controllers;
|
2
|
|
3
|
|
4
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
5
|
import com.google.gson.Gson;
|
6
|
import eu.dnetlib.domain.data.Repository;
|
7
|
import eu.dnetlib.repo.manager.shared.BrokerException;
|
8
|
import eu.dnetlib.repo.manager.shared.Tuple;
|
9
|
import eu.dnetlib.repo.manager.shared.broker.AdvQueryObject;
|
10
|
import eu.dnetlib.repo.manager.shared.broker.BrowseEntry;
|
11
|
import eu.dnetlib.repo.manager.shared.broker.DatasourcesBroker;
|
12
|
import eu.dnetlib.repo.manager.shared.broker.EventsPage;
|
13
|
import org.apache.xpath.operations.Bool;
|
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.Component;
|
24
|
import org.springframework.util.LinkedMultiValueMap;
|
25
|
import org.springframework.util.MultiValueMap;
|
26
|
import org.springframework.web.bind.annotation.PathVariable;
|
27
|
import org.springframework.web.client.RestClientException;
|
28
|
import org.springframework.web.client.RestTemplate;
|
29
|
import org.springframework.web.util.UriComponentsBuilder;
|
30
|
|
31
|
import java.util.*;
|
32
|
|
33
|
@Component
|
34
|
public class BrokerApiImpl implements BrokerApi {
|
35
|
|
36
|
|
37
|
@Autowired
|
38
|
private RepositoryApiImpl repoAPI;
|
39
|
@Value("${services.broker.url}:${services.broker.port}/${services.broker.api}${services.broker.openaire}")
|
40
|
private String openairePath;
|
41
|
|
42
|
@Override
|
43
|
public DatasourcesBroker getDatasourcesOfUser(String params) throws JSONException {
|
44
|
JSONObject json_params = new JSONObject(params);
|
45
|
DatasourcesBroker ret = new DatasourcesBroker();
|
46
|
|
47
|
String userEmail = json_params.getString("userEmail");
|
48
|
boolean includeShared = Boolean.parseBoolean( json_params.getString("includeShared") );
|
49
|
boolean includeByOthers = Boolean.parseBoolean( json_params.getString("includeByOthers") );
|
50
|
|
51
|
try {
|
52
|
ret.setDatasourcesOfUser(getDatasourcesOfUserType(getRepositoriesOfUser(userEmail)));
|
53
|
if (includeShared) {
|
54
|
//TODO whatever nikonas was saying
|
55
|
List<String> sharedDatasourceIds = new ArrayList<String>();
|
56
|
ret.setSharedDatasources(getDatasourcesOfUserType(getRepositoriesByIds(sharedDatasourceIds)));
|
57
|
}
|
58
|
|
59
|
if (includeByOthers) {
|
60
|
ret.setDatasourcesOfOthers(getDatasourcesOfUserType(getRepositoriesOfUser(userEmail)));
|
61
|
}
|
62
|
} catch (BrokerException e) {
|
63
|
e.printStackTrace();
|
64
|
}
|
65
|
|
66
|
return ret;
|
67
|
}
|
68
|
|
69
|
@Override
|
70
|
public List<BrowseEntry> getTopicsForDatasource(@PathVariable("datasourceName") String datasourceName) throws BrokerException {
|
71
|
final String service = "/topicsForDatasource";
|
72
|
|
73
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
|
74
|
.queryParam("ds", datasourceName);
|
75
|
|
76
|
RestTemplate template = new RestTemplate();
|
77
|
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
78
|
ResponseEntity<List<BrowseEntry>> resp;
|
79
|
try {
|
80
|
resp = template.exchange(
|
81
|
builder.build().encode().toUri(),
|
82
|
HttpMethod.GET,
|
83
|
null,
|
84
|
new ParameterizedTypeReference<List<BrowseEntry>>() {
|
85
|
});
|
86
|
} catch (RestClientException e) {
|
87
|
throw new BrokerException(e);
|
88
|
}
|
89
|
|
90
|
return resp.getBody();
|
91
|
}
|
92
|
|
93
|
@Override
|
94
|
public EventsPage advancedShowEvents(String params) throws BrokerException, JSONException {
|
95
|
JSONObject json_params = new JSONObject(params);
|
96
|
|
97
|
String page = json_params.getString("page");
|
98
|
String pagesize = json_params.getString("pagesize");
|
99
|
|
100
|
JSONObject json_advQueryObject = json_params.getJSONObject("advQueryObject");
|
101
|
ObjectMapper obj = new ObjectMapper();
|
102
|
AdvQueryObject advQueryObject = new Gson().fromJson(json_advQueryObject.toString(),AdvQueryObject.class);
|
103
|
|
104
|
|
105
|
final String service = "/events/{page}/{pageSize}";
|
106
|
|
107
|
Map<String, Long> uriParams = new HashMap<>();
|
108
|
uriParams.put("page", Long.parseLong(page));
|
109
|
uriParams.put("pageSize", Long.parseLong(pagesize));
|
110
|
|
111
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service);
|
112
|
|
113
|
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
|
114
|
headers.add("Content-Type", "application/json");
|
115
|
|
116
|
advQueryObject.setPage(Long.parseLong(page));
|
117
|
|
118
|
HttpEntity<AdvQueryObject> entity = new HttpEntity<>(advQueryObject, headers);
|
119
|
|
120
|
RestTemplate template = new RestTemplate();
|
121
|
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
122
|
ResponseEntity<EventsPage> resp;
|
123
|
try {
|
124
|
resp = template.exchange(
|
125
|
builder.buildAndExpand(uriParams).encode().toUri(),
|
126
|
HttpMethod.POST,
|
127
|
entity,
|
128
|
new ParameterizedTypeReference<EventsPage>() {
|
129
|
}
|
130
|
);
|
131
|
} catch (RestClientException e) {
|
132
|
throw new BrokerException(e);
|
133
|
}
|
134
|
return resp.getBody();
|
135
|
|
136
|
|
137
|
}
|
138
|
|
139
|
|
140
|
private List<Tuple<BrowseEntry, String>> getDatasourcesOfUserType(List<Repository> repositories) throws BrokerException {
|
141
|
|
142
|
List<Tuple<BrowseEntry, String>> entries = new ArrayList<>();
|
143
|
for (Repository repo : repositories) {
|
144
|
BrowseEntry temp = new BrowseEntry();
|
145
|
temp.setValue(repo.getOfficialName());
|
146
|
temp.setSize(new Long(0));
|
147
|
for (BrowseEntry e : getTopicsForDatasource(repo.getOfficialName())) {
|
148
|
temp.setSize(temp.getSize() + e.getSize());
|
149
|
}
|
150
|
Tuple<BrowseEntry, String> tup = new Tuple<>(temp, repo.getLogoUrl());
|
151
|
entries.add(tup);
|
152
|
}
|
153
|
|
154
|
// sort the collection by the second field of the tuple which is size
|
155
|
Collections.sort(entries, new Comparator<Tuple<BrowseEntry, String>>() {
|
156
|
@Override
|
157
|
public int compare(Tuple<BrowseEntry, String> e1, Tuple<BrowseEntry, String> e2) {
|
158
|
return (int) (e2.getFirst().getSize().longValue() - e1.getFirst().getSize().longValue());
|
159
|
}
|
160
|
});
|
161
|
|
162
|
return entries;
|
163
|
}
|
164
|
|
165
|
|
166
|
|
167
|
private List<Repository> getRepositoriesOfUser(String userEmail) throws JSONException {
|
168
|
|
169
|
int page = 1;
|
170
|
int size = 10;
|
171
|
List<Repository> rs = null;
|
172
|
List<Repository> resultSet = new ArrayList<>();
|
173
|
|
174
|
while (true){
|
175
|
rs = repoAPI.getRepositoriesOfUser(userEmail, String.valueOf(page), String.valueOf(size));
|
176
|
if(rs.size() == 0) break;
|
177
|
resultSet.addAll(rs);
|
178
|
}
|
179
|
return resultSet;
|
180
|
}
|
181
|
|
182
|
private List<Repository> getRepositoriesByIds(List<String> sharedDatasourceIds) {
|
183
|
return null;
|
184
|
}
|
185
|
|
186
|
@Override
|
187
|
public EventsPage showEvents(String params) throws BrokerException, JSONException {
|
188
|
|
189
|
JSONObject json_params = new JSONObject(params);
|
190
|
|
191
|
String datasourceName = json_params.getString("datasourceName");
|
192
|
String topic = json_params.getString("topic");
|
193
|
String page = json_params.getString("page");
|
194
|
|
195
|
final String service = "/showEvents";
|
196
|
|
197
|
//build the uri params
|
198
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
|
199
|
.queryParam("ds", datasourceName)
|
200
|
.queryParam("topic", topic)
|
201
|
.queryParam("page", page);
|
202
|
|
203
|
//create new template engine
|
204
|
RestTemplate template = new RestTemplate();
|
205
|
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
206
|
ResponseEntity<EventsPage> resp;
|
207
|
try {
|
208
|
//communicate with endpoint
|
209
|
resp = template.exchange(
|
210
|
builder.build().encode().toUri(),
|
211
|
HttpMethod.GET,
|
212
|
null,
|
213
|
new ParameterizedTypeReference<EventsPage>() {
|
214
|
});
|
215
|
} catch (RestClientException e) {
|
216
|
throw new BrokerException(e);
|
217
|
}
|
218
|
return resp.getBody();
|
219
|
}
|
220
|
|
221
|
|
222
|
}
|