001/*
002 * Copyright 2014 Global Biodiversity Information Facility (GBIF)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.gbif.api.model.common.search;
017
018import org.gbif.api.model.common.paging.Pageable;
019import org.gbif.api.model.common.paging.PageableBase;
020
021import java.text.DateFormat;
022import java.text.SimpleDateFormat;
023import java.util.Date;
024
025import com.google.common.collect.HashMultimap;
026import com.google.common.collect.Multimap;
027
028import static org.gbif.api.model.common.paging.PagingConstants.DEFAULT_PARAM_LIMIT;
029import static org.gbif.api.model.common.paging.PagingConstants.DEFAULT_PARAM_OFFSET;
030
031/**
032 * Generic request class for search operations. This class contains a list of parameters,
033 * a list of desired facets and paging options (page size and offset).
034 */
035public class SearchRequest<P extends SearchParameter> extends PageableBase {
036
037  private Multimap<P, String> parameters = HashMultimap.create();
038  private String q;
039  private boolean highlight;
040  private boolean spellCheck;
041  private int spellCheckCount;
042
043  /**
044   * Constructor with default paging offset & limit.
045   */
046  public SearchRequest() {
047    super(DEFAULT_PARAM_OFFSET, DEFAULT_PARAM_LIMIT);
048  }
049
050  /**
051   * Simple query constructor with default paging offset & limit.
052   *
053   * @param query string for request
054   */
055  public SearchRequest(String query) {
056    super(DEFAULT_PARAM_OFFSET, DEFAULT_PARAM_LIMIT);
057    q = query;
058  }
059
060  /**
061   * Minimal paging constructor.
062   */
063  public SearchRequest(Pageable page) {
064    super(page.getOffset(), page.getLimit());
065  }
066
067  /**
068   * Minimal paging constructor.
069   */
070  public SearchRequest(long offset, int limit) {
071    super(offset, limit);
072  }
073
074
075  /**
076   * @return true if highlighted search matches are requested
077   */
078  public boolean isHighlight() {
079    return highlight;
080  }
081
082  /**
083   * @param highlight the highlight to set
084   */
085  public void setHighlight(boolean highlight) {
086    this.highlight = highlight;
087  }
088
089  /**
090   *
091   * @return true if spellCheck search is requested
092   */
093  public boolean isSpellCheck() {
094    return spellCheck;
095  }
096
097  /**
098   * @param spellCheck the highlight to set
099   */
100  public void setSpellCheck(boolean spellCheck) {
101    this.spellCheck = spellCheck;
102  }
103
104  /**
105   *
106   * @return max number of spell check suggestions requested
107   */
108  public int getSpellCheckCount() {
109    return spellCheckCount;
110  }
111
112  /**
113   *
114   * @param spellCheckCount number of spell check suggestions
115   */
116  public void setSpellCheckCount(int spellCheckCount) {
117    this.spellCheckCount = spellCheckCount;
118  }
119
120  /**
121   * List of input parameters of the search operation.
122   * The parameters are handled as the parameter name and the string representation of its value.
123   *
124   * @return the list of parameters
125   */
126  public Multimap<P, String> getParameters() {
127    return parameters;
128  }
129
130  /**
131   * Sets the list of parameters.
132   */
133  public void setParameters(Multimap<P, String> parameters) {
134    this.parameters = parameters;
135  }
136
137  /**
138   * Query parameter.
139   *
140   * @return the q
141   */
142  public String getQ() {
143    return q;
144  }
145
146  /**
147   * @param q the q to set
148   */
149  public void setQ(String q) {
150    this.q = q;
151  }
152
153  /**
154   * Adds the specified parameter.
155   *
156   * @param parameter parameter to add values for
157   * @param values    list of values of the parameter to add
158   */
159  public void addParameter(P parameter, Iterable<String> values) {
160    for (String parameterValue : values) {
161      parameters.put(parameter, parameterValue);
162    }
163  }
164
165  /**
166   * Adds the specified parameter.
167   *
168   * @param parameter parameter to add values for
169   * @param values    list of values of the parameter to add
170   */
171  public void addParameter(P parameter, String... values) {
172    for (String value : values) {
173      addParameter(parameter, value);
174    }
175  }
176
177  /**
178   * Adds the specified parameter.
179   *
180   * @param parameter parameter to add values for
181   * @param value     value of the parameter to add
182   */
183  public void addParameter(P parameter, String value) {
184    if (value != null) {
185      parameters.put(parameter, value);
186    }
187  }
188
189  /**
190   * Adds the specified long parameter.
191   *
192   * @param parameter parameter to add values for
193   * @param value     value of the parameter to add
194   */
195  public void addParameter(P parameter, long value) {
196    parameters.put(parameter, String.valueOf(value));
197  }
198
199  /**
200   * Adds the specified int parameter.
201   *
202   * @param parameter parameter to add values for
203   * @param value     value of the parameter to add
204   */
205  public void addParameter(P parameter, int value) {
206    parameters.put(parameter, String.valueOf(value));
207  }
208
209  /**
210   * Adds the specified double parameter.
211   *
212   * @param parameter parameter to add values for
213   * @param value     value of the parameter to add
214   */
215  public void addParameter(P parameter, double value) {
216    parameters.put(parameter, String.valueOf(value));
217  }
218
219  /**
220   * Adds the specified boolean parameter.
221   *
222   * @param parameter parameter to add values for
223   * @param value     value of the parameter to add
224   */
225  public void addParameter(P parameter, boolean value) {
226    parameters.put(parameter, String.valueOf(value));
227  }
228
229  /**
230   * Adds the specified parameter.
231   *
232   * @param parameter parameter to add values for
233   * @param value     enum value of the parameter to add
234   */
235  public void addParameter(P parameter, Enum<?> value) {
236    if (value != null) {
237      parameters.put(parameter, value.name());
238    }
239  }
240
241
242  /**
243   * Adds the specified date parameter as an ISO date.
244   *
245   * @param parameter parameter to add date for
246   * @param value     date value of the parameter to add
247   */
248  public void addParameter(P parameter, Date value) {
249    if (value != null) {
250      // not thread safe, new instance
251      DateFormat iso = new SimpleDateFormat("yyyy-MM-dd");
252      parameters.put(parameter, iso.format(value));
253    }
254  }
255
256
257}