001package com.box.sdk; 002 003import com.eclipsesource.json.Json; 004import com.eclipsesource.json.JsonArray; 005import com.eclipsesource.json.JsonObject; 006import com.eclipsesource.json.JsonValue; 007import java.net.URL; 008import java.util.ArrayList; 009import java.util.Date; 010import java.util.List; 011 012/** 013 * Representing all holds on a file version. Note that every file version can have a maximum of one 014 * file version legal hold. 015 */ 016@BoxResourceType("file_version_legal_hold") 017public class BoxFileVersionLegalHold extends BoxResource { 018 019 /** 020 * The URL template used for operation with file version legal hold with given ID. 021 * 022 * @see #getInfo(String...) 023 */ 024 public static final URLTemplate FILE_VERSION_HOLD_URL_TEMPLATE = 025 new URLTemplate("file_version_legal_holds/%s"); 026 027 /** 028 * Constructs a file version legal hold with a given ID. 029 * 030 * @param api the API connection to be used by the resource. 031 * @param id the ID of the resource. 032 */ 033 public BoxFileVersionLegalHold(BoxAPIConnection api, String id) { 034 super(api, id); 035 } 036 037 /** 038 * @param fields the fields to retrieve. 039 * @return information about this file version legal hold. 040 */ 041 public BoxFileVersionLegalHold.Info getInfo(String... fields) { 042 QueryStringBuilder builder = new QueryStringBuilder(); 043 if (fields.length > 0) { 044 builder.appendParam("fields", fields); 045 } 046 URL url = 047 FILE_VERSION_HOLD_URL_TEMPLATE.buildWithQuery( 048 this.getAPI().getBaseURL(), builder.toString(), this.getID()); 049 BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "GET"); 050 try (BoxJSONResponse response = request.send()) { 051 JsonObject responseJSON = Json.parse(response.getJSON()).asObject(); 052 return new Info(responseJSON); 053 } 054 } 055 056 /** Contains information about the file version legal hold. */ 057 public class Info extends BoxResource.Info { 058 059 /** Used for file version in case it was retrieved separately from file. */ 060 private static final String DEFAULT_FILE_ID = "0"; 061 062 /** @see #getFileVersion() */ 063 private BoxFileVersion fileVersion; 064 065 /** @see #getFile() */ 066 private BoxFile.Info file; 067 068 /** @see #getAssignments() */ 069 private List<BoxLegalHoldAssignment.Info> assignments; 070 071 /** @see #getDeletedAt() */ 072 private Date deletedAt; 073 074 /** Constructs an empty Info object. */ 075 public Info() { 076 super(); 077 } 078 079 /** 080 * Constructs an Info object by parsing information from a JSON string. 081 * 082 * @param json the JSON string to parse. 083 */ 084 public Info(String json) { 085 super(json); 086 } 087 088 /** 089 * Constructs an Info object using an already parsed JSON object. 090 * 091 * @param jsonObject the parsed JSON object. 092 */ 093 Info(JsonObject jsonObject) { 094 super(jsonObject); 095 } 096 097 /** {@inheritDoc} */ 098 @Override 099 public BoxResource getResource() { 100 return BoxFileVersionLegalHold.this; 101 } 102 103 /** @return the file version that is held. */ 104 public BoxFileVersion getFileVersion() { 105 return this.fileVersion; 106 } 107 108 /** 109 * @return the parent file of the file version that is held. Note that there is no guarantee 110 * that the current version of this file is held. 111 */ 112 public BoxFile.Info getFile() { 113 return this.file; 114 } 115 116 /** @return iterable with the assignments contributing to this file version legal hold. */ 117 public Iterable<BoxLegalHoldAssignment.Info> getAssignments() { 118 return this.assignments; 119 } 120 121 /** @return time that this file version legal hold was deleted. */ 122 public Date getDeletedAt() { 123 return this.deletedAt; 124 } 125 126 /** {@inheritDoc} */ 127 @Override 128 void parseJSONMember(JsonObject.Member member) { 129 super.parseJSONMember(member); 130 String memberName = member.getName(); 131 JsonValue value = member.getValue(); 132 try { 133 if (memberName.equals("file")) { 134 JsonObject fileJSON = value.asObject(); 135 if (this.file == null) { 136 String fileID = fileJSON.get("id").asString(); 137 BoxFile file = new BoxFile(getAPI(), fileID); 138 this.file = file.new Info(fileJSON); 139 } else { 140 this.file.update(fileJSON); 141 } 142 if (this.fileVersion != null) { 143 this.fileVersion.setFileID(this.file.getID()); 144 } 145 } else if (memberName.equals("file_version")) { 146 JsonObject versionJSON = value.asObject(); 147 String fileID = this.file != null ? this.file.getID() : DEFAULT_FILE_ID; 148 this.fileVersion = new BoxFileVersion(getAPI(), versionJSON, fileID); 149 } else if (memberName.equals("legal_hold_policy_assignments")) { 150 JsonArray array = value.asArray(); 151 this.assignments = new ArrayList<>(); 152 for (JsonValue assignmentJSON : array) { 153 String assignmentID = ((JsonObject) assignmentJSON).get("id").asString(); 154 BoxLegalHoldAssignment assignment = new BoxLegalHoldAssignment(getAPI(), assignmentID); 155 this.assignments.add(assignment.new Info((JsonObject) assignmentJSON)); 156 } 157 } else if (memberName.equals("deleted_at")) { 158 this.deletedAt = BoxDateFormat.parse(value.asString()); 159 } 160 } catch (Exception e) { 161 throw new BoxDeserializationException(memberName, value.toString(), e); 162 } 163 } 164 } 165}