source: vlo/trunk/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/panels/search/SearchResultsPanel.java @ 6191

Last change on this file since 6191 was 6191, checked in by Twan Goosen, 9 years ago

Added pagination to the resource listing on the record page
Refs #565

File size: 7.1 KB
Line 
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 */
17package eu.clarin.cmdi.vlo.wicket.panels.search;
18
19import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection;
20import eu.clarin.cmdi.vlo.wicket.model.SearchContextModel;
21import eu.clarin.cmdi.vlo.wicket.model.SearchResultExpansionStateModel;
22import eu.clarin.cmdi.vlo.wicket.provider.SolrDocumentProvider;
23import java.util.Arrays;
24import java.util.HashSet;
25import java.util.List;
26import java.util.Set;
27import org.apache.solr.common.SolrDocument;
28import org.apache.wicket.ajax.AjaxRequestTarget;
29import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
30import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
31import org.apache.wicket.markup.html.basic.Label;
32import org.apache.wicket.markup.html.form.DropDownChoice;
33import org.apache.wicket.markup.html.form.Form;
34import org.apache.wicket.markup.html.navigation.paging.IPageableItems;
35import org.apache.wicket.markup.html.panel.Panel;
36import org.apache.wicket.markup.repeater.Item;
37import org.apache.wicket.markup.repeater.data.DataView;
38import org.apache.wicket.markup.repeater.data.IDataProvider;
39import org.apache.wicket.model.AbstractReadOnlyModel;
40import org.apache.wicket.model.IModel;
41import org.apache.wicket.model.Model;
42import org.apache.wicket.model.PropertyModel;
43
44/**
45 * Panel that has a data view on the current search results
46 *
47 * @author twagoo
48 */
49public class SearchResultsPanel extends Panel {
50
51    public static final List<Long> ITEMS_PER_PAGE_OPTIONS = Arrays.asList(5L, 10L, 25L, 50L, 100L);
52
53    private final IDataProvider<SolrDocument> solrDocumentProvider;
54    private final DataView<SolrDocument> resultsView;
55    private final IModel<Set<Object>> expansionsModel;
56   
57    public SearchResultsPanel(String id, final IModel<QueryFacetsSelection> selectionModel) {
58        super(id, selectionModel);
59        add(new Label("title", new SearchResultsTitleModel(selectionModel)));
60
61        solrDocumentProvider = new SolrDocumentProvider(selectionModel);
62
63        expansionsModel = new Model(new HashSet<Object>());
64        // data view for search results
65        resultsView = new DataView<SolrDocument>("resultItem", solrDocumentProvider, 10) {
66
67            @Override
68            protected void populateItem(Item<SolrDocument> item) {
69                final long index = (getCurrentPage() * getItemsPerPage()) + item.getIndex();
70                final long size = internalGetDataProvider().size();
71                final SearchContextModel contextModel = new SearchContextModel(index, size, selectionModel);
72                // single result item
73                item.add(new SearchResultItemPanel("resultItemDetails", item.getModel(), contextModel,
74                        new SearchResultExpansionStateModel(expansionsModel, item.getModel())
75                ));
76            }
77        };
78        add(resultsView);
79
80        // pagination navigators
81        add(new AjaxPagingNavigator("pagingTop", resultsView));
82        add(new AjaxPagingNavigator("pagingBottom", resultsView));
83
84        // total result counter
85        add(createResultCount("resultCount"));
86
87        // page result indicater
88        add(createResultPageIndicator("resultPageIndicator", resultsView));
89
90        // form to select number of results per page
91        add(createResultPageSizeForm("resultPageSizeForm", resultsView));
92
93        //For Ajax updating of search results
94        setOutputMarkupId(true);
95    }
96   
97    public void resetExpansion() {
98        expansionsModel.getObject().clear();
99    }
100
101    /**
102     * Gets called on each request before render
103     */
104    @Override
105    protected void onConfigure() {
106        super.onConfigure();
107
108        // only show pagination navigators if there's more than one page
109        final boolean showPaging = resultsView.getPageCount() > 1;
110        this.get("pagingTop").setVisible(showPaging);
111        this.get("pagingBottom").setVisible(showPaging);
112    }
113
114    private Label createResultCount(String id) {
115        final IModel<String> resultCountModel = new AbstractReadOnlyModel<String>() {
116
117            @Override
118            public String getObject() {
119                return String.format("%d results", solrDocumentProvider.size());
120            }
121        };
122        return new Label(id, resultCountModel);
123    }
124
125    private Label createResultPageIndicator(String id, final IPageableItems resultsView) {
126        IModel<String> indicatorModel = new AbstractReadOnlyModel<String>() {
127
128            @Override
129            public String getObject() {
130                final long firstShown = 1 + resultsView.getCurrentPage() * resultsView.getItemsPerPage();
131                final long lastShown = Math.min(resultsView.getItemCount(), firstShown + resultsView.getItemsPerPage() - 1);
132                return String.format("Showing %d to %d", firstShown, lastShown);
133            }
134        };
135        return new Label(id, indicatorModel) {
136
137            @Override
138            protected void onConfigure() {
139                // hide if no results
140                setVisible(resultsView.getItemCount() > 0);
141            }
142           
143        };
144    }
145
146    private Form createResultPageSizeForm(String id, final IPageableItems resultsView) {
147        final Form resultPageSizeForm = new Form(id);
148
149        final DropDownChoice<Long> pageSizeDropDown
150                = new DropDownChoice<Long>("resultPageSize",
151                        // bind to items per page property of pageable
152                        new PropertyModel<Long>(resultsView, "itemsPerPage"),
153                        ITEMS_PER_PAGE_OPTIONS);
154        pageSizeDropDown.add(new AjaxFormComponentUpdatingBehavior("onchange") {
155
156            @Override
157            protected void onUpdate(AjaxRequestTarget target) {
158                target.add(SearchResultsPanel.this);
159            }
160        });
161        resultPageSizeForm.add(pageSizeDropDown);
162
163        return resultPageSizeForm;
164    }
165
166    private static class SearchResultsTitleModel extends AbstractReadOnlyModel<String> {
167
168        private final IModel<QueryFacetsSelection> selectionModel;
169
170        public SearchResultsTitleModel(IModel<QueryFacetsSelection> selectionModel) {
171            this.selectionModel = selectionModel;
172        }
173
174        @Override
175        public String getObject() {
176            final QueryFacetsSelection selection = selectionModel.getObject();
177            if ((selection.getQuery() == null || selection.getQuery().isEmpty())
178                    && (selection.getSelection() == null || selection.getSelection().isEmpty())) {
179                return "All records";
180            } else {
181                return "Search results";
182            }
183        }
184    }
185
186}
Note: See TracBrowser for help on using the repository browser.