Project

General

Profile

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