001/*
002 * Copyright 2014 Global Biodiversity Information Facility (GBIF)
003 * Licensed under the Apache License, Version 2.0 (the "License");
004 * you may not use this file except in compliance with the License.
005 * You may obtain a copy of the License at
006 * http://www.apache.org/licenses/LICENSE-2.0
007 * Unless required by applicable law or agreed to in writing, software
008 * distributed under the License is distributed on an "AS IS" BASIS,
009 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010 * See the License for the specific language governing permissions and
011 * limitations under the License.
012 */
013package org.gbif.api.model.checklistbank.search;
014
015import com.google.common.collect.Sets;
016import org.gbif.api.model.common.paging.Pageable;
017import org.gbif.api.model.common.search.FacetedSearchRequest;
018import org.gbif.api.vocabulary.*;
019
020import java.util.Set;
021import java.util.UUID;
022
023import static org.gbif.api.model.checklistbank.search.NameUsageSearchRequest.QueryField.*;
024
025/**
026 * A name usage specific search request with convenience methods to add enum based search filters.
027 * By default the query q is send to all available query fields.
028 * Highlighting by default works for descriptions and vernacular names - if turned on.
029 */
030public class NameUsageSearchRequest extends FacetedSearchRequest<NameUsageSearchParameter> {
031  private boolean extended = true;
032  private Set<QueryField> queryFields = Sets.newHashSet(SCIENTIFIC, DESCRIPTION, VERNACULAR);
033  private Set<QueryField> highlightFields = Sets.newHashSet(DESCRIPTION, VERNACULAR);
034  private Integer highlightContext = 100;
035
036  public enum QueryField {
037    SCIENTIFIC,
038    VERNACULAR,
039    DESCRIPTION
040  }
041
042  public NameUsageSearchRequest() {
043  }
044
045  public NameUsageSearchRequest(Pageable page) {
046    super(page);
047  }
048
049  public NameUsageSearchRequest(long offset, int limit) {
050    super(offset, limit);
051  }
052
053  /**
054   * Defines whether to match against fields with scientific or vernacular names or both.
055   */
056  public Set<QueryField> getQueryFields() {
057    return queryFields;
058  }
059
060  public void setQueryFields(Set<QueryField> queryFields) {
061    this.queryFields = queryFields;
062  }
063
064  /**
065
066   * Defines the fields to be highlighted if highlighting is activated.
067   */
068  public Set<QueryField> getHighlightFields() {
069    return highlightFields;
070  }
071
072  public void setHighlightFields(Set<QueryField> highlightFields) {
073    this.highlightFields = highlightFields;
074  }
075
076  /**
077   * @return the number of characters of the context to show for the highlighted match, including the match itself.
078   */
079  public Integer getHighlightContext() {
080    return highlightContext;
081  }
082
083  public void setHighlightContext(Integer highlightContext) {
084    this.highlightContext = highlightContext;
085  }
086
087  /**
088   * Allows to request an extended search object with the larger list properties:
089   * <ul>
090   *  <li>habitats</li>
091   *  <li>nomenclaturalStatus</li>
092   *  <li>threatStatuses</li>
093   *  <li>descriptions</li>
094   *  <li>vernacularNames</li>
095   * </ul>
096   *
097   * If extended=false and highlighting is activated, the highlighted matches will still be added to the extended properties.
098   * In that case only the matched parts are shown, e.g. just the one vernacular name that matched.
099   *
100   * @return if true request the extended search model
101   */
102  public boolean isExtended() {
103    return extended;
104  }
105
106  public void setExtended(boolean extended) {
107    this.extended = extended;
108  }
109
110  public void addChecklistFilter(UUID datasetKey) {
111    addParameter(NameUsageSearchParameter.DATASET_KEY, datasetKey.toString());
112  }
113
114  public void addExtinctFilter(boolean isExtinct) {
115    addParameter(NameUsageSearchParameter.IS_EXTINCT, String.valueOf(isExtinct));
116  }
117
118  public void addHigherTaxonFilter(int taxonKey) {
119    addParameter(NameUsageSearchParameter.HIGHERTAXON_KEY, taxonKey);
120  }
121
122  public void addHabitatFilter(Habitat habitat) {
123    addParameter(NameUsageSearchParameter.HABITAT, habitat);
124  }
125
126  public void addRankFilter(Rank rank) {
127    addParameter(NameUsageSearchParameter.RANK, rank);
128  }
129
130  public void addTaxonomicStatusFilter(TaxonomicStatus status) {
131    addParameter(NameUsageSearchParameter.STATUS, status);
132  }
133
134  public void addTaxonomicStatusFilter(NomenclaturalStatus nomenclaturalStatus) {
135    addParameter(NameUsageSearchParameter.NOMENCLATURAL_STATUS, nomenclaturalStatus);
136  }
137
138  public void addThreatStatusFilter(ThreatStatus threat) {
139    addParameter(NameUsageSearchParameter.THREAT, threat);
140  }
141
142  public void addIssueFilter(NameUsageIssue issue) {
143    addParameter(NameUsageSearchParameter.ISSUE, issue);
144  }
145
146  public void addOriginFilter(Origin origin) {
147    addParameter(NameUsageSearchParameter.ORIGIN, origin);
148  }
149
150}