source: vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/components/SearchResultsPanel.java @ 4560

Last change on this file since 4560 was 4560, checked in by twagoo, 10 years ago

Separated out search result item into a separate panel.
Removed expanded search result item view (will not be in alpha)

File size: 5.6 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.components;
18
19import eu.clarin.cmdi.vlo.FacetConstants;
20import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection;
21import eu.clarin.cmdi.vlo.service.SolrDocumentService;
22import eu.clarin.cmdi.vlo.wicket.model.NullFallbackModel;
23import eu.clarin.cmdi.vlo.wicket.model.SolrFieldModel;
24import eu.clarin.cmdi.vlo.wicket.provider.SolrDocumentProvider;
25import java.util.Arrays;
26import java.util.List;
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.PropertyModel;
42import 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 */
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    @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}
Note: See TracBrowser for help on using the repository browser.