001package org.gbif.api.model.occurrence;
002
003import org.gbif.api.model.occurrence.predicate.Predicate;
004
005import java.util.Collection;
006import java.util.Set;
007
008import javax.annotation.Nullable;
009import javax.validation.Valid;
010import javax.validation.constraints.NotNull;
011
012import com.google.common.base.Joiner;
013import com.google.common.base.Objects;
014import com.google.common.base.Preconditions;
015import com.google.common.base.Splitter;
016import com.google.common.collect.ImmutableSet;
017import com.google.common.collect.Sets;
018import org.codehaus.jackson.annotate.JsonCreator;
019import org.codehaus.jackson.annotate.JsonIgnore;
020import org.codehaus.jackson.annotate.JsonProperty;
021
022/**
023 * Represents a request to download occurrence records.
024 * A download request with a null predicate is interpreted as a "download all" request.
025 */
026public class DownloadRequest {
027
028  private static final String DELIMITER = ",";
029  private static final Joiner COMMA_JOINER = Joiner.on(DELIMITER).skipNulls();
030  private static final Splitter COMMA_SPLITTER = Splitter.on(DELIMITER).omitEmptyStrings().trimResults();
031
032  private String creator;
033
034  private Predicate predicate;
035
036  private Set<String> notificationAddresses;
037
038  private boolean sendNotification;
039
040  private DownloadFormat format;
041
042  private static final DownloadFormat DEFAULT_DOWNLOAD_FORMAT = DownloadFormat.DWCA;
043
044  /**
045   * Default constructor.
046   */
047  public DownloadRequest() {
048    // Empty constructor required to create instances from the data access layer.
049  }
050
051  /**
052   * Full constructor. Used to create instances using JSON serialization.
053   */
054  @JsonCreator
055  public DownloadRequest(
056    @JsonProperty("predicate") Predicate predicate,
057    @JsonProperty("creator") @Nullable String creator,
058    @JsonProperty("notification_address") @Nullable Collection<String> notificationAddresses,
059    @JsonProperty("send_notification") @Nullable boolean sendNotification,
060    @JsonProperty("format") DownloadFormat format) {
061    this.creator = creator;
062    this.predicate = predicate;
063    this.notificationAddresses = notificationAddresses == null ?
064      ImmutableSet.<String>of() : ImmutableSet.copyOf(notificationAddresses);
065    this.sendNotification = sendNotification;
066    this.format = format == null ? DEFAULT_DOWNLOAD_FORMAT : format;
067  }
068
069  @Override
070  public boolean equals(Object obj) {
071    if (this == obj) {
072      return true;
073    }
074    if (!(obj instanceof DownloadRequest)) {
075      return false;
076    }
077
078    DownloadRequest that = (DownloadRequest) obj;
079    return Objects.equal(this.creator, that.creator)
080      && Objects.equal(this.predicate, that.predicate)
081      && Objects.equal(this.notificationAddresses, that.notificationAddresses)
082      && Objects.equal(this.sendNotification, that.sendNotification)
083      && Objects.equal(this.format, that.format);
084  }
085
086  /**
087   * @return the user account that initiated the download
088   */
089  @Nullable
090  public String getCreator() {
091    return creator;
092  }
093
094  /**
095   * @return set of email addresses for notifications
096   */
097  @Nullable
098  public Set<String> getNotificationAddresses() {
099    return notificationAddresses;
100  }
101
102  /**
103   * Returns the notification addresses as single string. The emails are separated by ','.
104   */
105  @Nullable
106  @JsonIgnore
107  public String getNotificationAddressesAsString() {
108    if (notificationAddresses != null) {
109      return COMMA_JOINER.join(notificationAddresses);
110    }
111    return null;
112  }
113
114  /**
115   * @return the download filter
116   */
117  @Nullable
118  @Valid
119  public Predicate getPredicate() {
120    return predicate;
121  }
122
123
124  @Override
125  public int hashCode() {
126    return Objects.hashCode(creator, predicate, notificationAddresses, sendNotification, format);
127  }
128
129
130  public void setCreator(String creator) {
131    this.creator = creator;
132  }
133
134
135  public void setNotificationAddresses(Set<String> notificationAddresses) {
136    this.notificationAddresses = notificationAddresses;
137  }
138
139  /**
140   * Sets the notificationAddresses using a single String value that is split by ','.
141   */
142  public void setNotificationAddressesAsString(String notificationAddressesAsString) {
143    if (notificationAddressesAsString != null) {
144      notificationAddresses = Sets.newHashSet(COMMA_SPLITTER.split(notificationAddressesAsString));
145    }
146  }
147
148  public void setPredicate(Predicate predicate) {
149    this.predicate = predicate;
150  }
151
152
153  public boolean getSendNotification() {
154    return sendNotification;
155  }
156
157  /**
158   * This parameter determines if the requested download must be notified to the created once it's ready.
159   */
160  public void setSendNotification(boolean sendNotification) {
161    this.sendNotification = sendNotification;
162  }
163
164  public DownloadFormat getFormat() {
165    return format;
166  }
167
168  /**
169   * This parameter determines the output format of the requested download.
170   */
171  public void setFormat(DownloadFormat format) {
172    this.format = format;
173  }
174
175  @Override
176  public String toString() {
177    return Objects.toStringHelper(this).add("creator", creator).add("predicate", predicate)
178      .add("notificationAddresses", notificationAddresses).add("emailNotification", sendNotification)
179      .add("format", format).toString();
180  }
181
182}