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.pojo; |
---|
18 | |
---|
19 | import com.google.common.collect.Maps; |
---|
20 | import java.io.Serializable; |
---|
21 | import java.util.Collection; |
---|
22 | import java.util.HashMap; |
---|
23 | import java.util.Map; |
---|
24 | import java.util.Map.Entry; |
---|
25 | |
---|
26 | /** |
---|
27 | * Represents a query and any number of selected values for zero or more facets |
---|
28 | * |
---|
29 | * @author twagoo |
---|
30 | */ |
---|
31 | public class QueryFacetsSelection implements Serializable { |
---|
32 | |
---|
33 | private String queryString; |
---|
34 | private final Map<String, FacetSelection> selection; |
---|
35 | |
---|
36 | /** |
---|
37 | * Creates an empty selection (no string, no facet values) |
---|
38 | */ |
---|
39 | public QueryFacetsSelection() { |
---|
40 | this(null, Maps.<String, FacetSelection>newHashMap()); |
---|
41 | } |
---|
42 | |
---|
43 | /** |
---|
44 | * Creates a selection with an empty facet selection |
---|
45 | * |
---|
46 | * @param query query string |
---|
47 | */ |
---|
48 | public QueryFacetsSelection(String query) { |
---|
49 | this(query, Maps.<String, FacetSelection>newHashMap()); |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * Creates a selection without a query |
---|
54 | * |
---|
55 | * @param selection facet values selection map |
---|
56 | */ |
---|
57 | public QueryFacetsSelection(Map<String, FacetSelection> selection) { |
---|
58 | this(null, selection); |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * Creates a selection with a textual query and facet value selection |
---|
63 | * |
---|
64 | * @param query textual query (can be null) |
---|
65 | * @param selection facet values selection map (can be null) |
---|
66 | */ |
---|
67 | public QueryFacetsSelection(String query, Map<String, FacetSelection> selection) { |
---|
68 | this.queryString = query; |
---|
69 | if (selection == null) { |
---|
70 | this.selection = new HashMap<String, FacetSelection>(); |
---|
71 | } else { |
---|
72 | this.selection = selection; |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * |
---|
78 | * @return a facet -> values map representing the current selection |
---|
79 | */ |
---|
80 | public Map<String, FacetSelection> getSelection() { |
---|
81 | return selection; |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * |
---|
86 | * @return the facets present in the current selection |
---|
87 | */ |
---|
88 | public Collection<String> getFacets() { |
---|
89 | return selection.keySet(); |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * |
---|
94 | * @param facet facet to get values for |
---|
95 | * @return the selected values for the specified facet. Can be null. |
---|
96 | */ |
---|
97 | public FacetSelection getSelectionValues(String facet) { |
---|
98 | return selection.get(facet); |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | * |
---|
103 | * @return the current textual query, may be null in case of no query |
---|
104 | */ |
---|
105 | public String getQuery() { |
---|
106 | return queryString; |
---|
107 | } |
---|
108 | |
---|
109 | public void setQuery(String queryString) { |
---|
110 | this.queryString = queryString; |
---|
111 | } |
---|
112 | |
---|
113 | public void selectValues(String facet, FacetSelection values) { |
---|
114 | if (values == null || values.isEmpty()) { |
---|
115 | selection.remove(facet); |
---|
116 | } else { |
---|
117 | if (values instanceof Serializable) { |
---|
118 | selection.put(facet, values); |
---|
119 | } else { |
---|
120 | selection.put(facet, values); |
---|
121 | } |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | public void addNewFacetValue(String facet, Collection<String> values){ |
---|
126 | FacetSelection curSel = selection.get(facet); |
---|
127 | if(curSel != null){ |
---|
128 | curSel.setValues(values); |
---|
129 | }else{ |
---|
130 | curSel = new FacetSelection(values); |
---|
131 | } |
---|
132 | |
---|
133 | selectValues(facet, curSel); |
---|
134 | } |
---|
135 | |
---|
136 | public void removeFacetValue(String facet, Collection<String> valuestoBeRemoved){ |
---|
137 | FacetSelection curSel = selection.get(facet); |
---|
138 | if (curSel != null){ |
---|
139 | curSel.removeValues(valuestoBeRemoved); |
---|
140 | } |
---|
141 | //to remove facet from map if does not have any value |
---|
142 | selectValues(facet, curSel); |
---|
143 | } |
---|
144 | |
---|
145 | @Override |
---|
146 | public String toString() { |
---|
147 | return String.format("[QueryFacetSelection queryString = %s, selection = %s]", queryString, selection); |
---|
148 | } |
---|
149 | |
---|
150 | public QueryFacetsSelection getCopy() { |
---|
151 | final Map<String, FacetSelection> selectionClone = new HashMap<String, FacetSelection>(selection.size()); |
---|
152 | for (Entry<String, FacetSelection> entry : selection.entrySet()) { |
---|
153 | selectionClone.put(entry.getKey(), entry.getValue().getCopy()); |
---|
154 | } |
---|
155 | return new QueryFacetsSelection(queryString, selectionClone); |
---|
156 | } |
---|
157 | |
---|
158 | } |
---|