1 | /* |
---|
2 | * Copyright (C) 2014 CLARIN |
---|
3 | * |
---|
4 | * This program is free software: you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation, either version 3 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | */ |
---|
17 | package eu.clarin.cmdi.vlo.wicket.components; |
---|
18 | |
---|
19 | import eu.clarin.cmdi.vlo.FacetConstants; |
---|
20 | import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; |
---|
21 | import eu.clarin.cmdi.vlo.service.SolrDocumentService; |
---|
22 | import eu.clarin.cmdi.vlo.wicket.model.NullFallbackModel; |
---|
23 | import eu.clarin.cmdi.vlo.wicket.model.SolrFieldModel; |
---|
24 | import eu.clarin.cmdi.vlo.wicket.provider.SolrDocumentProvider; |
---|
25 | import java.util.Arrays; |
---|
26 | import java.util.List; |
---|
27 | import org.apache.solr.common.SolrDocument; |
---|
28 | import org.apache.wicket.ajax.AjaxRequestTarget; |
---|
29 | import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior; |
---|
30 | import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator; |
---|
31 | import org.apache.wicket.markup.html.basic.Label; |
---|
32 | import org.apache.wicket.markup.html.form.DropDownChoice; |
---|
33 | import org.apache.wicket.markup.html.form.Form; |
---|
34 | import org.apache.wicket.markup.html.navigation.paging.IPageableItems; |
---|
35 | import org.apache.wicket.markup.html.panel.Panel; |
---|
36 | import org.apache.wicket.markup.repeater.Item; |
---|
37 | import org.apache.wicket.markup.repeater.data.DataView; |
---|
38 | import org.apache.wicket.markup.repeater.data.IDataProvider; |
---|
39 | import org.apache.wicket.model.AbstractReadOnlyModel; |
---|
40 | import org.apache.wicket.model.IModel; |
---|
41 | import org.apache.wicket.model.PropertyModel; |
---|
42 | import org.apache.wicket.spring.injection.annot.SpringBean; |
---|
43 | |
---|
44 | /** |
---|
45 | * Panel that has a data view on the current search results |
---|
46 | * |
---|
47 | * @author twagoo |
---|
48 | */ |
---|
49 | public class SearchResultsPanel extends Panel { |
---|
50 | |
---|
51 | public static final List<Long> ITEMS_PER_PAGE_OPTIONS = Arrays.asList(5L, 10L, 25L, 50L, 100L); |
---|
52 | |
---|
53 | @SpringBean |
---|
54 | private SolrDocumentService documentService; |
---|
55 | private final IDataProvider<SolrDocument> solrDocumentProvider; |
---|
56 | private final DataView<SolrDocument> resultsView; |
---|
57 | |
---|
58 | public SearchResultsPanel(String id, IModel<QueryFacetsSelection> model) { |
---|
59 | super(id, model); |
---|
60 | solrDocumentProvider = new SolrDocumentProvider(documentService, model); |
---|
61 | |
---|
62 | // data view for search results |
---|
63 | resultsView = new DataView<SolrDocument>("resultItem", solrDocumentProvider, 10) { |
---|
64 | |
---|
65 | @Override |
---|
66 | protected void populateItem(Item<SolrDocument> item) { |
---|
67 | // single result item |
---|
68 | item.add(new SearchResultItemPanel("resultItemDetails", item.getModel())); |
---|
69 | } |
---|
70 | }; |
---|
71 | add(resultsView); |
---|
72 | |
---|
73 | // pagination navigators |
---|
74 | add(new AjaxPagingNavigator("pagingTop", resultsView)); |
---|
75 | add(new AjaxPagingNavigator("pagingBottom", resultsView)); |
---|
76 | |
---|
77 | // total result counter |
---|
78 | add(createResultCount("resultCount")); |
---|
79 | |
---|
80 | // page result indicater |
---|
81 | add(createResultPageIndicator("resultPageIndicator", resultsView)); |
---|
82 | |
---|
83 | // form to select number of results per page |
---|
84 | add(createResultPageSizeForm("resultPageSizeForm", resultsView)); |
---|
85 | |
---|
86 | //For Ajax updating of search results |
---|
87 | setOutputMarkupId(true); |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * Gets called on each request before render |
---|
92 | */ |
---|
93 | @Override |
---|
94 | protected void onConfigure() { |
---|
95 | // only show pagination navigators if there's more than one page |
---|
96 | final boolean showPaging = resultsView.getPageCount() > 1; |
---|
97 | this.get("pagingTop").setVisible(showPaging); |
---|
98 | this.get("pagingBottom").setVisible(showPaging); |
---|
99 | } |
---|
100 | |
---|
101 | private Label createResultCount(String id) { |
---|
102 | final IModel<String> resultCountModel = new AbstractReadOnlyModel<String>() { |
---|
103 | |
---|
104 | @Override |
---|
105 | public String getObject() { |
---|
106 | return String.format("%d results", solrDocumentProvider.size()); |
---|
107 | } |
---|
108 | }; |
---|
109 | return new Label(id, resultCountModel); |
---|
110 | } |
---|
111 | |
---|
112 | private Label createResultPageIndicator(String id, final IPageableItems resultsView) { |
---|
113 | IModel<String> indicatorModel = new AbstractReadOnlyModel<String>() { |
---|
114 | |
---|
115 | @Override |
---|
116 | public String getObject() { |
---|
117 | final long firstShown = 1 + resultsView.getCurrentPage() * resultsView.getItemsPerPage(); |
---|
118 | final long lastShown = Math.min(resultsView.getItemCount(), firstShown + resultsView.getItemsPerPage() - 1); |
---|
119 | return String.format("Showing %d to %d", firstShown, lastShown); |
---|
120 | } |
---|
121 | }; |
---|
122 | return new Label(id, indicatorModel); |
---|
123 | } |
---|
124 | |
---|
125 | private Form createResultPageSizeForm(String id, final IPageableItems resultsView) { |
---|
126 | final Form resultPageSizeForm = new Form(id); |
---|
127 | |
---|
128 | final DropDownChoice<Long> pageSizeDropDown |
---|
129 | = new DropDownChoice<Long>("resultPageSize", |
---|
130 | // bind to items per page property of pageable |
---|
131 | new PropertyModel<Long>(resultsView, "itemsPerPage"), |
---|
132 | ITEMS_PER_PAGE_OPTIONS); |
---|
133 | pageSizeDropDown.add(new AjaxFormComponentUpdatingBehavior("onchange") { |
---|
134 | |
---|
135 | @Override |
---|
136 | protected void onUpdate(AjaxRequestTarget target) { |
---|
137 | target.add(SearchResultsPanel.this); |
---|
138 | } |
---|
139 | }); |
---|
140 | resultPageSizeForm.add(pageSizeDropDown); |
---|
141 | |
---|
142 | return resultPageSizeForm; |
---|
143 | } |
---|
144 | |
---|
145 | } |
---|