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