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.common.search;
014
015import org.gbif.api.model.common.paging.Pageable;
016import org.gbif.api.model.common.paging.PagingRequest;
017
018import java.util.HashMap;
019import java.util.Map;
020import java.util.Set;
021
022import com.google.common.collect.Sets;
023
024/**
025 * Generic request class for search operations requesting facets.
026 * It extends a search request with a list of desired facets and optional settings.
027 */
028public class FacetedSearchRequest<P extends SearchParameter> extends SearchRequest<P> {
029
030  private Set<P> facets = Sets.newHashSet();
031
032  private boolean multiSelectFacets;
033  private Integer facetMinCount;
034  private Integer facetLimit = 10;
035  private Integer facetOffset;
036
037  //Holds the paging configuration for each requested facet
038  private Map<P,Pageable> facetPages = new HashMap<P, Pageable>();
039
040  public FacetedSearchRequest() {
041  }
042
043  public FacetedSearchRequest(Pageable page) {
044    super(page);
045  }
046
047  public FacetedSearchRequest(SearchRequest<P> searchRequest) {
048    super(searchRequest);
049    setHighlight(searchRequest.isHighlight());
050    setParameters(searchRequest.getParameters());
051    setQ(searchRequest.getQ());
052  }
053
054  public FacetedSearchRequest(long offset, int limit) {
055    super(offset, limit);
056  }
057
058  /**
059   * Min count of facet to return, if the facet count is less than this number the facet won't be included.
060   */
061  public Integer getFacetMinCount() {
062    return facetMinCount;
063  }
064
065  public void setFacetMinCount(Integer facetMinCount) {
066    this.facetMinCount = facetMinCount;
067  }
068
069  /**
070   * Gets the list of requested facets by the search operation.
071   * The facets are a list of search parameters.
072   */
073  public Set<P> getFacets() {
074    return facets;
075  }
076
077  /**
078   * Sets the list of facets.
079   */
080  public void setFacets(Set<P> facets) {
081    this.facets = facets;
082  }
083
084  /**
085   * @return the multiSelectFacets
086   */
087  public boolean isMultiSelectFacets() {
088    return multiSelectFacets;
089  }
090
091  /**
092   * @param multiSelectFacets the multiSelectFacets to set
093   */
094  public void setMultiSelectFacets(boolean multiSelectFacets) {
095    this.multiSelectFacets = multiSelectFacets;
096  }
097
098  /**
099   * Page size of the facet request.
100   */
101  public Integer getFacetLimit() {
102    return facetLimit;
103  }
104
105  public void setFacetLimit(Integer facetLimit) {
106    this.facetLimit = facetLimit;
107  }
108
109  /**
110   * Holds the paging configuration for each requested facet.
111   */
112  public Map<P, Pageable> getFacetPages() {
113    return facetPages;
114  }
115
116  public void setFacetPages(Map<P, Pageable> facetPages) {
117    this.facetPages = facetPages;
118  }
119
120  /**
121   * Sets the paging setting of facet parameter.
122   */
123  public void addFacetPage(P parameter, int facetOffset, int facetLimit){
124    facetPages.put(parameter, new PagingRequest(facetOffset,facetLimit));
125  }
126
127  /**
128   * Gets the paging configuration of a facet parameter.
129   */
130  public Pageable getFacetPage(P parameter) {
131    return facetPages.get(parameter);
132  }
133
134  /**
135   * Offset of the facet request.
136   */
137  public Integer getFacetOffset() {
138    return facetOffset;
139  }
140
141  public void setFacetOffset(Integer facetOffset) {
142    this.facetOffset = facetOffset;
143  }
144
145  public void addFacets(P... facets) {
146    if (this.facets == null) {
147      this.facets = Sets.newHashSet(facets);
148    } else {
149      this.facets.addAll(Sets.newHashSet(facets));
150    }
151  }
152
153}