Project

General

Profile

1
package eu.dnetlib.openaire.dsm.dao;
2

    
3
import eu.dnetlib.OpenaireExporterConfig;
4
import eu.dnetlib.enabling.datasources.common.DsmException;
5
import eu.dnetlib.openaire.vocabularies.Vocabulary;
6
import org.apache.commons.logging.Log;
7
import org.apache.commons.logging.LogFactory;
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.cache.annotation.CacheEvict;
10
import org.springframework.cache.annotation.Cacheable;
11
import org.springframework.http.ResponseEntity;
12
import org.springframework.stereotype.Component;
13
import org.springframework.web.client.RestTemplate;
14

    
15
/**
16
 * Created by claudio on 15/09/2017.
17
 */
18
@Component
19
public class VocabularyClientImpl implements VocabularyClient {
20

    
21
	private static final Log log = LogFactory.getLog(VocabularyClientImpl.class);
22

    
23
	@Autowired
24
	private OpenaireExporterConfig config;
25

    
26
	@Override
27
	@Cacheable("vocabularies-cache")
28
	public Vocabulary getCountries() throws DsmException {
29
		return _getVocabulary(config.getVocabularies().getCountriesEndpoint(), Vocabulary.class);
30
	}
31

    
32
	@Override
33
	@Cacheable("vocabularies-cache")
34
	public Vocabulary getDatasourceTypologies() throws DsmException {
35
		return _getVocabulary(config.getVocabularies().getDatasourceTypologiesEndpoint(), Vocabulary.class);
36
	}
37

    
38
	private <T> T _getVocabulary(final String endpoint, Class<T> clazz) throws DsmException {
39
		final RestTemplate rt = new RestTemplate();
40
		log.info("get vocabulary from " + endpoint);
41
		final ResponseEntity<T> rsp = rt.getForEntity(endpoint, clazz);
42

    
43
		if (!rsp.getStatusCode().is2xxSuccessful()) {
44
			throw new DsmException(rsp.getStatusCodeValue(), "unable to read content from " + endpoint);
45
		}
46

    
47
		return rsp.getBody();
48
	}
49

    
50
	@Override
51
	@CacheEvict(cacheNames = "vocabularies-cache", allEntries = true)
52
	public void dropCache() {
53
		log.info("dropped dsManager vocabulary cache");
54
	}
55

    
56
}
(16-16/16)