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 com.google.common.base.Predicate; |
---|
20 | import com.google.common.collect.Collections2; |
---|
21 | import com.google.common.collect.ImmutableList; |
---|
22 | import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; |
---|
23 | import eu.clarin.cmdi.vlo.service.FacetFieldsService; |
---|
24 | import java.util.Collection; |
---|
25 | import java.util.List; |
---|
26 | import org.apache.solr.client.solrj.response.FacetField; |
---|
27 | import org.apache.wicket.model.IChainingModel; |
---|
28 | import org.apache.wicket.model.IModel; |
---|
29 | import org.apache.wicket.model.LoadableDetachableModel; |
---|
30 | |
---|
31 | /** |
---|
32 | * Model that provides a list of {@link FacetField}s based on the current query |
---|
33 | * and values selection, filtered through a selection of facet names. |
---|
34 | * |
---|
35 | * Notice that the actual retrieval is carried out by the provided |
---|
36 | * {@link FacetFieldsService}, which therefore should be configured to actually |
---|
37 | * retrieve the specified facets (in the list in the constructor), otherwise |
---|
38 | * some of these may not be present. |
---|
39 | * |
---|
40 | * @author twagoo |
---|
41 | */ |
---|
42 | public class FacetFieldsModel extends LoadableDetachableModel<List<FacetField>> { |
---|
43 | |
---|
44 | private final FacetFieldsService service; |
---|
45 | private final List<String> facets; |
---|
46 | private final IModel<QueryFacetsSelection> selectionModel; |
---|
47 | |
---|
48 | /** |
---|
49 | * |
---|
50 | * @param service service to use for facet field retrieval |
---|
51 | * @param facets facets to include |
---|
52 | * @param selectionModel model that provides current query/selection |
---|
53 | */ |
---|
54 | public FacetFieldsModel(FacetFieldsService service, List<String> facets, IModel<QueryFacetsSelection> selectionModel) { |
---|
55 | this.service = service; |
---|
56 | this.facets = facets; |
---|
57 | this.selectionModel = selectionModel; |
---|
58 | } |
---|
59 | |
---|
60 | @Override |
---|
61 | protected List<FacetField> load() { |
---|
62 | final List<FacetField> allFacetFields = service.getFacetFields(selectionModel.getObject()); |
---|
63 | final Collection<FacetField> filtered = Collections2.filter(allFacetFields, new Predicate<FacetField>() { |
---|
64 | |
---|
65 | @Override |
---|
66 | public boolean apply(FacetField t) { |
---|
67 | return facets.contains(t.getName()); |
---|
68 | } |
---|
69 | }); |
---|
70 | return ImmutableList.copyOf(filtered); |
---|
71 | } |
---|
72 | |
---|
73 | @Override |
---|
74 | public void detach() { |
---|
75 | super.detach(); |
---|
76 | selectionModel.detach(); |
---|
77 | } |
---|
78 | |
---|
79 | } |
---|