Project

General

Profile

1
package eu.dnetlib.openaire.exporter.datasource.clients;
2

    
3
import java.io.IOException;
4
import java.nio.charset.Charset;
5
import java.util.List;
6
import java.util.Queue;
7

    
8
import com.google.common.collect.Lists;
9
import eu.dnetlib.OpenaireExporterConfig;
10
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
11
import eu.dnetlib.openaire.exporter.datasource.clients.utils.IndexDsInfo;
12
import org.apache.commons.io.IOUtils;
13
import org.apache.commons.logging.Log;
14
import org.apache.commons.logging.LogFactory;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.cache.annotation.CacheEvict;
17
import org.springframework.cache.annotation.Cacheable;
18
import org.springframework.core.io.ClassPathResource;
19
import org.springframework.stereotype.Component;
20

    
21
/**
22
 * Created by claudio on 20/10/2016.
23
 */
24
@Component
25
public class ISLookupClient {
26

    
27
 	private static final Log log = LogFactory.getLog(ISLookupClient.class);
28

    
29
	@Autowired
30
	private OpenaireExporterConfig config;
31

    
32
	@Autowired
33
	private ISLookUpService isLookUpService;
34

    
35
	@Cacheable("datasources-is-cache")
36
	public IndexDsInfo calculateCurrentIndexDsInfo(final Queue<Throwable> errors) throws IOException {
37
		log.warn("calculateCurrentIndexDsInfo(): not using cache");
38
		final String[] arr = _isLookUp(_getQuery(config.getFindIndexDsInfo()), errors).split("@@@");
39
		return new IndexDsInfo(
40
				_isLookUp(_getQuery(config.getFindSolrIndexUrl()), errors),
41
				arr[0].trim(), arr[1].trim(), arr[2].trim());
42
	}
43

    
44
	@Cacheable("datasources-is-cache")
45
	public String getObjectStoreId(final String dsId, final Queue<Throwable> errors) throws IOException {
46
		log.warn(String.format("getObjectStoreId(%s): not using cache", dsId));
47
		final String xqueryTemplate = _getQuery(config.getFindObjectStore());
48
		return _isLookUp(String.format(xqueryTemplate, dsId), errors);
49
	}
50

    
51
	public List<String> getContextProfiles(final Queue<Throwable> errors) throws IOException {
52
		log.warn("getContextProfiles(): not using cache");
53
		final String xqueryTemplate = _getQuery(config.getFindFunderContexts());
54
		return _quickSeachProfile(xqueryTemplate, errors);
55
	}
56

    
57
	private String _getQuery(final ClassPathResource resource) throws IOException {
58
		return IOUtils.toString(resource.getInputStream(), Charset.defaultCharset());
59
	}
60

    
61
	private String _isLookUp(final String xquery, final Queue<Throwable> errors) {
62
		try {
63
			log.debug(String.format("running xquery: %s", xquery));
64
			final String res = isLookUpService.getResourceProfileByQuery(xquery);
65
			log.debug(String.format("query result: %s", res));
66
			return res;
67
		} catch (Throwable e) {
68
			errors.add(e);
69
			return "";
70
		}
71
	}
72

    
73
	private List<String> _quickSeachProfile(final String xquery, final Queue<Throwable> errors) {
74
		final List<String> res = Lists.newArrayList();
75
		try {
76
			log.debug(String.format("running xquery: %s", xquery));
77
			res.addAll(isLookUpService.quickSearchProfile(xquery)) ;
78
			log.debug(String.format("query result size: %s", res.size()));
79
		} catch (Throwable e) {
80
			errors.add(e);
81
			return Lists.newArrayList();
82
		} finally {
83
			return res;
84
		}
85
	}
86

    
87
	@CacheEvict(cacheNames = "datasources-is-cache", allEntries = true)
88
	public void dropCache() {
89
		log.info("dropped dsManager IS cache");
90
	}
91

    
92
}
(5-5/10)