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