001package org.gbif.api.model.occurrence;
002
003import java.util.Collection;
004import java.util.Objects;
005import java.util.StringJoiner;
006
007import javax.annotation.Nullable;
008import javax.validation.Valid;
009
010import com.fasterxml.jackson.annotation.JsonCreator;
011import com.fasterxml.jackson.annotation.JsonProperty;
012
013import io.swagger.v3.oas.annotations.ExternalDocumentation;
014import io.swagger.v3.oas.annotations.media.Schema;
015
016/**
017 * SQL Based downloads — experimental feature.
018 */
019@Schema(
020  description = "An occurrence download request defined in SQL.",
021  externalDocs = @ExternalDocumentation(url = "https://techdocs.gbif.org/en/data-use/api-sql-downloads")
022)
023public class SqlDownloadRequest extends DownloadRequest {
024
025  //Default download format.
026  private static final DownloadFormat DEFAULT_DOWNLOAD_FORMAT = DownloadFormat.SQL_TSV_ZIP;
027
028  @Schema(
029    description = "An SQL query defining the filter and output for the download."
030  )
031  private String sql;
032
033  public SqlDownloadRequest() {
034
035  }
036
037  /**
038   * Full constructor. Used to create instances using JSON serialization.
039   */
040  @JsonCreator
041  public SqlDownloadRequest(
042    @JsonProperty("sql") String sql,
043    @JsonProperty("creator") @Nullable String creator,
044    @JsonProperty("notificationAddresses") @Nullable Collection<String> notificationAddresses,
045    @JsonProperty("sendNotification") @Nullable Boolean sendNotification,
046    @JsonProperty("type") @Nullable DownloadType type,
047    @JsonProperty("format") @Nullable DownloadFormat format) {
048    // Check the format is suitable for an SQL download, and check predicate formats are suitable for predicate downloads.
049    super(creator, notificationAddresses, sendNotification == null ? Boolean.TRUE : sendNotification, format == null ? DEFAULT_DOWNLOAD_FORMAT : format, type == null? DownloadType.OCCURRENCE : type);
050    this.sql = sql;
051  }
052
053  /**
054   *
055   * @return the sql query.
056   */
057  @Valid
058  public String getSql() {
059    return sql;
060  }
061
062  /**
063   * This parameter when present provides the SQL query for custom download
064   * @param sql
065   */
066  public void setSql(String sql) {
067    this.sql = sql;
068  }
069
070  @Override
071  public int hashCode() {
072    return Objects.hash(super.hashCode(), sql);
073  }
074
075  @Override
076  public boolean equals(Object o) {
077    if (this == o) {
078      return true;
079    }
080    if (o == null || getClass() != o.getClass()) {
081      return false;
082    }
083    if (!super.equals(o)) {
084      return false;
085    }
086    SqlDownloadRequest that = (SqlDownloadRequest) o;
087    return Objects.equals(sql, that.sql);
088  }
089
090  @Override
091  public String toString() {
092    return new StringJoiner(", ", SqlDownloadRequest.class.getSimpleName() + "[", "]")
093      .add("sql=" + sql)
094      .toString();
095  }
096}