001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.api.model.occurrence;
015
016
017import org.gbif.api.model.predicate.Predicate;
018import org.gbif.api.vocabulary.Extension;
019
020import java.util.Collection;
021import java.util.Objects;
022import java.util.Set;
023import java.util.StringJoiner;
024
025import javax.annotation.Nullable;
026import javax.validation.Valid;
027
028import com.fasterxml.jackson.annotation.JsonCreator;
029import com.fasterxml.jackson.annotation.JsonProperty;
030
031import io.swagger.v3.oas.annotations.media.Schema;
032
033/**
034 * An occurrence download request whose filters are based on predicates ( see {@link Predicate}).
035 */
036@Schema(
037  description = "An occurrence download request whose filters are based on predicates."
038)
039public class PredicateDownloadRequest extends DownloadRequest {
040
041  //Default download format.
042  private static final DownloadFormat DEFAULT_DOWNLOAD_FORMAT = DownloadFormat.SIMPLE_CSV;
043
044  @Schema(
045    description = "A predicate defining the filters to apply to the download."
046  )
047  private Predicate predicate;
048
049  public PredicateDownloadRequest() {
050
051  }
052
053  /**
054   * Full constructor. Used to create instances using JSON serialization.
055   */
056  @JsonCreator
057  public PredicateDownloadRequest(
058    @JsonProperty("predicate") Predicate predicate,
059    @JsonProperty("creator") @Nullable String creator,
060    @JsonProperty("notificationAddresses") @Nullable Collection<String> notificationAddresses,
061    @JsonProperty("sendNotification") @Nullable Boolean sendNotification,
062    @JsonProperty("format") @Nullable DownloadFormat format,
063    @JsonProperty("type") @Nullable DownloadType type,
064    @JsonProperty("verbatimExtensions") @Nullable Set<Extension> verbatimExtensions) {
065    super(creator, notificationAddresses, sendNotification == null? Boolean.TRUE : sendNotification, format == null ? DEFAULT_DOWNLOAD_FORMAT : format, type == null? DownloadType.OCCURRENCE : type, verbatimExtensions);
066    this.predicate = predicate;
067  }
068
069  /**
070   * @return the download filter
071   */
072  @Nullable
073  @Valid
074  public Predicate getPredicate() {
075    return predicate;
076  }
077
078  public void setPredicate(Predicate predicate) {
079    this.predicate = predicate;
080  }
081
082  @Override
083  public boolean equals(Object o) {
084    if (this == o) {
085      return true;
086    }
087    if (o == null || getClass() != o.getClass()) {
088      return false;
089    }
090    if (!super.equals(o)) {
091      return false;
092    }
093    PredicateDownloadRequest that = (PredicateDownloadRequest) o;
094    return Objects.equals(predicate, that.predicate);
095  }
096
097  @Override
098  public int hashCode() {
099    return Objects.hash(super.hashCode(), predicate);
100  }
101
102  @Override
103  public String toString() {
104    return new StringJoiner(", ", PredicateDownloadRequest.class.getSimpleName() + "[", "]")
105      .add("predicate=" + predicate)
106      .toString();
107  }
108}