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