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