Project

General

Profile

1
package eu.dnetlib.repo.manager.server.services;
2

    
3
import com.fasterxml.jackson.databind.ObjectMapper;
4
import eu.dnetlib.domain.data.Repository;
5
import eu.dnetlib.gwt.server.service.SpringGwtRemoteServiceServlet;
6
import eu.dnetlib.repo.manager.client.services.BrokerService;
7
import eu.dnetlib.repo.manager.service.controllers.BrokerApi;
8
import eu.dnetlib.repo.manager.service.controllers.RepositoryApi;
9
import eu.dnetlib.repo.manager.shared.BrokerException;
10
import eu.dnetlib.repo.manager.shared.Term;
11
import eu.dnetlib.repo.manager.shared.Tuple;
12
import eu.dnetlib.repo.manager.shared.broker.*;
13
import eu.dnetlib.repos.RepoApi;
14
import org.apache.log4j.Logger;
15
import org.json.JSONException;
16
import org.json.JSONObject;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.beans.factory.annotation.Value;
19
import org.springframework.stereotype.Service;
20

    
21
import javax.servlet.ServletConfig;
22
import javax.servlet.ServletException;
23
import java.util.*;
24

    
25
/**
26
 * Created by stefanos on 10/26/16.
27
 */
28
@SuppressWarnings("serial")
29
@Service("brokerService")
30
public class BrokerServiceImpl extends SpringGwtRemoteServiceServlet implements BrokerService {
31

    
32
    private static final Logger LOGGER = Logger
33
            .getLogger(BrokerServiceImpl.class);
34

    
35
    @Autowired
36
    private RepoApi repoAPI;
37

    
38

    
39
    @Autowired
40
    private RepositoryApi repositoryApi;
41
    @Autowired
42
    private BrokerApi brokerApi;
43

    
44

    
45
    @Override
46
    public void init(ServletConfig config) throws ServletException {
47
        super.init(config);
48
        LOGGER.info("broker service init");
49
    }
50

    
51
    @Value("${services.broker.url}:${services.broker.port}/${services.broker.api}${services.broker.openaire}")
52
    private String openairePath;
53
    @Value("${services.broker.url}:${services.broker.port}/${services.broker.api}")
54
    private String apiPath;
55

    
56
    /**
57
     * @param datasourceName the name of the data source
58
     * @return a list of BrowseEvent entries for that datasource
59
     * @throws BrokerException containing the error code from the server
60
     * @author stefanos
61
     */
62
    @Override
63
    public List<BrowseEntry> getTopicsForDatasource(String datasourceName) throws BrokerException {
64
        return brokerApi.getTopicsForDatasource(datasourceName);
65
    }
66

    
67
    /**
68
     * @param datasourceName the name of the datasource
69
     * @param topic          the name of the topic to filter
70
     * @param page           the page number
71
     * @return an Events page with a constant 50 max number of entires
72
     * @throws BrokerException containing the error code from the server
73
     */
74
    @Override
75
    public EventsPage showEvents(String datasourceName, String topic, long page) throws BrokerException{
76
        JSONObject params = new JSONObject();
77
        try {
78
            params.put("datasourceName",datasourceName);
79
            params.put("topic",topic);
80
            params.put("page",String.valueOf(page));
81
            return brokerApi.showEvents(params.toString());
82
        } catch (JSONException e) {
83
            LOGGER.debug("Error on show events",e);
84
        }
85
        return null;
86
    }
87

    
88
    /**
89
     * @param advQueryObject a pojo class containing the filter parameters
90
     * @param page           the number of the page
91
     * @param pageSize       the page size
92
     * @return
93
     * @throws BrokerException
94
     */
95
    @Override
96
    public EventsPage advancedShowEvents(AdvQueryObject advQueryObject, long page, long pageSize) throws BrokerException {
97

    
98
        JSONObject jsonObject = new JSONObject();
99
        try {
100
            jsonObject.put("page",String.valueOf(page));
101
            jsonObject.put("pagesize",String.valueOf(pageSize));
102
            ObjectMapper mapper = new ObjectMapper();
103
            String json_advQueryObject = mapper.writeValueAsString(advQueryObject);
104
            jsonObject.put("advQueryObject",json_advQueryObject);
105
            return brokerApi.advancedShowEvents(jsonObject.toString());
106
        } catch (Exception e) {
107
            LOGGER.debug("Error on advanced show events",e);
108
        }
109
        return null;
110
    }
111

    
112
    @Override
113
    public DatasourcesBroker getDatasourcesOfUser(String userEmail, boolean includeShared, boolean includeByOthers)
114
            throws BrokerException {
115

    
116
        JSONObject params = new JSONObject();
117
        try {
118
            params.put("userEmail",userEmail);
119
            params.put("includeShared",includeShared);
120
            params.put("includeByOthers",includeByOthers);
121
            return brokerApi.getDatasourcesOfUser(params.toString());
122
        } catch (JSONException e) {
123
            LOGGER.debug("Error on get datasources of user",e);
124
        }
125
        return null;
126
      /*  DatasourcesBroker ret = new DatasourcesBroker();
127
        try {
128
            LOGGER.debug("In getDatasourcesOfUser");
129

    
130
            //set for user
131
            ret.setDatasourcesOfUser(getDatasourcesOfUserType(this.repoAPI.getRepositoriesOfUser(userEmail, false)));
132

    
133
            //set for shared
134
            if (includeShared) {
135
                //TODO whatever nikonas was saying
136
                List<String> sharedDatasourceIds = new ArrayList<String>();
137
                ret.setSharedDatasources(getDatasourcesOfUserType(this.repoAPI.getReposByIds(sharedDatasourceIds)));
138
            }
139

    
140
            //set others
141
            if (includeByOthers) {
142
                ret.setDatasourcesOfOthers(getDatasourcesOfUserType(this.repoAPI.getRepositoriesOfUser(userEmail, true)));
143
            }
144

    
145
        } catch (RepositoryServiceException e) {
146
            e.printStackTrace();
147
            throw new BrokerException(e);
148
        } catch (Exception e) {
149
            e.printStackTrace();
150
        }
151
        return ret;*/
152
    }
153

    
154
    @Override
155
    public Map<String, List<SimpleSubscriptionDesc>> getSimpleSubscriptionsOfUser(String userEmail) throws BrokerException {
156
        return brokerApi.getSimpleSubscriptionsOfUser(userEmail);
157
    }
158

    
159
    @Override
160
    public Subscription subscribe(OpenaireSubscription obj) throws BrokerException {
161
        return brokerApi.subscribe(obj);
162
    }
163

    
164
    @Override
165
    public void unsubscribe(String subscriptionId) throws BrokerException {
166
        brokerApi.unsubscribe(subscriptionId);
167
    }
168

    
169
    @Override
170
    public Subscription getSubscription(String subscriptionId) throws BrokerException {
171
        return brokerApi.getSubscription(subscriptionId);
172
    }
173

    
174
    @Override
175
    public void unsubscribe(List<String> subscriptionIds) throws BrokerException {
176
        for (String subscriptionId : subscriptionIds) {
177
            unsubscribe(subscriptionId);
178
        }
179
    }
180

    
181
    public Map<String,Term> getDnetTopics() throws BrokerException {
182
        return brokerApi.getDnetTopics();
183
    }
184

    
185
    /**
186
     * Helper class to aggregate the datasources topic sizes by datasource name.
187
     *
188
     * @param repositories to be aggregated
189
     * @return a List of BrowseEntry with the official name and the number of topics for each repo and logo url
190
     * @throws BrokerException
191
     */
192
    private List<Tuple<BrowseEntry, String>> getDatasourcesOfUserType(List<Repository> repositories) throws BrokerException {
193

    
194
        //get user entries
195
        LOGGER.debug("getDatasourcesOfUserType : " + repositories.size());
196
        List<Tuple<BrowseEntry, String>> entries = new ArrayList<>();
197
        for (Repository repo : repositories) {
198
            BrowseEntry temp = new BrowseEntry();
199
            temp.setValue(repo.getOfficialName());
200
            temp.setSize(new Long(0));
201
            for (BrowseEntry e : this.getTopicsForDatasource(repo.getOfficialName())) {
202
                temp.setSize(temp.getSize() + e.getSize());
203
            }
204
            Tuple<BrowseEntry, String> tup = new Tuple<>(temp, repo.getLogoUrl());
205
            entries.add(tup);
206
        }
207

    
208
        // sort the collection by the second field of the tuple which is size
209
        Collections.sort(entries, new Comparator<Tuple<BrowseEntry, String>>() {
210
            @Override
211
            public int compare(Tuple<BrowseEntry, String> e1, Tuple<BrowseEntry, String> e2) {
212
                return (int) (e2.getFirst().getSize().longValue() - e1.getFirst().getSize().longValue());
213
            }
214
        });
215

    
216
        return entries;
217
    }
218

    
219
    @Override
220
    public EventsPage getNotificationsBySubscriptionId(String subscriptionId,String page,String size) throws BrokerException {
221
        return brokerApi.getNotificationsBySubscriptionId(subscriptionId,page,size);
222
    }
223

    
224
    @Override
225
    public Map<String, List<Subscription>> getSubscriptionsOfUser(String userEmail) throws BrokerException {
226
        return brokerApi.getSubscriptionsOfUser(userEmail);
227
    }
228

    
229
}
(2-2/6)