1
|
package eu.dnetlib.functionality.modular.ui.patcheditor.client;
|
2
|
|
3
|
import eu.dnetlib.clients.index.client.IndexClient;
|
4
|
import eu.dnetlib.clients.index.client.IndexClientException;
|
5
|
import eu.dnetlib.clients.index.client.ResolvingIndexClientFactory;
|
6
|
import eu.dnetlib.clients.index.client.response.LookupResponse;
|
7
|
import eu.dnetlib.functionality.modular.ui.lightui.objects.IndexConfiguration;
|
8
|
import eu.dnetlib.rmi.common.ResultSetException;
|
9
|
import eu.dnetlib.rmi.provision.IndexServiceException;
|
10
|
import java.util.HashMap;
|
11
|
import java.util.Map;
|
12
|
import org.apache.commons.logging.Log;
|
13
|
import org.apache.commons.logging.LogFactory;
|
14
|
import org.springframework.beans.factory.annotation.Autowired;
|
15
|
|
16
|
public class IndexPatchEditorClient {
|
17
|
|
18
|
private static final Log log = LogFactory.getLog(IndexPatchEditorClient.class);
|
19
|
@Autowired
|
20
|
private ResolvingIndexClientFactory indexClientFactory;
|
21
|
private Map<String, IndexClient> indexClientMap;
|
22
|
|
23
|
public String getDocument(final IndexConfiguration idx, final String field, final String id) throws IndexServiceException,
|
24
|
ResultSetException {
|
25
|
|
26
|
final String query = field + " exact \"" + id.trim() + "\"";
|
27
|
final IndexClient indexClient = getIndexClient(idx);
|
28
|
final LookupResponse lookupResponse = indexClient.lookup(query, null, 0, 1);
|
29
|
|
30
|
final int total = (int) lookupResponse.getTotal();
|
31
|
if (total != 1) {
|
32
|
log.error("Invalid number of results (" + total + ") for query: " + query);
|
33
|
throw new IllegalArgumentException("Invalid number of results (" + total + ") for query: " + query);
|
34
|
}
|
35
|
|
36
|
final String record = lookupResponse.getRecords().get(0);
|
37
|
|
38
|
return record;
|
39
|
}
|
40
|
|
41
|
private IndexClient getIndexClient(final IndexConfiguration configuration) throws IndexClientException {
|
42
|
if (indexClientMap == null) {
|
43
|
indexClientMap = new HashMap<>();
|
44
|
}
|
45
|
if (indexClientMap.containsKey(configuration.getBackendId())) { return indexClientMap.get(configuration.getBackendId()); }
|
46
|
final IndexClient index = indexClientFactory.getClient(configuration.getFormat(), configuration.getLayout(), configuration.getInterpretation(),
|
47
|
configuration.getBackendId());
|
48
|
indexClientMap.put(configuration.getBackendId(), index);
|
49
|
|
50
|
return index;
|
51
|
}
|
52
|
}
|