source: vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/solr/impl/QueryFacetsSelectionParametersConverter.java @ 4661

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

Package structure: separated solr services from other services and moved all panels into new wicket.panels package

File size: 3.2 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.service.solr.impl;
18
19import com.google.common.collect.HashMultimap;
20import com.google.common.collect.Maps;
21import com.google.common.collect.Multimap;
22import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection;
23import eu.clarin.cmdi.vlo.service.PageParametersConverter;
24import java.util.Collection;
25import java.util.HashMap;
26import java.util.List;
27import java.util.Map.Entry;
28import org.apache.wicket.request.mapper.parameter.PageParameters;
29import org.apache.wicket.util.string.StringValue;
30
31/**
32 * Page parameter conversion service for {@link QueryFacetsSelection}
33 *
34 * @author twagoo
35 */
36public class QueryFacetsSelectionParametersConverter implements PageParametersConverter<QueryFacetsSelection> {
37
38    @Override
39    public QueryFacetsSelection fromParameters(PageParameters params) {
40        // Get query string from params
41        final String query = params.get("q").toOptionalString();
42
43        // Get facet selections from params
44        final List<StringValue> facetValues = params.getValues("fq");
45        // Store in a multimap to allow for multiple selections per facet
46        final Multimap<String, String> selectionMap = HashMultimap.<String, String>create(facetValues.size(), 1);
47        for (StringValue facetValue : facetValues) {
48            if (!facetValue.isEmpty()) {
49                String[] fq = facetValue.toString().split(":");
50                if (fq.length == 2) {
51                    // we have a facet - value pair
52                    selectionMap.put(fq[0], fq[1]);
53                }
54            }
55        }
56
57        // Facet selection expects a mutable map, so first convert back to
58        // ordinary map, then make a mutable copy
59        final HashMap<String, Collection<String>> selection = Maps.newHashMap(selectionMap.asMap());
60
61        // Put it all together
62        return new QueryFacetsSelection(query, selection);
63    }
64
65    @Override
66    public PageParameters toParameters(QueryFacetsSelection selection) {
67        final PageParameters params = new PageParameters();
68
69        // put the query in the 'q' parameter
70        final String query = selection.getQuery();
71        if (query != null) {
72            params.add("q", query);
73        }
74
75        // put all selections in 'fq' parameters
76        for (Entry<String, Collection<String>> facetSelection : selection.getSelection().entrySet()) {
77            for (String value : facetSelection.getValue()) {
78                params.add("fq", String.format("%s:%s", facetSelection.getKey(), value));
79            }
80        }
81
82        return params;
83    }
84
85}
Note: See TracBrowser for help on using the repository browser.