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 49236 panagiotis
import eu.dnetlib.domain.data.Repository;
6 57741 ioannis.di
import eu.dnetlib.repo.manager.domain.BrokerException;
7 57891 ioannis.di
import eu.dnetlib.repo.manager.domain.RepositorySnippet;
8 57741 ioannis.di
import eu.dnetlib.repo.manager.domain.Term;
9
import eu.dnetlib.repo.manager.domain.Tuple;
10
import eu.dnetlib.repo.manager.domain.broker.*;
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
    RestTemplate restTemplate ;
48 50319 panagiotis
49 50383 panagiotis
    private HttpHeaders httpHeaders;
50
51 50319 panagiotis
    private HashMap<String,Term> topics = new HashMap<String, Term>();
52
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 56661 ioannis.di
        try (InputStream is = new URL(topicsURL).openStream() ){
64 50319 panagiotis
            ObjectMapper mapper = new ObjectMapper();
65
            JsonNode root = mapper.readTree(is);
66
            for (JsonNode term : root.path("terms") )
67
                topics.put(term.path("code").textValue(), parseTerm(term));
68
        } catch (IOException e) {
69 51831 panagiotis
            LOGGER.debug("Exception on initDnetTopicsMap" , e);
70
            emailUtils.reportException(e);
71 50319 panagiotis
        }
72
    }
73
74
    private Term parseTerm(JsonNode term) {
75
        return new Term(term.path("englishName").textValue(),term.path("nativeName").textValue(),
76
                term.path("encoding").textValue(),term.path("code").textValue());
77
    }
78
79
80 49236 panagiotis
    @Override
81 54525 panagiotis
    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 57891 ioannis.di
            ret.setDatasourcesOfUser(getDatasourcesOfUserType(repoAPI.getRepositoriesSnippetOfUser(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
        } catch (BrokerException | IOException e) {
96 51831 panagiotis
            LOGGER.debug("Exception on getDatasourcesOfUser" , e);
97
            emailUtils.reportException(e);
98 49236 panagiotis
        }
99 57741 ioannis.di
        long end = System.currentTimeMillis();
100
        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 51831 panagiotis
            LOGGER.debug("Exception on getTopicsForDatasource" , e);
121
            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
                                         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 51831 panagiotis
            LOGGER.debug("Exception on advancedShowEvents" , e);
155
            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
        System.out.println("getDatasourcesOfUserType returned in " + (stop-start) + "ms ");
187 49236 panagiotis
188
        return entries;
189
    }
190
191 49362 panagiotis
    private List<Repository> getRepositoriesOfUser(String userEmail) throws JSONException {
192 49245 panagiotis
193 49898 panagiotis
        int page = 0;
194
        int size = 50;
195 49988 panagiotis
        List<Repository> rs ;
196 49245 panagiotis
        List<Repository> resultSet = new ArrayList<>();
197
198
        while (true){
199
            rs = repoAPI.getRepositoriesOfUser(userEmail, String.valueOf(page), String.valueOf(size));
200 49988 panagiotis
            resultSet.addAll(rs);
201
            page+=1;
202 49245 panagiotis
            if(rs.size() == 0) break;
203 49236 panagiotis
        }
204 49245 panagiotis
        return resultSet;
205
    }
206 49236 panagiotis
207 49245 panagiotis
    private List<Repository> getRepositoriesByIds(List<String> sharedDatasourceIds) {
208
        return null;
209 49236 panagiotis
    }
210
211 49852 panagiotis
    @Override
212 54525 panagiotis
    public EventsPage showEvents(String datasourceName,
213
                                 String topic,
214
                                 String page,
215
                                 String size) throws BrokerException, JSONException {
216 49852 panagiotis
217 50860 panagiotis
        final String service = "/events";
218 49852 panagiotis
219
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
220
                .queryParam("ds", datasourceName)
221
                .queryParam("topic", topic)
222 50860 panagiotis
                .path("/{page}/{size}/");
223 49852 panagiotis
224
        ResponseEntity<EventsPage> resp;
225
        try {
226 50383 panagiotis
            resp = restTemplate.exchange(
227 50860 panagiotis
                    builder.build().expand(page, size).encode().toUri(),
228 49852 panagiotis
                    HttpMethod.GET,
229
                    null,
230
                    new ParameterizedTypeReference<EventsPage>() {
231
                    });
232
        } catch (RestClientException e) {
233 51831 panagiotis
            LOGGER.debug("Exception on showEvents" , e);
234
            emailUtils.reportException(e);
235 49852 panagiotis
            throw new BrokerException(e);
236
        }
237
        return resp.getBody();
238
    }
239
240 49868 panagiotis
    @Override
241 54525 panagiotis
    public Map<String, List<SimpleSubscriptionDesc>> getSimpleSubscriptionsOfUser(String userEmail)
242 50686 panagiotis
            throws BrokerException {
243 49852 panagiotis
244 49868 panagiotis
        final String service = "/subscriptions";
245
246
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
247
                .queryParam("email", userEmail);
248
249 50686 panagiotis
        LOGGER.debug(builder.build().encode().toUri());
250 49868 panagiotis
        ResponseEntity<Map<String, List<SimpleSubscriptionDesc>>> resp;
251
        try {
252 50383 panagiotis
            resp = restTemplate.exchange(
253 49868 panagiotis
                    builder.build().encode().toUri(),
254
                    HttpMethod.GET,
255
                    null,
256
                    new ParameterizedTypeReference<Map<String, List<SimpleSubscriptionDesc>>>() {
257
                    });
258
        } catch (RestClientException e) {
259 51831 panagiotis
            LOGGER.debug("Exception on getSimpleSubscriptionsOfUser" , e);
260
            emailUtils.reportException(e);
261 49868 panagiotis
            throw new BrokerException(e);
262
        }
263
        return resp.getBody();
264
    }
265
266
    @Override
267 57176 ioannis.di
    public Map<String, List<SimpleSubscriptionDesc>> getSimpleSubscriptionsOfUserByRepoId(String userEmail, String repoId) throws BrokerException {
268
        Map<String, List<SimpleSubscriptionDesc>> subscriptionsOfUser = getSimpleSubscriptionsOfUser(userEmail);
269
270
        return null;
271
    }
272
273
    @Override
274 54525 panagiotis
    public Subscription subscribe(OpenaireSubscription obj) throws BrokerException {
275 49868 panagiotis
        final String service = "/subscribe";
276
277
        //build the uri params
278
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service);
279
280 50383 panagiotis
        HttpEntity<OpenaireSubscription> entity = new HttpEntity<>(obj, httpHeaders);
281 49868 panagiotis
282
        //create new template engine
283
        RestTemplate template = new RestTemplate();
284
        template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
285
        ResponseEntity<Subscription> resp;
286
        try {
287
            //communicate with endpoint
288 50383 panagiotis
            resp = restTemplate.exchange(
289 49868 panagiotis
                    builder.build().encode().toUri(),
290
                    HttpMethod.POST,
291
                    entity,
292
                    new ParameterizedTypeReference<Subscription>() {
293
                    });
294
        } catch (RestClientException e) {
295 51831 panagiotis
            LOGGER.debug("Exception on OpenaireSubscription" , e);
296
            emailUtils.reportException(e);
297 49868 panagiotis
            throw new BrokerException(e);
298
        }
299
300
        return resp.getBody();
301
    }
302
303
    @Override
304 54525 panagiotis
    public ResponseEntity<Object> unsubscribe(String subscriptionId) throws BrokerException {
305 49868 panagiotis
        final String service = "/subscriptions/" + subscriptionId;
306
307
        //build the uri params
308
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service);
309
310
        try {
311
            //communicate with endpoint
312 50383 panagiotis
            restTemplate.exchange(
313 49868 panagiotis
                    builder.build().encode().toUri(),
314
                    HttpMethod.DELETE,
315
                    null,
316
                    new ParameterizedTypeReference<Void>() {
317
                    });
318
        } catch (RestClientException e) {
319 51831 panagiotis
            LOGGER.debug("Exception on unsubscribe" , e);
320
            emailUtils.reportException(e);
321 49868 panagiotis
            throw new BrokerException(e);
322
        }
323 52781 panagiotis
        return new ResponseEntity<>("OK",HttpStatus.OK);
324 49868 panagiotis
    }
325
326
    @Override
327 54525 panagiotis
    public Subscription getSubscription( String subscriptionId) throws BrokerException {
328 49868 panagiotis
        final String service = "/subscriptions/" + subscriptionId;
329
330
        //build the uri params
331
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service);
332
333
        ResponseEntity<Subscription> resp;
334
        try {
335
            //communicate with endpoint
336 50383 panagiotis
            resp = restTemplate.exchange(
337 49868 panagiotis
                    builder.build().encode().toUri(),
338
                    HttpMethod.GET,
339
                    null,
340
                    new ParameterizedTypeReference<Subscription>() {
341
                    });
342
        } catch (RestClientException e) {
343 51831 panagiotis
            LOGGER.debug("Exception on getSubscription" , e);
344
            emailUtils.reportException(e);
345 49868 panagiotis
            throw new BrokerException(e);
346
        }
347
        return resp.getBody();
348
    }
349
350 50319 panagiotis
    @Override
351
    public Map<String, Term> getDnetTopics() throws BrokerException {
352
        return topics;
353
    }
354 49868 panagiotis
355 50383 panagiotis
    @Override
356 54525 panagiotis
    public EventsPage getNotificationsBySubscriptionId(String subscriptionId,
357
                                                       String page,
358
                                                       String size) throws BrokerException {
359 49868 panagiotis
360 50383 panagiotis
        UriComponents uriComponents = UriComponentsBuilder
361 50570 panagiotis
                .fromHttpUrl(openairePath + "/notifications/")
362 50383 panagiotis
                .path("/{id}/{page}/{size}/")
363
                .build().expand(subscriptionId,page, size).encode();
364
365
        ResponseEntity<EventsPage> resp;
366
        try {
367
            resp = restTemplate.exchange(
368
                    uriComponents.toUri(),
369
                    HttpMethod.GET,
370
                    null,
371
                    new ParameterizedTypeReference<EventsPage>() {
372
                    });
373
        } catch (RestClientException e) {
374 51831 panagiotis
            LOGGER.debug("Exception on getNotificationsBySubscriptionId" , e);
375
            emailUtils.reportException(e);
376 50383 panagiotis
            throw new BrokerException(e);
377
        }
378
        return resp.getBody();
379
    }
380
381 50945 panagiotis
    //@Override
382 54525 panagiotis
    public Map<String, List<Subscription>> getSubscriptionsOfUser(String userEmail)
383 50409 panagiotis
            throws BrokerException {
384 50383 panagiotis
385 50409 panagiotis
        Map<String, List<SimpleSubscriptionDesc>> simpleSubs = getSimpleSubscriptionsOfUser(userEmail);
386
        Map<String,List<Subscription>> subs = new HashMap<>();
387
        List<Subscription> subscriptions = null;
388
389
        for(String s:simpleSubs.keySet()){
390
            List<SimpleSubscriptionDesc> simpleSubscriptionDescs = simpleSubs.get(s);
391
            for(SimpleSubscriptionDesc simpleSubscriptionDesc : simpleSubscriptionDescs) {
392
                subscriptions = new ArrayList<>();
393
                subscriptions.add(getSubscription(simpleSubscriptionDesc.getId()));
394
            }
395
            subs.put(s,subscriptions);
396
        }
397
        return subs;
398
    }
399
400
401 49236 panagiotis
}