001package com.box.sdk; 002 003import java.net.URL; 004 005import com.box.sdk.http.HttpMethod; 006 007import com.eclipsesource.json.JsonObject; 008import com.eclipsesource.json.JsonValue; 009import com.eclipsesource.json.ParseException; 010 011/** 012 * Represents a BoxStoragePolicyAssignment. 013 */ 014@BoxResourceType("storage_policy_assignment") 015public class BoxStoragePolicyAssignment extends BoxResource { 016 017 /** 018 * Storage Policies Assignment URL Template. 019 */ 020 public static final URLTemplate STORAGE_POLICY_ASSIGNMENT_URL_TEMPLATE = new 021 URLTemplate("storage_policy_assignments"); 022 023 /** 024 * Storage Policy Assignment URL Template. 025 */ 026 public static final URLTemplate STORAGE_POLICY_ASSIGNMENT_WITH_ID_URL_TEMPLATE = new 027 URLTemplate("storage_policy_assignments/%s"); 028 029 /** 030 * Constructs a BoxStoragePolicyAssignment for a BoxStoragePolicy with a givenID. 031 * @param api the API connection to be used by the file. 032 * @param id the ID of the file. 033 */ 034 public BoxStoragePolicyAssignment(BoxAPIConnection api, String id) { 035 super(api, id); 036 } 037 038 /** 039 * Create a BoxStoragePolicyAssignment for a BoxStoragePolicy. 040 * @param api the API connection to be used by the resource. 041 * @param policyID the policy ID of the BoxStoragePolicy. 042 * @param userID the user ID of the to assign the BoxStoragePolicy to. 043 * @return the information about the BoxStoragePolicyAssignment created. 044 */ 045 public static BoxStoragePolicyAssignment.Info create(BoxAPIConnection api, String policyID, String userID) { 046 URL url = STORAGE_POLICY_ASSIGNMENT_URL_TEMPLATE.build(api.getBaseURL()); 047 BoxJSONRequest request = new BoxJSONRequest(api, url, HttpMethod.POST); 048 JsonObject requestJSON = new JsonObject() 049 .add("storage_policy", new JsonObject() 050 .add("type", "storage_policy") 051 .add("id", policyID)) 052 .add("assigned_to", new JsonObject() 053 .add("type", "user") 054 .add("id", userID)); 055 056 request.setBody(requestJSON.toString()); 057 BoxJSONResponse response = (BoxJSONResponse) request.send(); 058 JsonObject responseJSON = JsonObject.readFrom(response.getJSON()); 059 060 BoxStoragePolicyAssignment storagePolicyAssignment = new BoxStoragePolicyAssignment(api, 061 responseJSON.get("id").asString()); 062 063 return storagePolicyAssignment.new Info(responseJSON); 064 } 065 066 /** 067 * Updates the information about the BoxStoragePolicyAssignment with any info fields that have been 068 * modified locally. 069 * @param info the updated info. 070 */ 071 public void updateInfo(BoxStoragePolicyAssignment.Info info) { 072 URL url = STORAGE_POLICY_ASSIGNMENT_WITH_ID_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID()); 073 BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT"); 074 request.setBody(info.getPendingChanges()); 075 076 BoxJSONResponse response = (BoxJSONResponse) request.send(); 077 JsonObject responseJSON = JsonObject.readFrom(response.getJSON()); 078 info.update(responseJSON); 079 } 080 081 /** 082 * Returns a BoxStoragePolicyAssignment information. 083 * @param api the API connection to be used by the resource. 084 * @param resolvedForType the assigned entity type for the storage policy. 085 * @param resolvedForID the assigned entity id for the storage policy. 086 * @return information about this {@link BoxStoragePolicyAssignment}. 087 */ 088 public static BoxStoragePolicyAssignment.Info getAssignmentForTarget(final BoxAPIConnection api, 089 String resolvedForType, String resolvedForID) { 090 QueryStringBuilder builder = new QueryStringBuilder(); 091 builder.appendParam("resolved_for_type", resolvedForType) 092 .appendParam("resolved_for_id", resolvedForID); 093 URL url = STORAGE_POLICY_ASSIGNMENT_URL_TEMPLATE.buildWithQuery(api.getBaseURL(), builder.toString()); 094 BoxAPIRequest request = new BoxAPIRequest(api, url, HttpMethod.GET); 095 BoxJSONResponse response = (BoxJSONResponse) request.send(); 096 097 BoxStoragePolicyAssignment storagePolicyAssignment = new BoxStoragePolicyAssignment(api, 098 response.getJsonObject().get("entries").asArray().get(0).asObject().get("id").asString()); 099 BoxStoragePolicyAssignment.Info info = storagePolicyAssignment.new 100 Info(response.getJsonObject().get("entries").asArray().get(0).asObject()); 101 102 return info; 103 } 104 105 /** 106 * @return information about this {@link BoxStoragePolicyAssignment}. 107 */ 108 public BoxStoragePolicyAssignment.Info getInfo() { 109 URL url = STORAGE_POLICY_ASSIGNMENT_WITH_ID_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID()); 110 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, HttpMethod.GET); 111 BoxJSONResponse response = (BoxJSONResponse) request.send(); 112 113 return new Info(JsonObject.readFrom(response.getJSON())); 114 } 115 116 /** 117 * Deletes this BoxStoragePolicyAssignment. 118 */ 119 public void delete() { 120 URL url = STORAGE_POLICY_ASSIGNMENT_WITH_ID_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID()); 121 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, HttpMethod.DELETE); 122 123 request.send(); 124 } 125 126 /** 127 * Checks if there is already a Storage Policy Assignment and creates one if one does not exist. 128 * @param api the API connection to be used by the resource. 129 * @param storagePolicyID the ID of the Storage Policy you want to assign to user. 130 * @param userID the ID of the user you want to assign the Storage Policy to. 131 * @return information about this {@link BoxStoragePolicyAssignment}. 132 */ 133 public static BoxStoragePolicyAssignment.Info assign(BoxAPIConnection api, String storagePolicyID, String userID) { 134 BoxStoragePolicyAssignment.Info assignmentInfo = null; 135 assignmentInfo = getAssignmentForTarget(api, "user", userID); 136 137 if (assignmentInfo.getStoragePolicyID().equals(storagePolicyID)) { 138 return assignmentInfo; 139 } 140 141 if (assignmentInfo.getAssignedToType().equals("enterprise")) { 142 return create(api, storagePolicyID, userID); 143 } 144 145 assignmentInfo.setStoragePolicyID(storagePolicyID); 146 BoxStoragePolicyAssignment assignment = new BoxStoragePolicyAssignment(api, assignmentInfo.getID()); 147 assignment.updateInfo(assignmentInfo); 148 return assignmentInfo; 149 } 150 151 /** 152 * Contains information about a BoxStoragePolicyAssignment. 153 */ 154 public class Info extends BoxResource.Info { 155 156 /** 157 * @see #getStoragePolicyID() 158 */ 159 private String storagePolicyID; 160 161 /** 162 * @see #getStoragePolicyType() 163 */ 164 private String storagePolicyType; 165 166 /** 167 * @see #getAssignedToID() 168 */ 169 private String assignedToID; 170 171 /** 172 * @see #getAssignedToType() 173 */ 174 private String assignedToType; 175 176 /** 177 * Constructs an empty Info object. 178 */ 179 public Info() { 180 super(); 181 } 182 183 /** 184 * Constructs an Info object by parsing information from a JSON string. 185 * @param json the JSON string to parse. 186 */ 187 public Info(String json) { 188 super(json); 189 } 190 191 /** 192 * Constructs an Info object using an already parsed JSON object. 193 * @param jsonObject the parsed JSON object. 194 */ 195 Info(JsonObject jsonObject) { 196 super(jsonObject); 197 } 198 199 @Override 200 public BoxResource getResource() { 201 return BoxStoragePolicyAssignment.this; 202 } 203 204 /** 205 * @return the entity type that this is assigned to. 206 */ 207 public String getAssignedToType() { 208 return this.assignedToType; 209 } 210 211 /** 212 * @return the entity id that this is assigned to. 213 */ 214 public String getAssignedToID() { 215 return this.assignedToID; 216 } 217 218 /** 219 * @return storage policy id that is assigned to. 220 */ 221 public String getStoragePolicyID() { 222 return this.storagePolicyID; 223 } 224 225 /** 226 * @return storage policy type that is assigned to. 227 */ 228 public String getStoragePolicyType() { 229 return this.storagePolicyType; 230 } 231 232 /** 233 * Sets the storage policy of the storage policy assignment. 234 * @param storagePolicyID the Id of the storage policy you wish to assign. 235 */ 236 public void setStoragePolicyID(String storagePolicyID) { 237 this.storagePolicyID = storagePolicyID; 238 JsonObject storagePolicyObject = new JsonObject(); 239 storagePolicyObject.add("type", "storage_policy"); 240 storagePolicyObject.add("id", storagePolicyID); 241 242 this.addPendingChange("storage_policy", storagePolicyObject); 243 } 244 245 /** 246 * {@inheritDoc} 247 */ 248 @Override 249 void parseJSONMember(JsonObject.Member member) { 250 super.parseJSONMember(member); 251 String memberName = member.getName(); 252 JsonValue value = member.getValue(); 253 try { 254 if (memberName.equals("assigned_to")) { 255 JsonObject assignmentJSON = value.asObject(); 256 this.assignedToType = assignmentJSON.get("type").asString(); 257 this.assignedToID = assignmentJSON.get("id").asString(); 258 } else if (memberName.equals("storage_policy")) { 259 JsonObject storagePolicyJSON = value.asObject(); 260 this.storagePolicyID = storagePolicyJSON.get("id").asString(); 261 this.storagePolicyType = storagePolicyJSON.get("type").asString(); 262 } 263 } catch (ParseException e) { 264 assert false : "A ParseException indicates a bug in the SDK."; 265 } 266 } 267 } 268}