001package com.box.sdk; 002 003import java.net.URL; 004 005import com.eclipsesource.json.JsonObject; 006import com.eclipsesource.json.JsonValue; 007 008/** 009 * Represents a Metadata Cascade Policy. 010 */ 011@BoxResourceType("metadata_cascade_policy") 012public class BoxMetadataCascadePolicy extends BoxResource { 013 014 /** 015 * Get All Metadata Cascade Policies URL. 016 */ 017 public static final URLTemplate GET_ALL_METADATA_CASCADE_POLICIES_URL_TEMPLATE = 018 new URLTemplate("metadata_cascade_policies"); 019 020 /** 021 * Metadata Cascade Policies URL. 022 */ 023 public static final URLTemplate METADATA_CASCADE_POLICIES_URL_TEMPLATE = 024 new URLTemplate("metadata_cascade_policies/%s"); 025 026 /** 027 * Force Metadata Cascade Policies URL. 028 */ 029 public static final URLTemplate FORCE_METADATA_CASCADE_POLICIES_URL_TEMPLATE = 030 new URLTemplate("metadata_cascade_policies/%s/apply"); 031 032 private static final int DEFAULT_LIMIT = 100; 033 034 /** 035 * Constructs a BoxMetadataCascadePolicy for a metadata cascade policy with a given ID. 036 * 037 * @param api the API connection used to make the request. 038 * @param id the ID of the metadata cascade policy. 039 */ 040 public BoxMetadataCascadePolicy(BoxAPIConnection api, String id) { 041 super(api, id); 042 } 043 044 /** 045 * Retrieves list of Box Metadata Cascade Policies that belong to your Enterprise as an Iterable. 046 * 047 * @param api the API connection to be used by the resource. 048 * @param folderID the ID of the folder to retrieve cascade policies for. 049 * @param fields optional fields to retrieve for cascade policies. 050 * @return the Iterable of Box Metadata Cascade Policies in your enterprise. 051 */ 052 public static Iterable<BoxMetadataCascadePolicy.Info> getAll(final BoxAPIConnection api, 053 String folderID, String ... fields) { 054 return getAll(api, folderID, null, DEFAULT_LIMIT, fields); 055 } 056 057 /** 058 * Retrieves list of Box Metadata Cascade Policies that belong to your Enterprise as an Iterable. 059 * 060 * @param api the API connection to be used by the resource. 061 * @param folderID the ID of the folder to retrieve cascade policies for. 062 * @param ownerEnterpriseID the ID of the enterprise to retrieve Metadata Cascade Policies for. 063 * @param limit the number of entries for cascade policies to retrieve. 064 * @param fields optional fields to retrieve for cascade policies. 065 * @return the Iterable of Box Metadata Cascade Policies in your enterprise. 066 */ 067 public static Iterable<BoxMetadataCascadePolicy.Info> getAll(final BoxAPIConnection api, 068 String folderID, String ownerEnterpriseID, int limit, 069 String... fields) { 070 071 QueryStringBuilder builder = new QueryStringBuilder(); 072 builder.appendParam("folder_id", folderID); 073 if (ownerEnterpriseID != null) { 074 builder.appendParam("owner_enterprise_id", ownerEnterpriseID); 075 } 076 if (fields.length > 0) { 077 builder.appendParam("fields", fields); 078 } 079 return new BoxResourceIterable<Info>(api, GET_ALL_METADATA_CASCADE_POLICIES_URL_TEMPLATE 080 .buildWithQuery(api.getBaseURL(), builder.toString()), limit) { 081 @Override 082 protected BoxMetadataCascadePolicy.Info factory(JsonObject jsonObject) { 083 BoxMetadataCascadePolicy cascadePolicy = 084 new BoxMetadataCascadePolicy(api, jsonObject.get("id").asString()); 085 086 return cascadePolicy.new Info(jsonObject); 087 } 088 }; 089 } 090 091 /** 092 * Returns the information for a specific BoxMetadataCascadePolicy. 093 * 094 * @param fields the fields to retrieve. 095 * @return the information about this metadata cascade policy. 096 */ 097 public BoxMetadataCascadePolicy.Info getInfo(String... fields) { 098 QueryStringBuilder builder = new QueryStringBuilder(); 099 if (fields.length > 0) { 100 builder.appendParam("fields", fields); 101 } 102 URL url = METADATA_CASCADE_POLICIES_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), 103 builder.toString(), this.getID()); 104 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET"); 105 BoxJSONResponse response = (BoxJSONResponse) request.send(); 106 JsonObject responseJSON = JsonObject.readFrom(response.getJSON()); 107 return new Info(responseJSON); 108 } 109 110 /** 111 * Creates a new Metadata Cascade Policy on a folder. 112 * 113 * @param api the API connection to be used by the resource. 114 * @param folderID the ID of the folder to create a metadata cascade policy on. 115 * @param scope the scope of the metadata cascade policy. 116 * @param templateKey the key of the template. 117 * @return information about the Metadata Cascade Policy. 118 */ 119 public static BoxMetadataCascadePolicy.Info create(final BoxAPIConnection api, String folderID, String scope, 120 String templateKey) { 121 URL url = GET_ALL_METADATA_CASCADE_POLICIES_URL_TEMPLATE.build(api.getBaseURL()); 122 BoxJSONRequest request = new BoxJSONRequest(api, url, "POST"); 123 JsonObject requestJSON = new JsonObject() 124 .add("folder_id", folderID) 125 .add("scope", scope) 126 .add("templateKey", templateKey); 127 request.setBody(requestJSON.toString()); 128 BoxJSONResponse response = (BoxJSONResponse) request.send(); 129 JsonObject responseJSON = JsonObject.readFrom(response.getJSON()); 130 BoxMetadataCascadePolicy createdMetadataCascadePolicy = new BoxMetadataCascadePolicy(api, 131 responseJSON.get("id").asString()); 132 return createdMetadataCascadePolicy.new Info(responseJSON); 133 } 134 135 /** 136 * If a policy already exists on a folder, this will apply that policy to all existing files and sub folders within 137 * the target folder. 138 * 139 * @param conflictResolution the desired behavior for conflict-resolution. Set to either none or overwrite. 140 */ 141 public void forceApply(String conflictResolution) { 142 143 URL url = FORCE_METADATA_CASCADE_POLICIES_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID()); 144 BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "POST"); 145 JsonObject requestJSON = new JsonObject() 146 .add("conflict_resolution", conflictResolution); 147 request.setBody(requestJSON.toString()); 148 request.send(); 149 } 150 151 /** 152 * Deletes the metadata cascade policy. 153 */ 154 public void delete() { 155 URL url = METADATA_CASCADE_POLICIES_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID()); 156 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "DELETE"); 157 BoxAPIResponse response = request.send(); 158 } 159 160 /** 161 * Contains information about a BoxMetadataCascadePolicy. 162 */ 163 public class Info extends BoxResource.Info { 164 private BoxEnterprise ownerEnterprise; 165 private BoxFolder.Info parent; 166 private String scope; 167 private String templateKey; 168 169 /** 170 * Constructs an empty Info object. 171 */ 172 public Info() { 173 super(); 174 } 175 176 /** 177 * Constructs an Info object by parsing information from a JSON string. 178 * 179 * @param json the JSON string to parse. 180 */ 181 public Info(String json) { 182 super(json); 183 } 184 185 Info(JsonObject jsonObject) { 186 super(jsonObject); 187 } 188 189 /** 190 * Gets the enterprise the metadata cascade policy belongs to. 191 * 192 * @return the enterprise the metadata cascade policy belongs to. 193 */ 194 public BoxEnterprise getOwnerEnterprise() { 195 return this.ownerEnterprise; 196 } 197 198 /** 199 * Gets the folder the metadata cascade policy is on. 200 * 201 * @return the folder parent of the metadata cascade policy. 202 */ 203 public BoxFolder.Info getParent() { 204 return this.parent; 205 } 206 207 /** 208 * Gets the scope of the metadata cascade policy. 209 * 210 * @return the scope of the metadata cascade policy. 211 */ 212 public String getScope() { 213 return this.scope; 214 } 215 216 /** 217 * Gets the template key for the metadata cascade policy. 218 * 219 * @return the template key for the metadata cascade policy. 220 */ 221 public String getTemplateKey() { 222 return this.templateKey; 223 } 224 225 @Override 226 public BoxMetadataCascadePolicy getResource() { 227 return BoxMetadataCascadePolicy.this; 228 } 229 230 @Override 231 protected void parseJSONMember(JsonObject.Member member) { 232 super.parseJSONMember(member); 233 String memberName = member.getName(); 234 JsonValue value = member.getValue(); 235 try { 236 if (memberName.equals("owner_enterprise")) { 237 JsonObject jsonObject = value.asObject(); 238 this.ownerEnterprise = new BoxEnterprise(jsonObject); 239 } else if (memberName.equals("parent")) { 240 JsonObject parentJSON = value.asObject(); 241 if (this.parent == null) { 242 String parentID = parentJSON.get("id").asString(); 243 BoxFolder folder = new BoxFolder(getAPI(), parentID); 244 this.parent = folder.new Info(parentJSON); 245 } else { 246 this.parent.update(parentJSON); 247 } 248 } else if (memberName.equals("scope")) { 249 this.scope = value.asString(); 250 } else if (memberName.equals("templateKey")) { 251 this.templateKey = value.asString(); 252 } 253 } catch (Exception e) { 254 throw new BoxDeserializationException(memberName, value.toString(), e); 255 } 256 } 257 } 258}