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