Project

General

Profile

1 49362 panagiotis
package eu.dnetlib.repo.manager.service.controllers;
2 49236 panagiotis
3 49975 panagiotis
import com.fasterxml.jackson.databind.DeserializationFeature;
4 49960 panagiotis
import com.fasterxml.jackson.databind.ObjectMapper;
5 49975 panagiotis
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
6 49450 panagiotis
import eu.dnetlib.domain.enabling.Vocabulary;
7 49236 panagiotis
import eu.dnetlib.repo.manager.service.utils.Converter;
8
import eu.dnetlib.domain.data.Repository;
9
import eu.dnetlib.domain.data.RepositoryInterface;
10 49450 panagiotis
import gr.uoa.di.driver.enabling.vocabulary.VocabularyLoader;
11 49763 panagiotis
import org.apache.log4j.Logger;
12 49236 panagiotis
import org.json.JSONArray;
13 49362 panagiotis
import org.json.JSONException;
14 49236 panagiotis
import org.json.JSONObject;
15 49450 panagiotis
import org.springframework.beans.factory.annotation.Autowired;
16 49236 panagiotis
import org.springframework.beans.factory.annotation.Value;
17 49245 panagiotis
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
18 49236 panagiotis
import org.springframework.stereotype.Component;
19
import org.springframework.web.bind.annotation.PathVariable;
20
import org.springframework.web.bind.annotation.RequestBody;
21
import org.springframework.web.bind.annotation.RequestParam;
22 49245 panagiotis
import org.springframework.web.client.RestTemplate;
23 49304 panagiotis
import org.springframework.web.util.UriComponents;
24 49245 panagiotis
import org.springframework.web.util.UriComponentsBuilder;
25 49304 panagiotis
import javax.annotation.PostConstruct;
26 49960 panagiotis
import java.sql.Timestamp;
27 49963 panagiotis
import java.text.Normalizer;
28 49450 panagiotis
import java.util.*;
29
import java.util.concurrent.ConcurrentHashMap;
30 49763 panagiotis
import eu.dnetlib.repo.manager.shared.*;
31 49236 panagiotis
32
@Component
33
public class RepositoryApiImpl implements RepositoryApi {
34
35 49304 panagiotis
    @Value("${api.baseAddress}")
36 49236 panagiotis
    private String baseAddress;
37
38 49304 panagiotis
    private RestTemplate restTemplate = null;
39
40 49450 panagiotis
    private final String[] vocabularyNames = {"dnet:countries", "dnet:datasource_typologies", "dnet:compatibilityLevel"};
41
42 49763 panagiotis
    private static final Logger LOGGER = Logger.getLogger(RepositoryApiImpl.class);
43
44 49450 panagiotis
    @Autowired
45
    private VocabularyLoader vocabularyLoader;
46
47
    private Map<String, Vocabulary> vocabularyMap = new ConcurrentHashMap<String, Vocabulary>();
48
49 49960 panagiotis
    private Map<String, String> countriesMap = new HashMap<>();
50
    private Map<String, String> inverseCountriesMap = new HashMap<>();
51 49450 panagiotis
52 49898 panagiotis
53 49304 panagiotis
    @PostConstruct
54 49960 panagiotis
    private void init() {
55 49770 panagiotis
        LOGGER.debug("Initialization method of repository api!");
56
57 49304 panagiotis
        restTemplate = new RestTemplate();
58
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
59 49450 panagiotis
60
        for (String vocName : vocabularyNames) {
61
            vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT));
62
        }
63
64 49898 panagiotis
        Country[] countries = getCountries();
65 49960 panagiotis
        for (Country c : countries) {
66
            countriesMap.put(c.getName(), c.getCode());
67
            inverseCountriesMap.put(c.getCode(), c.getName());
68 49898 panagiotis
        }
69
70
71 49304 panagiotis
    }
72
73 49236 panagiotis
    @Override
74 49935 panagiotis
    public List<String> testAggregations() throws JSONException {
75 49763 panagiotis
76 49931 panagiotis
        int page = 0;
77 49763 panagiotis
        int size = 1000;
78
79
        UriComponents uriComponents = UriComponentsBuilder
80
                .fromHttpUrl(baseAddress + "/ds/list/")
81
                .path("/{page}/{size}/")
82 49960 panagiotis
                .build().expand(page, size).encode();
83 49763 panagiotis
84
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
85 49935 panagiotis
        List<String> ids = new ArrayList<>();
86 49960 panagiotis
        while (!rs.equals("[]")) {
87
88 49935 panagiotis
            ids.addAll(getIdsWithNonEmptyAggregations(rs));
89 49763 panagiotis
90 49960 panagiotis
            LOGGER.debug("Checked " + (page + 1) * size + " records!");
91 49763 panagiotis
92 49960 panagiotis
            page += 1;
93 49763 panagiotis
            uriComponents = UriComponentsBuilder
94
                    .fromHttpUrl(baseAddress + "/ds/list/")
95
                    .path("/{page}/{size}/")
96 49960 panagiotis
                    .build().expand(page, size).encode();
97 49763 panagiotis
            rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
98
        }
99 49960 panagiotis
100 49935 panagiotis
        return ids;
101 49763 panagiotis
    }
102
103 49935 panagiotis
    private List<String> getIdsWithNonEmptyAggregations(String rs) throws JSONException {
104 49763 panagiotis
105
        JSONArray ids = new JSONArray(rs);
106 49935 panagiotis
        List<String> agg_ids = new ArrayList<>();
107 49960 panagiotis
        for (int i = 0; i < ids.length(); i++) {
108 49763 panagiotis
            String id = ids.getString(i);
109
            Aggregations aggregations = getRepositoryAggregations(id);
110 49960 panagiotis
            if (aggregations.getAggregationHistory() != null)
111 49935 panagiotis
                agg_ids.add(id);
112 49763 panagiotis
        }
113 49935 panagiotis
        return agg_ids;
114 49763 panagiotis
    }
115
116
    @Override
117 49960 panagiotis
    public Country[] getCountries() {
118 49304 panagiotis
        UriComponents uriComponents = UriComponentsBuilder
119
                .fromHttpUrl(baseAddress + "/ds/countries")
120
                .build().encode();
121 49960 panagiotis
        return restTemplate.getForObject(uriComponents.toUri(), Country[].class);
122 49236 panagiotis
    }
123
124
125
    @Override
126 49790 panagiotis
    public List<Repository> getRepositoriesByCountry(@PathVariable("country") String country,
127 49898 panagiotis
                                                     @PathVariable("mode") String mode) throws JSONException {
128 49245 panagiotis
129 49790 panagiotis
        LOGGER.debug("Getting repositories by country!");
130 49898 panagiotis
        int page = 0;
131
        int size = 100;
132
133
        String countryCode = countriesMap.get(country);
134 49813 panagiotis
        String filterKey = "UNKNOWN";
135 49960 panagiotis
        if (mode.equalsIgnoreCase("opendoar")) {
136 49813 panagiotis
            filterKey = "openaire____::opendoar";
137 49960 panagiotis
        } else if (mode.equalsIgnoreCase("re3data")) {
138 49813 panagiotis
            filterKey = "openaire____::re3data";
139 49960 panagiotis
        } else if (mode.equalsIgnoreCase("jour_aggr")) {
140 49813 panagiotis
            filterKey = "infrastruct_::openaire";
141
        }
142 49431 panagiotis
143 49790 panagiotis
        LOGGER.debug("Country code equals : " + countryCode);
144 49813 panagiotis
        LOGGER.debug("Filter mode equals : " + filterKey);
145 49304 panagiotis
        UriComponents uriComponents = UriComponentsBuilder
146 49960 panagiotis
                .fromHttpUrl(baseAddress + "/ds/search/country/")
147
                .path("/{page}/{size}/")
148
                .queryParam("country", countryCode)
149
                .build().expand(page, size).encode();
150 49245 panagiotis
151 49431 panagiotis
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
152 49790 panagiotis
        List<Repository> resultSet = new ArrayList<>();
153 49960 panagiotis
        while (!rs.equals("[]")) {
154 49431 panagiotis
            List<Repository> rep = Converter.jsonToRepositoryList(new JSONArray(rs));
155 49450 panagiotis
156 49960 panagiotis
            Collection<Repository> repos = this.getRepositoriesByMode(filterKey, rep);
157 49898 panagiotis
            resultSet.addAll(repos);
158 49790 panagiotis
159 49960 panagiotis
            page += 1;
160 49431 panagiotis
            uriComponents = UriComponentsBuilder
161
                    .fromHttpUrl(baseAddress + "/ds/search/country/")
162
                    .path("/{page}/{size}/")
163 49960 panagiotis
                    .queryParam("country", countryCode)
164
                    .build().expand(page, size).encode();
165 49431 panagiotis
            rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
166
        }
167 49960 panagiotis
        for (Repository r : resultSet)
168 49931 panagiotis
            r.setCountryName(getCountryName(r.getCountryCode()));
169 49431 panagiotis
        return resultSet;
170 49236 panagiotis
    }
171
172 49790 panagiotis
173
    private Collection<Repository> getRepositoriesByMode(String mode, List<Repository> rs) {
174 49431 panagiotis
175 49790 panagiotis
        List<Repository> reps = new ArrayList<>();
176 49960 panagiotis
        for (Repository r : rs) {
177
            if (r.getCollectedFrom().equals(mode))
178 49790 panagiotis
                reps.add(r);
179 49813 panagiotis
        }
180 49431 panagiotis
        return reps;
181
    }
182
183 49236 panagiotis
    @Override
184
    public List<Repository> getRepositoriesOfUser(@PathVariable("userEmail") String userEmail,
185
                                                  @PathVariable("page") String page,
186 49362 panagiotis
                                                  @PathVariable("size") String size) throws JSONException {
187 49397 panagiotis
188 49975 panagiotis
        LOGGER.debug("Retreiving repositories of user : " + userEmail );
189 49397 panagiotis
        UriComponents uriComponents = UriComponentsBuilder
190 49975 panagiotis
                .fromHttpUrl(baseAddress + "/ds/search/registeredBy/")
191 49397 panagiotis
                .path("/{page}/{size}/")
192 49975 panagiotis
                .queryParam("registeredBy", userEmail)
193 49960 panagiotis
                .build().expand(page, size).encode();
194
195 49397 panagiotis
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
196 49898 panagiotis
        List<Repository> repos = Converter.jsonToRepositoryList(new JSONArray(rs));
197 49960 panagiotis
        for (Repository r : repos)
198 49898 panagiotis
            r.setCountryName(getCountryName(r.getCountryCode()));
199
        return repos;
200 49236 panagiotis
    }
201
202
    @Override
203 49362 panagiotis
    public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException {
204 49397 panagiotis
205
        UriComponents uriComponents = UriComponentsBuilder
206
                .fromHttpUrl(baseAddress + "/ds/get/")
207
                .path("/{id}/")
208
                .build().expand(id).encode();
209
210
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
211 49898 panagiotis
        Repository repo = Converter.jsonToRepositoryObject(new JSONObject(rs));
212
        if (repo != null)
213
            repo.setCountryName(getCountryName(repo.getCountryCode()));
214
        return repo;
215 49236 panagiotis
    }
216
217
    @Override
218 49763 panagiotis
    public Aggregations getRepositoryAggregations(@PathVariable("id") String id) throws JSONException {
219
220
        UriComponents uriComponents = UriComponentsBuilder
221
                .fromHttpUrl(baseAddress + "/ds/get/")
222
                .path("/{id}/")
223
                .build().expand(id).encode();
224
225
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
226
        JSONObject repository = new JSONObject(rs);
227
228
        Aggregations aggregations = new Aggregations();
229 49935 panagiotis
230 49960 panagiotis
        try {
231 49935 panagiotis
            aggregations.setAggregationHistory(Converter.getAggregationHistoryFromJson(repository));
232
            aggregations.setLastCollection(Converter.getLastCollectionFromJson(repository));
233
            aggregations.setLastTransformation(Converter.getLastTransformationFromJson(repository));
234
            return aggregations;
235 49960 panagiotis
        } catch (JSONException e) {
236
            LOGGER.debug("JSON aggregation exception ", e);
237 49935 panagiotis
            throw e;
238
        }
239
240 49763 panagiotis
    }
241
242
    @Override
243 49236 panagiotis
    public List<Repository> getRepositoriesByName(@PathVariable("name") String name,
244
                                                  @PathVariable("page") String page,
245 49362 panagiotis
                                                  @PathVariable("size") String size) throws JSONException {
246 49397 panagiotis
        UriComponents uriComponents = UriComponentsBuilder
247
                .fromHttpUrl(baseAddress + "/ds/search/name/")
248
                .path("/{page}/{size}")
249 49960 panagiotis
                .queryParam("name", name)
250
                .build().expand(page, size).encode();
251 49397 panagiotis
252
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
253 49898 panagiotis
        List<Repository> repos = Converter.jsonToRepositoryList(new JSONArray(rs));
254 49960 panagiotis
        for (Repository r : repos)
255 49898 panagiotis
            r.setCountryName(getCountryName(r.getCountryCode()));
256
        return repos;
257 49236 panagiotis
    }
258
259
    @Override
260 49362 panagiotis
    public RepositoryInterface getRepositoyInterface(@PathVariable("id") String id) throws JSONException {
261 49397 panagiotis
262
        UriComponents uriComponents = UriComponentsBuilder
263 49813 panagiotis
                .fromHttpUrl(baseAddress + "/ds/api/")
264 49397 panagiotis
                .path("/{id}/")
265
                .build().expand(id).encode();
266
267 49960 panagiotis
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
268
        return Converter.jsonToRepositoryInterfaceObject(new JSONArray(rs).getJSONObject(0));
269 49236 panagiotis
    }
270
271
    @Override
272 49960 panagiotis
    public void addRepository(@RequestBody String params) throws Exception {
273 49898 panagiotis
274 49960 panagiotis
        JSONObject json_params = new JSONObject(params);
275
        String datatype = json_params.getString("datatype");
276
        String json_repository = json_params.getString("repository");
277
        Repository repository = null;
278
        ObjectMapper mapper = new ObjectMapper();
279 49975 panagiotis
        mapper.disable(DeserializationFeature.FAIL_ON_UNK‌​NOWN_PROPERTIES);
280 49960 panagiotis
        try {
281 49975 panagiotis
            LOGGER.debug("repository -> " + json_repository);
282 49960 panagiotis
            repository = mapper.readValue(json_repository, Repository.class);
283
        } catch (Exception e) {
284
            LOGGER.debug("Error parsing repository ", e);
285 49975 panagiotis
            throw  e;
286 49960 panagiotis
        }
287
288 49963 panagiotis
        repository = this.setRepositoryFeatures(datatype,repository);
289 49960 panagiotis
290
        LOGGER.debug("storing " + datatype + " repository with id: " + repository.getId());
291 49963 panagiotis
        if (!datatype.equalsIgnoreCase("opendoar") && !datatype.equalsIgnoreCase("re3data")) {
292
            if (datatype.equalsIgnoreCase("journal") || datatype.equalsIgnoreCase("aggregator")) {
293
                LOGGER.debug("looking if " + datatype + " " + repository.getOfficialName() + " is already in datasources");
294
                if (getRepositoryById(repository.getId()) != null) {
295
                    String retMessage = datatype + " '" + repository.getOfficialName() + "' is already in datasources.";
296
                    repository.getInterfaces().clear();
297
                    LOGGER.debug(retMessage);
298
                } else {
299
                    LOGGER.debug(datatype + " " + repository.getOfficialName() + " is not in datasources. Inserting..");
300 49975 panagiotis
                    this.storeRepository(repository);
301 49960 panagiotis
                }
302
            }
303 49963 panagiotis
        } else {
304 49975 panagiotis
            this.updateRegisteredByValue(repository.getId(),repository.getRegisteredBy());
305 49960 panagiotis
        }
306
307
        LOGGER.debug("Inserting Interfaces");
308 49963 panagiotis
        Iterator var11 = repository.getInterfaces().iterator();
309 49960 panagiotis
310
        while (var11.hasNext()) {
311
            RepositoryInterface iFace = (RepositoryInterface) var11.next();
312
            if (!iFace.getBaseUrl().isEmpty() && !iFace.getDesiredCompatibilityLevel().isEmpty()) {
313
                if (iFace.getId() != null && !iFace.getId().isEmpty()) {
314
                    LOGGER.debug("updating iface..");
315 49963 panagiotis
                    this.updateInterface(datatype,iFace);
316 49960 panagiotis
                    LOGGER.debug("updated successfully");
317
                } else {
318
                    LOGGER.debug("adding new iface..");
319 49975 panagiotis
                    this.registerRepositoryInterface(repository.getId(),iFace,datatype);
320 49963 panagiotis
                }
321
            }
322
        }
323
    }
324 49960 panagiotis
325 49975 panagiotis
    private void updateRegisteredByValue(String id, String registeredBy) {
326
327
        LOGGER.debug("Updating registered by value with  : " + registeredBy );
328
        UriComponents uriComponents = UriComponentsBuilder
329
                .fromHttpUrl(baseAddress + "/ds/registeredBy/")
330
                .queryParam("dsId",id)
331
                .queryParam("registeredBy", registeredBy)
332
                .build()
333
                .encode();
334
        restTemplate.postForObject(uriComponents.toUri(), null,String.class);
335
    }
336
337 49963 panagiotis
    private Repository setRepositoryFeatures(String datatype, Repository repository) {
338 49960 panagiotis
339 49963 panagiotis
        //TODO update map
340
        repository.setCountryCode(countriesMap.get(repository.getCountryName()));
341 49960 panagiotis
342 49963 panagiotis
        repository.setActivationId(UUID.randomUUID().toString());
343
        // repo.setRegisteredBy((String) session.get(LocalVocabularies.loggedInField));
344
345
        if (datatype.equals("opendoar") || datatype.equals("re3data")) {
346
            repository.setProvenanceActionClass("sysimport:crosswalk:entityregistry");
347
        } else if (datatype.equals("journal")) {
348
            repository.setProvenanceActionClass("user:insert");
349
            repository.setCollectedFrom("infrastruct_::openaire");
350
            if (repository.getIssn() != null && repository.getIssn().length() == 0)
351
                repository.setIssn(com.unboundid.util.Base64.encode(repository.getOfficialName()).substring(0, 8));
352
            repository.setId("openaire____::issn" + repository.getIssn());
353
            repository.setNamespacePrefix("issn" + repository.getIssn());
354
        } else if (datatype.equals("aggregator")) {
355
            repository.setProvenanceActionClass("user:insert");
356
            repository.setCollectedFrom("infrastruct_::openaire");
357
            repository.setId("openaire____::" + com.unboundid.util.Base64.encode(repository.getOfficialName()));
358
            repository.setNamespacePrefix(Normalizer.normalize(repository.getOfficialName().toLowerCase().replace(" ", "_"), Normalizer.Form.NFD).replaceAll("[^a-zA-Z0-9]", ""));
359
            if (repository.getNamespacePrefix().length() > 12) {
360
                repository.setNamespacePrefix(repository.getNamespacePrefix().substring(0, 12));
361
            } else {
362
                while (repository.getNamespacePrefix().length() < 12)
363
                    repository.setNamespacePrefix(repository.getNamespacePrefix().concat("_"));
364 49960 panagiotis
            }
365
        }
366 49963 panagiotis
        return repository;
367 49960 panagiotis
    }
368
369 49963 panagiotis
    private void updateInterface(String datatype,RepositoryInterface iFace) {
370
        //TODO call update base url
371
        //((DatasourceManagerService) this.dmService.getService()).updateBaseUrl(repo.getId(), iFace.getId(), iFace.getBaseUrl());
372
373
        if (!iFace.getAccessSet().isEmpty()) {
374
            LOGGER.debug("set not empty: " + iFace.getAccessSet());
375
            //TODO call update method for access params
376
            // ((DatasourceManagerService) this.dmService.getService()).updateAccessParam(repo.getId(), iFace.getId(), "set", iFace.getAccessSet(), false);
377
        } else {
378
            //TODO call deleteAccessParamOrExtraField
379
            //((DatasourceManagerService) this.dmService.getService()).deleteAccessParamOrExtraField(repo.getId(), iFace.getId(), "set");
380
        }
381
        //TODO update content description
382
        //((DatasourceManagerService) this.dmService.getService()).updateContentDescription(repo.getId(), iFace.getId(), "metadata");
383
        if (datatype.equals("re3data")) {
384
            //TODO call update access params
385
            //  ((DatasourceManagerService) this.dmService.getService()).updateAccessParam(repo.getId(), iFace.getId(), "format", "oai_datacite", false);
386
            iFace.setAccessFormat("oai_datacite");
387
        } else {
388
            //TODO call update access params
389
            //((DatasourceManagerService) this.dmService.getService()).updateAccessParam(repo.getId(), iFace.getId(), "format", "oai_dc", false);
390
            iFace.setAccessFormat("oai_dc");
391
        }
392
393
    }
394
395
    private RepositoryInterface createRepositoryInterface(Repository repo, RepositoryInterface iFace, String datatype) {
396
397
        iFace.setContentDescription("metadata");
398
        iFace.setCompliance("UNKNOWN");
399
        if (datatype.equals("re3data")) {
400
            iFace.setAccessFormat("oai_datacite");
401
        } else {
402
            iFace.setAccessFormat("oai_dc");
403
        }
404
405
        if (repo.getDatasourceClass() != null && !repo.getDatasourceClass().isEmpty()) {
406
            iFace.setTypology(repo.getDatasourceClass());
407
        } else if (datatype.equalsIgnoreCase("journal")) {
408
            iFace.setTypology("pubsrepository::journal");
409
        } else if (datatype.equalsIgnoreCase("aggregator")) {
410
            iFace.setTypology("aggregator::pubsrepository::unknown");
411
        } else if (datatype.equalsIgnoreCase("opendoar")) {
412
            iFace.setTypology("pubsrepository::unknown");
413
        } else if (datatype.equalsIgnoreCase("re3data")) {
414
            iFace.setTypology("datarepository::unknown");
415
        }
416
417
        iFace.setRemovable(true);
418
        iFace.setAccessProtocol("oai");
419
        iFace.setMetadataIdentifierPath("//*[local-name()='header']/*[local-name()='identifier']");
420
        iFace.setId("api_________::" + repo.getId() + "::" + UUID.randomUUID().toString().substring(0, 8));
421
        if (iFace.getAccessSet().isEmpty()) {
422
            LOGGER.debug("set is empty: " + iFace.getAccessSet());
423
            iFace.removeAccessSet();
424
        }
425
        return iFace;
426
    }
427
428 49960 panagiotis
    private void storeRepository(Repository repository) throws JSONException {
429
430 49963 panagiotis
        Date utilDate = new Date();
431
        Timestamp date = new Timestamp(utilDate.getTime());
432
        repository.setDateOfCollection(date);
433
        repository.setAggregator("OPENAIRE");
434
435 49397 panagiotis
        UriComponents uriComponents = UriComponentsBuilder
436
                .fromHttpUrl(baseAddress + "/ds/add/")
437
                .build()
438
                .encode();
439 49908 panagiotis
        try {
440
            LOGGER.debug("Repository equals : " + Converter.repositoryObjectToJson(repository));
441
        }catch (Exception e){
442
            LOGGER.debug("JSON exception ", e);
443
            throw e;
444
        }
445 49898 panagiotis
        restTemplate.postForObject(uriComponents.toUri(),Converter.repositoryObjectToJson(repository),String.class);
446 49236 panagiotis
    }
447
448
    @Override
449 49813 panagiotis
    public void deleteRepositoryInterface(@PathVariable("id") String id){
450 49397 panagiotis
        UriComponents uriComponents = UriComponentsBuilder
451 49813 panagiotis
                .fromHttpUrl(baseAddress + "/ds/api/")
452
                .path("/{id}/")
453
                .build().expand(id).encode();
454
        restTemplate.delete(uriComponents.toUri());
455
    }
456
457
    @Override
458 49963 panagiotis
    public RepositoryInterface addRepositoryInterface(@RequestBody String params) throws JSONException {
459 49813 panagiotis
460 49963 panagiotis
        JSONObject json_params = new JSONObject(params);
461
        String datatype = json_params.getString("datatype");
462
        String repoId = json_params.getString("repoId");
463
        String json_iFace = json_params.getString("iFace");
464
        RepositoryInterface iFace = null;
465
466
        ObjectMapper mapper = new ObjectMapper();
467 49908 panagiotis
        try {
468 49963 panagiotis
            iFace = mapper.readValue(json_iFace, RepositoryInterface.class);
469
        } catch (Exception e1) {
470
            LOGGER.debug("Error parsing repository interface ", e1);
471 49908 panagiotis
        }
472
473 49963 panagiotis
        return registerRepositoryInterface(repoId,iFace,datatype);
474 49236 panagiotis
    }
475
476 49975 panagiotis
    private RepositoryInterface registerRepositoryInterface(String repoId, RepositoryInterface iFace, String datatype) {
477 49963 panagiotis
        Repository e = null;
478
        try {
479
            e = this.getRepositoryById(repoId);
480
            iFace = createRepositoryInterface(e,iFace,datatype);
481
482
            UriComponents uriComponents = UriComponentsBuilder
483
                    .fromHttpUrl(baseAddress + "/ds/api/add/")
484
                    .build()
485
                    .encode();
486
            String rs =  restTemplate.postForObject(uriComponents.toUri(), Converter.repositoryInterfaceObjectToJson(e,iFace),String.class);
487
            return Converter.jsonToRepositoryInterfaceObject(new JSONArray(rs).getJSONObject(0));
488
489
        } catch (JSONException e1) {
490
            LOGGER.debug("Error parsing json ",e1);
491
        }
492
        return null;
493
    }
494
495 49236 panagiotis
    @Override
496
    public List<String> getDnetCountries() {
497 49770 panagiotis
        LOGGER.debug("Getting dnet-countries!");
498 49236 panagiotis
        return Converter.readFile("countries.txt");
499
    }
500
501
    @Override
502
    public List<String> getTypologies() {
503
        return Converter.readFile("typologies.txt");
504
    }
505
506
    @Override
507 49763 panagiotis
    public List<Timezone> getTimezones() {
508
        List<String> timezones =  Converter.readFile("timezones.txt");
509
        return Converter.toTimezones(timezones);
510 49236 panagiotis
    }
511
512
    @Override
513
    public String updateManagedStatus(@RequestParam(value = "id")   String id,
514
                                      @RequestParam(value = "managed")  String managed) {
515 49397 panagiotis
516
        UriComponents uriComponents = UriComponentsBuilder
517
                .fromHttpUrl(baseAddress + "/ds/manage/")
518
                .queryParam("id",id)
519
                .queryParam("managed",managed)
520
                .build().encode();
521
522
        return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
523 49236 panagiotis
    }
524
525
    @Override
526
    public String updateEnglishName(@RequestParam(value = "id")   String id,
527
                                    @RequestParam(value = "englishname")  String englishName) {
528 49397 panagiotis
529
        UriComponents uriComponents = UriComponentsBuilder
530 49975 panagiotis
                .fromHttpUrl(baseAddress + "/ds/englishname/")
531 49397 panagiotis
                .queryParam("dsId",id)
532
                .queryParam("englishname",englishName)
533
                .build().encode();
534
        return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
535
536
537 49236 panagiotis
    }
538
539
    @Override
540
    public String updateLatitude(@RequestParam(value = "id")   String id,
541
                                 @RequestParam(value = "managed")  String latitude) {
542 49397 panagiotis
543
        UriComponents uriComponents = UriComponentsBuilder
544 49975 panagiotis
                .fromHttpUrl(baseAddress + "/ds/latitude/")
545 49397 panagiotis
                .queryParam("dsId",id)
546
                .queryParam("latitude",latitude)
547
                .build().encode();
548
        return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
549 49236 panagiotis
    }
550
551
    @Override
552
    public String updateLongitude(@RequestParam(value = "id")   String id,
553
                                  @RequestParam(value = "longitude")  String longitude) {
554 49397 panagiotis
555
        UriComponents uriComponents = UriComponentsBuilder
556 49975 panagiotis
                .fromHttpUrl(baseAddress + "/ds/longitude/")
557 49397 panagiotis
                .queryParam("dsId",id)
558
                .queryParam("longitude",longitude)
559
                .build().encode();
560
        return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
561 49236 panagiotis
    }
562
563
    @Override
564
    public String updateOfficialName(@RequestParam(value = "id")   String id,
565
                                     @RequestParam(value = "officialname")  String officialname) {
566 49397 panagiotis
567
        UriComponents uriComponents = UriComponentsBuilder
568 49975 panagiotis
                .fromHttpUrl(baseAddress + "/ds/officialname/")
569 49397 panagiotis
                .queryParam("dsId",id)
570
                .queryParam("officialname",officialname)
571
                .build().encode();
572
        return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
573 49236 panagiotis
    }
574
575
    @Override
576 49898 panagiotis
    public String updateTimezone(@RequestParam(value = "id")   String id,
577
                                 @RequestParam(value = "timezone")  String timezone) {
578
        UriComponents uriComponents = UriComponentsBuilder
579 49975 panagiotis
                .fromHttpUrl(baseAddress + "/ds/timezone")
580 49898 panagiotis
                .queryParam("dsId",id)
581
                .queryParam("timezone",timezone)
582
                .build().encode();
583
        return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
584
    }
585
586
    @Override
587
    public String updateTypology(@RequestParam(value = "id")   String id,
588
                                 @RequestParam(value = "typology")  String typology) {
589
        UriComponents uriComponents = UriComponentsBuilder
590 49975 panagiotis
                .fromHttpUrl(baseAddress + "/ds/typology")
591 49898 panagiotis
                .queryParam("dsId",id)
592
                .queryParam("typology",typology)
593
                .build().encode();
594
        return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
595
    }
596
597
    @Override
598
    public String updateLogoUrl(@RequestParam(value = "id")   String id,
599
                                @RequestParam(value = "logoUrl") String logoUrl) {
600
        UriComponents uriComponents = UriComponentsBuilder
601 49975 panagiotis
                .fromHttpUrl(baseAddress + "/ds/logourl")
602 49898 panagiotis
                .queryParam("dsId",id)
603
                .queryParam("logourl",logoUrl)
604
                .build().encode();
605
        return restTemplate.postForObject(uriComponents.toUri(), null,String.class);
606
    }
607
608
    @Override
609 49763 panagiotis
    public List<String> getUrlsOfUserRepos(@PathVariable("user_email") String user_email,
610
                                           @PathVariable("page") String page,
611
                                           @PathVariable("size") String size) throws JSONException {
612
        UriComponents uriComponents = UriComponentsBuilder
613
                .fromHttpUrl(baseAddress + "/api/baseurl/")
614
                .path("/{page}/{size}")
615
                .queryParam("userEmail",user_email)
616
                .build().expand(page,size).encode();
617
        return Arrays.asList(restTemplate.getForObject(uriComponents.toUri(), String[].class));
618 49236 panagiotis
    }
619
620 49450 panagiotis
    @Override
621
    public List<String> getDatasourceVocabularies(@PathVariable("mode") String mode) {
622
623
        List<String> resultSet = new ArrayList<>();
624
        for (Map.Entry<String, String> entry : this.getVocabulary("dnet:datasource_typologies").getAsMap().entrySet()) {
625
            if (mode.equalsIgnoreCase("aggregator")) {
626
                if (entry.getKey().contains("aggregator"))
627
                    resultSet.add(entry.getValue());
628
            } else if (mode.equalsIgnoreCase("journal")) {
629
                if (entry.getKey().contains("journal"))
630
                    resultSet.add(entry.getValue());
631
            } else if (mode.equalsIgnoreCase("opendoar")) {
632
                if (entry.getKey().contains("pubsrepository"))
633
                    resultSet.add(entry.getValue());
634
            } else if (mode.equalsIgnoreCase("re3data")) {
635
                if (entry.getKey().contains("datarepository"))
636
                    resultSet.add(entry.getValue());
637
            }
638
        }
639
640
641
        return resultSet;
642
    }
643
644
    private Vocabulary getVocabulary(String vocName) {
645
646
        if (!vocabularyMap.containsKey(vocName)) {
647
            vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT));
648
        }
649
        return vocabularyMap.get(vocName);
650
    }
651
652 49790 panagiotis
653
    @Override
654
    public Map<String, String> getCompatibilityClasses(@PathVariable("mode") String mode)  {
655
656
        LOGGER.debug("Getting compatibility classes for mode: " + mode);
657
        Map<String, String> retMap = new HashMap<String, String>();
658
659
        Map<String, String> compatibilityClasses = this.getVocabulary("dnet:compatibilityLevel").getAsMap();
660
        boolean foundData = false;
661
        for (Map.Entry<String, String> entry : compatibilityClasses.entrySet()) {
662
            if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_ALL))
663
                return compatibilityClasses;
664
            else if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_RE3DATA)) {
665
                if (entry.getKey().matches("^openaire[1-9].0_data$")) {
666
                    retMap.put(entry.getKey(), entry.getValue());
667
                    foundData = true;
668
                }
669
            } else {
670
                if (entry.getKey().matches("^openaire[1-9].0$") || entry.getKey().equals("driver"))
671
                    retMap.put(entry.getKey(), entry.getValue());
672
            }
673
        }
674
675
        //TODO TO BE REMOVED WHEN VOCABULARIES ARE UPDATED
676
        if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_RE3DATA) && !foundData)
677
            retMap.put("openaire2.0_data", "OpenAIRE Data (funded, referenced datasets)");
678
679
        return retMap;
680
    }
681
682
    @Override
683
    public Map<String, String> getDatasourceClasses(@PathVariable("mode") String mode)  {
684
685
        LOGGER.debug("Getting datasource classes for mode: " + mode);
686
687
        Map<String, String> retMap = new HashMap<String, String>();
688
689
        for (Map.Entry<String, String> entry : this.getVocabulary("dnet:datasource_typologies").getAsMap().entrySet()) {
690
            if (mode.equalsIgnoreCase("aggregator")) {
691
                if (entry.getKey().contains("aggregator"))
692
                    retMap.put(entry.getKey(), entry.getValue());
693
            } else if (mode.equalsIgnoreCase("journal")) {
694
                if (entry.getKey().contains("journal"))
695
                    retMap.put(entry.getKey(), entry.getValue());
696
            } else if (mode.equalsIgnoreCase("opendoar")) {
697
                if (entry.getKey().contains("pubsrepository"))
698
                    retMap.put(entry.getKey(), entry.getValue());
699
            } else if (mode.equalsIgnoreCase("re3data")) {
700
                if (entry.getKey().contains("datarepository"))
701
                    retMap.put(entry.getKey(), entry.getValue());
702
            }
703
        }
704
        return retMap;
705
706
    }
707
708 49898 panagiotis
    @Override
709
    public String getCountryName(String countryCode) {
710
        return inverseCountriesMap.get(countryCode);
711
    }
712
713 49236 panagiotis
}