Project

General

Profile

1
package eu.dnetlib.repo.manager.service.controllers;
2

    
3
import eu.dnetlib.domain.enabling.Vocabulary;
4
import eu.dnetlib.repo.manager.service.utils.Converter;
5
import eu.dnetlib.domain.data.Repository;
6
import eu.dnetlib.domain.data.RepositoryInterface;
7
import gr.uoa.di.driver.enabling.vocabulary.VocabularyLoader;
8
import org.apache.log4j.Logger;
9
import org.json.JSONArray;
10
import org.json.JSONException;
11
import org.json.JSONObject;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.beans.factory.annotation.Value;
14
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
15
import org.springframework.stereotype.Component;
16
import org.springframework.web.bind.annotation.PathVariable;
17
import org.springframework.web.bind.annotation.RequestBody;
18
import org.springframework.web.bind.annotation.RequestParam;
19
import org.springframework.web.client.RestTemplate;
20
import org.springframework.web.util.UriComponents;
21
import org.springframework.web.util.UriComponentsBuilder;
22
import javax.annotation.PostConstruct;
23
import java.util.*;
24
import java.util.concurrent.ConcurrentHashMap;
25
import eu.dnetlib.repo.manager.shared.*;
26

    
27
@Component
28
public class RepositoryApiImpl implements RepositoryApi {
29

    
30
    @Value("${api.baseAddress}")
31
    private String baseAddress;
32

    
33
    private RestTemplate restTemplate = null;
34

    
35
    private final String[] vocabularyNames = {"dnet:countries", "dnet:datasource_typologies", "dnet:compatibilityLevel"};
36

    
37
    private static final Logger LOGGER = Logger.getLogger(RepositoryApiImpl.class);
38

    
39
    @Autowired
40
    private VocabularyLoader vocabularyLoader;
41

    
42
    private Map<String, Vocabulary> vocabularyMap = new ConcurrentHashMap<String, Vocabulary>();
43

    
44

    
45
    @PostConstruct
46
    private void init(){
47
        LOGGER.debug("Initialization method of repository api!");
48

    
49
        restTemplate = new RestTemplate();
50
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
51

    
52
        for (String vocName : vocabularyNames) {
53
            vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT));
54
        }
55

    
56
    }
57

    
58
    @Override
59
    public Aggregations testAggregations() throws JSONException {
60

    
61
        int page = 1;
62
        int size = 1000;
63

    
64
        UriComponents uriComponents = UriComponentsBuilder
65
                .fromHttpUrl(baseAddress + "/ds/list/")
66
                .path("/{page}/{size}/")
67
                .build().expand(page,size).encode();
68

    
69
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
70
        while(!rs.equals("[]")){
71

    
72
            Aggregations aggregations = getFirstNonEmptyAggregation(rs);
73
            if(aggregations != null)
74
                return aggregations;
75

    
76
            LOGGER.debug("Checked " + page*size + " records!");
77

    
78
            page+=1;
79
            uriComponents = UriComponentsBuilder
80
                    .fromHttpUrl(baseAddress + "/ds/list/")
81
                    .path("/{page}/{size}/")
82
                    .build().expand(page,size).encode();
83
            rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
84
        }
85
        
86
        return null;
87
    }
88

    
89
    private Aggregations getFirstNonEmptyAggregation(String rs) throws JSONException {
90

    
91
        JSONArray ids = new JSONArray(rs);
92
        for(int i=0;i<ids.length();i++){
93
            String id = ids.getString(i);
94
            Aggregations aggregations = getRepositoryAggregations(id);
95
            if(aggregations.getAggregationHistory() != null)
96
                return aggregations;
97
        }
98
        return null;
99
    }
100

    
101
    @Override
102
    public Country[] getCountries()  {
103
        UriComponents uriComponents = UriComponentsBuilder
104
                .fromHttpUrl(baseAddress + "/ds/countries")
105
                .build().encode();
106
        return restTemplate.getForObject(uriComponents.toUri(),Country[].class);
107
    }
108

    
109

    
110
    @Override
111
    public Set<String> getRepositoriesByCountry(@PathVariable("country") String country) throws JSONException {
112

    
113

    
114
        int page = 1;
115
        int size = 10;
116
        String mode = "openaire____::opendoar";
117

    
118
        UriComponents uriComponents = UriComponentsBuilder
119
                                            .fromHttpUrl(baseAddress + "/ds/search/country/")
120
                                            .path("/{page}/{size}/")
121
                                            .queryParam("country",country)
122
                                            .build().expand(page,size).encode();
123

    
124
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
125
        Set<String> resultSet = new HashSet<>();
126
        while(!rs.equals("[]")){
127
            List<Repository> rep = Converter.jsonToRepositoryList(new JSONArray(rs));
128

    
129
            Collection<String> repos = this.getRepositoriesByMode(mode,rep);
130
            this.addRepos(resultSet,repos);
131
            
132
            page+=1;
133
            uriComponents = UriComponentsBuilder
134
                    .fromHttpUrl(baseAddress + "/ds/search/country/")
135
                    .path("/{page}/{size}/")
136
                    .queryParam("country",country)
137
                    .build().expand(page,size).encode();
138
            rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
139
        }
140

    
141
        return resultSet;
142
    }
143

    
144
    private void addRepos(Set<String> resultSet, Collection<String> repos) {
145
        for(String s : repos)
146
            if(!resultSet.contains(s))
147
                resultSet.add(s);
148
    }
149

    
150
    private Collection<String> getRepositoriesByMode(String mode, List<Repository> rs) {
151

    
152
        List<String> reps = new ArrayList<>();
153

    
154
        for(Repository r : rs)
155
            if(r.getCollectedFrom().equals(mode))
156
                reps.add(r.getOfficialName());
157

    
158
        return reps;
159
    }
160

    
161
    @Override
162
    public List<Repository> getRepositoriesOfUser(@PathVariable("userEmail") String userEmail,
163
                                                  @PathVariable("page") String page,
164
                                                  @PathVariable("size") String size) throws JSONException {
165

    
166
        LOGGER.debug("Retreiving repositories of user : " + userEmail + " with params: " + baseAddress + "-" + page + "/" + size);
167
        UriComponents uriComponents = UriComponentsBuilder
168
                .fromHttpUrl(baseAddress + "/ds/search/email/")
169
                .path("/{page}/{size}/")
170
                .queryParam("contactemail",userEmail)
171
                .build().expand(page,size).encode();
172
        
173
        LOGGER.debug("URI -> " + uriComponents.toUri());
174
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
175
        LOGGER.debug("Repositories are : " + rs);
176
        return Converter.jsonToRepositoryList(new JSONArray(rs));
177
    }
178

    
179
    @Override
180
    public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException {
181

    
182
        UriComponents uriComponents = UriComponentsBuilder
183
                .fromHttpUrl(baseAddress + "/ds/get/")
184
                .path("/{id}/")
185
                .build().expand(id).encode();
186

    
187
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
188
        return  Converter.jsonToRepositoryObject(new JSONObject(rs));
189
    }
190

    
191
    @Override
192
    public Aggregations getRepositoryAggregations(@PathVariable("id") String id) throws JSONException {
193

    
194
        UriComponents uriComponents = UriComponentsBuilder
195
                .fromHttpUrl(baseAddress + "/ds/get/")
196
                .path("/{id}/")
197
                .build().expand(id).encode();
198

    
199
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
200
        JSONObject repository = new JSONObject(rs);
201

    
202
        Aggregations aggregations = new Aggregations();
203
        
204
        aggregations.setAggregationHistory(Converter.getAggregationHistoryFromJson(repository));
205
        aggregations.setLastCollection(Converter.getLastCollectionFromJson(repository));
206
        aggregations.setLastTransformation(Converter.getLastTransformationFromJson(repository));
207
        return aggregations;
208
    }
209

    
210
    @Override
211
    public List<Repository> getRepositoriesByName(@PathVariable("name") String name,
212
                                                  @PathVariable("page") String page,
213
                                                  @PathVariable("size") String size) throws JSONException {
214
        UriComponents uriComponents = UriComponentsBuilder
215
                .fromHttpUrl(baseAddress + "/ds/search/name/")
216
                .path("/{page}/{size}")
217
                .queryParam("name",name)
218
                .build().expand(page,size).encode();
219

    
220
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
221
        return Converter.jsonToRepositoryList(new JSONArray(rs));
222
    }
223

    
224
    @Override
225
    public RepositoryInterface getRepositoyInterface(@PathVariable("id") String id) throws JSONException {
226

    
227
        UriComponents uriComponents = UriComponentsBuilder
228
                .fromHttpUrl(baseAddress + "/ds/service/")
229
                .path("/{id}/")
230
                .build().expand(id).encode();
231

    
232
        String rs = restTemplate.getForObject(uriComponents.toUri(),String.class);
233
        return  Converter.jsonToRepositoryInterfaceObject(new JSONArray(rs).getJSONObject(0));
234
    }
235

    
236
    @Override
237
    public String addRepository(@RequestBody Repository repository) throws JSONException {
238
        UriComponents uriComponents = UriComponentsBuilder
239
                .fromHttpUrl(baseAddress + "/ds/add/")
240
                .build()
241
                .encode();
242
        return restTemplate.postForObject(uriComponents.toUri(),Converter.repositoryObjectToJson(repository),String.class);
243
    }
244

    
245
    @Override
246
    public String addRepositoryInterface(RepositoryInterface repositoryInterface) throws JSONException {
247
        UriComponents uriComponents = UriComponentsBuilder
248
                .fromHttpUrl(baseAddress + "/ds/service/add/")
249
                .build()
250
                .encode();
251
        return restTemplate.postForObject(uriComponents.toUri(), Converter.repositoryInterfaceObjectToJson(repositoryInterface),String.class);
252
    }
253

    
254
    @Override
255
    public List<String> getDnetCountries() {
256
        LOGGER.debug("Getting dnet-countries!");
257
        return Converter.readFile("countries.txt");
258
    }
259

    
260
    @Override
261
    public List<String> getTypologies() {
262
        return Converter.readFile("typologies.txt");
263
    }
264

    
265
    @Override
266
    public List<Timezone> getTimezones() {
267
        List<String> timezones =  Converter.readFile("timezones.txt");
268
        return Converter.toTimezones(timezones);
269
    }
270

    
271
    @Override
272
    public String updateManagedStatus(@RequestParam(value = "id")   String id,
273
                                      @RequestParam(value = "managed")  String managed) {
274

    
275
        UriComponents uriComponents = UriComponentsBuilder
276
                .fromHttpUrl(baseAddress + "/ds/manage/")
277
                .queryParam("id",id)
278
                .queryParam("managed",managed)
279
                .build().encode();
280

    
281
        return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
282
    }
283

    
284
    @Override
285
    public String updateEnglishName(@RequestParam(value = "id")   String id,
286
                                    @RequestParam(value = "englishname")  String englishName) {
287

    
288
        UriComponents uriComponents = UriComponentsBuilder
289
                .fromHttpUrl(baseAddress + "/ds/manage/")
290
                .queryParam("dsId",id)
291
                .queryParam("englishname",englishName)
292
                .build().encode();
293
        return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
294

    
295

    
296
    }
297

    
298
    @Override
299
    public String updateLatitude(@RequestParam(value = "id")   String id,
300
                                 @RequestParam(value = "managed")  String latitude) {
301

    
302
        UriComponents uriComponents = UriComponentsBuilder
303
                .fromHttpUrl(baseAddress + "/ds/manage/")
304
                .queryParam("dsId",id)
305
                .queryParam("latitude",latitude)
306
                .build().encode();
307
        return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
308
    }
309

    
310
    @Override
311
    public String updateLongitude(@RequestParam(value = "id")   String id,
312
                                  @RequestParam(value = "longitude")  String longitude) {
313

    
314
        UriComponents uriComponents = UriComponentsBuilder
315
                .fromHttpUrl(baseAddress + "/ds/manage/")
316
                .queryParam("dsId",id)
317
                .queryParam("longitude",longitude)
318
                .build().encode();
319
        return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
320
    }
321

    
322
    @Override
323
    public String updateOfficialName(@RequestParam(value = "id")   String id,
324
                                     @RequestParam(value = "officialname")  String officialname) {
325

    
326
        UriComponents uriComponents = UriComponentsBuilder
327
                .fromHttpUrl(baseAddress + "/ds/manage/")
328
                .queryParam("dsId",id)
329
                .queryParam("officialname",officialname)
330
                .build().encode();
331
        return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
332
    }
333

    
334
    @Override
335
    public List<String> getUrlsOfUserRepos(@PathVariable("user_email") String user_email,
336
                                           @PathVariable("page") String page,
337
                                           @PathVariable("size") String size) throws JSONException {
338
        UriComponents uriComponents = UriComponentsBuilder
339
                .fromHttpUrl(baseAddress + "/api/baseurl/")
340
                .path("/{page}/{size}")
341
                .queryParam("userEmail",user_email)
342
                .build().expand(page,size).encode();
343
        return Arrays.asList(restTemplate.getForObject(uriComponents.toUri(), String[].class));
344
    }
345

    
346
    @Override
347
    public List<String> getDatasourceVocabularies(@PathVariable("mode") String mode) {
348

    
349
        List<String> resultSet = new ArrayList<>();
350
        for (Map.Entry<String, String> entry : this.getVocabulary("dnet:datasource_typologies").getAsMap().entrySet()) {
351
            if (mode.equalsIgnoreCase("aggregator")) {
352
                if (entry.getKey().contains("aggregator"))
353
                    resultSet.add(entry.getValue());
354
            } else if (mode.equalsIgnoreCase("journal")) {
355
                if (entry.getKey().contains("journal"))
356
                    resultSet.add(entry.getValue());
357
            } else if (mode.equalsIgnoreCase("opendoar")) {
358
                if (entry.getKey().contains("pubsrepository"))
359
                    resultSet.add(entry.getValue());
360
            } else if (mode.equalsIgnoreCase("re3data")) {
361
                if (entry.getKey().contains("datarepository"))
362
                    resultSet.add(entry.getValue());
363
            }
364
        }
365

    
366

    
367
        return resultSet;
368
    }
369

    
370
    private Vocabulary getVocabulary(String vocName) {
371

    
372
        if (!vocabularyMap.containsKey(vocName)) {
373
            vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT));
374
        }
375
        return vocabularyMap.get(vocName);
376
    }
377

    
378
}
(6-6/8)