001package com.box.sdk; 002 003import com.eclipsesource.json.Json; 004import com.eclipsesource.json.JsonObject; 005import com.eclipsesource.json.JsonValue; 006import java.net.URL; 007import java.util.Date; 008 009/** 010 * Represents a file version retention. 011 * A retention policy blocks permanent deletion of content for a specified amount of time. 012 * Admins can apply policies to specified folders, or an entire enterprise. 013 * A file version retention is a record for a retained file version. 014 * 015 * @see <a href="https://developer.box.com/reference/resources/file-version-retention/">Box file version retention</a> 016 * 017 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link BoxAPIException} (unchecked 018 * meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error 019 * handling for errors related to the Box REST API, you should capture this exception explicitly.</p> 020 */ 021@BoxResourceType("file_version_retention") 022public class BoxFileVersionRetention extends BoxResource { 023 024 /** 025 * @see #getInfo(String...) 026 */ 027 public static final URLTemplate RETENTION_URL_TEMPLATE = new URLTemplate("file_version_retentions/%s"); 028 029 /** 030 * The URL template used for operation with file version retentions. 031 */ 032 public static final URLTemplate ALL_RETENTIONS_URL_TEMPLATE = new URLTemplate("file_version_retentions"); 033 034 /** 035 * The default limit of entries per response. 036 */ 037 private static final int DEFAULT_LIMIT = 100; 038 039 /** 040 * Constructs a BoxFileVersionRetention for a resource with a given ID. 041 * 042 * @param api the API connection to be used by the resource. 043 * @param id the ID of the resource. 044 */ 045 public BoxFileVersionRetention(BoxAPIConnection api, String id) { 046 super(api, id); 047 } 048 049 /** 050 * Retrieves all file version retentions. 051 * 052 * @param api the API connection to be used by the resource. 053 * @param fields the fields to retrieve. 054 * @return an iterable contains information about all file version retentions. 055 */ 056 public static Iterable<BoxFileVersionRetention.Info> getAll(BoxAPIConnection api, String... fields) { 057 return getRetentions(api, new QueryFilter(), fields); 058 } 059 060 /** 061 * @param api the API connection to be used by the resource. 062 * @param filter filters for the query stored in QueryFilter object. 063 * @param fields the fields to retrieve. 064 * @return an iterable contains information about all file version retentions matching given filter. 065 * @deprecated This method will be deprecated in the future. Please use 066 * {@link BoxRetentionPolicyAssignment#getFilesUnderRetention(int, String...)} 067 * and {@link BoxRetentionPolicyAssignment#getFileVersionsUnderRetention(String...)} instead. 068 * Retrieves all file version retentions matching given filters as an Iterable. 069 */ 070 @Deprecated 071 public static Iterable<BoxFileVersionRetention.Info> getRetentions( 072 final BoxAPIConnection api, QueryFilter filter, String... fields) { 073 filter.addFields(fields); 074 return new BoxResourceIterable<BoxFileVersionRetention.Info>(api, 075 ALL_RETENTIONS_URL_TEMPLATE.buildWithQuery(api.getBaseURL(), filter.toString()), 076 DEFAULT_LIMIT) { 077 078 @Override 079 protected BoxFileVersionRetention.Info factory(JsonObject jsonObject) { 080 BoxFileVersionRetention retention = new BoxFileVersionRetention(api, jsonObject.get("id").asString()); 081 return retention.new Info(jsonObject); 082 } 083 }; 084 } 085 086 /** 087 * @param fields the fields to retrieve. 088 * @return information about this retention policy. 089 */ 090 public BoxFileVersionRetention.Info getInfo(String... fields) { 091 QueryStringBuilder builder = new QueryStringBuilder(); 092 if (fields.length > 0) { 093 builder.appendParam("fields", fields); 094 } 095 URL url = RETENTION_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), builder.toString(), this.getID()); 096 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET"); 097 BoxJSONResponse response = (BoxJSONResponse) request.send(); 098 JsonObject responseJSON = Json.parse(response.getJSON()).asObject(); 099 return new Info(responseJSON); 100 } 101 102 /** 103 * Represents possible query filters for "Get File Version Retentions" function. 104 */ 105 public static class QueryFilter extends QueryStringBuilder { 106 107 /** 108 * Param name for the file id to filter the file version retentions by. 109 */ 110 private static final String PARAM_FILE_ID = "file_id"; 111 112 /** 113 * Param name for the file version id to filter the file version retentions by. 114 */ 115 private static final String PARAM_FILE_VERSION_ID = "file_version_id"; 116 117 /** 118 * Param name for the policy id to filter the file version retentions by. 119 */ 120 private static final String PARAM_POLICY_ID = "policy_id"; 121 122 /** 123 * Param name for the disposition action of the retention policy. 124 */ 125 private static final String PARAM_DISPOSITION_ACTION = "disposition_action"; 126 127 /** 128 * Param name for the datetime to filter file version retentions. 129 */ 130 private static final String PARAM_DISPOSITION_BEFORE = "disposition_before"; 131 132 /** 133 * Param name for the datetime to filter file version retentions. 134 */ 135 private static final String PARAM_DISPOSITION_AFTER = "disposition_after"; 136 137 /** 138 * Param name for the the fields to retrieve. 139 */ 140 private static final String PARAM_FIELDS = "fields"; 141 142 /** 143 * Constructs empty query filter. 144 */ 145 public QueryFilter() { 146 super(); 147 } 148 149 /** 150 * @param id a file id to filter the file version retentions by. 151 * @return modified query filter. 152 */ 153 public QueryFilter addFileID(String id) { 154 this.appendParam(PARAM_FILE_ID, id); 155 return this; 156 } 157 158 /** 159 * @param id a file version id to filter the file version retentions by. 160 * @return modified query filter. 161 */ 162 public QueryFilter addFileVersionID(String id) { 163 this.appendParam(PARAM_FILE_VERSION_ID, id); 164 return this; 165 } 166 167 /** 168 * @param id a policy id to filter the file version retentions by. 169 * @return modified query filter. 170 */ 171 public QueryFilter addPolicyID(String id) { 172 this.appendParam(PARAM_POLICY_ID, id); 173 return this; 174 } 175 176 /** 177 * The action can be "permanently_delete" or "remove_retention". 178 * 179 * @param action the disposition action of the retention policy. 180 * @return modified query filter. 181 */ 182 public QueryFilter addDispositionAction(String action) { 183 this.appendParam(PARAM_DISPOSITION_ACTION, action); 184 return this; 185 } 186 187 /** 188 * @param date the datetime to filter file version retentions. 189 * @return modified query filter. 190 */ 191 public QueryFilter addDispositionBefore(Date date) { 192 this.appendParam(PARAM_DISPOSITION_BEFORE, BoxDateFormat.format(date)); 193 return this; 194 } 195 196 /** 197 * @param date the datetime to filter file version retentions. 198 * @return modified query filter. 199 */ 200 public QueryFilter addDispositionAfter(Date date) { 201 this.appendParam(PARAM_DISPOSITION_AFTER, BoxDateFormat.format(date)); 202 return this; 203 } 204 205 /** 206 * @param fields the fields to retrieve. 207 * @return modified query filter. 208 */ 209 public QueryFilter addFields(String... fields) { 210 if (fields.length > 0) { 211 this.appendParam(PARAM_FIELDS, fields); 212 } 213 return this; 214 } 215 } 216 217 /** 218 * Contains information about the retention policy. 219 */ 220 public class Info extends BoxResource.Info { 221 222 /** 223 * @see #getFileVersion() 224 */ 225 private BoxFileVersion fileVersion; 226 227 /** 228 * @see #getFile() 229 */ 230 private BoxFile.Info file; 231 232 /** 233 * @see #getAppliedAt() 234 */ 235 private Date appliedAt; 236 237 /** 238 * @see #getDispositionAt() 239 */ 240 private Date dispositionAt; 241 242 /** 243 * @see #getWinningPolicy() 244 */ 245 private BoxRetentionPolicy.Info winningPolicy; 246 247 /** 248 * Constructs an empty Info object. 249 */ 250 public Info() { 251 super(); 252 } 253 254 /** 255 * Constructs an Info object by parsing information from a JSON string. 256 * 257 * @param json the JSON string to parse. 258 */ 259 public Info(String json) { 260 super(json); 261 } 262 263 /** 264 * Constructs an Info object using an already parsed JSON object. 265 * 266 * @param jsonObject the parsed JSON object. 267 */ 268 Info(JsonObject jsonObject) { 269 super(jsonObject); 270 } 271 272 /** 273 * {@inheritDoc} 274 */ 275 @Override 276 public BoxResource getResource() { 277 return BoxFileVersionRetention.this; 278 } 279 280 /** 281 * @return the file version this file version retention was applied to. 282 */ 283 public BoxFileVersion getFileVersion() { 284 return this.fileVersion; 285 } 286 287 /** 288 * @return the file this file version retention was applied to. 289 */ 290 public BoxFile.Info getFile() { 291 return this.file; 292 } 293 294 /** 295 * @return the time that this file version retention was created. 296 */ 297 public Date getAppliedAt() { 298 return this.appliedAt; 299 } 300 301 /** 302 * @return the time that the retention period expires on this file version retention. 303 */ 304 public Date getDispositionAt() { 305 return this.dispositionAt; 306 } 307 308 /** 309 * @return the winning retention policy applied to this file version retention. 310 */ 311 public BoxRetentionPolicy.Info getWinningPolicy() { 312 return this.winningPolicy; 313 } 314 315 /** 316 * {@inheritDoc} 317 */ 318 @Override 319 void parseJSONMember(JsonObject.Member member) { 320 super.parseJSONMember(member); 321 String memberName = member.getName(); 322 JsonValue value = member.getValue(); 323 try { 324 if (memberName.equals("winning_retention_policy")) { 325 JsonObject policyJSON = value.asObject(); 326 if (this.winningPolicy == null) { 327 String policyID = policyJSON.get("id").asString(); 328 BoxRetentionPolicy policy = new BoxRetentionPolicy(getAPI(), policyID); 329 this.winningPolicy = policy.new Info(policyJSON); 330 } else { 331 this.winningPolicy.update(policyJSON); 332 } 333 } else if (memberName.equals("file")) { 334 JsonObject fileJSON = value.asObject(); 335 if (this.file == null) { 336 String fileID = fileJSON.get("id").asString(); 337 BoxFile file = new BoxFile(getAPI(), fileID); 338 this.file = file.new Info(fileJSON); 339 } else { 340 this.file.update(fileJSON); 341 } 342 } else if (memberName.equals("file_version")) { 343 JsonObject versionJSON = value.asObject(); 344 String fileVersionID = versionJSON.get("id").asString(); 345 this.fileVersion = new BoxFileVersion(getAPI(), versionJSON, fileVersionID); 346 } else if (memberName.equals("applied_at")) { 347 this.appliedAt = BoxDateFormat.parse(value.asString()); 348 } else if (memberName.equals("disposition_at")) { 349 this.dispositionAt = BoxDateFormat.parse(value.asString()); 350 } 351 } catch (Exception e) { 352 throw new BoxDeserializationException(memberName, value.toString(), e); 353 } 354 } 355 } 356}