001package com.box.sdk; 002 003import static com.box.sdk.BinaryBodyUtils.writeStream; 004 005import com.eclipsesource.json.Json; 006import com.eclipsesource.json.JsonObject; 007import com.eclipsesource.json.JsonValue; 008import java.io.OutputStream; 009import java.net.URL; 010import java.text.ParseException; 011import java.util.Date; 012 013/** 014 * Represents a particular version of a file on Box. 015 */ 016@BoxResourceType("file_version") 017public class BoxFileVersion extends BoxResource { 018 /** 019 * Content URL Template. 020 */ 021 public static final URLTemplate CONTENT_URL_TEMPLATE = new URLTemplate("files/%s/content?version=%s"); 022 /** 023 * Version URL Template. 024 */ 025 public static final URLTemplate VERSION_URL_TEMPLATE = new URLTemplate("files/%s/versions/%s"); 026 027 private String fileID; 028 029 private String versionID; 030 private String sha1; 031 private String name; 032 private long size; 033 private String uploaderDisplayName; 034 private Date createdAt; 035 private Date modifiedAt; 036 private BoxUser.Info modifiedBy; 037 private Date trashedAt; 038 private BoxUser.Info trashedBy; 039 private Date restoredAt; 040 private BoxUser.Info restoredBy; 041 private Date purgedAt; 042 private Long versionNumber; 043 044 /** 045 * Constructs a BoxFileVersion from a JSON string. 046 * 047 * @param api the API connection to be used by the file. 048 * @param json the JSON encoded file version. 049 * @param fileID the ID of the file. 050 */ 051 public BoxFileVersion(BoxAPIConnection api, String json, String fileID) { 052 this(api, Json.parse(json).asObject(), fileID); 053 } 054 055 BoxFileVersion(BoxAPIConnection api, JsonObject jsonObject, String fileID) { 056 super(api, jsonObject.get("id").asString()); 057 058 this.fileID = fileID; 059 this.parseJSON(jsonObject); 060 } 061 062 /** 063 * Method used to update fields with values received from API. 064 * 065 * @param jsonObject JSON-encoded info about File Version object. 066 */ 067 private void parseJSON(JsonObject jsonObject) { 068 for (JsonObject.Member member : jsonObject) { 069 JsonValue value = member.getValue(); 070 if (value.isNull()) { 071 continue; 072 } 073 074 try { 075 String memberName = member.getName(); 076 if (memberName.equals("id")) { 077 this.versionID = value.asString(); 078 } else if (memberName.equals("sha1")) { 079 this.sha1 = value.asString(); 080 } else if (memberName.equals("name")) { 081 this.name = value.asString(); 082 } else if (memberName.equals("size")) { 083 this.size = Double.valueOf(value.toString()).longValue(); 084 } else if (memberName.equals("uploader_display_name")) { 085 this.uploaderDisplayName = value.asString(); 086 } else if (memberName.equals("created_at")) { 087 this.createdAt = BoxDateFormat.parse(value.asString()); 088 } else if (memberName.equals("modified_at")) { 089 this.modifiedAt = BoxDateFormat.parse(value.asString()); 090 } else if (memberName.equals("trashed_at")) { 091 this.trashedAt = BoxDateFormat.parse(value.asString()); 092 } else if (memberName.equals("trashed_by")) { 093 JsonObject userJSON = value.asObject(); 094 String userID = userJSON.get("id").asString(); 095 BoxUser user = new BoxUser(getAPI(), userID); 096 this.trashedBy = user.new Info(userJSON); 097 } else if (memberName.equals("modified_by")) { 098 JsonObject userJSON = value.asObject(); 099 String userID = userJSON.get("id").asString(); 100 BoxUser user = new BoxUser(getAPI(), userID); 101 this.modifiedBy = user.new Info(userJSON); 102 } else if (memberName.equals("restored_at")) { 103 this.restoredAt = BoxDateFormat.parse(value.asString()); 104 } else if (memberName.equals("restored_by")) { 105 JsonObject userJSON = value.asObject(); 106 String userID = userJSON.get("id").asString(); 107 BoxUser user = new BoxUser(getAPI(), userID); 108 this.restoredBy = user.new Info(userJSON); 109 } else if (memberName.equals("purged_at")) { 110 this.purgedAt = BoxDateFormat.parse(value.asString()); 111 } else if (memberName.equals("version_number")) { 112 this.versionNumber = Long.parseLong(value.asString()); 113 } 114 } catch (ParseException e) { 115 assert false : "A ParseException indicates a bug in the SDK."; 116 } 117 } 118 } 119 120 /** 121 * @return the file id this file version belongs to. 122 */ 123 public String getFileID() { 124 return this.fileID; 125 } 126 127 /** 128 * Used if no or wrong file id was set with constructor. 129 * 130 * @param fileID the file id this file version belongs to. 131 */ 132 public void setFileID(String fileID) { 133 this.fileID = fileID; 134 } 135 136 /** 137 * Gets the version ID of this version of the file. 138 * 139 * @return the version ID of this version of the file. 140 */ 141 public String getVersionID() { 142 return this.versionID; 143 } 144 145 /** 146 * Gets the SHA1 hash of this version of the file. 147 * 148 * @return the SHA1 hash of this version of the file. 149 */ 150 public String getSha1() { 151 return this.sha1; 152 } 153 154 /** 155 * Gets the name of this version of the file. 156 * 157 * @return the name of this version of the file. 158 */ 159 public String getName() { 160 return this.name; 161 } 162 163 /** 164 * Gets the size of this version of the file. 165 * 166 * @return the size of this version of the file. 167 */ 168 public long getSize() { 169 return this.size; 170 } 171 172 /** 173 * Gets the time that this version of the file was created. 174 * 175 * @return the time that this version of the file was created. 176 */ 177 public Date getCreatedAt() { 178 return this.createdAt; 179 } 180 181 /** 182 * Gets the user's name at the time of upload. 183 * 184 * @return the time user's name at the time of upload. 185 */ 186 public String getUploaderDisplayName() { 187 return this.uploaderDisplayName; 188 } 189 190 /** 191 * Gets the time that this version of the file was modified. 192 * 193 * @return the time that this version of the file was modified. 194 */ 195 public Date getModifiedAt() { 196 return this.modifiedAt; 197 } 198 199 /** 200 * Gets the time that this version of the file was deleted. 201 * 202 * @return the time that this version of the file was deleted. 203 */ 204 public Date getTrashedAt() { 205 return this.trashedAt; 206 } 207 208 /** 209 * Gets information about the user who trashed this version of the file. 210 * 211 * @return info about the user who trashed this version of the file. 212 */ 213 public BoxUser.Info getTrashedBy() { 214 return this.trashedBy; 215 } 216 217 /** 218 * Gets information about the user who last modified this version of the file. 219 * 220 * @return info about the user who last modified this version of the file. 221 */ 222 public BoxUser.Info getModifiedBy() { 223 return this.modifiedBy; 224 } 225 226 /** 227 * Gets the time that this version of the file was restored. 228 * 229 * @return the time that this version of the file was restored. 230 */ 231 public Date getRestoredAt() { 232 return this.restoredAt; 233 } 234 235 /** 236 * Gets information about the user who restored this version of the file. 237 * 238 * @return info about the user who restored this version of the file. 239 */ 240 public BoxUser.Info getRestoredBy() { 241 return this.restoredBy; 242 } 243 244 /** 245 * Gets the time that this version of the file was purged. 246 * 247 * @return the time that this version of the file was purged. 248 */ 249 public Date getPurgedAt() { 250 return this.purgedAt; 251 } 252 253 /** 254 * Returns version number. 255 * 256 * @return version number 257 */ 258 public Long getVersionNumber() { 259 return versionNumber; 260 } 261 262 /** 263 * Deletes this version of the file. 264 */ 265 public void delete() { 266 URL url = VERSION_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.fileID, this.getID()); 267 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "DELETE"); 268 request.send().close(); 269 } 270 271 /** 272 * Downloads this version of the file to a given OutputStream. 273 * 274 * @param output the stream to where the file will be written. 275 */ 276 public void download(OutputStream output) { 277 this.download(output, null); 278 } 279 280 /** 281 * Downloads this version of the file to a given OutputStream while reporting the progress to a ProgressListener. 282 * 283 * @param output the stream to where the file will be written. 284 * @param listener a listener for monitoring the download's progress. 285 */ 286 public void download(OutputStream output, ProgressListener listener) { 287 URL url = CONTENT_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.fileID, this.getID()); 288 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET"); 289 writeStream(request.send(), output, listener); 290 } 291 292 /** 293 * Promotes this version of the file to be the latest version. 294 */ 295 public void promote() { 296 URL url = VERSION_URL_TEMPLATE.buildAlpha(this.getAPI().getBaseURL(), this.fileID, "current"); 297 298 JsonObject jsonObject = new JsonObject(); 299 jsonObject.add("type", "file_version"); 300 jsonObject.add("id", this.getID()); 301 302 BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "POST"); 303 request.setBody(jsonObject.toString()); 304 try (BoxJSONResponse response = request.send()) { 305 this.parseJSON(Json.parse(response.getJSON()).asObject()); 306 } 307 } 308}