source: vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/provider/FacetFieldValuesProvider.java @ 4664

Last change on this file since 4664 was 4664, checked in by Twan Goosen, 10 years ago

Added support for sorting to field values provider

File size: 4.3 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.provider;
18
19import com.google.common.collect.Ordering;
20import eu.clarin.cmdi.vlo.pojo.FieldValuesOrder;
21import java.util.Iterator;
22import java.util.List;
23import org.apache.solr.client.solrj.response.FacetField;
24import org.apache.solr.client.solrj.response.FacetField.Count;
25import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
26import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
27import org.apache.wicket.model.IModel;
28import org.apache.wicket.model.Model;
29
30/**
31 * Provides facet values and counts (both through the {@link Count} object of
32 * SOLR) present on a {@link FacetField}, sortable by either count or name
33 *
34 * @see FieldValuesOrder
35 * @author twagoo
36 */
37public class FacetFieldValuesProvider extends SortableDataProvider<FacetField.Count, FieldValuesOrder> {
38
39    private final IModel<FacetField> model;
40    private final int maxNumberOfItems;
41
42    /**
43     * Creates a provider without a maximum number of values. Bound to
44     * {@link Integer#MAX_VALUE}.
45     *
46     * @param model FacetField model to get values and counts for
47     */
48    public FacetFieldValuesProvider(IModel<FacetField> model) {
49        this(model, Integer.MAX_VALUE);
50    }
51
52    public FacetFieldValuesProvider(IModel<FacetField> model, int max) {
53        this(model, max, new SortParam<FieldValuesOrder>(FieldValuesOrder.COUNT, false));
54    }
55
56    /**
57     * Creates a provider with a set maximum number of values
58     *
59     * @param model FacetField model to get values and counts for
60     * @param max maximum number of values to show
61     * @param sort initial sort property and order
62     */
63    public FacetFieldValuesProvider(IModel<FacetField> model, int max, SortParam<FieldValuesOrder> sort) {
64        this.model = model;
65        this.maxNumberOfItems = max;
66        setSort(sort);
67    }
68
69    @Override
70    public Iterator<? extends FacetField.Count> iterator(long first, long count) {
71        // TODO: From old VLO:
72        // IGNORABLE_VALUES (like "unknown") are move to the back of the list and should only be shown when you click "more...", unless the list is too small then whe can just show them.
73        final List<FacetField.Count> values = model.getObject().getValues();
74        return getOrdering().sortedCopy(values).listIterator((int) first);
75    }
76
77    @Override
78    public long size() {
79        // Actual value count might be higher than what we want to show
80        return Math.min(maxNumberOfItems, model.getObject().getValueCount());
81    }
82
83    @Override
84    public IModel<FacetField.Count> model(FacetField.Count object) {
85        return new Model(object);
86    }
87
88    @Override
89    public void detach() {
90        model.detach();
91    }
92
93    private Ordering getOrdering() {
94        final Ordering ordering;
95        if (getSort().getProperty() == FieldValuesOrder.COUNT) {
96            ordering = COUNT_ORDERING;
97        } else if (getSort().getProperty() == FieldValuesOrder.NAME) {
98            ordering = NAME_ORDERING;
99        } else {
100            ordering = Ordering.natural();
101        }
102
103        if (getSort().isAscending()) {
104            return ordering;
105        } else {
106            return ordering.reverse();
107        }
108    }
109
110    private static Ordering<FacetField.Count> COUNT_ORDERING = new Ordering<FacetField.Count>() {
111
112        @Override
113        public int compare(Count arg0, Count arg1) {
114            return Long.compare(arg0.getCount(), arg1.getCount());
115        }
116    };
117
118    private static Ordering<FacetField.Count> NAME_ORDERING = new Ordering<FacetField.Count>() {
119
120        @Override
121        public int compare(Count arg0, Count arg1) {
122            return arg0.getName().compareTo(arg1.getName());
123        }
124    };
125
126}
Note: See TracBrowser for help on using the repository browser.