Project

General

Profile

« Previous | Next » 

Revision 61375

minor refactoring

View differences:

BrokerServiceImpl.java
2 2

  
3 3
import com.fasterxml.jackson.databind.JsonNode;
4 4
import com.fasterxml.jackson.databind.ObjectMapper;
5
import eu.dnetlib.domain.data.Repository;
6 5
import eu.dnetlib.repo.manager.domain.BrokerException;
7 6
import eu.dnetlib.repo.manager.domain.RepositorySnippet;
8 7
import eu.dnetlib.repo.manager.domain.Term;
......
44 43
            .getLogger(BrokerServiceImpl.class);
45 44

  
46 45
    @Autowired
47
    RestTemplate restTemplate ;
46
    RestTemplate restTemplate;
48 47

  
49 48
    private HttpHeaders httpHeaders;
50 49

  
51
    private HashMap<String,Term> topics = new HashMap<String, Term>();
50
    private HashMap<String, Term> topics = new HashMap<String, Term>();
52 51

  
53 52
    @Autowired
54 53
    private EmailUtils emailUtils;
......
60 59
        httpHeaders.set("Content-Type", "application/json");
61 60

  
62 61
        LOGGER.debug("Init dnet topics!");
63
        try (InputStream is = new URL(topicsURL).openStream() ){
62
        try (InputStream is = new URL(topicsURL).openStream()) {
64 63
            ObjectMapper mapper = new ObjectMapper();
65 64
            JsonNode root = mapper.readTree(is);
66
            for (JsonNode term : root.path("terms") )
65
            for (JsonNode term : root.path("terms"))
67 66
                topics.put(term.path("code").textValue(), parseTerm(term));
68 67
        } catch (IOException e) {
69
            LOGGER.debug("Exception on initDnetTopicsMap" , e);
68
            LOGGER.debug("Exception on initDnetTopicsMap", e);
70 69
            emailUtils.reportException(e);
71 70
        }
72 71
    }
73 72

  
74 73
    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());
74
        return new Term(term.path("englishName").textValue(), term.path("nativeName").textValue(),
75
                term.path("encoding").textValue(), term.path("code").textValue());
77 76
    }
78 77

  
79 78

  
80 79
    @Override
81
    public DatasourcesBroker getDatasourcesOfUser(String user,String includeShared,String includeByOthers) throws JSONException {
80
    public DatasourcesBroker getDatasourcesOfUser(String user, String includeShared, String includeByOthers) throws JSONException {
82 81
        long start = System.currentTimeMillis();
83 82
        DatasourcesBroker ret = new DatasourcesBroker();
84 83
        try {
85
            ret.setDatasourcesOfUser(getDatasourcesOfUserType(repoAPI.getRepositoriesSnippetOfUser(user,"0","100")));
84
            ret.setDatasourcesOfUser(getDatasourcesOfUserType(repoAPI.getRepositoriesSnippetOfUser(user, "0", "100")));
86 85
            //TODO fix bug when values are true
87 86
//            if (Boolean.parseBoolean(includeShared)) {
88 87
//                List<String> sharedDatasourceIds = new ArrayList<String>();
......
93 92
//                ret.setDatasourcesOfOthers(getDatasourcesOfUserType(getRepositoriesOfUser(user)));
94 93
//            }
95 94
        } catch (Exception e) {
96
            LOGGER.debug("Exception on getDatasourcesOfUser" , e);
95
            LOGGER.debug("Exception on getDatasourcesOfUser", e);
97 96
            emailUtils.reportException(e);
98 97
        }
99 98
        long end = System.currentTimeMillis();
100
        System.out.println("Getting datasources of user in " + (end-start)+"ms");
99
        System.out.println("Getting datasources of user in " + (end - start) + "ms");
101 100
        return ret;
102 101
    }
103 102

  
......
117 116
                    new ParameterizedTypeReference<List<BrowseEntry>>() {
118 117
                    });
119 118
        } catch (RestClientException e) {
120
            LOGGER.debug("Exception on getTopicsForDatasource" , e);
119
            LOGGER.debug("Exception on getTopicsForDatasource", e);
121 120
            emailUtils.reportException(e);
122 121
            throw new BrokerException(e);
123 122
        }
......
128 127
    @Override
129 128
    public EventsPage advancedShowEvents(String page,
130 129
                                         String size,
131
                                         AdvQueryObject advQueryObject) throws BrokerException, JSONException ,IOException {
130
                                         AdvQueryObject advQueryObject) throws BrokerException, JSONException, IOException {
132 131

  
133 132
        final String service = "/events/{page}/{pageSize}";
134 133

  
......
151 150
                    }
152 151
            );
153 152
        } catch (RestClientException e) {
154
            LOGGER.debug("Exception on advancedShowEvents" , e);
153
            LOGGER.debug("Exception on advancedShowEvents", e);
155 154
            emailUtils.reportException(e);
156 155
            throw new BrokerException(e);
157 156
        }
......
166 165
        List<Tuple<BrowseEntry, String>> entries = new ArrayList<>();
167 166
        for (RepositorySnippet repo : repositories) {
168 167
            BrowseEntry temp = new BrowseEntry();
169
            temp.setValue(repo.getOfficialname());
168
            temp.setValue(repo.getOfficialName());
170 169
            temp.setSize(new Long(0));
171
            for (BrowseEntry e : getTopicsForDatasource(repo.getOfficialname())) {
170
            for (BrowseEntry e : getTopicsForDatasource(repo.getOfficialName())) {
172 171
                temp.setSize(temp.getSize() + e.getSize());
173 172
            }
174 173
            Tuple<BrowseEntry, String> tup = new Tuple<>(temp, repo.getLogoUrl());
......
183 182
            }
184 183
        });
185 184
        long stop = System.currentTimeMillis();
186
        System.out.println("getDatasourcesOfUserType returned in " + (stop-start) + "ms ");
185
        System.out.println("getDatasourcesOfUserType returned in " + (stop - start) + "ms ");
187 186

  
188 187
        return entries;
189 188
    }
190 189

  
191
    private List<Repository> getRepositoriesOfUser(String userEmail) throws JSONException {
192

  
193
        int page = 0;
194
        int size = 50;
195
        List<Repository> rs ;
196
        List<Repository> resultSet = new ArrayList<>();
197

  
198
        while (true){
199
            rs = repoAPI.getRepositoriesOfUser(userEmail, String.valueOf(page), String.valueOf(size));
200
            resultSet.addAll(rs);
201
            page+=1;
202
            if(rs.size() == 0) break;
203
        }
204
        return resultSet;
205
    }
206

  
207
    private List<Repository> getRepositoriesByIds(List<String> sharedDatasourceIds) {
208
        return null;
209
    }
210

  
211 190
    @Override
212 191
    public EventsPage showEvents(String datasourceName,
213 192
                                 String topic,
......
230 209
                    new ParameterizedTypeReference<EventsPage>() {
231 210
                    });
232 211
        } catch (RestClientException e) {
233
            LOGGER.debug("Exception on showEvents" , e);
212
            LOGGER.debug("Exception on showEvents", e);
234 213
            emailUtils.reportException(e);
235 214
            throw new BrokerException(e);
236 215
        }
......
256 235
                    new ParameterizedTypeReference<Map<String, List<SimpleSubscriptionDesc>>>() {
257 236
                    });
258 237
        } catch (RestClientException e) {
259
            LOGGER.debug("Exception on getSimpleSubscriptionsOfUser" , e);
238
            LOGGER.debug("Exception on getSimpleSubscriptionsOfUser", e);
260 239
            emailUtils.reportException(e);
261 240
            throw new BrokerException(e);
262 241
        }
......
292 271
                    new ParameterizedTypeReference<Subscription>() {
293 272
                    });
294 273
        } catch (RestClientException e) {
295
            LOGGER.debug("Exception on OpenaireSubscription" , e);
274
            LOGGER.debug("Exception on OpenaireSubscription", e);
296 275
            emailUtils.reportException(e);
297 276
            throw new BrokerException(e);
298 277
        }
......
316 295
                    new ParameterizedTypeReference<Void>() {
317 296
                    });
318 297
        } catch (RestClientException e) {
319
            LOGGER.debug("Exception on unsubscribe" , e);
298
            LOGGER.debug("Exception on unsubscribe", e);
320 299
            emailUtils.reportException(e);
321 300
            throw new BrokerException(e);
322 301
        }
323
        return new ResponseEntity<>("OK",HttpStatus.OK);
302
        return new ResponseEntity<>("OK", HttpStatus.OK);
324 303
    }
325 304

  
326 305
    @Override
327
    public Subscription getSubscription( String subscriptionId) throws BrokerException {
306
    public Subscription getSubscription(String subscriptionId) throws BrokerException {
328 307
        final String service = "/subscriptions/" + subscriptionId;
329 308

  
330 309
        //build the uri params
......
340 319
                    new ParameterizedTypeReference<Subscription>() {
341 320
                    });
342 321
        } catch (RestClientException e) {
343
            LOGGER.debug("Exception on getSubscription" , e);
322
            LOGGER.debug("Exception on getSubscription", e);
344 323
            emailUtils.reportException(e);
345 324
            throw new BrokerException(e);
346 325
        }
......
360 339
        UriComponents uriComponents = UriComponentsBuilder
361 340
                .fromHttpUrl(openairePath + "/notifications/")
362 341
                .path("/{id}/{page}/{size}/")
363
                .build().expand(subscriptionId,page, size).encode();
342
                .build().expand(subscriptionId, page, size).encode();
364 343

  
365 344
        ResponseEntity<EventsPage> resp;
366 345
        try {
......
371 350
                    new ParameterizedTypeReference<EventsPage>() {
372 351
                    });
373 352
        } catch (RestClientException e) {
374
            LOGGER.debug("Exception on getNotificationsBySubscriptionId" , e);
353
            LOGGER.debug("Exception on getNotificationsBySubscriptionId", e);
375 354
            emailUtils.reportException(e);
376 355
            throw new BrokerException(e);
377 356
        }
......
383 362
            throws BrokerException {
384 363

  
385 364
        Map<String, List<SimpleSubscriptionDesc>> simpleSubs = getSimpleSubscriptionsOfUser(userEmail);
386
        Map<String,List<Subscription>> subs = new HashMap<>();
365
        Map<String, List<Subscription>> subs = new HashMap<>();
387 366
        List<Subscription> subscriptions = null;
388 367

  
389
        for(String s:simpleSubs.keySet()){
368
        for (String s : simpleSubs.keySet()) {
390 369
            List<SimpleSubscriptionDesc> simpleSubscriptionDescs = simpleSubs.get(s);
391
            for(SimpleSubscriptionDesc simpleSubscriptionDesc : simpleSubscriptionDescs) {
370
            for (SimpleSubscriptionDesc simpleSubscriptionDesc : simpleSubscriptionDescs) {
392 371
                subscriptions = new ArrayList<>();
393 372
                subscriptions.add(getSubscription(simpleSubscriptionDesc.getId()));
394 373
            }
395
            subs.put(s,subscriptions);
374
            subs.put(s, subscriptions);
396 375
        }
397 376
        return subs;
398 377
    }

Also available in: Unified diff