1
|
package eu.dnetlib.functionality.modular.ui.lightui.clients;
|
2
|
|
3
|
import java.io.StringWriter;
|
4
|
import java.util.ArrayList;
|
5
|
import java.util.HashMap;
|
6
|
import java.util.List;
|
7
|
import java.util.Map;
|
8
|
|
9
|
import eu.dnetlib.functionality.index.client.IndexClientFactory;
|
10
|
import org.apache.commons.logging.Log;
|
11
|
import org.apache.commons.logging.LogFactory;
|
12
|
import org.dom4j.Document;
|
13
|
import org.dom4j.DocumentException;
|
14
|
import org.dom4j.DocumentHelper;
|
15
|
import org.dom4j.Element;
|
16
|
import org.springframework.beans.factory.annotation.Autowired;
|
17
|
import org.springframework.core.io.Resource;
|
18
|
|
19
|
import com.google.common.collect.Lists;
|
20
|
|
21
|
import eu.dnetlib.data.provision.index.rmi.IndexServiceException;
|
22
|
import eu.dnetlib.enabling.resultset.rmi.ResultSetException;
|
23
|
import eu.dnetlib.functionality.index.client.IndexClient;
|
24
|
import eu.dnetlib.functionality.index.client.IndexClientException;
|
25
|
import eu.dnetlib.functionality.index.client.response.BrowseEntry;
|
26
|
import eu.dnetlib.functionality.index.client.response.BrowseValueEntry;
|
27
|
import eu.dnetlib.functionality.index.client.response.LookupResponse;
|
28
|
import eu.dnetlib.functionality.modular.ui.lightui.objects.IndexConfiguration;
|
29
|
import eu.dnetlib.functionality.modular.ui.lightui.objects.SearchResult;
|
30
|
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
|
31
|
|
32
|
public class IndexLightUIClient {
|
33
|
|
34
|
@Autowired
|
35
|
private IndexClientFactory indexClientFactory;
|
36
|
|
37
|
private Map<String, IndexClient> indexClientMap;
|
38
|
|
39
|
private static final Log log = LogFactory.getLog(IndexLightUIClient.class);
|
40
|
|
41
|
/**
|
42
|
* Fixes the labels of the index response according to the specification of the given map.
|
43
|
* <p>
|
44
|
* If a browse entry has label = x and field=xForBrowsing, and the map contains (x, xForUI), the returned BrowseEntry will contain label
|
45
|
* = xForUI, field=xForBrowsing.
|
46
|
* </p>
|
47
|
*
|
48
|
* @param browseResult
|
49
|
* List of BrowseEntry
|
50
|
* @param fieldLabels
|
51
|
* @return
|
52
|
*/
|
53
|
private List<BrowseEntry> fixBrowseEntryLabels(final List<BrowseEntry> browseResult, final Map<String, String> fieldLabels) {
|
54
|
List<BrowseEntry> fixedBrowseLabels = Lists.newArrayListWithExpectedSize(browseResult.size());
|
55
|
for (BrowseEntry be : browseResult) {
|
56
|
String fieldName = be.getLabel();
|
57
|
String browsableFieldName = be.getField();
|
58
|
if (fieldLabels.containsKey(fieldName)) {
|
59
|
fixedBrowseLabels.add(new BrowseEntry(browsableFieldName, fieldLabels.get(fieldName), be.getValues()));
|
60
|
} else {
|
61
|
fixedBrowseLabels.add(be);
|
62
|
}
|
63
|
}
|
64
|
return fixedBrowseLabels;
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
*
|
69
|
* @param query
|
70
|
* @param idx
|
71
|
* @param browseFieldLabels
|
72
|
* map with key = field name to browse for, value = label to use in the UI
|
73
|
* @param max
|
74
|
* @return
|
75
|
* @throws IndexServiceException
|
76
|
* @throws DocumentException
|
77
|
* @throws ResultSetException
|
78
|
*/
|
79
|
public List<BrowseEntry> browse(final String query, final IndexConfiguration idx, final Map<String, String> browseFieldLabels, final int max)
|
80
|
throws IndexServiceException, DocumentException, ResultSetException {
|
81
|
|
82
|
final List<String> browseFields = new ArrayList<String>(browseFieldLabels.keySet());
|
83
|
|
84
|
IndexClient indexClient = getIndexClient(idx);
|
85
|
List<BrowseEntry> browseResult = indexClient.browse(query, browseFields, max);
|
86
|
|
87
|
return fixBrowseEntryLabels(browseResult, browseFieldLabels);
|
88
|
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
*
|
93
|
* @param query
|
94
|
* @param idx
|
95
|
* @param browseFieldLabels
|
96
|
* map with key = field name to browse for, value = label to use in the UI
|
97
|
* @param max
|
98
|
* @param xslt
|
99
|
* @return
|
100
|
* @throws IndexServiceException
|
101
|
* @throws DocumentException
|
102
|
* @throws ResultSetException
|
103
|
*/
|
104
|
public String browse(final String query, final IndexConfiguration idx, final Map<String, String> browseFieldLabels, final int max, final Resource xslt)
|
105
|
throws IndexServiceException, DocumentException, ResultSetException {
|
106
|
List<BrowseEntry> myResults = browse(query, idx, browseFieldLabels, max);
|
107
|
int total = myResults.size();
|
108
|
|
109
|
if (total > 0) {
|
110
|
|
111
|
final Element docRoot = DocumentHelper.createElement("browse");
|
112
|
final Document docRes = DocumentHelper.createDocument(docRoot);
|
113
|
|
114
|
for (BrowseEntry myEntry : myResults) {
|
115
|
|
116
|
String xpath = "./field[@name='" + myEntry.getField() + "']";
|
117
|
|
118
|
Element f = (Element) docRoot.selectSingleNode(xpath);
|
119
|
if (f == null) {
|
120
|
f = docRoot.addElement("field");
|
121
|
f.addAttribute("name", myEntry.getField());
|
122
|
f.addAttribute("label", myEntry.getLabel());
|
123
|
}
|
124
|
|
125
|
for (BrowseValueEntry valueEntry : myEntry.getValues()) {
|
126
|
final Element v = f.addElement("value");
|
127
|
v.addAttribute("name", valueEntry.getValue().replaceAll("\"", "%22").replaceAll("'", "%27"));
|
128
|
v.addAttribute("size", "" + valueEntry.getSize());
|
129
|
}
|
130
|
|
131
|
}
|
132
|
|
133
|
return new ApplyXslt(xslt).evaluate(docRes.asXML());
|
134
|
}
|
135
|
|
136
|
return "";
|
137
|
}
|
138
|
|
139
|
public SearchResult search(final String query, final IndexConfiguration idx, final int page, final int pageSize, final Resource xslt)
|
140
|
throws IndexServiceException, DocumentException, ResultSetException {
|
141
|
|
142
|
log.info("executing query: " + query);
|
143
|
IndexClient indexClient = getIndexClient(idx);
|
144
|
int prod = page * pageSize;
|
145
|
final int from = prod - pageSize;
|
146
|
final int to = (from + pageSize) - 1;
|
147
|
LookupResponse lookupResponse = indexClient.lookup(query, null, from, to);
|
148
|
|
149
|
final long total = lookupResponse.getTotal();
|
150
|
final StringWriter html = new StringWriter();
|
151
|
|
152
|
ApplyXslt f = new ApplyXslt(xslt);
|
153
|
int totalPages = 0;
|
154
|
if (lookupResponse.getRecords() != null) {
|
155
|
for (String s : lookupResponse.getRecords()) {
|
156
|
html.append(f.evaluate(s));
|
157
|
}
|
158
|
totalPages = ((int) lookupResponse.getTotal() / pageSize) + 1;
|
159
|
}
|
160
|
|
161
|
return new SearchResult(page, totalPages, total, html.toString());
|
162
|
}
|
163
|
|
164
|
public String getDocument(final IndexConfiguration idx, final String field, final String id, final Resource xslt) throws IndexServiceException,
|
165
|
ResultSetException {
|
166
|
|
167
|
final String query = field + " exact \"" + id.trim() + "\"";
|
168
|
IndexClient indexClient = getIndexClient(idx);
|
169
|
LookupResponse lookupResponse = indexClient.lookup(query, null, 0, 1);
|
170
|
|
171
|
final int total = (int) lookupResponse.getTotal();
|
172
|
if (total != 1) {
|
173
|
log.error("Invalid number of results (" + total + ") for query: " + query);
|
174
|
throw new IllegalArgumentException("Invalid number of results (" + total + ") for query: " + query);
|
175
|
}
|
176
|
|
177
|
final String record = lookupResponse.getRecords().get(0);
|
178
|
|
179
|
return new ApplyXslt(xslt).evaluate(record);
|
180
|
}
|
181
|
|
182
|
private IndexClient getIndexClient(final IndexConfiguration configuration) throws IndexClientException {
|
183
|
if (indexClientMap == null) {
|
184
|
indexClientMap = new HashMap<String, IndexClient>();
|
185
|
}
|
186
|
if (indexClientMap.containsKey(configuration.getBackendId())) return indexClientMap.get(configuration.getBackendId());
|
187
|
|
188
|
final IndexClient index = indexClientFactory.getClient(configuration.getFormat(), configuration.getLayout(), configuration.getInterpretation());
|
189
|
indexClientMap.put(configuration.getBackendId(), index);
|
190
|
|
191
|
return index;
|
192
|
}
|
193
|
}
|