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

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

Major dependency upgrades: bumped Wicket to 7.x and Spring to 4.x and made required migration changes in code

File size: 8.0 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.config.VloConfig;
20import eu.clarin.cmdi.vlo.pojo.FacetSelection;
21import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection;
22import eu.clarin.cmdi.vlo.service.PageParametersConverter;
23import eu.clarin.cmdi.vlo.service.solr.FacetFieldsService;
24import eu.clarin.cmdi.vlo.service.solr.SolrDocumentService;
25import eu.clarin.cmdi.vlo.wicket.model.FacetFieldsModel;
26import eu.clarin.cmdi.vlo.wicket.model.SolrFieldDescriptionModel;
27import eu.clarin.cmdi.vlo.wicket.model.SolrFieldNameModel;
28import eu.clarin.cmdi.vlo.wicket.pages.FacetedSearchPage;
29import eu.clarin.cmdi.vlo.wicket.pages.SimpleSearchPage;
30import org.apache.solr.client.solrj.response.FacetField;
31import org.apache.wicket.ajax.AjaxRequestTarget;
32import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
33import org.apache.wicket.behavior.AttributeAppender;
34import org.apache.wicket.markup.html.WebMarkupContainer;
35import org.apache.wicket.markup.html.basic.Label;
36import org.apache.wicket.markup.html.link.Link;
37import org.apache.wicket.markup.html.list.ListItem;
38import org.apache.wicket.markup.html.list.ListView;
39import org.apache.wicket.markup.html.panel.GenericPanel;
40import org.apache.wicket.migrate.StringResourceModelMigration;
41import org.apache.wicket.model.AbstractReadOnlyModel;
42import org.apache.wicket.model.IModel;
43import org.apache.wicket.model.Model;
44import org.apache.wicket.model.PropertyModel;
45import org.apache.wicket.spring.injection.annot.SpringBean;
46
47/**
48 * Panel to be shown on {@link SimpleSearchPage} that has a number of links for
49 * browsing the records; either all records or by making a value selection in
50 * one of a number of predefined facets
51 *
52 * @author twagoo
53 */
54public class SimpleSearchBrowsePanel extends GenericPanel<QueryFacetsSelection> {
55
56    @SpringBean
57    private SolrDocumentService documentService;
58
59    /**
60     *
61     * @param id component id
62     * @param model model of current selection
63     */
64    public SimpleSearchBrowsePanel(String id, IModel<QueryFacetsSelection> model) {
65        super(id, model);
66
67        final IModel<Long> documentCountModel = new AbstractReadOnlyModel<Long>() {
68
69            @Override
70            public Long getObject() {
71                return documentService.getDocumentCount(getModel().getObject());
72            }
73        };
74
75        // add a link to browse all records
76        final Link browseAllLink = new Link("browseAll") {
77
78            @Override
79            public void onClick() {
80                setResponsePage(new FacetedSearchPage(SimpleSearchBrowsePanel.this.getModel()));
81            }
82        };
83        // set label on basis of string defined in resource bundle that takes the count model as a parameter
84        browseAllLink.add(new Label("recordCount", StringResourceModelMigration.of("simplesearch.allrecords", documentCountModel, new Object[]{})));
85        add(browseAllLink);
86
87        // add selectors for some facets
88        add(new FacetSelectorsView("facet", getModel()));
89
90        // make this panel AJAX-updatable
91        setOutputMarkupId(true);
92    }
93
94    /**
95     * List model of links that open up {@link FacetValuesPanel}s for a number
96     * of facets. Which facets are included is based on the value returned by
97     * {@link VloConfig#getSimpleSearchFacetFields() } in the {@link VloConfig}
98     * instance injected into this instance.
99     */
100    private class FacetSelectorsView extends ListView<FacetField> {
101
102        @SpringBean
103        private FacetFieldsService facetFieldsService;
104        @SpringBean(name = "queryParametersConverter")
105        private PageParametersConverter<QueryFacetsSelection> paramsConverter;
106        @SpringBean
107        private VloConfig vloConfig;
108
109        /**
110         * Model that holds the currently selected facet
111         */
112        private final IModel<String> selectedFacetModel = new Model<>(null);
113        private final IModel<QueryFacetsSelection> selectionModel;
114
115        public FacetSelectorsView(String id, IModel<QueryFacetsSelection> selectionModel) {
116            super(id);
117            this.selectionModel = selectionModel;
118            setModel(new FacetFieldsModel(facetFieldsService, vloConfig.getSimpleSearchFacetFields(), selectionModel, -1));
119        }
120
121        @Override
122        protected void populateItem(final ListItem<FacetField> item) {
123            // add a panel showing the values for selection (constrained by the current model)
124            final FacetValuesPanel values = new FacetValuesPanel("values", item.getModel(), selectionModel) {
125
126                @Override
127                protected void onValuesSelected(String facet, FacetSelection values, AjaxRequestTarget target) {
128                    // value selected, make a new selection (in this panel we do not want to change the existing selection)...
129                    final QueryFacetsSelection newSelection = selectionModel.getObject().getCopy();
130                    newSelection.selectValues(facet, values);
131                    // ...then submit to search page
132                    setResponsePage(FacetedSearchPage.class, paramsConverter.toParameters(newSelection));
133                }
134
135            };
136            // wrap in a container that is only visible if this is the selected facet
137            final WebMarkupContainer valuesContainer = new WebMarkupContainer("valuesContainer") {
138
139                @Override
140                protected void onConfigure() {
141                    super.onConfigure();
142                    setVisible(item.getModelObject().getName().equals(selectedFacetModel.getObject()));
143                }
144            };
145            valuesContainer.add(values);
146            item.add(valuesContainer);
147
148            // add a link for selecting this facet
149            final AjaxFallbackLink select = new AjaxFallbackLink("select") {
150
151                @Override
152                public void onClick(AjaxRequestTarget target) {
153                    final String facetName = item.getModelObject().getName();
154                    if (facetName.equals(selectedFacetModel.getObject())) {
155                        // already selected, hide
156                        selectedFacetModel.setObject(null);
157                    } else {
158                        // set this facet as the selected one
159                        selectedFacetModel.setObject(facetName);
160                    }
161
162                    if (target != null) {
163                        // AJAX update
164                        target.add(SimpleSearchBrowsePanel.this);
165                    }
166                }
167            };
168           
169            // add name label (with a description title attribute)
170            final PropertyModel facetNameModel = new PropertyModel(item.getModel(), "name");
171            // wrap in field name model to get a friendly facet name based on name in FacetField
172            final Label name = new Label("name", new SolrFieldNameModel(facetNameModel));
173            select.add(name);
174           
175            // add title attribute to get the facet description in a tooltip
176            select.add(new AttributeAppender("title", new SolrFieldDescriptionModel(facetNameModel)));
177           
178            item.add(select);
179
180            // show a separator except for the last item
181            item.add(new WebMarkupContainer("separator") {
182
183                @Override
184                protected void onConfigure() {
185                    super.onConfigure();
186                    setVisible(item.getIndex() + 1 < getList().size());
187                }
188            });
189        }
190    }
191
192}
Note: See TracBrowser for help on using the repository browser.