Revision 28290
Added by Michele Artini about 10 years ago
IndexClient.java | ||
---|---|---|
8 | 8 |
import javax.annotation.Resource; |
9 | 9 |
import javax.xml.ws.wsaddressing.W3CEndpointReference; |
10 | 10 |
|
11 |
import org.apache.commons.logging.Log; |
|
12 |
import org.apache.commons.logging.LogFactory; |
|
11 | 13 |
import org.dom4j.Document; |
12 | 14 |
import org.dom4j.DocumentException; |
13 | 15 |
import org.dom4j.Node; |
... | ... | |
18 | 20 |
|
19 | 21 |
import eu.dnetlib.data.provision.index.rmi.IndexService; |
20 | 22 |
import eu.dnetlib.data.provision.index.rmi.IndexServiceException; |
21 |
import eu.dnetlib.enabling.resultset.ResultSetFactory; |
|
22 |
import eu.dnetlib.enabling.resultset.client.ResultSetClientFactory; |
|
23 | 23 |
import eu.dnetlib.enabling.resultset.rmi.ResultSetException; |
24 | 24 |
import eu.dnetlib.enabling.resultset.rmi.ResultSetService; |
25 | 25 |
import eu.dnetlib.enabling.tools.ServiceLocator; |
... | ... | |
27 | 27 |
import eu.dnetlib.functionality.modular.ui.lightui.objects.BrowseFieldResult; |
28 | 28 |
import eu.dnetlib.functionality.modular.ui.lightui.objects.BrowseValue; |
29 | 29 |
import eu.dnetlib.functionality.modular.ui.lightui.objects.SearchResult; |
30 |
import eu.dnetlib.miscutils.functional.UnaryFunction; |
|
30 | 31 |
|
31 | 32 |
public class IndexClient { |
32 | 33 |
|
33 | 34 |
private ServiceLocator<IndexService> indexLocator; |
34 | 35 |
|
35 | 36 |
@Resource |
36 |
private ResultSetClientFactory resultSetClientFactory; |
|
37 |
@Resource |
|
38 | 37 |
private ServiceResolver serviceResolver; |
39 |
|
|
40 |
|
|
38 |
|
|
41 | 39 |
private String mdFormat; |
42 | 40 |
private String layout; |
43 | 41 |
private String interpretation; |
42 |
private String browseFields; |
|
44 | 43 |
private int maxBrowseValues = 20; |
45 | 44 |
private int pageSize = 10; |
45 |
|
|
46 |
private UnaryFunction<String, String> toRow; |
|
47 |
private UnaryFunction<String, String> toDocument; |
|
48 |
|
|
49 |
private static final Log log = LogFactory.getLog(IndexClient.class); |
|
50 |
|
|
46 | 51 |
|
47 |
public List<BrowseFieldResult> browse(final String query) throws IndexServiceException, DocumentException { |
|
48 |
final W3CEndpointReference epr = indexLocator.getService().browse(query, mdFormat, layout, interpretation);
|
|
52 |
public List<BrowseFieldResult> browse(final String query) throws IndexServiceException, DocumentException, ResultSetException {
|
|
53 |
final String q = "(" + query + ")&groupby=" + browseFields.replaceAll("\\s", "");
|
|
49 | 54 |
|
55 |
log.info("executing browse query: " + q); |
|
56 |
|
|
57 |
final W3CEndpointReference epr = indexLocator.getService().browse(q, mdFormat, layout, interpretation); |
|
58 |
|
|
50 | 59 |
final List<BrowseFieldResult> list = Lists.newArrayList(); |
51 | 60 |
|
52 |
final SAXReader reader = new SAXReader(); |
|
53 |
|
|
54 |
for (String s : resultSetClientFactory.getClient(epr)) { |
|
55 |
final Document doc = reader.read(new StringReader(s)); |
|
61 |
final ResultSetService rs = serviceResolver.getService(ResultSetService.class, epr); |
|
62 |
final String rsId = serviceResolver.getResourceIdentifier(epr); |
|
63 |
final int total = rs.getNumberOfElements(rsId); |
|
56 | 64 |
|
57 |
for (Object o : doc.selectNodes("//groupresult")) { |
|
58 |
final Node node = (Node) o; |
|
59 |
final String field = node.valueOf("@field"); |
|
60 |
final String value = node.valueOf("./value"); |
|
61 |
|
|
62 |
int size = Integer.parseInt(node.valueOf("./count")); |
|
65 |
if (total > 0) { |
|
63 | 66 |
|
64 |
boolean notFound = true; |
|
65 |
for (int i = 0; (i < list.size()) && notFound; i++) { |
|
66 |
final BrowseFieldResult elem = list.get(i); |
|
67 |
if (elem.getField().equals(field)) { |
|
67 |
int to = Math.min(maxBrowseValues, total); |
|
68 |
|
|
69 |
final SAXReader reader = new SAXReader(); |
|
70 |
|
|
71 |
for (String s : rs.getResult(rsId, 1, to, "WAITING")) { |
|
72 |
final Document doc = reader.read(new StringReader(s)); |
|
73 |
|
|
74 |
for (Object o : doc.selectNodes("//groupresult")) { |
|
75 |
final Node node = (Node) o; |
|
76 |
final String field = node.valueOf("@field"); |
|
77 |
final String value = node.valueOf("./value"); |
|
78 |
final int size = Integer.parseInt(node.valueOf("./count")); |
|
79 |
|
|
80 |
boolean notFound = true; |
|
81 |
for (int i = 0; (i < list.size()) && notFound; i++) { |
|
82 |
final BrowseFieldResult elem = list.get(i); |
|
83 |
if (elem.getField().equals(field)) { |
|
84 |
elem.getValues().add(new BrowseValue(value, size)); |
|
85 |
notFound = false; |
|
86 |
} |
|
87 |
} |
|
88 |
if (notFound) { |
|
89 |
final BrowseFieldResult elem = new BrowseFieldResult(field, new ArrayList<BrowseValue>()); |
|
68 | 90 |
elem.getValues().add(new BrowseValue(value, size)); |
69 |
notFound = false;
|
|
91 |
list.add(elem);
|
|
70 | 92 |
} |
71 | 93 |
} |
72 |
if (notFound) { |
|
73 |
final BrowseFieldResult elem = new BrowseFieldResult(field, new ArrayList<BrowseValue>()); |
|
74 |
elem.getValues().add(new BrowseValue(value, size)); |
|
75 |
list.add(elem); |
|
94 |
|
|
95 |
if (list.size() >= maxBrowseValues) { |
|
96 |
break; |
|
76 | 97 |
} |
77 | 98 |
} |
78 |
|
|
79 |
if (list.size() >= maxBrowseValues) { |
|
80 |
break; |
|
81 |
} |
|
82 | 99 |
} |
83 | 100 |
|
84 | 101 |
return list; |
85 | 102 |
} |
86 | 103 |
|
87 | 104 |
public SearchResult search(final String query, final int page) throws IndexServiceException, DocumentException, ResultSetException { |
105 |
|
|
106 |
log.info("executing query: " + query); |
|
107 |
|
|
88 | 108 |
final W3CEndpointReference epr = indexLocator.getService().lookup(query, mdFormat, layout, interpretation); |
89 | 109 |
|
110 |
final ResultSetService rs = serviceResolver.getService(ResultSetService.class, epr); |
|
90 | 111 |
final String rsId = serviceResolver.getResourceIdentifier(epr); |
91 |
final ResultSetService rs = serviceResolver.getService(ResultSetService.class, epr); |
|
92 | 112 |
|
113 |
final StringWriter html = new StringWriter(); |
|
93 | 114 |
final int total = rs.getNumberOfElements(rsId); |
94 |
final int totalPages = (total / pageSize) + 1;
|
|
115 |
final int totalPages = total > 0 ? (total / pageSize) + 1 : 0;
|
|
95 | 116 |
|
96 |
final int to = page * pageSize; |
|
97 |
final int from = (to - pageSize) + 1; |
|
98 |
|
|
99 |
final StringWriter html = new StringWriter(); |
|
100 |
final SAXReader reader = new SAXReader(); |
|
117 |
if (totalPages > 0 && page > 0 && page <= totalPages) { |
|
118 |
int prod = page * pageSize; |
|
119 |
final int from = prod - pageSize + 1; |
|
120 |
final int to = prod < total ? prod : total; |
|
121 |
|
|
122 |
log.info("Getting results from " + from + " to " + to + ", total: " + total); |
|
123 |
|
|
124 |
for (String s : rs.getResult(rsId, from, to, "WAITING")) { |
|
125 |
html.append(toRow.evaluate(s)); |
|
126 |
} |
|
127 |
} |
|
101 | 128 |
|
102 |
for (String s : rs.getResult(rsId, from, to, "WAITING")) { |
|
103 |
html.append(""); //TODO |
|
104 |
} |
|
105 |
|
|
106 | 129 |
return new SearchResult(page, totalPages, html.toString()); |
107 | 130 |
} |
108 | 131 |
|
... | ... | |
116 | 139 |
this.indexLocator = indexLocator; |
117 | 140 |
} |
118 | 141 |
|
119 |
public ResultSetClientFactory getResultSetClientFactory() { |
|
120 |
return resultSetClientFactory; |
|
121 |
} |
|
122 |
|
|
123 |
public void setResultSetClientFactory(ResultSetClientFactory resultSetClientFactory) { |
|
124 |
this.resultSetClientFactory = resultSetClientFactory; |
|
125 |
} |
|
126 |
|
|
127 | 142 |
public String getMdFormat() { |
128 | 143 |
return mdFormat; |
129 | 144 |
} |
... | ... | |
159 | 174 |
public void setMaxBrowseValues(int maxBrowseValues) { |
160 | 175 |
this.maxBrowseValues = maxBrowseValues; |
161 | 176 |
} |
162 |
|
|
177 |
|
|
178 |
public String getBrowseFields() { |
|
179 |
return browseFields; |
|
180 |
} |
|
181 |
|
|
182 |
@Required |
|
183 |
public void setBrowseFields(String browseFields) { |
|
184 |
this.browseFields = browseFields; |
|
185 |
} |
|
186 |
|
|
187 |
public UnaryFunction<String, String> getToRow() { |
|
188 |
return toRow; |
|
189 |
} |
|
190 |
|
|
191 |
@Required |
|
192 |
public void setToRow(UnaryFunction<String, String> toRow) { |
|
193 |
this.toRow = toRow; |
|
194 |
} |
|
195 |
|
|
196 |
public UnaryFunction<String, String> getToDocument() { |
|
197 |
return toDocument; |
|
198 |
} |
|
199 |
|
|
200 |
@Required |
|
201 |
public void setToDocument(UnaryFunction<String, String> toDocument) { |
|
202 |
this.toDocument = toDocument; |
|
203 |
} |
|
163 | 204 |
} |
Also available in: Unified diff
partial implementation