001package com.box.sdk;
002
003import com.eclipsesource.json.JsonArray;
004import com.eclipsesource.json.JsonObject;
005import com.eclipsesource.json.JsonValue;
006import java.net.URL;
007import java.util.ArrayList;
008import java.util.Date;
009import java.util.List;
010
011/**
012 * Represents a user status on a custom Box Terms of Service object.
013 */
014@BoxResourceType("terms_of_service_user_status")
015public class BoxTermsOfServiceUserStatus extends BoxResource {
016    /**
017     * All Terms of Services User Statuses URL Template.
018     */
019    public static final URLTemplate TERMS_OF_SERVICE_USER_STATUSES_TEMPLATE =
020        new URLTemplate("terms_of_service_user_statuses/%s");
021    /**
022     * Terms of Services User Statuses URL Template.
023     */
024    public static final URLTemplate ALL_TERMS_OF_SERVICE_USER_STATUSES_TEMPLATE =
025        new URLTemplate("terms_of_service_user_statuses");
026
027    /**
028     * Constructs a BoxTermsOfServiceUserStatus for a resource with a given ID.
029     *
030     * @param api the API connection to be used by the resource.
031     * @param id  the ID of the resource.
032     */
033    public BoxTermsOfServiceUserStatus(BoxAPIConnection api, String id) {
034        super(api, id);
035    }
036
037    /**
038     * Creates a User Status on a custom Terms of Service.
039     *
040     * @param api              the API connection to be used by the resource.
041     * @param termsOfServiceID the ID of the terms of service.
042     * @param isAccepted       the indicator for whether the terms of service has been accepted.
043     * @return information about the User Status for Terms of Service created.
044     */
045    public static BoxTermsOfServiceUserStatus.Info create(final BoxAPIConnection api, String termsOfServiceID,
046                                                          Boolean isAccepted) {
047        return create(api, termsOfServiceID, isAccepted, null);
048    }
049
050    /**
051     * Creates a User Status on a custom Terms of Service.
052     *
053     * @param api              the API connection to be used by the resource.
054     * @param termsOfServiceID the ID of the terms of service.
055     * @param isAccepted       the indicator for whether the terms of service has been accepted.
056     * @param userID           the ID of the user for the terms of service.
057     * @return information about the User Status for Terms of Service created.
058     */
059    public static BoxTermsOfServiceUserStatus.Info create(final BoxAPIConnection api, String termsOfServiceID,
060                                                          Boolean isAccepted, String userID) {
061        URL url = ALL_TERMS_OF_SERVICE_USER_STATUSES_TEMPLATE.build(api.getBaseURL());
062        BoxJSONRequest request = new BoxJSONRequest(api, url, "POST");
063        JsonObject requestJSON = new JsonObject()
064            .add("tos", new JsonObject()
065                .add("type", "terms_of_service")
066                .add("id", termsOfServiceID))
067            .add("is_accepted", isAccepted);
068
069        if (userID != null) {
070            requestJSON.add("user", new JsonObject()
071                .add("type", "user")
072                .add("id", userID));
073        }
074
075        request.setBody(requestJSON.toString());
076        BoxJSONResponse response = (BoxJSONResponse) request.send();
077        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
078        BoxTermsOfServiceUserStatus termsOfServiceUserStatus = new BoxTermsOfServiceUserStatus(api,
079            responseJSON.get("id").asString());
080
081        return termsOfServiceUserStatus.new Info(responseJSON);
082    }
083
084    /**
085     * Retrieves a list of User Status for Terms of Service as an Iterable.
086     *
087     * @param api              the API connection to be used by the resource.
088     * @param termsOfServiceID the ID of the terms of service.
089     * @return the Iterable of User Status for Terms of Service.
090     */
091    public static List<BoxTermsOfServiceUserStatus.Info> getInfo(final BoxAPIConnection api, String termsOfServiceID) {
092        return getInfo(api, termsOfServiceID, null);
093    }
094
095    /**
096     * Retrieves a list of User Status for Terms of Service as an Iterable.
097     *
098     * @param api              the API connection to be used by the resource.
099     * @param termsOfServiceID the ID of the terms of service.
100     * @param userID           the ID of the user to retrieve terms of service for.
101     * @return the Iterable of User Status for Terms of Service.
102     */
103    public static List<BoxTermsOfServiceUserStatus.Info> getInfo(final BoxAPIConnection api,
104                                                                 String termsOfServiceID, String userID) {
105        QueryStringBuilder builder = new QueryStringBuilder();
106        builder.appendParam("tos_id", termsOfServiceID);
107        if (userID != null) {
108            builder.appendParam("user_id", userID);
109        }
110
111        URL url = ALL_TERMS_OF_SERVICE_USER_STATUSES_TEMPLATE.buildWithQuery(api.getBaseURL(), builder.toString());
112        BoxAPIRequest request = new BoxAPIRequest(api, url, "GET");
113        BoxJSONResponse response = (BoxJSONResponse) request.send();
114        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
115
116        int totalCount = responseJSON.get("total_count").asInt();
117        List<BoxTermsOfServiceUserStatus.Info> termsOfServiceUserStatuses = new
118            ArrayList<BoxTermsOfServiceUserStatus.Info>(totalCount);
119        JsonArray entries = responseJSON.get("entries").asArray();
120        for (JsonValue value : entries) {
121            JsonObject termsOfServiceUserStatusJSON = value.asObject();
122            BoxTermsOfServiceUserStatus termsOfServiceUserStatus = new
123                BoxTermsOfServiceUserStatus(api, termsOfServiceUserStatusJSON.get("id").asString());
124            BoxTermsOfServiceUserStatus.Info info = termsOfServiceUserStatus.new Info(termsOfServiceUserStatusJSON);
125            termsOfServiceUserStatuses.add(info);
126        }
127
128        return termsOfServiceUserStatuses;
129    }
130
131    /**
132     * Updates the information about the user status for this terms of service with any info fields that have
133     * been modified locally.
134     *
135     * @param info the updated info.
136     */
137    public void updateInfo(BoxTermsOfServiceUserStatus.Info info) {
138        URL url = TERMS_OF_SERVICE_USER_STATUSES_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
139        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT");
140        request.setBody(info.getPendingChanges());
141
142        BoxJSONResponse response = (BoxJSONResponse) request.send();
143        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
144        info.update(responseJSON);
145    }
146
147    /**
148     * Contains information about the user status on a terms of service.
149     */
150    public class Info extends BoxResource.Info {
151
152        /**
153         * @see #getTermsOfService()
154         */
155        private BoxTermsOfService.Info termsOfService;
156
157        /**
158         * @see #getUser()
159         */
160        private BoxUser.Info user;
161
162        /**
163         * @see #getType()
164         */
165        private String termsOfServiceUserStatusType;
166
167
168        /**
169         * @see #getIsAccepted() ()
170         */
171        private Boolean isAccepted;
172
173        /**
174         * @see #getCreatedAt()
175         */
176        private Date createdAt;
177
178        /**
179         * @see #getModifiedAt()
180         */
181        private Date modifiedAt;
182
183        /**
184         * Constructs an empty Info object.
185         */
186        public Info() {
187            super();
188        }
189
190        /**
191         * Constructs an Info object by parsing information from a JSON string.
192         *
193         * @param json the JSON string to parse.
194         */
195        public Info(String json) {
196            super(json);
197        }
198
199        /**
200         * Constructs an Info object using an already parsed JSON object.
201         *
202         * @param jsonObject the parsed JSON object.
203         */
204        Info(JsonObject jsonObject) {
205            super(jsonObject);
206        }
207
208        /**
209         * {@inheritDoc}
210         */
211        @Override
212        public BoxResource getResource() {
213            return BoxTermsOfServiceUserStatus.this;
214        }
215
216        /**
217         * @return the terms of service.
218         */
219        public BoxTermsOfService.Info getTermsOfService() {
220            return this.termsOfService;
221        }
222
223        /**
224         * @return the user.
225         */
226        public BoxUser.Info getUser() {
227            return this.user;
228        }
229
230        /**
231         * @return the is_accepted state of the terms of service.
232         */
233        public Boolean getIsAccepted() {
234            return this.isAccepted;
235        }
236
237        /**
238         * Accept or decline the terms of service.
239         *
240         * @param enabled the new user status of the terms of service.
241         */
242        public void setIsAccepted(Boolean enabled) {
243            this.isAccepted = enabled;
244            this.addPendingChange("is_accepted", this.isAccepted);
245        }
246
247        /**
248         * @return the type of the terms of service user status.
249         */
250        public String getType() {
251            return this.termsOfServiceUserStatusType;
252        }
253
254        /**
255         * @return time the policy was created.
256         */
257        public Date getCreatedAt() {
258            return this.createdAt;
259        }
260
261        /**
262         * @return time the policy was modified.
263         */
264        public Date getModifiedAt() {
265            return this.modifiedAt;
266        }
267
268        /**
269         * {@inheritDoc}
270         */
271        @Override
272        void parseJSONMember(JsonObject.Member member) {
273            super.parseJSONMember(member);
274            String memberName = member.getName();
275            JsonValue value = member.getValue();
276            try {
277                if (memberName.equals("tos")) {
278                    JsonObject tosJSON = value.asObject();
279                    String termsOfServiceID = tosJSON.get("id").asString();
280                    BoxTermsOfService termsOfService = new BoxTermsOfService(getAPI(), termsOfServiceID);
281                    this.termsOfService = termsOfService.new Info(tosJSON);
282                } else if (memberName.equals("user")) {
283                    JsonObject userJSON = value.asObject();
284                    String userID = userJSON.get("id").asString();
285                    BoxUser user = new BoxUser(getAPI(), userID);
286                    this.user = user.new Info(userJSON);
287                } else if (memberName.equals("is_accepted")) {
288                    this.isAccepted = value.asBoolean();
289                } else if (memberName.equals("created_at")) {
290                    this.createdAt = BoxDateFormat.parse(value.asString());
291                } else if (memberName.equals("modified_at")) {
292                    this.modifiedAt = BoxDateFormat.parse(value.asString());
293                } else if (memberName.equals("type")) {
294                    this.termsOfServiceUserStatusType = value.asString();
295                }
296            } catch (Exception e) {
297                throw new BoxDeserializationException(memberName, value.toString(), e);
298            }
299        }
300    }
301}