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.Map;
|
7
|
import java.util.Queue;
|
8
|
import java.util.concurrent.LinkedBlockingQueue;
|
9
|
import java.util.function.Function;
|
10
|
import java.util.stream.Collectors;
|
11
|
|
12
|
import com.google.common.collect.Lists;
|
13
|
import com.google.common.xml.XmlEscapers;
|
14
|
import eu.dnetlib.OpenaireExporterConfig;
|
15
|
import eu.dnetlib.enabling.datasources.common.DatasourceManagerException;
|
16
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
17
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
18
|
import eu.dnetlib.openaire.exporter.datasource.clients.utils.IndexDsInfo;
|
19
|
import eu.dnetlib.openaire.exporter.funders.context.Context;
|
20
|
import eu.dnetlib.openaire.exporter.funders.context.MappingUtils;
|
21
|
import org.apache.commons.io.IOUtils;
|
22
|
import org.apache.commons.lang3.StringUtils;
|
23
|
import org.apache.commons.logging.Log;
|
24
|
import org.apache.commons.logging.LogFactory;
|
25
|
import org.springframework.beans.factory.annotation.Autowired;
|
26
|
import org.springframework.cache.annotation.CacheEvict;
|
27
|
import org.springframework.cache.annotation.Cacheable;
|
28
|
import org.springframework.core.io.ClassPathResource;
|
29
|
import org.springframework.stereotype.Component;
|
30
|
|
31
|
/**
|
32
|
* Created by claudio on 20/10/2016.
|
33
|
*/
|
34
|
@Component
|
35
|
public class ISLookupClient {
|
36
|
|
37
|
private static final Log log = LogFactory.getLog(ISLookupClient.class);
|
38
|
|
39
|
@Autowired
|
40
|
private OpenaireExporterConfig config;
|
41
|
|
42
|
@Autowired
|
43
|
private ISLookUpService isLookUpService;
|
44
|
|
45
|
@Cacheable("datasources-is-cache")
|
46
|
public IndexDsInfo calculateCurrentIndexDsInfo() throws DatasourceManagerException {
|
47
|
log.warn("calculateCurrentIndexDsInfo(): not using cache");
|
48
|
final String[] arr;
|
49
|
try {
|
50
|
arr = _isLookUp(_getQuery(config.getFindIndexDsInfo())).split("@@@");
|
51
|
return new IndexDsInfo(
|
52
|
_isLookUp(_getQuery(config.getFindSolrIndexUrl())),
|
53
|
arr[0].trim(), arr[1].trim(), arr[2].trim());
|
54
|
} catch (IOException | ISLookUpException e) {
|
55
|
throw new DatasourceManagerException("unable fetch index DS information from IS");
|
56
|
}
|
57
|
}
|
58
|
|
59
|
@Cacheable("datasources-is-cache")
|
60
|
public String getObjectStoreId(final String dsId, final Queue<Throwable> errors) throws IOException {
|
61
|
log.warn(String.format("getObjectStoreId(%s): not using cache", dsId));
|
62
|
final String xqueryTemplate = _getQuery(config.getFindObjectStore());
|
63
|
try {
|
64
|
return _isLookUp(String.format(xqueryTemplate, dsId));
|
65
|
} catch (ISLookUpException e) {
|
66
|
errors.add(new DatasourceManagerException("unable to find objectStore for datasource " + dsId));
|
67
|
return "";
|
68
|
}
|
69
|
}
|
70
|
|
71
|
@Cacheable("datasources-is-cache")
|
72
|
public Map<String, Context> getContextMap() throws IOException {
|
73
|
final Queue<Throwable> errors = new LinkedBlockingQueue<>();
|
74
|
try {
|
75
|
return getContextProfiles(errors).stream()
|
76
|
.filter(StringUtils::isNotBlank)
|
77
|
.map(s -> MappingUtils.parseContext(s, errors))
|
78
|
.collect(Collectors.toMap(
|
79
|
Context::getId,
|
80
|
Function.identity()));
|
81
|
} finally {
|
82
|
if (!errors.isEmpty()) {
|
83
|
log.error(errors);
|
84
|
}
|
85
|
}
|
86
|
}
|
87
|
|
88
|
private void updateDatasourceField(final String dsId, final String element, final String value, final Queue<Throwable> errors) {
|
89
|
try {
|
90
|
final String xquery = _getQuery(config.getUpdateRepoField());
|
91
|
_isLookUp(
|
92
|
String.format(
|
93
|
xquery,
|
94
|
XmlEscapers.xmlAttributeEscaper().escape(dsId),
|
95
|
XmlEscapers.xmlContentEscaper().escape(element),
|
96
|
XmlEscapers.xmlContentEscaper().escape(value)));
|
97
|
} catch (IOException | ISLookUpException e) {
|
98
|
errors.add(e);
|
99
|
}
|
100
|
}
|
101
|
|
102
|
/// HELPERS
|
103
|
|
104
|
private List<String> getContextProfiles(final Queue<Throwable> errors) throws IOException {
|
105
|
log.warn("getContextProfiles(): not using cache");
|
106
|
final String xqueryTemplate = _getQuery(config.getFindFunderContexts());
|
107
|
return _quickSeachProfile(xqueryTemplate, errors);
|
108
|
}
|
109
|
|
110
|
private String _getQuery(final ClassPathResource resource) throws IOException {
|
111
|
return IOUtils.toString(resource.getInputStream(), Charset.defaultCharset());
|
112
|
}
|
113
|
|
114
|
private String _isLookUp(final String xquery) throws ISLookUpException {
|
115
|
|
116
|
log.debug(String.format("running xquery: %s", xquery));
|
117
|
final String res = isLookUpService.getResourceProfileByQuery(xquery);
|
118
|
log.debug(String.format("query result: %s", res));
|
119
|
return res;
|
120
|
}
|
121
|
|
122
|
private List<String> _quickSeachProfile(final String xquery, final Queue<Throwable> errors) {
|
123
|
final List<String> res = Lists.newArrayList();
|
124
|
try {
|
125
|
log.debug(String.format("running xquery: %s", xquery));
|
126
|
res.addAll(isLookUpService.quickSearchProfile(xquery)) ;
|
127
|
log.debug(String.format("query result size: %s", res.size()));
|
128
|
} catch (Throwable e) {
|
129
|
errors.add(e);
|
130
|
return Lists.newArrayList();
|
131
|
} finally {
|
132
|
return res;
|
133
|
}
|
134
|
}
|
135
|
|
136
|
@CacheEvict(cacheNames = "datasources-is-cache", allEntries = true)
|
137
|
public void dropCache() {
|
138
|
log.info("dropped dsManager IS cache");
|
139
|
}
|
140
|
|
141
|
}
|