001package com.box.sdk;
002
003import com.eclipsesource.json.JsonObject;
004import com.eclipsesource.json.JsonValue;
005import java.net.URL;
006import java.util.Date;
007
008/**
009 * Represents a file version retention.
010 * A retention policy blocks permanent deletion of content for a specified amount of time.
011 * Admins can apply policies to specified folders, or an entire enterprise.
012 * A file version retention is a record for a retained file version.
013 *
014 * @see <a href="https://developer.box.com/reference/resources/file-version-retention/">Box file version retention</a>
015 *
016 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link BoxAPIException} (unchecked
017 * meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error
018 * handling for errors related to the Box REST API, you should capture this exception explicitly.</p>
019 */
020@BoxResourceType("file_version_retention")
021public class BoxFileVersionRetention extends BoxResource {
022
023    /**
024     * @see #getInfo(String...)
025     */
026    public static final URLTemplate RETENTION_URL_TEMPLATE = new URLTemplate("file_version_retentions/%s");
027
028    /**
029     * The URL template used for operation with file version retentions.
030     */
031    public static final URLTemplate ALL_RETENTIONS_URL_TEMPLATE = new URLTemplate("file_version_retentions");
032
033    /**
034     * The default limit of entries per response.
035     */
036    private static final int DEFAULT_LIMIT = 100;
037
038    /**
039     * Constructs a BoxFileVersionRetention for a resource with a given ID.
040     *
041     * @param api the API connection to be used by the resource.
042     * @param id  the ID of the resource.
043     */
044    public BoxFileVersionRetention(BoxAPIConnection api, String id) {
045        super(api, id);
046    }
047
048    /**
049     * Retrieves all file version retentions.
050     *
051     * @param api    the API connection to be used by the resource.
052     * @param fields the fields to retrieve.
053     * @return an iterable contains information about all file version retentions.
054     */
055    public static Iterable<BoxFileVersionRetention.Info> getAll(BoxAPIConnection api, String... fields) {
056        return getRetentions(api, new QueryFilter(), fields);
057    }
058
059    /**
060     * @param api    the API connection to be used by the resource.
061     * @param filter filters for the query stored in QueryFilter object.
062     * @param fields the fields to retrieve.
063     * @return an iterable contains information about all file version retentions matching given filter.
064     * @deprecated This method will be deprecated in the future. Please use
065     * BoxRetentionPolicyAssignment.getFilesUnderRetentionForAssignmentAsync(...)
066     * and BoxRetentionPolicyAssignment.getFileVersionsUnderRetentionForAssignmentAsync(...) instead."
067     * Retrieves all file version retentions matching given filters as an Iterable.
068     */
069    public static Iterable<BoxFileVersionRetention.Info> getRetentions(
070        final BoxAPIConnection api, QueryFilter filter, String... fields) {
071        filter.addFields(fields);
072        return new BoxResourceIterable<BoxFileVersionRetention.Info>(api,
073            ALL_RETENTIONS_URL_TEMPLATE.buildWithQuery(api.getBaseURL(), filter.toString()),
074            DEFAULT_LIMIT) {
075
076            @Override
077            protected BoxFileVersionRetention.Info factory(JsonObject jsonObject) {
078                BoxFileVersionRetention retention = new BoxFileVersionRetention(api, jsonObject.get("id").asString());
079                return retention.new Info(jsonObject);
080            }
081        };
082    }
083
084    /**
085     * @param fields the fields to retrieve.
086     * @return information about this retention policy.
087     */
088    public BoxFileVersionRetention.Info getInfo(String... fields) {
089        QueryStringBuilder builder = new QueryStringBuilder();
090        if (fields.length > 0) {
091            builder.appendParam("fields", fields);
092        }
093        URL url = RETENTION_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), builder.toString(), this.getID());
094        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
095        BoxJSONResponse response = (BoxJSONResponse) request.send();
096        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
097        return new Info(responseJSON);
098    }
099
100    /**
101     * Represents possible query filters for "Get File Version Retentions" function.
102     */
103    public static class QueryFilter extends QueryStringBuilder {
104
105        /**
106         * Param name for the file id to filter the file version retentions by.
107         */
108        private static final String PARAM_FILE_ID = "file_id";
109
110        /**
111         * Param name for the file version id to filter the file version retentions by.
112         */
113        private static final String PARAM_FILE_VERSION_ID = "file_version_id";
114
115        /**
116         * Param name for the policy id to filter the file version retentions by.
117         */
118        private static final String PARAM_POLICY_ID = "policy_id";
119
120        /**
121         * Param name for the disposition action of the retention policy.
122         */
123        private static final String PARAM_DISPOSITION_ACTION = "disposition_action";
124
125        /**
126         * Param name for the datetime to filter file version retentions.
127         */
128        private static final String PARAM_DISPOSITION_BEFORE = "disposition_before";
129
130        /**
131         * Param name for the datetime to filter file version retentions.
132         */
133        private static final String PARAM_DISPOSITION_AFTER = "disposition_after";
134
135        /**
136         * Param name for the the fields to retrieve.
137         */
138        private static final String PARAM_FIELDS = "fields";
139
140        /**
141         * Constructs empty query filter.
142         */
143        public QueryFilter() {
144            super();
145        }
146
147        /**
148         * @param id a file id to filter the file version retentions by.
149         * @return modified query filter.
150         */
151        public QueryFilter addFileID(String id) {
152            this.appendParam(PARAM_FILE_ID, id);
153            return this;
154        }
155
156        /**
157         * @param id a file version id to filter the file version retentions by.
158         * @return modified query filter.
159         */
160        public QueryFilter addFileVersionID(String id) {
161            this.appendParam(PARAM_FILE_VERSION_ID, id);
162            return this;
163        }
164
165        /**
166         * @param id a policy id to filter the file version retentions by.
167         * @return modified query filter.
168         */
169        public QueryFilter addPolicyID(String id) {
170            this.appendParam(PARAM_POLICY_ID, id);
171            return this;
172        }
173
174        /**
175         * The action can be "permanently_delete" or "remove_retention".
176         *
177         * @param action the disposition action of the retention policy.
178         * @return modified query filter.
179         */
180        public QueryFilter addDispositionAction(String action) {
181            this.appendParam(PARAM_DISPOSITION_ACTION, action);
182            return this;
183        }
184
185        /**
186         * @param date the datetime to filter file version retentions.
187         * @return modified query filter.
188         */
189        public QueryFilter addDispositionBefore(Date date) {
190            this.appendParam(PARAM_DISPOSITION_BEFORE, BoxDateFormat.format(date));
191            return this;
192        }
193
194        /**
195         * @param date the datetime to filter file version retentions.
196         * @return modified query filter.
197         */
198        public QueryFilter addDispositionAfter(Date date) {
199            this.appendParam(PARAM_DISPOSITION_AFTER, BoxDateFormat.format(date));
200            return this;
201        }
202
203        /**
204         * @param fields the fields to retrieve.
205         * @return modified query filter.
206         */
207        public QueryFilter addFields(String... fields) {
208            if (fields.length > 0) {
209                this.appendParam(PARAM_FIELDS, fields);
210            }
211            return this;
212        }
213    }
214
215    /**
216     * Contains information about the retention policy.
217     */
218    public class Info extends BoxResource.Info {
219
220        /**
221         * @see #getFileVersion()
222         */
223        private BoxFileVersion fileVersion;
224
225        /**
226         * @see #getFile()
227         */
228        private BoxFile.Info file;
229
230        /**
231         * @see #getAppliedAt()
232         */
233        private Date appliedAt;
234
235        /**
236         * @see #getDispositionAt()
237         */
238        private Date dispositionAt;
239
240        /**
241         * @see #getWinningPolicy()
242         */
243        private BoxRetentionPolicy.Info winningPolicy;
244
245        /**
246         * Constructs an empty Info object.
247         */
248        public Info() {
249            super();
250        }
251
252        /**
253         * Constructs an Info object by parsing information from a JSON string.
254         *
255         * @param json the JSON string to parse.
256         */
257        public Info(String json) {
258            super(json);
259        }
260
261        /**
262         * Constructs an Info object using an already parsed JSON object.
263         *
264         * @param jsonObject the parsed JSON object.
265         */
266        Info(JsonObject jsonObject) {
267            super(jsonObject);
268        }
269
270        /**
271         * {@inheritDoc}
272         */
273        @Override
274        public BoxResource getResource() {
275            return BoxFileVersionRetention.this;
276        }
277
278        /**
279         * @return the file version this file version retention was applied to.
280         */
281        public BoxFileVersion getFileVersion() {
282            return this.fileVersion;
283        }
284
285        /**
286         * @return the file this file version retention was applied to.
287         */
288        public BoxFile.Info getFile() {
289            return this.file;
290        }
291
292        /**
293         * @return the time that this file version retention was created.
294         */
295        public Date getAppliedAt() {
296            return this.appliedAt;
297        }
298
299        /**
300         * @return the time that the retention period expires on this file version retention.
301         */
302        public Date getDispositionAt() {
303            return this.dispositionAt;
304        }
305
306        /**
307         * @return the winning retention policy applied to this file version retention.
308         */
309        public BoxRetentionPolicy.Info getWinningPolicy() {
310            return this.winningPolicy;
311        }
312
313        /**
314         * {@inheritDoc}
315         */
316        @Override
317        void parseJSONMember(JsonObject.Member member) {
318            super.parseJSONMember(member);
319            String memberName = member.getName();
320            JsonValue value = member.getValue();
321            try {
322                if (memberName.equals("winning_retention_policy")) {
323                    JsonObject policyJSON = value.asObject();
324                    if (this.winningPolicy == null) {
325                        String policyID = policyJSON.get("id").asString();
326                        BoxRetentionPolicy policy = new BoxRetentionPolicy(getAPI(), policyID);
327                        this.winningPolicy = policy.new Info(policyJSON);
328                    } else {
329                        this.winningPolicy.update(policyJSON);
330                    }
331                } else if (memberName.equals("file")) {
332                    JsonObject fileJSON = value.asObject();
333                    if (this.file == null) {
334                        String fileID = fileJSON.get("id").asString();
335                        BoxFile file = new BoxFile(getAPI(), fileID);
336                        this.file = file.new Info(fileJSON);
337                    } else {
338                        this.file.update(fileJSON);
339                    }
340                } else if (memberName.equals("file_version")) {
341                    JsonObject versionJSON = value.asObject();
342                    String fileVersionID = versionJSON.get("id").asString();
343                    this.fileVersion = new BoxFileVersion(getAPI(), versionJSON, fileVersionID);
344                } else if (memberName.equals("applied_at")) {
345                    this.appliedAt = BoxDateFormat.parse(value.asString());
346                } else if (memberName.equals("disposition_at")) {
347                    this.dispositionAt = BoxDateFormat.parse(value.asString());
348                }
349            } catch (Exception e) {
350                throw new BoxDeserializationException(memberName, value.toString(), e);
351            }
352        }
353    }
354}