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.model; |
---|
18 | |
---|
19 | import eu.clarin.cmdi.vlo.pojo.FacetSelection; |
---|
20 | import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; |
---|
21 | import java.util.Collection; |
---|
22 | import java.util.Collections; |
---|
23 | import java.util.List; |
---|
24 | import java.util.concurrent.CopyOnWriteArrayList; |
---|
25 | import org.apache.solr.client.solrj.response.FacetField; |
---|
26 | import org.apache.wicket.model.AbstractReadOnlyModel; |
---|
27 | import org.apache.wicket.model.IModel; |
---|
28 | |
---|
29 | /** |
---|
30 | * Model for FacetSelection that simply wraps a QueryFacetsSelection model and |
---|
31 | * 'filters' for the specified facet |
---|
32 | * |
---|
33 | * @author twagoo |
---|
34 | */ |
---|
35 | public class FacetSelectionModel extends AbstractReadOnlyModel<FacetSelection> implements FacetSelection { |
---|
36 | |
---|
37 | private final IModel<QueryFacetsSelection> selectionModel; |
---|
38 | private final IModel<FacetField> facetFieldModel; |
---|
39 | |
---|
40 | /** |
---|
41 | * |
---|
42 | * @param facetFieldModel |
---|
43 | * @param selectionModel broad (multi-facet) selection model |
---|
44 | */ |
---|
45 | public FacetSelectionModel(IModel<FacetField> facetFieldModel, IModel<QueryFacetsSelection> selectionModel) { |
---|
46 | this.facetFieldModel = facetFieldModel; |
---|
47 | this.selectionModel = selectionModel; |
---|
48 | } |
---|
49 | |
---|
50 | @Override |
---|
51 | public FacetField getFacetField() { |
---|
52 | return facetFieldModel.getObject(); |
---|
53 | } |
---|
54 | |
---|
55 | @Override |
---|
56 | public List<String> getFacetValues() { |
---|
57 | final String facetName = getFacetField().getName(); |
---|
58 | final Collection<String> selectionValues = getSelection().getSelectionValues(facetName); |
---|
59 | if (selectionValues == null) { |
---|
60 | return Collections.emptyList(); |
---|
61 | } else { |
---|
62 | return new CopyOnWriteArrayList<String>(selectionValues); |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | @Override |
---|
67 | public QueryFacetsSelection getSelection() { |
---|
68 | return selectionModel.getObject(); |
---|
69 | } |
---|
70 | |
---|
71 | @Override |
---|
72 | public FacetSelection getObject() { |
---|
73 | return this; |
---|
74 | } |
---|
75 | |
---|
76 | @Override |
---|
77 | public void detach() { |
---|
78 | selectionModel.detach(); |
---|
79 | } |
---|
80 | |
---|
81 | } |
---|