source: vlo/branches/vlo-3.3-oeaw/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/panels/search/FacetPanel.java @ 6480

Last change on this file since 6480 was 6480, checked in by davor.ostojic@oeaw.ac.at, 9 years ago

Model of FacetsPanel? is replaced with FacetNamesModel?
components in FacetPanel? are rendered only if state is expanded and smth is selected -> reduces number of SOLR requests

File size: 6.4 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.wicket.model.SolrFieldDescriptionModel;
20import eu.clarin.cmdi.vlo.pojo.ExpansionState;
21import eu.clarin.cmdi.vlo.pojo.FacetFieldSelection;
22import eu.clarin.cmdi.vlo.pojo.FacetSelection;
23import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection;
24import eu.clarin.cmdi.vlo.wicket.model.SolrFieldNameModel;
25import eu.clarin.cmdi.vlo.wicket.panels.ExpandablePanel;
26import java.util.Collection;
27import java.util.HashSet;
28import org.apache.solr.client.solrj.response.FacetField;
29import org.apache.wicket.ajax.AjaxRequestTarget;
30import org.apache.wicket.behavior.AttributeAppender;
31import org.apache.wicket.markup.html.basic.Label;
32import org.apache.wicket.model.IModel;
33import org.apache.wicket.model.PropertyModel;
34import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
37/**
38 * Panel that displays a single facet based on the current query/value
39 * selection. Two children will be generated: a {@link FacetValuesPanel} and a
40 * {@link SelectedFacetPanel}. One of them is set visible (at {@link
41 * #onConfigure()}), depending on whether this facet has selected values.
42 *
43 * @author twagoo
44 */
45public abstract class FacetPanel extends ExpandablePanel<FacetFieldSelection> {
46    private final static Logger logger = LoggerFactory.getLogger(FacetPanel.class);
47
48    private final SelectedFacetPanel selectedFacetPanel;
49    private final FacetValuesPanel facetValuesPanel;
50
51    public FacetPanel(String id, IModel<FacetFieldSelection> selectionModel, IModel<ExpansionState> expansionState) {
52        this(id, selectionModel, expansionState, 0);
53    }
54
55    public FacetPanel(String id, IModel<FacetFieldSelection> selectionModel, IModel<ExpansionState> expansionState, int subListSize) {
56        super(id, selectionModel, expansionState);
57
58        // panel showing values for selection
59        facetValuesPanel = createFacetValuesPanel("facetValues", subListSize);
60        add(facetValuesPanel);
61
62        // panel showing current selection, allowing for deselection
63        selectedFacetPanel = createSelectedFacetPanel("facetSelection");
64        add(selectedFacetPanel);
65    }
66
67    @Override
68    protected Label createTitleLabel(String id) {
69        final IModel<String> facetNameModel = new PropertyModel<>(getModel(), "facetField.name");
70        final Label label = new Label(id, new SolrFieldNameModel(facetNameModel));
71        label.add(new AttributeAppender("title", new SolrFieldDescriptionModel(facetNameModel)));
72        return label;
73    }
74
75    @Override
76    protected void onConfigure() {
77        super.onConfigure();
78
79        final boolean valuesSelected = !getModelObject().getFacetValues().isEmpty();
80       
81        selectedFacetPanel.setVisible(valuesSelected);
82       
83        facetValuesPanel.setVisible((!valuesSelected && expansionModel.getObject() == ExpansionState.COLLAPSED)? false : true);       
84
85        // hide this entire panel is no values are selectable
86        setVisible(!isHideIfNoValues() || valuesSelected || getModelObject().getFacetField().getValueCount() > 0);
87    }
88   
89
90    /**
91     *
92     * @return whether the panel should be hidden if no values are present given
93     * the current selection and query
94     */
95    protected boolean isHideIfNoValues() {
96        return true;
97    }
98
99    private FacetValuesPanel createFacetValuesPanel(String id, int subListSize) {
100        return new FacetValuesPanel(id,
101                new PropertyModel<FacetField>(getModel(), "facetField"),
102                new PropertyModel<QueryFacetsSelection>(getModel(), "selection"), subListSize) {
103                    @Override
104                    public void onValuesSelected(String facet, FacetSelection value, AjaxRequestTarget target) {
105                        // A value has been selected on this facet's panel, update the model!
106                        FacetPanel.this.getModelObject().getSelection().addNewFacetValue(facet, value);
107                        //FacetPanel.this.getModelObject().getSelection().selectValues(facet, value);
108                        if (target != null) {
109                            // reload entire page for now
110                            selectionChanged(target);
111                        }
112                    }
113                };
114    }
115
116    private SelectedFacetPanel createSelectedFacetPanel(String id) {
117        return new SelectedFacetPanel(id, getModel()) {
118            @Override
119            public void onValuesUnselected(String facet, Collection<String> valuesRemoved, AjaxRequestTarget target) {
120                final QueryFacetsSelection selection = getModelObject().getSelection();
121
122                // Values have been removed, calculate remainder
123               
124                final FacetSelection facetSelection = selection.getSelectionValues(facet);
125//                final Collection<String> currentSelection = facetSelection.getValues();
126//                final Collection<String> newSelection = new HashSet<String>(currentSelection);
127//                newSelection.removeAll(valuesRemoved);
128
129                // Update model (keep selection type)
130//                              selection.selectValues(facet, new FacetSelection(facetSelection.getSelectionType(), newSelection));
131               
132                FacetSelection fSelToRemove = new FacetSelection(facetSelection.getSelectionType(), valuesRemoved);
133                selection.removeFacetValue(facet, fSelToRemove);
134
135                // collapse after removal
136                // TODO: should be removed, but then list of values
137                // does not seem to update correctly
138                expansionModel.setObject(ExpansionState.COLLAPSED);
139
140                if (target != null) {
141                    // reload entire page for now
142                    selectionChanged(target);
143                }
144            }
145        };
146    }
147
148    protected abstract void selectionChanged(AjaxRequestTarget target);
149
150}
Note: See TracBrowser for help on using the repository browser.