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