001package org.gbif.api.model.occurrence;
002
003import org.gbif.api.model.common.DOI;
004import org.gbif.api.model.registry.PostPersist;
005import org.gbif.api.model.registry.PrePersist;
006import org.gbif.api.vocabulary.License;
007
008import java.util.Date;
009import java.util.EnumSet;
010
011import javax.annotation.Nullable;
012import javax.validation.constraints.NotNull;
013import javax.validation.constraints.Null;
014
015import com.google.common.base.Objects;
016import org.codehaus.jackson.annotate.JsonIgnore;
017
018
019public class Download {
020
021  /**
022   * Reflects the possibles statuses of a download during its execution.
023   * The download statuses are:
024   * - PREPARING: the download is initiating
025   * - RUNNING: the download is being processed
026   * - SUCCEEDED: the file is ready to be downloaded
027   * - CANCELLED: the download was cancelled by the user
028   * - KILLED: the download was killed by the workflow engine
029   * - FAILED: the download failed
030   * - SUSPENDED: the download was paused and its executing will be resumed later
031   */
032  public enum Status {
033    PREPARING, RUNNING, SUCCEEDED, CANCELLED, KILLED, FAILED, SUSPENDED;
034
035    /**
036     * Statuses that represent a download that that hasn't finished.
037     */
038    public static final EnumSet<Status> EXECUTING_STATUSES = EnumSet.of(PREPARING,RUNNING,SUSPENDED);
039
040    /**
041     * Statuses that represent a download that that has finished.
042     */
043    public static final EnumSet<Status> FINISH_STATUSES = EnumSet.of(SUCCEEDED, CANCELLED, KILLED, FAILED);
044  }
045
046  private String key;
047
048  private DOI doi;
049
050  private License license;
051
052  private DownloadRequest request;
053
054  private Date created;
055
056  private Date modified;
057
058  private Status status;
059
060  private String downloadLink;
061
062  private long size;
063
064  private long totalRecords;
065
066  private long numberDatasets;
067
068  /**
069   * @return timestamp when the download was created
070   */
071  @Null(groups = {PrePersist.class})
072  @NotNull(groups = {PostPersist.class})
073  @Nullable
074  public Date getCreated() {
075    return created;
076  }
077
078  /**
079   * @return the downloadLink
080   */
081  @Nullable
082  public String getDownloadLink() {
083    return downloadLink;
084  }
085
086
087  /**
088   * @return unique key that can be used in the REST service to retrieve its status
089   */
090  @NotNull
091  public String getKey() {
092    return key;
093  }
094
095
096  /**
097   * @return timestamp when the download result was last modified
098   */
099  @Null(groups = {PrePersist.class})
100  @NotNull(groups = {PostPersist.class})
101  @Nullable
102  public Date getModified() {
103    return modified;
104  }
105
106  /**
107   * Request that originated the download.
108   */
109  @NotNull
110  public DownloadRequest getRequest() {
111    return request;
112  }
113
114
115  /**
116   * @return the current download status
117   */
118  @NotNull
119  public Status getStatus() {
120    return status;
121  }
122
123  /**
124   * @return true if the download is completed and a result is available
125   */
126  @JsonIgnore
127  public boolean isAvailable() {
128    return status == Status.SUCCEEDED;
129  }
130
131  /**
132   * Size in bytes of the produced file.
133   */
134  @Nullable
135  public long getSize() {
136    return size;
137  }
138
139  /**
140   * Number of occurrence records in the download file.
141   */
142  @Nullable
143  public long getTotalRecords() {
144    return totalRecords;
145  }
146
147  /**
148   * Number of occurrence records in the download file.
149   */
150  @Nullable
151  public long getNumberDatasets() {
152    return numberDatasets;
153  }
154
155  /**
156   * Occurrence download Digital Object Identifier.
157   */
158  @Nullable
159  public DOI getDoi() {
160    return doi;
161  }
162
163  /**
164   * Get the license assigned to this occurrence download.
165   * @return
166   */
167  public License getLicense() {
168    return license;
169  }
170
171  public void setCreated(Date created) {
172    this.created = created;
173  }
174
175  public void setDownloadLink(String downloadLink) {
176    this.downloadLink = downloadLink;
177  }
178
179  public void setKey(String key) {
180    this.key = key;
181  }
182
183  public void setDoi(DOI doi) {
184    this.doi = doi;
185  }
186
187  public void setLicense(License license) {
188    this.license = license;
189  }
190
191  public void setModified(Date modified) {
192    this.modified = modified;
193  }
194
195  public void setRequest(DownloadRequest request) {
196    this.request = request;
197  }
198
199  public void setStatus(Status status) {
200    this.status = status;
201  }
202
203  public void setSize(long size) {
204    this.size = size;
205  }
206
207  public void setTotalRecords(long totalRecords) {
208    this.totalRecords = totalRecords;
209  }
210
211  public void setNumberDatasets(long numberDatasets) {
212    this.numberDatasets = numberDatasets;
213  }
214
215  @Override
216  public String toString() {
217    return Objects.toStringHelper(this).add("key", key).add("doi", doi)
218      .add("status", status).add("downloadLink", downloadLink)
219      .add("request", request).add("created", created)
220      .add("license", license)
221      .add("modified", modified)
222      .add("size", size)
223      .add("totalRecords", totalRecords)
224      .add("numberDatasets", numberDatasets).toString();
225  }
226
227  @Override
228  public boolean equals(Object obj) {
229    if (this == obj) {
230      return true;
231    }
232    if (!(obj instanceof Download)) {
233      return false;
234    }
235
236    Download that = (Download) obj;
237    return Objects.equal(this.request, that.request)
238      && Objects.equal(this.key, that.key)
239      && Objects.equal(this.doi, that.doi)
240      && Objects.equal(this.license, that.license)
241      && Objects.equal(this.created, that.created)
242      && Objects.equal(this.modified, that.modified)
243      && Objects.equal(this.status, that.status)
244      && Objects.equal(this.downloadLink, that.downloadLink)
245      && Objects.equal(this.size, that.size)
246      && Objects.equal(this.totalRecords, that.totalRecords)
247      && Objects.equal(this.numberDatasets, that.numberDatasets);
248  }
249
250  @Override
251  public int hashCode() {
252    return Objects.hashCode(key, doi, license, request, created, modified, status, downloadLink, size, totalRecords,
253            numberDatasets);
254  }
255}