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