001package com.box.sdk; 002 003import com.eclipsesource.json.Json; 004import com.eclipsesource.json.JsonObject; 005import com.eclipsesource.json.JsonValue; 006import java.net.URL; 007import java.util.Date; 008 009/** 010 * Represents a legal hold policy assignment. Legal hold assignments are used to assign legal hold 011 * policies to custodians, folders, files, or file versions. 012 * 013 * @see <a href="https://developer.box.com/reference/resources/legal-hold-policy-assignment/">Box 014 * legal holds</a> 015 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link 016 * BoxAPIException} (unchecked meaning that the compiler won't force you to handle it) if an 017 * error occurs. If you wish to implement custom error handling for errors related to the Box 018 * REST API, you should capture this exception explicitly. 019 */ 020@BoxResourceType("legal_hold_assignment") 021public class BoxLegalHoldAssignment extends BoxResource { 022 023 /** Used to assign legal hold policy to file version. */ 024 public static final String TYPE_FILE_VERSION = 025 BoxFileVersion.getResourceType(BoxFileVersion.class); 026 027 /** Used to assign legal hold policy to file. */ 028 public static final String TYPE_FILE = BoxFile.getResourceType(BoxFile.class); 029 030 /** Used to assign legal hold policy to folder. */ 031 public static final String TYPE_FOLDER = BoxFolder.getResourceType(BoxFolder.class); 032 033 /** Used to assign legal hold policy to user. */ 034 public static final String TYPE_USER = BoxUser.getResourceType(BoxUser.class); 035 036 /** The URL template used for operation with legal hold policy assignments. */ 037 public static final URLTemplate ASSIGNMENTS_URL_TEMPLATE = 038 new URLTemplate("legal_hold_policy_assignments"); 039 040 /** The URL template used for operation with legal hold policy assignment with given ID. */ 041 public static final URLTemplate LEGAL_HOLD_ASSIGNMENT_URL_TEMPLATE = 042 new URLTemplate("legal_hold_policy_assignments/%s"); 043 044 /** 045 * Constructs a BoxLegalHoldAssignment for a resource with a given ID. 046 * 047 * @param api the API connection to be used by the resource. 048 * @param id the ID of the resource. 049 */ 050 public BoxLegalHoldAssignment(BoxAPIConnection api, String id) { 051 super(api, id); 052 } 053 054 /** 055 * Creates new legal hold policy assignment. 056 * 057 * @param api the API connection to be used by the resource. 058 * @param policyID ID of policy to create assignment for. 059 * @param resourceType type of target resource. Can be 'file_version', 'file', 'folder', or 060 * 'user'. 061 * @param resourceID ID of the target resource. 062 * @return info about created legal hold policy assignment. 063 */ 064 public static BoxLegalHoldAssignment.Info create( 065 BoxAPIConnection api, String policyID, String resourceType, String resourceID) { 066 URL url = ASSIGNMENTS_URL_TEMPLATE.build(api.getBaseURL()); 067 BoxJSONRequest request = new BoxJSONRequest(api, url, "POST"); 068 069 JsonObject requestJSON = 070 new JsonObject() 071 .add("policy_id", policyID) 072 .add("assign_to", new JsonObject().add("type", resourceType).add("id", resourceID)); 073 request.setBody(requestJSON.toString()); 074 try (BoxJSONResponse response = request.send()) { 075 JsonObject responseJSON = Json.parse(response.getJSON()).asObject(); 076 BoxLegalHoldAssignment createdAssignment = 077 new BoxLegalHoldAssignment(api, responseJSON.get("id").asString()); 078 return createdAssignment.new Info(responseJSON); 079 } 080 } 081 082 /** Deletes the legal hold policy assignment. */ 083 public void delete() { 084 URL url = LEGAL_HOLD_ASSIGNMENT_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID()); 085 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "DELETE"); 086 request.send().close(); 087 } 088 089 /** 090 * @param fields the fields to retrieve. 091 * @return information about this retention policy. 092 */ 093 public BoxLegalHoldAssignment.Info getInfo(String... fields) { 094 QueryStringBuilder builder = new QueryStringBuilder(); 095 if (fields.length > 0) { 096 builder.appendParam("fields", fields); 097 } 098 URL url = 099 LEGAL_HOLD_ASSIGNMENT_URL_TEMPLATE.buildWithQuery( 100 this.getAPI().getBaseURL(), builder.toString(), this.getID()); 101 BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "GET"); 102 try (BoxJSONResponse response = request.send()) { 103 JsonObject responseJSON = Json.parse(response.getJSON()).asObject(); 104 return new Info(responseJSON); 105 } 106 } 107 108 /** Contains information about the legal hold policy. */ 109 public class Info extends BoxResource.Info { 110 111 /** @see #getLegalHold() */ 112 private BoxLegalHoldPolicy.Info legalHold; 113 114 /** @see #getAssignedBy() */ 115 private BoxUser.Info assignedBy; 116 117 /** @see #getAssignedAt() */ 118 private Date assignedAt; 119 120 /** @see #getDeletedAt() */ 121 private Date deletedAt; 122 123 /** @see #getAssignedToType() */ 124 private String assignedToType; 125 126 /** @see #getAssignedToID() */ 127 private String assignedToID; 128 129 /** Constructs an empty Info object. */ 130 public Info() { 131 super(); 132 } 133 134 /** 135 * Constructs an Info object by parsing information from a JSON string. 136 * 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 * 146 * @param jsonObject the parsed JSON object. 147 */ 148 Info(JsonObject jsonObject) { 149 super(jsonObject); 150 } 151 152 /** {@inheritDoc} */ 153 @Override 154 public BoxResource getResource() { 155 return BoxLegalHoldAssignment.this; 156 } 157 158 /** @return info about the policy that this legal hold policy assignment is part of. */ 159 public BoxLegalHoldPolicy.Info getLegalHold() { 160 return this.legalHold; 161 } 162 163 /** @return the info about the user who created that legal hold policy assignment. */ 164 public BoxUser.Info getAssignedBy() { 165 return this.assignedBy; 166 } 167 168 /** @return the time that the legal hold policy assignment was created. */ 169 public Date getAssignedAt() { 170 return this.assignedAt; 171 } 172 173 /** @return the time that the assignment release request was sent. */ 174 public Date getDeletedAt() { 175 return this.deletedAt; 176 } 177 178 /** @return the entity type that this is assigned to. */ 179 public String getAssignedToType() { 180 return this.assignedToType; 181 } 182 183 /** @return the entity id that this is assigned to. */ 184 public String getAssignedToID() { 185 return this.assignedToID; 186 } 187 188 /** {@inheritDoc} */ 189 @Override 190 void parseJSONMember(JsonObject.Member member) { 191 super.parseJSONMember(member); 192 String memberName = member.getName(); 193 JsonValue value = member.getValue(); 194 try { 195 if (memberName.equals("legal_hold_policy")) { 196 JsonObject policyJSON = value.asObject(); 197 if (this.legalHold == null) { 198 String policyID = policyJSON.get("id").asString(); 199 BoxLegalHoldPolicy policy = new BoxLegalHoldPolicy(getAPI(), policyID); 200 this.legalHold = policy.new Info(policyJSON); 201 } else { 202 this.legalHold.update(policyJSON); 203 } 204 } else if (memberName.equals("assigned_to")) { 205 JsonObject assignmentJSON = value.asObject(); 206 this.assignedToType = assignmentJSON.get("type").asString(); 207 this.assignedToID = assignmentJSON.get("id").asString(); 208 } else if (memberName.equals("assigned_by")) { 209 JsonObject userJSON = value.asObject(); 210 if (this.assignedBy == null) { 211 String userID = userJSON.get("id").asString(); 212 BoxUser user = new BoxUser(getAPI(), userID); 213 this.assignedBy = user.new Info(userJSON); 214 } else { 215 this.assignedBy.update(userJSON); 216 } 217 } else if (memberName.equals("assigned_at")) { 218 this.assignedAt = BoxDateFormat.parse(value.asString()); 219 } else if (memberName.equals("deleted_at")) { 220 this.deletedAt = BoxDateFormat.parse(value.asString()); 221 } 222 } catch (Exception e) { 223 throw new BoxDeserializationException(memberName, value.toString(), e); 224 } 225 } 226 } 227}