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 | */ |
---|
17 | package eu.clarin.cmdi.vlo.wicket.provider; |
---|
18 | |
---|
19 | import java.util.Iterator; |
---|
20 | import java.util.List; |
---|
21 | import org.apache.solr.client.solrj.response.FacetField; |
---|
22 | import org.apache.wicket.markup.repeater.data.IDataProvider; |
---|
23 | import org.apache.wicket.model.IModel; |
---|
24 | import org.apache.wicket.model.Model; |
---|
25 | |
---|
26 | /** |
---|
27 | * Provides facet values and counts (both through the {@link Count} object of |
---|
28 | * SOLR) present on a {@link FacetField} |
---|
29 | * |
---|
30 | * @author twagoo |
---|
31 | */ |
---|
32 | public class FacetFieldValuesProvider implements IDataProvider<FacetField.Count> { |
---|
33 | |
---|
34 | private final IModel<FacetField> model; |
---|
35 | private final int maxNumberOfItems; |
---|
36 | |
---|
37 | /** |
---|
38 | * Creates a provider without a maximum number of values. Bound to |
---|
39 | * {@link Integer#MAX_VALUE}. |
---|
40 | * |
---|
41 | * @param model FacetField model to get values and counts for |
---|
42 | */ |
---|
43 | public FacetFieldValuesProvider(IModel<FacetField> model) { |
---|
44 | this(model, Integer.MAX_VALUE); |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * Creates a provider with a set maximum number of values |
---|
49 | * |
---|
50 | * @param model FacetField model to get values and counts for |
---|
51 | * @param max maximum number of values to show |
---|
52 | */ |
---|
53 | public FacetFieldValuesProvider(IModel<FacetField> model, int max) { |
---|
54 | this.model = model; |
---|
55 | this.maxNumberOfItems = max; |
---|
56 | } |
---|
57 | |
---|
58 | @Override |
---|
59 | public Iterator<? extends FacetField.Count> iterator(long first, long count) { |
---|
60 | // TODO: From old VLO: |
---|
61 | // 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. |
---|
62 | final List<FacetField.Count> values = model.getObject().getValues(); |
---|
63 | return values.listIterator((int) first); |
---|
64 | } |
---|
65 | |
---|
66 | @Override |
---|
67 | public long size() { |
---|
68 | // Actual value count might be higher than what we want to show |
---|
69 | return Math.min(maxNumberOfItems, model.getObject().getValueCount()); |
---|
70 | } |
---|
71 | |
---|
72 | @Override |
---|
73 | public IModel<FacetField.Count> model(FacetField.Count object) { |
---|
74 | return new Model(object); |
---|
75 | } |
---|
76 | |
---|
77 | @Override |
---|
78 | public void detach() { |
---|
79 | } |
---|
80 | |
---|
81 | } |
---|