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