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/**
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     * Creates a new Metadata Cascade Policy on a folder.
093     *
094     * @param api         the API connection to be used by the resource.
095     * @param folderID    the ID of the folder to create a metadata cascade policy on.
096     * @param scope       the scope of the metadata cascade policy.
097     * @param templateKey the key of the template.
098     * @return information about the Metadata Cascade Policy.
099     */
100    public static BoxMetadataCascadePolicy.Info create(final BoxAPIConnection api, String folderID, String scope,
101                                                       String templateKey) {
102        URL url = GET_ALL_METADATA_CASCADE_POLICIES_URL_TEMPLATE.build(api.getBaseURL());
103        BoxJSONRequest request = new BoxJSONRequest(api, url, "POST");
104        JsonObject requestJSON = new JsonObject()
105            .add("folder_id", folderID)
106            .add("scope", scope)
107            .add("templateKey", templateKey);
108        request.setBody(requestJSON.toString());
109        try (BoxJSONResponse response = request.send()) {
110            JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
111            BoxMetadataCascadePolicy createdMetadataCascadePolicy = new BoxMetadataCascadePolicy(api,
112                responseJSON.get("id").asString());
113            return createdMetadataCascadePolicy.new Info(responseJSON);
114        }
115    }
116
117    /**
118     * Returns the information for a specific BoxMetadataCascadePolicy.
119     *
120     * @param fields the fields to retrieve.
121     * @return the information about this metadata cascade policy.
122     */
123    public BoxMetadataCascadePolicy.Info getInfo(String... fields) {
124        QueryStringBuilder builder = new QueryStringBuilder();
125        if (fields.length > 0) {
126            builder.appendParam("fields", fields);
127        }
128        URL url = METADATA_CASCADE_POLICIES_URL_TEMPLATE.buildAlphaWithQuery(this.getAPI().getBaseURL(),
129            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 sub folders within
139     * the target folder.
140     *
141     * @param conflictResolution the desired behavior for conflict-resolution. Set to either none or overwrite.
142     */
143    public void forceApply(String conflictResolution) {
144
145        URL url = FORCE_METADATA_CASCADE_POLICIES_URL_TEMPLATE.buildAlpha(this.getAPI().getBaseURL(), this.getID());
146        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "POST");
147        JsonObject requestJSON = new JsonObject()
148            .add("conflict_resolution", conflictResolution);
149        request.setBody(requestJSON.toString());
150        request.send().close();
151    }
152
153    /**
154     * Deletes the metadata cascade policy.
155     */
156    public void delete() {
157        URL url = METADATA_CASCADE_POLICIES_URL_TEMPLATE.buildAlpha(this.getAPI().getBaseURL(), this.getID());
158        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "DELETE");
159        request.send().close();
160    }
161
162    /**
163     * Contains information about a BoxMetadataCascadePolicy.
164     */
165    public class Info extends BoxResource.Info {
166        private BoxEnterprise ownerEnterprise;
167        private BoxFolder.Info parent;
168        private String scope;
169        private String templateKey;
170
171        /**
172         * Constructs an empty Info object.
173         */
174        public Info() {
175            super();
176        }
177
178        /**
179         * Constructs an Info object by parsing information from a JSON string.
180         *
181         * @param json the JSON string to parse.
182         */
183        public Info(String json) {
184            super(json);
185        }
186
187        Info(JsonObject jsonObject) {
188            super(jsonObject);
189        }
190
191        /**
192         * Gets the enterprise the metadata cascade policy belongs to.
193         *
194         * @return the enterprise the metadata cascade policy belongs to.
195         */
196        public BoxEnterprise getOwnerEnterprise() {
197            return this.ownerEnterprise;
198        }
199
200        /**
201         * Gets the folder the metadata cascade policy is on.
202         *
203         * @return the folder parent of the metadata cascade policy.
204         */
205        public BoxFolder.Info getParent() {
206            return this.parent;
207        }
208
209        /**
210         * Gets the scope of the metadata cascade policy.
211         *
212         * @return the scope of the metadata cascade policy.
213         */
214        public String getScope() {
215            return this.scope;
216        }
217
218        /**
219         * Gets the template key for the metadata cascade policy.
220         *
221         * @return the template key for the metadata cascade policy.
222         */
223        public String getTemplateKey() {
224            return this.templateKey;
225        }
226
227        @Override
228        public BoxMetadataCascadePolicy getResource() {
229            return BoxMetadataCascadePolicy.this;
230        }
231
232        @Override
233        protected void parseJSONMember(JsonObject.Member member) {
234            super.parseJSONMember(member);
235            String memberName = member.getName();
236            JsonValue value = member.getValue();
237            try {
238                if (memberName.equals("owner_enterprise")) {
239                    JsonObject jsonObject = value.asObject();
240                    this.ownerEnterprise = new BoxEnterprise(jsonObject);
241                } else if (memberName.equals("parent")) {
242                    JsonObject parentJSON = value.asObject();
243                    if (this.parent == null) {
244                        String parentID = parentJSON.get("id").asString();
245                        BoxFolder folder = new BoxFolder(getAPI(), parentID);
246                        this.parent = folder.new Info(parentJSON);
247                    } else {
248                        this.parent.update(parentJSON);
249                    }
250                } else if (memberName.equals("scope")) {
251                    this.scope = value.asString();
252                } else if (memberName.equals("templateKey")) {
253                    this.templateKey = value.asString();
254                }
255            } catch (Exception e) {
256                throw new BoxDeserializationException(memberName, value.toString(), e);
257            }
258        }
259    }
260}