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