1 | package eu.clarin.cmdi.vlo.pages; |
---|
2 | |
---|
3 | import java.util.Iterator; |
---|
4 | |
---|
5 | import org.apache.solr.client.solrj.SolrQuery; |
---|
6 | import org.apache.solr.common.SolrDocument; |
---|
7 | import org.apache.solr.common.SolrDocumentList; |
---|
8 | import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider; |
---|
9 | import org.apache.wicket.model.IModel; |
---|
10 | import org.apache.wicket.model.Model; |
---|
11 | import org.slf4j.Logger; |
---|
12 | import org.slf4j.LoggerFactory; |
---|
13 | |
---|
14 | import eu.clarin.cmdi.vlo.FacetConstants; |
---|
15 | import eu.clarin.cmdi.vlo.dao.DaoLocator; |
---|
16 | import eu.clarin.cmdi.vlo.dao.SearchResultsDao; |
---|
17 | |
---|
18 | /** |
---|
19 | * Data provider of all documents with SearchService entry (FCS) based on existing SolrQuery |
---|
20 | * |
---|
21 | * @author Thomas Eckart |
---|
22 | * |
---|
23 | */ |
---|
24 | public class SearchServiceDataProvider extends SortableDataProvider<SolrDocument> { |
---|
25 | private final static Logger LOG = LoggerFactory.getLogger(SearchServiceDataProvider.class); |
---|
26 | |
---|
27 | private static final long serialVersionUID = -5355607690141772113L; |
---|
28 | private final SolrQuery query; |
---|
29 | private SolrDocumentList docList; |
---|
30 | |
---|
31 | public SearchServiceDataProvider(SolrQuery query) { |
---|
32 | this.query = query; |
---|
33 | this.query.setFacet(false); |
---|
34 | this.query.setStart(0); |
---|
35 | this.query.setRows(10); |
---|
36 | this.query.setFields(FacetConstants.FIELD_SEARCH_SERVICE, FacetConstants.FIELD_ID); |
---|
37 | this.query.setQuery(query.getQuery()); |
---|
38 | this.query.addFilterQuery(FacetConstants.FIELD_SEARCH_SERVICE + ":*"); |
---|
39 | LOG.debug("Used query for search services: "+this.query.toString()); |
---|
40 | } |
---|
41 | |
---|
42 | private SearchResultsDao getSearchResultsDao() { |
---|
43 | return DaoLocator.getSearchResultsDao(); |
---|
44 | } |
---|
45 | |
---|
46 | private SolrDocumentList getDocList() { |
---|
47 | if (docList == null) { |
---|
48 | docList = getSearchResultsDao().getResults(query); |
---|
49 | } |
---|
50 | return docList; |
---|
51 | } |
---|
52 | |
---|
53 | @Override |
---|
54 | public Iterator<SolrDocument> iterator(int first, int count) { |
---|
55 | if (first != query.getStart().intValue() || count != query.getRows().intValue()) { |
---|
56 | query.setStart(first).setRows(count); |
---|
57 | docList = null; |
---|
58 | } |
---|
59 | return getDocList().iterator(); |
---|
60 | } |
---|
61 | |
---|
62 | public Iterator<SolrDocument> iterator() { |
---|
63 | return getDocList().iterator(); |
---|
64 | } |
---|
65 | |
---|
66 | @Override |
---|
67 | public IModel<SolrDocument> model(SolrDocument solrDocument) { |
---|
68 | return new Model<SolrDocument>(solrDocument); |
---|
69 | } |
---|
70 | |
---|
71 | @Override |
---|
72 | public int size() { |
---|
73 | return (int) getDocList().getNumFound(); |
---|
74 | } |
---|
75 | } |
---|