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
016import io.swagger.v3.oas.annotations.ExternalDocumentation;
017
018import io.swagger.v3.oas.annotations.media.ArraySchema;
019
020import org.gbif.api.model.predicate.ConjunctionPredicate;
021import org.gbif.api.model.predicate.DisjunctionPredicate;
022import org.gbif.api.model.predicate.EqualsPredicate;
023import org.gbif.api.model.predicate.GeoDistancePredicate;
024import org.gbif.api.model.predicate.GreaterThanOrEqualsPredicate;
025import org.gbif.api.model.predicate.GreaterThanPredicate;
026import org.gbif.api.model.predicate.InPredicate;
027import org.gbif.api.model.predicate.IsNotNullPredicate;
028import org.gbif.api.model.predicate.IsNullPredicate;
029import org.gbif.api.model.predicate.LessThanOrEqualsPredicate;
030import org.gbif.api.model.predicate.LessThanPredicate;
031import org.gbif.api.model.predicate.LikePredicate;
032import org.gbif.api.model.predicate.NotPredicate;
033import org.gbif.api.model.predicate.Predicate;
034import org.gbif.api.model.predicate.WithinPredicate;
035import org.gbif.api.vocabulary.Extension;
036
037import java.util.Collection;
038import java.util.Collections;
039import java.util.Objects;
040import java.util.Set;
041import java.util.StringJoiner;
042
043import javax.annotation.Nullable;
044import javax.validation.Valid;
045
046import com.fasterxml.jackson.annotation.JsonCreator;
047import com.fasterxml.jackson.annotation.JsonProperty;
048
049import io.swagger.v3.oas.annotations.media.Schema;
050
051/**
052 * An occurrence download request whose filters are based on predicates ( see {@link Predicate}).
053 */
054@Schema(
055  description = "An occurrence download request whose filters are based on predicates."
056)
057public class PredicateDownloadRequest extends DownloadRequest {
058
059  //Default download format.
060  private static final DownloadFormat DEFAULT_DOWNLOAD_FORMAT = DownloadFormat.SIMPLE_CSV;
061
062  @Schema(
063    description = "A predicate defining the filters to apply to the download.",
064    externalDocs = @ExternalDocumentation(url = "https://techdocs.gbif.org/en/data-use/api-downloads#predicates"),
065    oneOf = {
066      ConjunctionPredicate.class,
067      DisjunctionPredicate.class,
068      EqualsPredicate.class,
069      GeoDistancePredicate.class,
070      GreaterThanOrEqualsPredicate.class,
071      GreaterThanPredicate.class,
072      InPredicate.class,
073      IsNotNullPredicate.class,
074      IsNullPredicate.class,
075      LessThanOrEqualsPredicate.class,
076      LessThanPredicate.class,
077      LikePredicate.class,
078      NotPredicate.class,
079      WithinPredicate.class
080    }
081  )
082  private Predicate predicate;
083
084  @ArraySchema(
085    schema = @Schema(
086      description = "A verbatim (unprocessed) extension to include on a Darwin Core Archive download."
087    )
088  )
089  @JsonProperty("verbatimExtensions")
090  private Set<Extension> verbatimExtensions;
091
092  public PredicateDownloadRequest() {
093
094  }
095
096  /**
097   * Full constructor. Used to create instances using JSON serialization.
098   */
099  @JsonCreator
100  public PredicateDownloadRequest(
101    @JsonProperty("predicate") Predicate predicate,
102    @JsonProperty("creator") @Nullable String creator,
103    @JsonProperty("notificationAddresses") @Nullable Collection<String> notificationAddresses,
104    @JsonProperty("sendNotification") @Nullable Boolean sendNotification,
105    @JsonProperty("format") @Nullable DownloadFormat format,
106    @JsonProperty("type") @Nullable DownloadType type,
107    @JsonProperty("verbatimExtensions") @Nullable Set<Extension> verbatimExtensions) {
108    super(creator, notificationAddresses, sendNotification == null? Boolean.TRUE : sendNotification, format == null ? DEFAULT_DOWNLOAD_FORMAT : format, type == null? DownloadType.OCCURRENCE : type);
109    this.predicate = predicate;
110    this.verbatimExtensions = verbatimExtensions == null? Collections.emptySet(): verbatimExtensions;
111  }
112
113  /**
114   * @return the download filter
115   */
116  @Nullable
117  @Valid
118  public Predicate getPredicate() {
119    return predicate;
120  }
121
122  public void setPredicate(Predicate predicate) {
123    this.predicate = predicate;
124  }
125
126  /**
127   * Requested verbatimExtensions for this download.
128   */
129  @Nullable
130  public Set<Extension> getVerbatimExtensions() {
131    return verbatimExtensions;
132  }
133
134  public void setVerbatimExtensions(Set<Extension> verbatimExtensions) {
135    this.verbatimExtensions = verbatimExtensions;
136  }
137
138  @Override
139  public boolean equals(Object o) {
140    if (this == o) {
141      return true;
142    }
143    if (o == null || getClass() != o.getClass()) {
144      return false;
145    }
146    if (!super.equals(o)) {
147      return false;
148    }
149    PredicateDownloadRequest that = (PredicateDownloadRequest) o;
150    return Objects.equals(predicate, that.predicate) &&
151      Objects.equals(verbatimExtensions, that.verbatimExtensions);
152  }
153
154  @Override
155  public int hashCode() {
156    return Objects.hash(super.hashCode(), predicate, verbatimExtensions);
157  }
158
159  @Override
160  public String toString() {
161    return new StringJoiner(", ", PredicateDownloadRequest.class.getSimpleName() + "[", "]")
162      .add("predicate=" + predicate)
163      .add("creator='" + getCreator() + "'")
164      .add("notificationAddresses=" + getNotificationAddresses())
165      .add("sendNotification=" + getSendNotification())
166      .add("format=" + getFormat())
167      .add("type=" + getType())
168      .add("verbatimExtensions=" + verbatimExtensions)
169      .toString();
170  }
171}