001package com.box.sdk;
002
003import java.net.URL;
004import java.text.ParseException;
005import java.util.Date;
006
007import com.eclipsesource.json.JsonObject;
008import com.eclipsesource.json.JsonValue;
009
010/**
011 * Represents a retention policy assignment.
012 *
013 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link BoxAPIException} (unchecked
014 * meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error
015 * handling for errors related to the Box REST API, you should capture this exception explicitly.</p>
016 */
017@BoxResourceType("retention_policy_assignment")
018public class BoxRetentionPolicyAssignment extends BoxResource {
019
020    /**
021     * Type for folder policy assignment.
022     */
023    public static final String TYPE_FOLDER = "folder";
024
025    /**
026     * Type for enterprise policy assignment.
027     */
028    public static final String TYPE_ENTERPRISE = "enterprise";
029
030    /**
031     * The URL template used for operation with retention policy assignments.
032     */
033    public static final URLTemplate ASSIGNMENTS_URL_TEMPLATE = new URLTemplate("retention_policy_assignments");
034
035    /**
036     * The URL template used for operation with retention policy assignment with given ID.
037     */
038    public static final URLTemplate RETENTION_POLICY_ASSIGNMENT_URL_TEMPLATE
039        = new URLTemplate("retention_policy_assignments/%s");
040
041    /**
042     * Constructs a BoxResource for a resource with a given ID.
043     *
044     * @param api the API connection to be used by the resource.
045     * @param id  the ID of the resource.
046     */
047    public BoxRetentionPolicyAssignment(BoxAPIConnection api, String id) {
048        super(api, id);
049    }
050
051    /**
052     * Assigns retention policy with givenID to the enterprise.
053     * @param api the API connection to be used by the created assignment.
054     * @param policyID id of the assigned retention policy.
055     * @return info about created assignment.
056     */
057    public static BoxRetentionPolicyAssignment.Info createAssignmentToEnterprise(BoxAPIConnection api,
058                                                                                 String policyID) {
059        return createAssignment(api, policyID, new JsonObject().add("type", TYPE_ENTERPRISE));
060    }
061
062    /**
063     * Assigns retention policy with givenID to the folder.
064     * @param api the API connection to be used by the created assignment.
065     * @param policyID id of the assigned retention policy.
066     * @param folderID id of the folder to assign policy to.
067     * @return info about created assignment.
068     */
069    public static BoxRetentionPolicyAssignment.Info createAssignmentToFolder(BoxAPIConnection api, String policyID,
070                                                                             String folderID) {
071        return createAssignment(api, policyID, new JsonObject().add("type", TYPE_FOLDER).add("id", folderID));
072    }
073
074    /**
075     * Assigns retention policy with givenID to folder or enterprise.
076     * @param api the API connection to be used by the created assignment.
077     * @param policyID id of the assigned retention policy.
078     * @param assignTo object representing folder or enterprise to assign policy to.
079     * @return info about created assignment.
080     */
081    private static BoxRetentionPolicyAssignment.Info createAssignment(BoxAPIConnection api, String policyID,
082                                                                      JsonObject assignTo) {
083        URL url = ASSIGNMENTS_URL_TEMPLATE.build(api.getBaseURL());
084        BoxJSONRequest request = new BoxJSONRequest(api, url, "POST");
085
086        JsonObject requestJSON = new JsonObject()
087                .add("policy_id", policyID)
088                .add("assign_to", assignTo);
089        request.setBody(requestJSON.toString());
090        BoxJSONResponse response = (BoxJSONResponse) request.send();
091        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
092        BoxRetentionPolicyAssignment createdAssignment
093            = new BoxRetentionPolicyAssignment(api, responseJSON.get("id").asString());
094        return createdAssignment.new Info(responseJSON);
095    }
096
097    /**
098     * @param fields the fields to retrieve.
099     * @return information about this retention policy assignment.
100     */
101    public BoxRetentionPolicyAssignment.Info getInfo(String ... fields) {
102        QueryStringBuilder builder = new QueryStringBuilder();
103        if (fields.length > 0) {
104            builder.appendParam("fields", fields);
105        }
106        URL url = RETENTION_POLICY_ASSIGNMENT_URL_TEMPLATE.buildWithQuery(
107                this.getAPI().getBaseURL(), builder.toString(), this.getID());
108        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
109        BoxJSONResponse response = (BoxJSONResponse) request.send();
110        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
111        return new Info(responseJSON);
112    }
113
114    /**
115     * Contains information about the retention policy.
116     */
117    public class Info extends BoxResource.Info {
118
119        /**
120         * @see #getRetentionPolicy()
121         */
122        private BoxRetentionPolicy.Info retentionPolicy;
123
124        /**
125         * @see #getAssignedBy()
126         */
127        private BoxUser.Info assignedBy;
128
129        /**
130         * @see #getAssignedAt()
131         */
132        private Date assignedAt;
133
134        /**
135         * @see #getAssignedToType()
136         */
137        private String assignedToType;
138
139        /**
140         * @see #getAssignedToID()
141         */
142        private String assignedToID;
143
144        /**
145         * Constructs an empty Info object.
146         */
147        public Info() {
148            super();
149        }
150
151        /**
152         * Constructs an Info object by parsing information from a JSON string.
153         * @param  json the JSON string to parse.
154         */
155        public Info(String json) {
156            super(json);
157        }
158
159        /**
160         * Constructs an Info object using an already parsed JSON object.
161         * @param  jsonObject the parsed JSON object.
162         */
163        Info(JsonObject jsonObject) {
164            super(jsonObject);
165        }
166
167        /**
168         * {@inheritDoc}
169         */
170        @Override
171        public BoxResource getResource() {
172            return BoxRetentionPolicyAssignment.this;
173        }
174
175        /**
176         * @return the retention policy that has been assigned to this content.
177         */
178        public BoxRetentionPolicy.Info getRetentionPolicy() {
179            return this.retentionPolicy;
180        }
181
182        /**
183         * @return the info about the user that created the retention policy assignment.
184         */
185        public BoxUser.Info getAssignedBy() {
186            return this.assignedBy;
187        }
188
189        /**
190         * @return the time that the retention policy assignment was created.
191         */
192        public Date getAssignedAt() {
193            return this.assignedAt;
194        }
195
196        /**
197         * @return type of the content that is under retention. Can either be "enterprise" or "folder".
198         */
199        public String getAssignedToType() {
200            return this.assignedToType;
201        }
202
203        /**
204         * @return id of the folder that is under retention.
205         */
206        public String getAssignedToID() {
207            return this.assignedToID;
208        }
209
210        /**
211         * {@inheritDoc}
212         */
213        @Override
214        void parseJSONMember(JsonObject.Member member) {
215            super.parseJSONMember(member);
216            String memberName = member.getName();
217            JsonValue value = member.getValue();
218            try {
219                if (memberName.equals("retention_policy")) {
220                    JsonObject policyJSON = value.asObject();
221                    if (this.retentionPolicy == null) {
222                        String policyID = policyJSON.get("id").asString();
223                        BoxRetentionPolicy policy = new BoxRetentionPolicy(getAPI(), policyID);
224                        this.retentionPolicy = policy.new Info(policyJSON);
225                    } else {
226                        this.retentionPolicy.update(policyJSON);
227                    }
228                } else if (memberName.equals("assigned_to")) {
229                    JsonObject assignmentJSON = value.asObject();
230                    this.assignedToType = assignmentJSON.get("type").asString();
231                    if (this.assignedToType.equals(TYPE_FOLDER)) {
232                        this.assignedToID = assignmentJSON.get("id").asString();
233                    } else {
234                        this.assignedToID = null;
235                    }
236                } else if (memberName.equals("assigned_by")) {
237                    JsonObject userJSON = value.asObject();
238                    if (this.assignedBy == null) {
239                        String userID = userJSON.get("id").asString();
240                        BoxUser user = new BoxUser(getAPI(), userID);
241                        this.assignedBy = user.new Info(userJSON);
242                    } else {
243                        this.assignedBy.update(userJSON);
244                    }
245                } else if (memberName.equals("assigned_at")) {
246                    this.assignedAt = BoxDateFormat.parse(value.asString());
247                }
248            } catch (ParseException e) {
249                assert false : "A ParseException indicates a bug in the SDK.";
250            }
251        }
252    }
253}