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 |
---|
20 | * existing SolrQuery |
---|
21 | * |
---|
22 | * @author Thomas Eckart |
---|
23 | * |
---|
24 | */ |
---|
25 | public class SearchServiceDataProvider extends SortableDataProvider<SolrDocument, String> { |
---|
26 | |
---|
27 | private final static Logger LOG = LoggerFactory.getLogger(SearchServiceDataProvider.class); |
---|
28 | private static final long serialVersionUID = -5355607690141772113L; |
---|
29 | private final SolrQuery query; |
---|
30 | private SolrDocumentList docList; |
---|
31 | |
---|
32 | public SearchServiceDataProvider(SolrQuery query) { |
---|
33 | this.query = query; |
---|
34 | this.query.setFacet(false); |
---|
35 | this.query.setStart(0); |
---|
36 | this.query.setRows(10); |
---|
37 | this.query.setFields(FacetConstants.FIELD_SEARCH_SERVICE, FacetConstants.FIELD_ID); |
---|
38 | this.query.setQuery(query.getQuery()); |
---|
39 | this.query.addFilterQuery(FacetConstants.FIELD_SEARCH_SERVICE + ":*"); |
---|
40 | LOG.debug("Used query for search services: " + this.query.toString()); |
---|
41 | } |
---|
42 | |
---|
43 | private SearchResultsDao getSearchResultsDao() { |
---|
44 | return DaoLocator.getSearchResultsDao(); |
---|
45 | } |
---|
46 | |
---|
47 | private SolrDocumentList getDocList() { |
---|
48 | if (docList == null) { |
---|
49 | docList = getSearchResultsDao().getResults(query); |
---|
50 | } |
---|
51 | return docList; |
---|
52 | } |
---|
53 | |
---|
54 | @Override |
---|
55 | public Iterator<SolrDocument> iterator(long first, long count) { |
---|
56 | if (first != query.getStart().intValue() || count != query.getRows().intValue()) { |
---|
57 | query.setStart((int) first).setRows((int) count); |
---|
58 | docList = null; |
---|
59 | } |
---|
60 | return getDocList().iterator(); |
---|
61 | } |
---|
62 | |
---|
63 | public Iterator<SolrDocument> iterator() { |
---|
64 | return getDocList().iterator(); |
---|
65 | } |
---|
66 | |
---|
67 | @Override |
---|
68 | public IModel<SolrDocument> model(SolrDocument solrDocument) { |
---|
69 | return new Model<SolrDocument>(solrDocument); |
---|
70 | } |
---|
71 | |
---|
72 | @Override |
---|
73 | public long size() { |
---|
74 | return getDocList().getNumFound(); |
---|
75 | } |
---|
76 | } |
---|