001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014package org.gbif.api.model.occurrence; 015 016import com.fasterxml.jackson.databind.JsonNode; 017 018import io.swagger.v3.oas.annotations.Hidden; 019import io.swagger.v3.oas.annotations.media.ArraySchema; 020import io.swagger.v3.oas.annotations.media.Schema; 021 022import org.gbif.api.jackson.DownloadRequestSerde; 023 024import java.io.Serializable; 025import java.util.Collection; 026import java.util.Collections; 027import java.util.HashSet; 028import java.util.Objects; 029import java.util.Set; 030import java.util.StringJoiner; 031import java.util.UUID; 032import java.util.stream.Collectors; 033import java.util.stream.Stream; 034 035import jakarta.annotation.Nullable; 036 037import org.apache.commons.lang3.StringUtils; 038 039import com.fasterxml.jackson.annotation.JsonIgnore; 040import com.fasterxml.jackson.annotation.JsonProperty; 041import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 042 043/** 044 * Represents a request to download occurrence records. 045 * This is the base class for specific type of downloads: predicate based downloads and SQL downloads. 046 */ 047@Schema( 048 description = "An occurrence download request.", 049 oneOf = {PredicateDownloadRequest.class, SqlDownloadRequest.class} 050) 051@SuppressWarnings("unused") 052@JsonDeserialize(using = DownloadRequestSerde.class) 053public abstract class DownloadRequest implements Serializable { 054 055 private static final String DELIMITER = ","; 056 057 @Schema( 058 description = "The GBIF username of the initiator of the download request." 059 ) 060 @JsonProperty("creator") 061 private String creator; 062 063 @ArraySchema( 064 schema = @Schema( 065 description = "Ignored since November 2026. Email notifications are sent to the registered user's email address." 066 ) 067 ) 068 @JsonProperty("notificationAddresses") 069 @Deprecated 070 private Set<String> notificationAddresses; 071 072 @Schema( 073 description = "Whether to send a notification email when the download finishes." 074 ) 075 @JsonProperty("sendNotification") 076 private Boolean sendNotification; 077 078 @Schema( 079 description = "The data format of the download." 080 ) 081 @JsonProperty("format") 082 private DownloadFormat format; 083 084 @Hidden 085 @JsonProperty("type") 086 private DownloadType type; 087 088 @Schema( 089 description = "A user-specified description of the download, such as the intended purpose or a tag " + 090 "for later reference." 091 ) 092 @JsonProperty("description") 093 private String description; 094 095 @JsonProperty("machineDescription") 096 private JsonNode machineDescription; 097 098 @JsonProperty("checklistKey") 099 private UUID checklistKey; 100 101 /** 102 * Default constructor. 103 */ 104 public DownloadRequest() { 105 // Empty constructor required to create instances from the data access layer. 106 } 107 108 public DownloadRequest(String creator, Collection<String> notificationAddresses, 109 Boolean sendNotification, DownloadFormat format, 110 DownloadType downloadType, String description, 111 JsonNode machineDescription, UUID checklistKey) { 112 this.creator = creator; 113 this.notificationAddresses = notificationAddresses == null ? Collections.emptySet() : 114 Collections.unmodifiableSet(new HashSet<>(notificationAddresses)); 115 this.sendNotification = sendNotification; 116 this.format = format; 117 this.type = downloadType; 118 this.description = description; 119 this.machineDescription = machineDescription; 120 this.checklistKey = checklistKey; 121 } 122 123 /** 124 * @return the user account that initiated the download 125 */ 126 @Nullable 127 public String getCreator() { 128 return creator; 129 } 130 131 public void setCreator(String creator) { 132 this.creator = creator; 133 } 134 135 /** 136 * @return set of email addresses for notifications 137 */ 138 @Nullable 139 @Deprecated 140 public Set<String> getNotificationAddresses() { 141 return notificationAddresses; 142 } 143 144 public void setNotificationAddresses(Set<String> notificationAddresses) { 145 this.notificationAddresses = notificationAddresses; 146 } 147 148 /** 149 * Returns the notification addresses as single string. The emails are separated by ','. 150 */ 151 @Nullable 152 @JsonIgnore 153 @Deprecated 154 public String getNotificationAddressesAsString() { 155 if (notificationAddresses != null) { 156 return notificationAddresses.stream() 157 .filter(Objects::nonNull) 158 .map(String::trim) 159 .collect(Collectors.joining(DELIMITER)); 160 } 161 return null; 162 } 163 164 /** 165 * Sets the notificationAddresses using a single String value that is split by ','. 166 */ 167 @Deprecated 168 public void setNotificationAddressesAsString(String notificationAddressesAsString) { 169 if (notificationAddressesAsString != null) { 170 notificationAddresses = Stream.of(notificationAddressesAsString.split(DELIMITER)) 171 .filter(StringUtils::isNotEmpty) 172 .map(String::trim) 173 .collect(Collectors.toSet()); 174 } 175 } 176 177 @Nullable 178 public Boolean getSendNotification() { 179 return sendNotification; 180 } 181 182 /** 183 * This parameter determines if the requested download must be notified to the created once it's ready. 184 */ 185 public void setSendNotification(boolean sendNotification) { 186 this.sendNotification = sendNotification; 187 } 188 189 public DownloadFormat getFormat() { 190 return format; 191 } 192 193 /** 194 * This parameter determines the output format of the requested download. 195 */ 196 public void setFormat(DownloadFormat format) { 197 this.format = format; 198 } 199 200 public DownloadType getType() { 201 return type; 202 } 203 204 /** 205 * Download type: Occurrence or Event. 206 */ 207 public void setType(DownloadType type) { 208 this.type = type; 209 } 210 211 public String getDescription() { 212 return description; 213 } 214 215 public void setDescription(String description) { 216 this.description = description; 217 } 218 219 public JsonNode getMachineDescription() { 220 return machineDescription; 221 } 222 223 public void setMachineDescription(JsonNode machineDescription) { 224 this.machineDescription = machineDescription; 225 } 226 227 @JsonIgnore 228 public String getFileExtension() { 229 return format.getExtension(); 230 } 231 232 public UUID getChecklistKey() { 233 return checklistKey; 234 } 235 236 public void setChecklistKey(UUID checklistKey) { 237 this.checklistKey = checklistKey; 238 } 239 240 @Override 241 public boolean equals(Object o) { 242 if (this == o) { 243 return true; 244 } 245 if (o == null || getClass() != o.getClass()) { 246 return false; 247 } 248 DownloadRequest that = (DownloadRequest) o; 249 return sendNotification == that.sendNotification && 250 Objects.equals(creator, that.creator) && 251 Objects.equals(notificationAddresses, that.notificationAddresses) && 252 format == that.format && 253 type == that.type && 254 Objects.equals(checklistKey, that.checklistKey) && 255 Objects.equals(description, that.description); 256 } 257 258 @Override 259 public int hashCode() { 260 return Objects.hash(creator, notificationAddresses, sendNotification, format, type, description, machineDescription, checklistKey); 261 } 262 263 @Override 264 public String toString() { 265 return new StringJoiner(", ", DownloadRequest.class.getSimpleName() + "[", "]") 266 .add("creator='" + creator + "'") 267 .add("notificationAddresses=" + notificationAddresses) 268 .add("sendNotification=" + sendNotification) 269 .add("format=" + format) 270 .add("type=" + type) 271 .add("description=" + description) 272 .add("machineDescription=" + machineDescription) 273 .add("checklistKey=" + checklistKey) 274 .toString(); 275 } 276}