1 |
48332
|
claudio.at
|
package eu.dnetlib.openaire.exporter.datasource.clients;
|
2 |
44381
|
claudio.at
|
|
3 |
49994
|
claudio.at
|
import java.io.IOException;
|
4 |
48399
|
claudio.at
|
import java.nio.charset.Charset;
|
5 |
50022
|
claudio.at
|
import java.util.List;
|
6 |
49994
|
claudio.at
|
import java.util.Queue;
|
7 |
48399
|
claudio.at
|
|
8 |
50022
|
claudio.at
|
import com.google.common.collect.Lists;
|
9 |
49994
|
claudio.at
|
import eu.dnetlib.OpenaireExporterConfig;
|
10 |
|
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
11 |
|
|
import eu.dnetlib.openaire.exporter.datasource.clients.utils.IndexDsInfo;
|
12 |
44381
|
claudio.at
|
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 |
49994
|
claudio.at
|
import org.springframework.cache.annotation.CacheEvict;
|
17 |
44381
|
claudio.at
|
import org.springframework.cache.annotation.Cacheable;
|
18 |
49994
|
claudio.at
|
import org.springframework.core.io.ClassPathResource;
|
19 |
48332
|
claudio.at
|
import org.springframework.stereotype.Component;
|
20 |
44381
|
claudio.at
|
|
21 |
|
|
/**
|
22 |
|
|
* Created by claudio on 20/10/2016.
|
23 |
|
|
*/
|
24 |
48332
|
claudio.at
|
@Component
|
25 |
44381
|
claudio.at
|
public class ISLookupClient {
|
26 |
|
|
|
27 |
49994
|
claudio.at
|
private static final Log log = LogFactory.getLog(ISLookupClient.class);
|
28 |
44381
|
claudio.at
|
|
29 |
|
|
@Autowired
|
30 |
48399
|
claudio.at
|
private OpenaireExporterConfig config;
|
31 |
|
|
|
32 |
|
|
@Autowired
|
33 |
48332
|
claudio.at
|
private ISLookUpService isLookUpService;
|
34 |
44381
|
claudio.at
|
|
35 |
44690
|
claudio.at
|
@Cacheable("datasources-is-cache")
|
36 |
50022
|
claudio.at
|
public IndexDsInfo calculateCurrentIndexDsInfo(final Queue<Throwable> errors) throws IOException {
|
37 |
44690
|
claudio.at
|
log.warn("calculateCurrentIndexDsInfo(): not using cache");
|
38 |
49994
|
claudio.at
|
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 |
50022
|
claudio.at
|
public String getObjectStoreId(final String dsId, final Queue<Throwable> errors) throws IOException {
|
46 |
49994
|
claudio.at
|
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 |
50022
|
claudio.at
|
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 |
49994
|
claudio.at
|
}
|
56 |
44381
|
claudio.at
|
|
57 |
50022
|
claudio.at
|
private String _getQuery(final ClassPathResource resource) throws IOException {
|
58 |
|
|
return IOUtils.toString(resource.getInputStream(), Charset.defaultCharset());
|
59 |
|
|
}
|
60 |
|
|
|
61 |
49994
|
claudio.at
|
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 |
48725
|
claudio.at
|
} catch (Throwable e) {
|
68 |
49994
|
claudio.at
|
errors.add(e);
|
69 |
|
|
return "";
|
70 |
44381
|
claudio.at
|
}
|
71 |
|
|
}
|
72 |
|
|
|
73 |
50022
|
claudio.at
|
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 |
49994
|
claudio.at
|
@CacheEvict(cacheNames = "datasources-is-cache", allEntries = true)
|
88 |
|
|
public void dropCache() {
|
89 |
|
|
log.info("dropped dsManager IS cache");
|
90 |
|
|
}
|
91 |
|
|
|
92 |
44381
|
claudio.at
|
}
|