Project

General

Profile

1 51088 claudio.at
package eu.dnetlib.openaire.dsm.dao;
2 48975 claudio.at
3
import eu.dnetlib.OpenaireExporterConfig;
4 50560 claudio.at
import eu.dnetlib.enabling.datasources.common.DsmException;
5 51088 claudio.at
import eu.dnetlib.openaire.vocabularies.Vocabulary;
6 48975 claudio.at
import org.apache.commons.logging.Log;
7
import org.apache.commons.logging.LogFactory;
8
import org.springframework.beans.factory.annotation.Autowired;
9 49534 claudio.at
import org.springframework.cache.annotation.CacheEvict;
10 48975 claudio.at
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 50696 claudio.at
public class VocabularyClientImpl implements VocabularyClient {
20 48975 claudio.at
21 50696 claudio.at
	private static final Log log = LogFactory.getLog(VocabularyClientImpl.class);
22 48975 claudio.at
23
	@Autowired
24
	private OpenaireExporterConfig config;
25
26 50696 claudio.at
	@Override
27 48975 claudio.at
	@Cacheable("vocabularies-cache")
28 50560 claudio.at
	public Vocabulary getCountries() throws DsmException {
29 49887 claudio.at
		return _getVocabulary(config.getVocabularies().getCountriesEndpoint(), Vocabulary.class);
30
	}
31 48975 claudio.at
32 50696 claudio.at
	@Override
33 49887 claudio.at
	@Cacheable("vocabularies-cache")
34 50560 claudio.at
	public Vocabulary getDatasourceTypologies() throws DsmException {
35 49887 claudio.at
		return _getVocabulary(config.getVocabularies().getDatasourceTypologiesEndpoint(), Vocabulary.class);
36
	}
37
38 50560 claudio.at
	private <T> T _getVocabulary(final String endpoint, Class<T> clazz) throws DsmException {
39 48975 claudio.at
		final RestTemplate rt = new RestTemplate();
40 49887 claudio.at
		log.info("get vocabulary from " + endpoint);
41
		final ResponseEntity<T> rsp = rt.getForEntity(endpoint, clazz);
42 50560 claudio.at
43
		if (!rsp.getStatusCode().is2xxSuccessful()) {
44
			throw new DsmException(rsp.getStatusCodeValue(), "unable to read content from " + endpoint);
45
		}
46
47 48975 claudio.at
		return rsp.getBody();
48
	}
49
50 50696 claudio.at
	@Override
51 49534 claudio.at
	@CacheEvict(cacheNames = "vocabularies-cache", allEntries = true)
52
	public void dropCache() {
53
		log.info("dropped dsManager vocabulary cache");
54
	}
55
56 48975 claudio.at
}