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