001package com.box.sdk;
002
003import com.eclipsesource.json.Json;
004import com.eclipsesource.json.JsonArray;
005import com.eclipsesource.json.JsonObject;
006import com.eclipsesource.json.JsonValue;
007import java.net.URL;
008import java.util.ArrayList;
009import java.util.Date;
010import java.util.List;
011
012/**
013 * Represents a retention policy.
014 * A retention policy blocks permanent deletion of content for a specified amount of time.
015 * Admins can create retention policies and then later assign them to specific folders or their entire enterprise.
016 *
017 * @see <a href="https://developer.box.com/reference/resources/retention-policy/">Box retention policy</a>
018 *
019 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link BoxAPIException} (unchecked
020 * meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error
021 * handling for errors related to the Box REST API, you should capture this exception explicitly.</p>
022 */
023@BoxResourceType("retention_policy")
024public class BoxRetentionPolicy extends BoxResource {
025    /**
026     * The URL template used for operation with retention policies.
027     */
028    public static final URLTemplate RETENTION_POLICIES_URL_TEMPLATE = new URLTemplate("retention_policies");
029
030    /**
031     * The URL template used for operation with retention policy with given ID.
032     */
033    public static final URLTemplate POLICY_URL_TEMPLATE = new URLTemplate("retention_policies/%s");
034
035    /**
036     * The URL template used for operation with retention policy assignments.
037     */
038    public static final URLTemplate ASSIGNMENTS_URL_TEMPLATE = new URLTemplate("retention_policies/%s/assignments");
039
040    /**
041     * Will cause the content retained by the policy to be permanently deleted.
042     */
043    public static final String ACTION_PERMANENTLY_DELETE = "permanently_delete";
044
045    /**
046     * Will lift the retention policy from the content, allowing it to be deleted by users.
047     */
048    public static final String ACTION_REMOVE_RETENTION = "remove_retention";
049
050    /**
051     * Status corresponding to active retention policy.
052     */
053    public static final String STATUS_ACTIVE = "active";
054
055    /**
056     * Status corresponding to retired retention policy.
057     */
058    public static final String STATUS_RETIRED = "retired";
059
060    /**
061     * The default limit of entries per response.
062     */
063    private static final int DEFAULT_LIMIT = 100;
064
065    /**
066     * Constructs a retention policy for a resource with a given ID.
067     *
068     * @param api the API connection to be used by the resource.
069     * @param id  the ID of the resource.
070     */
071    public BoxRetentionPolicy(BoxAPIConnection api, String id) {
072        super(api, id);
073    }
074
075    /**
076     * Used to create a new indefinite retention policy.
077     *
078     * @param api  the API connection to be used by the created user.
079     * @param name the name of the retention policy.
080     * @return the created retention policy's info.
081     */
082    public static BoxRetentionPolicy.Info createIndefinitePolicy(BoxAPIConnection api, String name) {
083        return createRetentionPolicy(
084            api, name, BoxRetentionPolicyType.Indefinite, 0, BoxRetentionPolicyAction.RemoveRetention
085        );
086    }
087
088    /**
089     * Used to create a new indefinite retention policy with optional parameters.
090     *
091     * @param api            the API connection to be used by the created user.
092     * @param name           the name of the retention policy.
093     * @param optionalParams the optional parameters.
094     * @return the created retention policy's info.
095     */
096    public static BoxRetentionPolicy.Info createIndefinitePolicy(
097        BoxAPIConnection api, String name, RetentionPolicyParams optionalParams
098    ) {
099        return createRetentionPolicy(
100            api, name, BoxRetentionPolicyType.Indefinite, 0, BoxRetentionPolicyAction.RemoveRetention, optionalParams
101        );
102    }
103
104    /**
105     * Used to create a new finite retention policy.
106     *
107     * @param api    the API connection to be used by the created user.
108     * @param name   the name of the retention policy.
109     * @param length the duration in days that the retention policy will be active for after being assigned to content.
110     * @param action the disposition action can be "permanently_delete" or "remove_retention".
111     * @return the created retention policy's info.
112     */
113    public static BoxRetentionPolicy.Info createFinitePolicy(BoxAPIConnection api,
114                                                             String name,
115                                                             int length,
116                                                             BoxRetentionPolicyAction action) {
117        return createRetentionPolicy(api, name, BoxRetentionPolicyType.Finite, length, action);
118    }
119
120    /**
121     * Used to create a new finite retention policy with optional parameters.
122     *
123     * @param api            the API connection to be used by the created user.
124     * @param name           the name of the retention policy.
125     * @param length         the duration in days that the retention policy will be active for after being assigned to content.
126     * @param action         the disposition action can be "permanently_delete" or "remove_retention".
127     * @param optionalParams the optional parameters.
128     * @return the created retention policy's info.
129     */
130    public static BoxRetentionPolicy.Info createFinitePolicy(
131        BoxAPIConnection api,
132        String name,
133        int length,
134        BoxRetentionPolicyAction action,
135        RetentionPolicyParams optionalParams
136    ) {
137        return createRetentionPolicy(api, name, BoxRetentionPolicyType.Finite, length, action, optionalParams);
138    }
139
140    /**
141     * Used to create a new retention policy.
142     *
143     * @param api    the API connection to be used by the created user.
144     * @param name   the name of the retention policy.
145     * @param type   the type of the retention policy. Can be "finite" or "indefinite".
146     * @param length the duration in days that the retention policy will be active for after being assigned to content.
147     * @param action the disposition action can be "permanently_delete" or "remove_retention".
148     * @return the created retention policy's info.
149     */
150    private static BoxRetentionPolicy.Info createRetentionPolicy(BoxAPIConnection api,
151                                                                 String name,
152                                                                 BoxRetentionPolicyType type,
153                                                                 int length,
154                                                                 BoxRetentionPolicyAction action) {
155        return createRetentionPolicy(api, name, type, length, action, null);
156    }
157
158    /**
159     * Used to create a new retention policy with optional parameters.
160     *
161     * @param api            the API connection to be used by the created user.
162     * @param name           the name of the retention policy.
163     * @param type           the type of the retention policy. Can be "finite" or "indefinite".
164     * @param length         the duration in days that the retention policy will be active for after being assigned to content.
165     * @param action         the disposition action can be "permanently_delete" or "remove_retention".
166     * @param optionalParams the optional parameters.
167     * @return the created retention policy's info.
168     */
169    private static BoxRetentionPolicy.Info createRetentionPolicy(
170        BoxAPIConnection api,
171        String name,
172        BoxRetentionPolicyType type,
173        int length,
174        BoxRetentionPolicyAction action,
175        RetentionPolicyParams optionalParams
176    ) {
177        URL url = RETENTION_POLICIES_URL_TEMPLATE.build(api.getBaseURL());
178        BoxJSONRequest request = new BoxJSONRequest(api, url, "POST");
179        JsonObject requestJSON = new JsonObject()
180            .add("policy_name", name)
181            .add("policy_type", type.value)
182            .add("disposition_action", action.value);
183        if (type != BoxRetentionPolicyType.Indefinite) {
184            requestJSON.add("retention_length", length);
185        }
186        if (optionalParams != null) {
187            requestJSON.add("can_owner_extend_retention", optionalParams.getCanOwnerExtendRetention());
188            requestJSON.add("are_owners_notified", optionalParams.getAreOwnersNotified());
189            requestJSON.add("description", optionalParams.getDescription());
190            requestJSON.add("retention_type", optionalParams.getRetentionType().toJSONString());
191
192            List<BoxUser.Info> customNotificationRecipients = optionalParams.getCustomNotificationRecipients();
193            if (customNotificationRecipients.size() > 0) {
194                JsonArray users = new JsonArray();
195                for (BoxUser.Info user : customNotificationRecipients) {
196                    JsonObject userJSON = new JsonObject()
197                        .add("type", "user")
198                        .add("id", user.getID());
199                    users.add(userJSON);
200                }
201                requestJSON.add("custom_notification_recipients", users);
202            }
203        }
204        request.setBody(requestJSON.toString());
205        try (BoxJSONResponse response = request.send()) {
206            JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
207            BoxRetentionPolicy createdPolicy = new BoxRetentionPolicy(api, responseJSON.get("id").asString());
208            return createdPolicy.new Info(responseJSON);
209        }
210    }
211
212    /**
213     * Returns all the retention policies.
214     *
215     * @param api    the API connection to be used by the resource.
216     * @param fields the fields to retrieve.
217     * @return an iterable with all the retention policies.
218     */
219    public static Iterable<BoxRetentionPolicy.Info> getAll(final BoxAPIConnection api, String... fields) {
220        return getAll(null, null, null, DEFAULT_LIMIT, api, fields);
221    }
222
223    /**
224     * Returns all the retention policies with specified filters.
225     *
226     * @param name   a name to filter the retention policies by. A trailing partial match search is performed.
227     *               Set to null if no name filtering is required.
228     * @param type   a policy type to filter the retention policies by. Set to null if no type filtering is required.
229     * @param userID a user id to filter the retention policies by. Set to null if no type filtering is required.
230     * @param limit  the limit of items per single response. The default value is 100.
231     * @param api    the API connection to be used by the resource.
232     * @param fields the fields to retrieve.
233     * @return an iterable with all the retention policies met search conditions.
234     */
235    public static Iterable<BoxRetentionPolicy.Info> getAll(
236        String name, String type, String userID, int limit, final BoxAPIConnection api, String... fields) {
237        QueryStringBuilder queryString = new QueryStringBuilder();
238        if (name != null) {
239            queryString.appendParam("policy_name", name);
240        }
241        if (type != null) {
242            queryString.appendParam("policy_type", type);
243        }
244        if (userID != null) {
245            queryString.appendParam("created_by_user_id", userID);
246        }
247        if (fields.length > 0) {
248            queryString.appendParam("fields", fields);
249        }
250        URL url = RETENTION_POLICIES_URL_TEMPLATE.buildWithQuery(api.getBaseURL(), queryString.toString());
251        return new BoxResourceIterable<BoxRetentionPolicy.Info>(api, url, limit) {
252
253            @Override
254            protected BoxRetentionPolicy.Info factory(JsonObject jsonObject) {
255                BoxRetentionPolicy policy = new BoxRetentionPolicy(api, jsonObject.get("id").asString());
256                return policy.new Info(jsonObject);
257            }
258
259        };
260    }
261
262    /**
263     * Returns iterable with all folder assignments of this retention policy.
264     *
265     * @param fields the fields to retrieve.
266     * @return an iterable containing all folder assignments.
267     */
268    public Iterable<BoxRetentionPolicyAssignment.Info> getFolderAssignments(String... fields) {
269        return this.getFolderAssignments(DEFAULT_LIMIT, fields);
270    }
271
272    /**
273     * Returns iterable with all folder assignments of this retention policy.
274     *
275     * @param limit  the limit of entries per response. The default value is 100.
276     * @param fields the fields to retrieve.
277     * @return an iterable containing all folder assignments.
278     */
279    public Iterable<BoxRetentionPolicyAssignment.Info> getFolderAssignments(int limit, String... fields) {
280        return this.getAssignments(BoxRetentionPolicyAssignment.TYPE_FOLDER, limit, fields);
281    }
282
283    /**
284     * Returns iterable with all enterprise assignments of this retention policy.
285     *
286     * @param fields the fields to retrieve.
287     * @return an iterable containing all enterprise assignments.
288     */
289    public Iterable<BoxRetentionPolicyAssignment.Info> getEnterpriseAssignments(String... fields) {
290        return this.getEnterpriseAssignments(DEFAULT_LIMIT, fields);
291    }
292
293    /**
294     * Returns iterable with all enterprise assignments of this retention policy.
295     *
296     * @param limit  the limit of entries per response. The default value is 100.
297     * @param fields the fields to retrieve.
298     * @return an iterable containing all enterprise assignments.
299     */
300    public Iterable<BoxRetentionPolicyAssignment.Info> getEnterpriseAssignments(int limit, String... fields) {
301        return this.getAssignments(BoxRetentionPolicyAssignment.TYPE_ENTERPRISE, limit, fields);
302    }
303
304    /**
305     * Returns iterable with all assignments of this retention policy.
306     *
307     * @param fields the fields to retrieve.
308     * @return an iterable containing all assignments.
309     */
310    public Iterable<BoxRetentionPolicyAssignment.Info> getAllAssignments(String... fields) {
311        return this.getAllAssignments(DEFAULT_LIMIT, fields);
312    }
313
314    /**
315     * Returns iterable with all assignments of this retention policy.
316     *
317     * @param limit  the limit of entries per response. The default value is 100.
318     * @param fields the fields to retrieve.
319     * @return an iterable containing all assignments.
320     */
321    public Iterable<BoxRetentionPolicyAssignment.Info> getAllAssignments(int limit, String... fields) {
322        return this.getAssignments(null, limit, fields);
323    }
324
325    /**
326     * Returns iterable with all assignments of given type of this retention policy.
327     *
328     * @param type   the type of the retention policy assignment to retrieve. Can either be "folder" or "enterprise".
329     * @param limit  the limit of entries per response. The default value is 100.
330     * @param fields the fields to retrieve.
331     * @return an iterable containing all assignments of given type.
332     */
333    private Iterable<BoxRetentionPolicyAssignment.Info> getAssignments(String type, int limit, String... fields) {
334        QueryStringBuilder queryString = new QueryStringBuilder();
335        if (type != null) {
336            queryString.appendParam("type", type);
337        }
338        if (fields.length > 0) {
339            queryString.appendParam("fields", fields);
340        }
341        URL url = ASSIGNMENTS_URL_TEMPLATE.buildWithQuery(getAPI().getBaseURL(), queryString.toString(), getID());
342        return new BoxResourceIterable<BoxRetentionPolicyAssignment.Info>(getAPI(), url, limit) {
343
344            @Override
345            protected BoxRetentionPolicyAssignment.Info factory(JsonObject jsonObject) {
346                BoxRetentionPolicyAssignment assignment
347                    = new BoxRetentionPolicyAssignment(getAPI(), jsonObject.get("id").asString());
348                return assignment.new Info(jsonObject);
349            }
350
351        };
352    }
353
354    /**
355     * Assigns this retention policy to folder.
356     *
357     * @param folder the folder to assign policy to.
358     * @return info about created assignment.
359     */
360    public BoxRetentionPolicyAssignment.Info assignTo(BoxFolder folder) {
361        return BoxRetentionPolicyAssignment.createAssignmentToFolder(this.getAPI(), this.getID(), folder.getID());
362    }
363
364    /**
365     * Assigns this retention policy to the current enterprise.
366     *
367     * @return info about created assignment.
368     */
369    public BoxRetentionPolicyAssignment.Info assignToEnterprise() {
370        return BoxRetentionPolicyAssignment.createAssignmentToEnterprise(this.getAPI(), this.getID());
371    }
372
373    /**
374     * Assigns this retention policy to a metadata template, optionally with certain field values.
375     *
376     * @param templateID   the ID of the metadata template to apply to.
377     * @param fieldFilters optional field value filters.
378     * @return info about the created assignment.
379     */
380    public BoxRetentionPolicyAssignment.Info assignToMetadataTemplate(String templateID,
381                                                                      MetadataFieldFilter... fieldFilters) {
382        return assignToMetadataTemplate(templateID, null, fieldFilters);
383    }
384
385    /**
386     * Assigns this retention policy to a metadata template, optionally with certain field values.
387     *
388     * @param templateID     the ID of the metadata template to apply to.
389     * @param startDateField the date the retention policy assignment begins. This field can be a date field's metadata attribute key id.
390     * @param fieldFilters   optional field value filters.
391     * @return info about the created assignment.
392     */
393    public BoxRetentionPolicyAssignment.Info assignToMetadataTemplate(String templateID,
394                                                                      String startDateField,
395                                                                      MetadataFieldFilter... fieldFilters) {
396        return BoxRetentionPolicyAssignment.createAssignmentToMetadata(this.getAPI(), this.getID(), templateID,
397            startDateField, fieldFilters);
398    }
399
400    /**
401     * Updates the information about this retention policy with any info fields that have been modified locally.
402     *
403     * @param info the updated info.
404     */
405    public void updateInfo(BoxRetentionPolicy.Info info) {
406        URL url = POLICY_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
407        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT");
408        request.setBody(info.getPendingChanges());
409        try (BoxJSONResponse response = request.send()) {
410            JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
411            info.update(responseJSON);
412        }
413    }
414
415    /**
416     * Returns information about this retention policy.
417     *
418     * @param fields the fields to retrieve.
419     * @return information about this retention policy.
420     */
421    public BoxRetentionPolicy.Info getInfo(String... fields) {
422        QueryStringBuilder builder = new QueryStringBuilder();
423        if (fields.length > 0) {
424            builder.appendParam("fields", fields);
425        }
426        URL url = POLICY_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), builder.toString(), this.getID());
427        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "GET");
428        try (BoxJSONResponse response = request.send()) {
429            JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
430            return new Info(responseJSON);
431        }
432    }
433
434    /**
435     * Contains information about the retention policy.
436     */
437    public class Info extends BoxResource.Info {
438
439        /**
440         * @see #getPolicyName()
441         */
442        private String policyName;
443
444        /**
445         * @see #getPolicyType()
446         */
447        private String policyType;
448
449        /**
450         * @see #getRetentionLength()
451         */
452        private int retentionLength;
453
454        /**
455         * @see #getDispositionAction()
456         */
457        private String dispositionAction;
458
459        /**
460         * @see #getStatus()
461         */
462        private String status;
463
464        /**
465         * @see #getCreatedBy()
466         */
467        private BoxUser.Info createdBy;
468
469        /**
470         * @see #getCreatedAt()
471         */
472        private Date createdAt;
473
474        /**
475         * @see #getModifiedAt()
476         */
477        private Date modifiedAt;
478
479        /**
480         * @see #getCanOwnerExtendRetention()
481         */
482        private boolean canOwnerExtendRetention;
483
484        /**
485         * @see #getAreOwnersNotified()
486         */
487        private boolean areOwnersNotified;
488
489        /**
490         * @see #getDescription()
491         */
492        private String description;
493
494        /**
495         * @see #getRetentionType()
496         */
497        private RetentionPolicyParams.RetentionType retentionType;
498
499        private List<BoxUser.Info> customNotificationRecipients;
500
501        /**
502         * Constructs an empty Info object.
503         */
504        public Info() {
505            super();
506        }
507
508        /**
509         * Constructs an Info object by parsing information from a JSON string.
510         *
511         * @param json the JSON string to parse.
512         */
513        public Info(String json) {
514            super(json);
515        }
516
517        /**
518         * Constructs an Info object using an already parsed JSON object.
519         *
520         * @param jsonObject the parsed JSON object.
521         */
522        Info(JsonObject jsonObject) {
523            super(jsonObject);
524        }
525
526        /**
527         * {@inheritDoc}
528         */
529        @Override
530        public BoxResource getResource() {
531            return BoxRetentionPolicy.this;
532        }
533
534        /**
535         * Gets the name given to the retention policy.
536         *
537         * @return name given to the retention policy.
538         */
539        public String getPolicyName() {
540            return this.policyName;
541        }
542
543        /**
544         * Update the policy name to a new value.
545         *
546         * @param policyName the new policy name.
547         */
548        public void setPolicyName(String policyName) {
549            this.policyName = policyName;
550            this.addPendingChange("policy_name", policyName);
551        }
552
553        /**
554         * Gets the type of the retention policy.
555         * A retention policy type can either be "finite",
556         * where a specific amount of time to retain the content is known upfront,
557         * or "indefinite", where the amount of time to retain the content is still unknown.
558         *
559         * @return the type of the retention policy.
560         */
561        public String getPolicyType() {
562            return this.policyType;
563        }
564
565        /**
566         * Gets the length of the retention policy. This length specifies the duration
567         * in days that the retention policy will be active for after being assigned to content.
568         *
569         * @return the length of the retention policy.
570         */
571        public int getRetentionLength() {
572            return this.retentionLength;
573        }
574
575        /**
576         *
577         * @param retentionLength The length of the retention policy.
578         */
579        public void setRetentionLength(int retentionLength) {
580            this.retentionLength = retentionLength;
581            this.addPendingChange("retention_length", retentionLength);
582        }
583
584        /**
585         * Gets the disposition action of the retention policy.
586         * This action can be "permanently_delete", or "remove_retention".
587         *
588         * @return the disposition action of the retention policy.
589         */
590        public String getDispositionAction() {
591            return this.dispositionAction;
592        }
593
594        /**
595         * Set the action to take when retention period ends.
596         *
597         * @param dispositionAction the new action.
598         */
599        public void setDispositionAction(String dispositionAction) {
600            this.dispositionAction = dispositionAction;
601            this.addPendingChange("disposition_action", dispositionAction);
602        }
603
604        /**
605         * Gets the status of the retention policy.
606         * The status can be "active" or "retired".
607         *
608         * @return the status of the retention policy.
609         */
610        public String getStatus() {
611            return this.status;
612        }
613
614        /**
615         * Set the policy status.
616         *
617         * @param status the new status value.
618         */
619        public void setStatus(String status) {
620            this.status = status;
621            this.addPendingChange("status", status);
622        }
623
624        /**
625         * Gets info about the user created the retention policy.
626         *
627         * @return info about the user created the retention policy.
628         */
629        public BoxUser.Info getCreatedBy() {
630            return this.createdBy;
631        }
632
633        /**
634         * Gets the time that the retention policy was created.
635         *
636         * @return the time that the retention policy was created.
637         */
638        public Date getCreatedAt() {
639            return this.createdAt;
640        }
641
642        /**
643         * Gets the time that the retention policy was last modified.
644         *
645         * @return the time that the retention policy was last modified.
646         */
647        public Date getModifiedAt() {
648            return this.modifiedAt;
649        }
650
651        /**
652         * Gets the flag to denote that the owner of a retained file can extend the retention when near expiration.
653         *
654         * @return the boolean flag.
655         */
656        public boolean getCanOwnerExtendRetention() {
657            return this.canOwnerExtendRetention;
658        }
659
660        /**
661         * Gets the flag to denote that owners and co-owners of a retained file will get notified when near expiration.
662         *
663         * @return the boolean flag.
664         */
665        public boolean getAreOwnersNotified() {
666            return this.areOwnersNotified;
667        }
668
669        /**
670         * Gets the additional text desription of the retention policy.
671         *
672         * @return the additional text desription of the retention policy
673         */
674        public String getDescription() {
675            return this.description;
676        }
677
678        /**
679         * Set the additional text desription of the retention policy.
680         *
681         * @param description the new text desription of the retention policy
682         */
683        public void setDescription(String description) {
684            this.description = description;
685            this.addPendingChange("description", description);
686        }
687
688        /**
689         *
690         * @return retention type. It can be one of values: `modifiable` or `non-modifiable`.
691         * `modifiable` means that you can modify the retention policy. For example, you can add or remove folders,
692         *  shorten or lengthen the policy duration, or delete the assignment.
693         * `non-modifiable` means that can modify the retention policy only in a limited way: add a folder,
694         *  lengthen the duration, retire the policy, change the disposition action or notification settings.
695         *  You cannot perform other actions, such as deleting the assignment or shortening the policy duration.
696         */
697        public RetentionPolicyParams.RetentionType getRetentionType() {
698            return retentionType;
699        }
700
701        /**
702         *
703         * It is not possible to set retention type to `modifiable` once it was set to `non-modifiable`.
704         */
705        public void setRetentionTypeToNonModifiable() {
706            this.retentionType = RetentionPolicyParams.RetentionType.NON_MODIFIABLE;
707            this.addPendingChange("retention_type", retentionType.toJSONString());
708        }
709
710        /**
711         * Gets the list of users to be notified of a retained file when near expiration.
712         *
713         * @return the list of users to be notified.
714         */
715        public List<BoxUser.Info> getCustomNotificationRecipients() {
716            return this.customNotificationRecipients;
717        }
718
719        /**
720         * {@inheritDoc}
721         */
722        @Override
723        void parseJSONMember(JsonObject.Member member) {
724            super.parseJSONMember(member);
725            String memberName = member.getName();
726            JsonValue value = member.getValue();
727            try {
728                switch (memberName) {
729                    case "policy_name":
730                        this.policyName = value.asString();
731                        break;
732                    case "policy_type":
733                        this.policyType = value.asString();
734                        break;
735                    case "retention_length":
736                        int intVal;
737                        if (value.asString().equals(BoxRetentionPolicyType.Indefinite.value)) {
738                            intVal = -1;
739                        } else {
740                            intVal = Integer.parseInt(value.asString());
741                        }
742                        this.retentionLength = intVal;
743                        break;
744                    case "disposition_action":
745                        this.dispositionAction = value.asString();
746                        break;
747                    case "status":
748                        this.status = value.asString();
749                        break;
750                    case "created_by":
751                        JsonObject userJSON = value.asObject();
752                        if (this.createdBy == null) {
753                            String userID = userJSON.get("id").asString();
754                            BoxUser user = new BoxUser(getAPI(), userID);
755                            this.createdBy = user.new Info(userJSON);
756                        } else {
757                            this.createdBy.update(userJSON);
758                        }
759                        break;
760                    case "created_at":
761                        this.createdAt = BoxDateFormat.parse(value.asString());
762                        break;
763                    case "modified_at":
764                        this.modifiedAt = BoxDateFormat.parse(value.asString());
765                        break;
766                    case "can_owner_extend_retention":
767                        this.canOwnerExtendRetention = value.asBoolean();
768                        break;
769                    case "are_owners_notified":
770                        this.areOwnersNotified = value.asBoolean();
771                        break;
772                    case "description":
773                        this.description = value.asString();
774                        break;
775                    case "retention_type":
776                        this.retentionType = RetentionPolicyParams.RetentionType.fromJSONString(value.asString());
777                        break;
778                    case "custom_notification_recipients":
779                        List<BoxUser.Info> recipients = new ArrayList<>();
780                        for (JsonValue recipientJSON : value.asArray()) {
781                            String userID = recipientJSON.asObject().get("id").asString();
782                            BoxUser user = new BoxUser(getAPI(), userID);
783                            recipients.add(user.new Info(recipientJSON.asObject()));
784                        }
785                        this.customNotificationRecipients = recipients;
786                        break;
787                    default:
788                        break;
789                }
790            } catch (Exception e) {
791                throw new BoxDeserializationException(memberName, value.toString(), e);
792            }
793        }
794    }
795
796    private enum BoxRetentionPolicyType {
797        /**
798         * Type for finite retention policies. Finite retention policies has the duration.
799         */
800        Finite("finite"),
801        /**
802         * Type for indefinite retention policies. Indefinite retention policies can have only
803         * {@link BoxRetentionPolicyAction#RemoveRetention} assigned action.
804         */
805        Indefinite("indefinite");
806
807        private final String value;
808
809        BoxRetentionPolicyType(String value) {
810            this.value = value;
811        }
812    }
813
814    /**
815     * The disposition action of the retention policy.
816     */
817    public enum BoxRetentionPolicyAction {
818        /**
819         * Will cause the content retained by the policy to be permanently deleted.
820         */
821        PermanentlyDelete("permanently_delete"),
822
823        /**
824         * Will lift the retention policy from the content, allowing it to be deleted by users.
825         */
826        RemoveRetention("remove_retention");
827
828        private final String value;
829
830        BoxRetentionPolicyAction(String value) {
831            this.value = value;
832        }
833    }
834}