Project

General

Profile

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.json.JSONException;
14
import org.json.JSONObject;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.beans.factory.annotation.Value;
17
import org.springframework.core.ParameterizedTypeReference;
18
import org.springframework.http.HttpEntity;
19
import org.springframework.http.HttpMethod;
20
import org.springframework.http.ResponseEntity;
21
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
22
import org.springframework.stereotype.Component;
23
import org.springframework.util.LinkedMultiValueMap;
24
import org.springframework.util.MultiValueMap;
25
import org.springframework.web.bind.annotation.PathVariable;
26
import org.springframework.web.client.RestClientException;
27
import org.springframework.web.client.RestTemplate;
28
import org.springframework.web.util.UriComponentsBuilder;
29

    
30
import java.util.*;
31

    
32
@Component
33
public class BrokerApiImpl implements BrokerApi {
34

    
35

    
36
    @Autowired
37
    private RepositoryApiImpl repoAPI;
38
    @Value("${services.broker.url}:${services.broker.port}/${services.broker.api}${services.broker.openaire}")
39
    private String openairePath;
40

    
41
    @Override
42
    public DatasourcesBroker getDatasourcesOfUser(String params) throws JSONException {
43
        JSONObject json_params = new JSONObject(params);
44
        DatasourcesBroker ret = new DatasourcesBroker();
45

    
46
        String userEmail = json_params.getString("userEmail");
47
        boolean includeShared = json_params.getBoolean("includeShared");
48
        boolean includeByOthers = json_params.getBoolean("includeByOthers");
49

    
50
        try {
51
            ret.setDatasourcesOfUser(getDatasourcesOfUserType(getRepositoriesOfUser(userEmail)));
52
            if (includeShared) {
53
                //TODO whatever nikonas was saying
54
                List<String> sharedDatasourceIds = new ArrayList<String>();
55
                ret.setSharedDatasources(getDatasourcesOfUserType(getRepositoriesByIds(sharedDatasourceIds)));
56
            }
57

    
58
            if (includeByOthers) {
59
                ret.setDatasourcesOfOthers(getDatasourcesOfUserType(getRepositoriesOfUser(userEmail)));
60
            }
61
        } catch (BrokerException e) {
62
            e.printStackTrace();
63
        }
64

    
65
        return ret;
66
    }
67

    
68
    @Override
69
    public List<BrowseEntry> getTopicsForDatasource(@PathVariable("datasourceName")  String datasourceName) throws BrokerException {
70
        final String service = "/topicsForDatasource";
71

    
72
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
73
                .queryParam("ds", datasourceName);
74

    
75
        RestTemplate template = new RestTemplate();
76
        template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
77
        ResponseEntity<List<BrowseEntry>> resp;
78
        try {
79
            resp = template.exchange(
80
                    builder.build().encode().toUri(),
81
                    HttpMethod.GET,
82
                    null,
83
                    new ParameterizedTypeReference<List<BrowseEntry>>() {
84
                    });
85
        } catch (RestClientException e) {
86
            throw new BrokerException(e);
87
        }
88

    
89
        return resp.getBody();
90
    }
91

    
92
    @Override
93
    public EventsPage advancedShowEvents(String params) throws BrokerException, JSONException {
94
        JSONObject json_params = new JSONObject(params);
95

    
96
        long page = json_params.getLong("page");
97
        long pagesize = json_params.getLong("pagesize");
98

    
99
        JSONObject json_advQueryObject = json_params.getJSONObject("advQueryObject");
100
        ObjectMapper obj = new ObjectMapper();
101
        AdvQueryObject advQueryObject = new Gson().fromJson(json_advQueryObject.toString(),AdvQueryObject.class);
102

    
103

    
104
        final String service = "/events/{page}/{pageSize}";
105

    
106
        Map<String, Long> uriParams = new HashMap<>();
107
        uriParams.put("page", page);
108
        uriParams.put("pageSize", pagesize);
109

    
110
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service);
111

    
112
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
113
        headers.add("Content-Type", "application/json");
114

    
115
        advQueryObject.setPage(page);
116

    
117
        HttpEntity<AdvQueryObject> entity = new HttpEntity<>(advQueryObject, headers);
118

    
119
        RestTemplate template = new RestTemplate();
120
        template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
121
        ResponseEntity<EventsPage> resp;
122
        try {
123
            resp = template.exchange(
124
                    builder.buildAndExpand(uriParams).encode().toUri(),
125
                    HttpMethod.POST,
126
                    entity,
127
                    new ParameterizedTypeReference<EventsPage>() {
128
                    }
129
            );
130
        } catch (RestClientException e) {
131
            throw new BrokerException(e);
132
        }
133
        return resp.getBody();
134

    
135

    
136
    }
137

    
138

    
139
    private List<Tuple<BrowseEntry, String>> getDatasourcesOfUserType(List<Repository> repositories) throws BrokerException {
140

    
141
        List<Tuple<BrowseEntry, String>> entries = new ArrayList<>();
142
        for (Repository repo : repositories) {
143
            BrowseEntry temp = new BrowseEntry();
144
            temp.setValue(repo.getOfficialName());
145
            temp.setSize(new Long(0));
146
            for (BrowseEntry e : getTopicsForDatasource(repo.getOfficialName())) {
147
                temp.setSize(temp.getSize() + e.getSize());
148
            }
149
            Tuple<BrowseEntry, String> tup = new Tuple<>(temp, repo.getLogoUrl());
150
            entries.add(tup);
151
        }
152

    
153
        // sort the collection by the second field of the tuple which is size
154
        Collections.sort(entries, new Comparator<Tuple<BrowseEntry, String>>() {
155
            @Override
156
            public int compare(Tuple<BrowseEntry, String> e1, Tuple<BrowseEntry, String> e2) {
157
                return (int) (e2.getFirst().getSize().longValue() - e1.getFirst().getSize().longValue());
158
            }
159
        });
160

    
161
        return entries;
162
    }
163

    
164

    
165

    
166
    private List<Repository> getRepositoriesOfUser(String userEmail) throws JSONException {
167

    
168
        int page = 1;
169
        int size = 10;
170
        List<Repository> rs = null;
171
        List<Repository> resultSet = new ArrayList<>();
172

    
173
        while (true){
174
            rs = repoAPI.getRepositoriesOfUser(userEmail, String.valueOf(page), String.valueOf(size));
175
            if(rs.size() == 0) break;
176
            resultSet.addAll(rs);
177
        }
178
        return resultSet;
179
    }
180

    
181
    private List<Repository> getRepositoriesByIds(List<String> sharedDatasourceIds) {
182
        return null;
183
    }
184

    
185
}
(2-2/8)