1
|
package eu.dnetlib.functionality.modular.ui.lightui.objects;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.List;
|
5
|
import java.util.stream.Collectors;
|
6
|
import javax.annotation.Resource;
|
7
|
|
8
|
import com.google.common.collect.Lists;
|
9
|
import eu.dnetlib.functionality.modular.ui.lightui.LightUIUtils;
|
10
|
import eu.dnetlib.functionality.modular.ui.lightui.clients.ISLookupLightUIClient;
|
11
|
import eu.dnetlib.functionality.modular.ui.lightui.clients.IndexLightUIClient;
|
12
|
import org.apache.commons.logging.Log;
|
13
|
import org.apache.commons.logging.LogFactory;
|
14
|
import org.dom4j.Document;
|
15
|
import org.dom4j.Element;
|
16
|
import org.dom4j.Node;
|
17
|
import org.dom4j.io.SAXReader;
|
18
|
|
19
|
public class SearchFieldGrouper {
|
20
|
|
21
|
private static final int maxBrowseValues = 100;
|
22
|
private static final Log log = LogFactory.getLog(SearchFieldGrouper.class);
|
23
|
@Resource
|
24
|
private IndexLightUIClient indexClient;
|
25
|
@Resource
|
26
|
private ISLookupLightUIClient isLookupClient;
|
27
|
|
28
|
public List<SearchField> getAllFields(final String lightuiId) {
|
29
|
final List<SearchField> res = Lists.newArrayList();
|
30
|
final SAXReader reader = new SAXReader();
|
31
|
try {
|
32
|
final Document doc = reader.read(new StringReader(isLookupClient.getLightUiProfile(lightuiId)));
|
33
|
final Node searchNode = doc.selectSingleNode("//CONFIGURATION/SEARCH");
|
34
|
final IndexConfiguration idx = LightUIUtils.calculateIndexConfiguration(searchNode);
|
35
|
res.addAll(getPredefinedFields(searchNode));
|
36
|
res.addAll(calculateBrowseFields(idx, searchNode));
|
37
|
} catch (Exception e) {
|
38
|
log.error("Error obtaining search fields", e);
|
39
|
}
|
40
|
return res;
|
41
|
}
|
42
|
|
43
|
private List<SearchField> calculateBrowseFields(final IndexConfiguration idx, final Node node) {
|
44
|
try {
|
45
|
return indexClient.browse("*=*", idx, LightUIUtils.getBrowseFields(node), maxBrowseValues)
|
46
|
.stream()
|
47
|
.map(r -> {
|
48
|
final SearchField sf = new SearchField();
|
49
|
sf.setField(r.getField());
|
50
|
sf.setLabel(r.getLabel());
|
51
|
sf.setOperator("exact");
|
52
|
sf.setPredefinedValues(
|
53
|
r.getValues().stream()
|
54
|
.map(bv -> new ValueWithLabel(bv.getValue() + " (" + bv.getSize() + ")", bv.getValue()))
|
55
|
.collect(Collectors.toList()));
|
56
|
return sf;
|
57
|
})
|
58
|
.collect(Collectors.toList());
|
59
|
|
60
|
} catch (Exception e) {
|
61
|
log.warn("Error executing default browse query", e);
|
62
|
return Lists.newArrayList();
|
63
|
}
|
64
|
}
|
65
|
|
66
|
private List<SearchField> getPredefinedFields(final Node node) {
|
67
|
final List<SearchField> list = Lists.newArrayList();
|
68
|
|
69
|
for (Object o : node.selectNodes(".//TEXT_FIELD")) {
|
70
|
final String id = ((Element) o).valueOf("@id");
|
71
|
final String label = ((Element) o).valueOf("@label");
|
72
|
final String operator = ((Element) o).valueOf("@operator");
|
73
|
list.add(new SearchField(id, label, operator));
|
74
|
}
|
75
|
|
76
|
for (Object o : node.selectNodes(".//SELECT_FIELD")) {
|
77
|
final String id = ((Element) o).valueOf("@id");
|
78
|
final String label = ((Element) o).valueOf("@label");
|
79
|
final String operator = ((Element) o).valueOf("@operator");
|
80
|
|
81
|
final List<ValueWithLabel> values = Lists.newArrayList();
|
82
|
for (Object v : ((Element) o).selectNodes("./VALUE")) {
|
83
|
final String v_id = ((Element) v).valueOf("@id");
|
84
|
final String v_label = ((Element) v).valueOf("@label");
|
85
|
values.add(new ValueWithLabel(v_label, v_id));
|
86
|
}
|
87
|
list.add(new SearchField(id, label, operator, values));
|
88
|
}
|
89
|
|
90
|
return list;
|
91
|
}
|
92
|
|
93
|
}
|