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.IModel; |
---|
28 | import org.apache.wicket.model.LoadableDetachableModel; |
---|
29 | |
---|
30 | /** |
---|
31 | * |
---|
32 | * @author twagoo |
---|
33 | */ |
---|
34 | public class FacetFieldsModel extends LoadableDetachableModel<List<FacetField>> { |
---|
35 | |
---|
36 | private final FacetFieldsService service; |
---|
37 | private final List<String> facets; |
---|
38 | private final IModel<QueryFacetsSelection> selectionModel; |
---|
39 | |
---|
40 | public FacetFieldsModel(FacetFieldsService service, List<String> facets, IModel<QueryFacetsSelection> selectionModel) { |
---|
41 | this.service = service; |
---|
42 | this.facets = facets; |
---|
43 | this.selectionModel = selectionModel; |
---|
44 | } |
---|
45 | |
---|
46 | @Override |
---|
47 | protected List<FacetField> load() { |
---|
48 | final List<FacetField> allFacetFields = service.getFacetFields(selectionModel.getObject()); |
---|
49 | final Collection<FacetField> filtered = Collections2.filter(allFacetFields, new Predicate<FacetField>() { |
---|
50 | |
---|
51 | @Override |
---|
52 | public boolean apply(FacetField t) { |
---|
53 | return facets.contains(t.getName()); |
---|
54 | } |
---|
55 | }); |
---|
56 | return ImmutableList.copyOf(filtered); |
---|
57 | } |
---|
58 | |
---|
59 | } |
---|