001package com.box.sdk;
002
003import com.eclipsesource.json.JsonObject;
004import com.eclipsesource.json.JsonValue;
005import java.net.URL;
006
007/**
008 * Represents a Metadata Cascade Policy.
009 */
010@BoxResourceType("metadata_cascade_policy")
011public class BoxMetadataCascadePolicy extends BoxResource {
012
013    /**
014     * Get All Metadata Cascade Policies URL.
015     */
016    public static final URLTemplate GET_ALL_METADATA_CASCADE_POLICIES_URL_TEMPLATE =
017        new URLTemplate("metadata_cascade_policies");
018
019    /**
020     * Metadata Cascade Policies URL.
021     */
022    public static final URLTemplate METADATA_CASCADE_POLICIES_URL_TEMPLATE =
023        new URLTemplate("metadata_cascade_policies/%s");
024
025    /**
026     * Force Metadata Cascade Policies URL.
027     */
028    public static final URLTemplate FORCE_METADATA_CASCADE_POLICIES_URL_TEMPLATE =
029        new URLTemplate("metadata_cascade_policies/%s/apply");
030
031    private static final int DEFAULT_LIMIT = 100;
032
033    /**
034     * Constructs a BoxMetadataCascadePolicy for a metadata cascade policy with a given ID.
035     *
036     * @param api the API connection used to make the request.
037     * @param id  the ID of the metadata cascade policy.
038     */
039    public BoxMetadataCascadePolicy(BoxAPIConnection api, String id) {
040        super(api, id);
041    }
042
043    /**
044     * Retrieves list of Box Metadata Cascade Policies that belong to your Enterprise as an Iterable.
045     *
046     * @param api      the API connection to be used by the resource.
047     * @param folderID the ID of the folder to retrieve cascade policies for.
048     * @param fields   optional fields to retrieve for cascade policies.
049     * @return the Iterable of Box Metadata Cascade Policies in your enterprise.
050     */
051    public static Iterable<BoxMetadataCascadePolicy.Info> getAll(final BoxAPIConnection api,
052                                                                 String folderID, String... fields) {
053        return getAll(api, folderID, null, DEFAULT_LIMIT, fields);
054    }
055
056    /**
057     * Retrieves list of Box Metadata Cascade Policies that belong to your Enterprise as an Iterable.
058     *
059     * @param api               the API connection to be used by the resource.
060     * @param folderID          the ID of the folder to retrieve cascade policies for.
061     * @param ownerEnterpriseID the ID of the enterprise to retrieve Metadata Cascade Policies for.
062     * @param limit             the number of entries for cascade policies to retrieve.
063     * @param fields            optional fields to retrieve for cascade policies.
064     * @return the Iterable of Box Metadata Cascade Policies in your enterprise.
065     */
066    public static Iterable<BoxMetadataCascadePolicy.Info> getAll(final BoxAPIConnection api,
067                                                                 String folderID, String ownerEnterpriseID, int limit,
068                                                                 String... fields) {
069
070        QueryStringBuilder builder = new QueryStringBuilder();
071        builder.appendParam("folder_id", folderID);
072        if (ownerEnterpriseID != null) {
073            builder.appendParam("owner_enterprise_id", ownerEnterpriseID);
074        }
075        if (fields.length > 0) {
076            builder.appendParam("fields", fields);
077        }
078        return new BoxResourceIterable<Info>(api, GET_ALL_METADATA_CASCADE_POLICIES_URL_TEMPLATE
079            .buildWithQuery(api.getBaseURL(), builder.toString()), limit) {
080            @Override
081            protected BoxMetadataCascadePolicy.Info factory(JsonObject jsonObject) {
082                BoxMetadataCascadePolicy cascadePolicy =
083                    new BoxMetadataCascadePolicy(api, jsonObject.get("id").asString());
084
085                return cascadePolicy.new Info(jsonObject);
086            }
087        };
088    }
089
090    /**
091     * Creates a new Metadata Cascade Policy on a folder.
092     *
093     * @param api         the API connection to be used by the resource.
094     * @param folderID    the ID of the folder to create a metadata cascade policy on.
095     * @param scope       the scope of the metadata cascade policy.
096     * @param templateKey the key of the template.
097     * @return information about the Metadata Cascade Policy.
098     */
099    public static BoxMetadataCascadePolicy.Info create(final BoxAPIConnection api, String folderID, String scope,
100                                                       String templateKey) {
101        URL url = GET_ALL_METADATA_CASCADE_POLICIES_URL_TEMPLATE.build(api.getBaseURL());
102        BoxJSONRequest request = new BoxJSONRequest(api, url, "POST");
103        JsonObject requestJSON = new JsonObject()
104            .add("folder_id", folderID)
105            .add("scope", scope)
106            .add("templateKey", templateKey);
107        request.setBody(requestJSON.toString());
108        BoxJSONResponse response = (BoxJSONResponse) request.send();
109        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
110        BoxMetadataCascadePolicy createdMetadataCascadePolicy = new BoxMetadataCascadePolicy(api,
111            responseJSON.get("id").asString());
112        return createdMetadataCascadePolicy.new Info(responseJSON);
113    }
114
115    /**
116     * Returns the information for a specific BoxMetadataCascadePolicy.
117     *
118     * @param fields the fields to retrieve.
119     * @return the information about this metadata cascade policy.
120     */
121    public BoxMetadataCascadePolicy.Info getInfo(String... fields) {
122        QueryStringBuilder builder = new QueryStringBuilder();
123        if (fields.length > 0) {
124            builder.appendParam("fields", fields);
125        }
126        URL url = METADATA_CASCADE_POLICIES_URL_TEMPLATE.buildAlphaWithQuery(this.getAPI().getBaseURL(),
127            builder.toString(), this.getID());
128        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
129        BoxJSONResponse response = (BoxJSONResponse) request.send();
130        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
131        return new Info(responseJSON);
132    }
133
134    /**
135     * If a policy already exists on a folder, this will apply that policy to all existing files and sub folders within
136     * the target folder.
137     *
138     * @param conflictResolution the desired behavior for conflict-resolution. Set to either none or overwrite.
139     */
140    public void forceApply(String conflictResolution) {
141
142        URL url = FORCE_METADATA_CASCADE_POLICIES_URL_TEMPLATE.buildAlpha(this.getAPI().getBaseURL(), this.getID());
143        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "POST");
144        JsonObject requestJSON = new JsonObject()
145            .add("conflict_resolution", conflictResolution);
146        request.setBody(requestJSON.toString());
147        request.send();
148    }
149
150    /**
151     * Deletes the metadata cascade policy.
152     */
153    public void delete() {
154        URL url = METADATA_CASCADE_POLICIES_URL_TEMPLATE.buildAlpha(this.getAPI().getBaseURL(), this.getID());
155        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "DELETE");
156        BoxAPIResponse response = request.send();
157    }
158
159    /**
160     * Contains information about a BoxMetadataCascadePolicy.
161     */
162    public class Info extends BoxResource.Info {
163        private BoxEnterprise ownerEnterprise;
164        private BoxFolder.Info parent;
165        private String scope;
166        private String templateKey;
167
168        /**
169         * Constructs an empty Info object.
170         */
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}