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