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    @JsonProperty("checklistKey") @Nullable String checklistKey
053    ) {
054    // Check the format is suitable for an SQL download, and check predicate formats are suitable for predicate downloads.
055    super(
056      creator,
057      notificationAddresses,
058      sendNotification == null ? Boolean.TRUE : sendNotification,
059      format == null ? DEFAULT_DOWNLOAD_FORMAT : format,
060      type == null ? DownloadType.OCCURRENCE : type,
061      description,
062      machineDescription,
063      checklistKey
064    );
065    this.sql = sql;
066  }
067
068  /**
069   *
070   * @return the sql query.
071   */
072  @Valid
073  public String getSql() {
074    return sql;
075  }
076
077  /**
078   * This parameter when present provides the SQL query for custom download
079   * @param sql
080   */
081  public void setSql(String sql) {
082    this.sql = sql;
083  }
084
085  @Override
086  public int hashCode() {
087    return Objects.hash(super.hashCode(), sql);
088  }
089
090  @Override
091  public boolean equals(Object o) {
092    if (this == o) {
093      return true;
094    }
095    if (o == null || getClass() != o.getClass()) {
096      return false;
097    }
098    if (!super.equals(o)) {
099      return false;
100    }
101    SqlDownloadRequest that = (SqlDownloadRequest) o;
102    return Objects.equals(sql, that.sql);
103  }
104
105  @Override
106  public String toString() {
107    return new StringJoiner(", ", SqlDownloadRequest.class.getSimpleName() + "[", "]")
108      .add("sql=" + sql)
109      .toString();
110  }
111}