source: vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/model/FacetFieldsModel.java @ 4606

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

detaching in compound models

File size: 2.8 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.model;
18
19import com.google.common.base.Predicate;
20import com.google.common.collect.Collections2;
21import com.google.common.collect.ImmutableList;
22import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection;
23import eu.clarin.cmdi.vlo.service.FacetFieldsService;
24import java.util.Collection;
25import java.util.List;
26import org.apache.solr.client.solrj.response.FacetField;
27import org.apache.wicket.model.IChainingModel;
28import org.apache.wicket.model.IModel;
29import 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 */
42public 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}
Note: See TracBrowser for help on using the repository browser.