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 custom Box Terms of Service object.
014 */
015@BoxResourceType("terms_of_service")
016public class BoxTermsOfService extends BoxResource {
017    /**
018     * Terms of Services URL Template.
019     */
020    public static final URLTemplate TERMS_OF_SERVICE_URL_TEMPLATE = new URLTemplate("terms_of_services/%s");
021    /**
022     * All Terms of Services URL Template.
023     */
024    public static final URLTemplate ALL_TERMS_OF_SERVICES_URL_TEMPLATE = new URLTemplate("terms_of_services");
025
026    /**
027     * Constructs a BoxTermsOfService for a Box Enterprise with a given ID.
028     *
029     * @param api the API connection to be used by the resource.
030     * @param id  the ID of the resource.
031     */
032    public BoxTermsOfService(BoxAPIConnection api, String id) {
033        super(api, id);
034    }
035
036    /**
037     * Creates a new Terms of Services.
038     *
039     * @param api                  the API connection to be used by the resource.
040     * @param termsOfServiceStatus the current status of the terms of services. Set to "enabled" or "disabled".
041     * @param termsOfServiceType   the scope of terms of service. Set to "external" or "managed".
042     * @param text                 the text field of terms of service containing terms of service agreement info.
043     * @return information about the Terms of Service created.
044     */
045    public static BoxTermsOfService.Info create(BoxAPIConnection api,
046                                                BoxTermsOfService.TermsOfServiceStatus termsOfServiceStatus,
047                                                BoxTermsOfService.TermsOfServiceType termsOfServiceType, String text) {
048        URL url = ALL_TERMS_OF_SERVICES_URL_TEMPLATE.build(api.getBaseURL());
049        BoxJSONRequest request = new BoxJSONRequest(api, url, "POST");
050        JsonObject requestJSON = new JsonObject()
051            .add("status", termsOfServiceStatus.toString())
052            .add("tos_type", termsOfServiceType.toString())
053            .add("text", text);
054
055        request.setBody(requestJSON.toString());
056        BoxJSONResponse response = (BoxJSONResponse) request.send();
057        JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
058        BoxTermsOfService createdTermsOfServices = new BoxTermsOfService(api, responseJSON.get("id").asString());
059
060        return createdTermsOfServices.new Info(responseJSON);
061    }
062
063    /**
064     * Retrieves a list of Terms of Services that belong to your Enterprise as an Iterable.
065     *
066     * @param api the API connection to be used by the resource.
067     * @return the Iterable of Terms of Service in your Enterprise.
068     */
069    public static List<BoxTermsOfService.Info> getAllTermsOfServices(final BoxAPIConnection api) {
070        return getAllTermsOfServices(api, null);
071    }
072
073    /**
074     * Retrieves a list of Terms of Service that belong to your Enterprise as an Iterable.
075     *
076     * @param api                api the API connection to be used by the resource.
077     * @param termsOfServiceType the type of terms of service to be retrieved. Can be set to "managed" or "external"
078     * @return the Iterable of Terms of Service in an Enterprise that match the filter parameters.
079     */
080    public static List<BoxTermsOfService.Info> getAllTermsOfServices(final BoxAPIConnection api,
081                                                                     BoxTermsOfService.TermsOfServiceType
082                                                                         termsOfServiceType) {
083        QueryStringBuilder builder = new QueryStringBuilder();
084        if (termsOfServiceType != null) {
085            builder.appendParam("tos_type", termsOfServiceType.toString());
086        }
087
088        URL url = ALL_TERMS_OF_SERVICES_URL_TEMPLATE.buildWithQuery(api.getBaseURL(), builder.toString());
089        BoxAPIRequest request = new BoxAPIRequest(api, url, "GET");
090        BoxJSONResponse response = (BoxJSONResponse) request.send();
091        JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
092
093        int totalCount = responseJSON.get("total_count").asInt();
094        List<BoxTermsOfService.Info> termsOfServices = new ArrayList<>(totalCount);
095        JsonArray entries = responseJSON.get("entries").asArray();
096        for (JsonValue value : entries) {
097            JsonObject termsOfServiceJSON = value.asObject();
098            BoxTermsOfService termsOfService = new BoxTermsOfService(api, termsOfServiceJSON.get("id").asString());
099            BoxTermsOfService.Info info = termsOfService.new Info(termsOfServiceJSON);
100            termsOfServices.add(info);
101        }
102
103        return termsOfServices;
104    }
105
106    /**
107     * Updates the information about this terms of service with modified locally info.
108     * Only status and text can be modified.
109     *
110     * @param info the updated info.
111     */
112    public void updateInfo(BoxTermsOfService.Info info) {
113        URL url = TERMS_OF_SERVICE_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
114        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT");
115        request.setBody(info.getPendingChanges());
116        BoxJSONResponse response = (BoxJSONResponse) request.send();
117        JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
118        info.update(responseJSON);
119    }
120
121    /**
122     * @return Gets information about this {@link BoxTermsOfService}.
123     */
124    public BoxTermsOfService.Info getInfo() {
125        URL url = TERMS_OF_SERVICE_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
126        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
127        BoxJSONResponse response = (BoxJSONResponse) request.send();
128
129        return new Info(Json.parse(response.getJSON()).asObject());
130    }
131
132    /**
133     * Enumerates the possible types of terms of service.
134     */
135    public enum TermsOfServiceType {
136        /**
137         * The terms of service is managed by an enterprise.
138         */
139        MANAGED("managed"),
140
141        /**
142         * The terms of service is external to an enterprise.
143         */
144        EXTERNAL("external");
145
146        private final String tosType;
147
148        TermsOfServiceType(String tosType) {
149            this.tosType = tosType;
150        }
151
152        static TermsOfServiceType fromTosType(String tosType) {
153            if (tosType.equals("managed")) {
154                return TermsOfServiceType.MANAGED;
155            } else if (tosType.equals("external")) {
156                return TermsOfServiceType.EXTERNAL;
157            } else {
158                System.out.print("Invalid Terms of Service Type");
159                return null;
160            }
161        }
162
163        /**
164         * Returns a String containing terms of service type.
165         *
166         * @return a String containing information about terms of service type.
167         */
168        public String toString() {
169            return this.tosType;
170        }
171    }
172
173    /**
174     * Enumerates the possible status that a terms of service can have.
175     */
176    public enum TermsOfServiceStatus {
177        /**
178         * The terms of service is enabled.
179         */
180        ENABLED("enabled"),
181
182        /**
183         * The terms of service is disabled.
184         */
185        DISABLED("disabled");
186
187        private final String status;
188
189        TermsOfServiceStatus(String status) {
190            this.status = status;
191        }
192
193        static TermsOfServiceStatus fromStatus(String status) {
194            if (status.equals("enabled")) {
195                return TermsOfServiceStatus.ENABLED;
196            } else if (status.equals("disabled")) {
197                return TermsOfServiceStatus.DISABLED;
198            } else {
199                System.out.print("Invalid Terms of Service Status");
200                return null;
201            }
202        }
203
204        /**
205         * Returns a String containing current status of the terms of service.
206         *
207         * @return a String containing information about the status of the terms of service.
208         */
209        public String toString() {
210            return this.status;
211        }
212    }
213
214    /**
215     * Contains information about the terms of service.
216     */
217    public class Info extends BoxResource.Info {
218
219        /**
220         * @see #getStatus()
221         */
222        private TermsOfServiceStatus status;
223
224        /**
225         * @see #getType()
226         */
227        private String type;
228
229        /**
230         * @see #getTosType()
231         */
232        private TermsOfServiceType tosType;
233
234        /**
235         * @see #getEnterprise()
236         */
237        private BoxEnterprise enterprise;
238
239        /**
240         * @see #getText()
241         */
242        private String text;
243
244        /**
245         * @see #getCreatedAt()
246         */
247        private Date createdAt;
248
249        /**
250         * @see #getModifiedAt()
251         */
252        private Date modifiedAt;
253
254        /**
255         * Constructs an empty Info object.
256         */
257        public Info() {
258            super();
259        }
260
261        /**
262         * Constructs an Info object by parsing information from a JSON string.
263         *
264         * @param json the JSON string to parse.
265         */
266        public Info(String json) {
267            super(json);
268        }
269
270        /**
271         * Constructs an Info object using an already parsed JSON object.
272         *
273         * @param jsonObject the parsed JSON object.
274         */
275        Info(JsonObject jsonObject) {
276            super(jsonObject);
277        }
278
279        /**
280         * {@inheritDoc}
281         */
282        @Override
283        public BoxResource getResource() {
284            return BoxTermsOfService.this;
285        }
286
287        /**
288         * TermsOfServiceStatus can be "enabled" or "disabled".
289         *
290         * @return the status of the terms of service.
291         */
292        public TermsOfServiceStatus getStatus() {
293            return this.status;
294        }
295
296        /**
297         * Sets the status of the terms of service in order to enable or disable it.
298         *
299         * @param status the new status of the terms of service.
300         */
301        public void setStatus(TermsOfServiceStatus status) {
302            this.status = status;
303            this.addPendingChange("status", status.toString());
304        }
305
306        /**
307         * The type is terms_of_service.
308         *
309         * @return the type terms_of_service.
310         */
311        public String getType() {
312            return this.type;
313        }
314
315        /**
316         * TermsOfServiceType can be "managed" or "external".
317         *
318         * @return the type of the terms of service.
319         */
320        public TermsOfServiceType getTosType() {
321            return this.tosType;
322        }
323
324
325        /**
326         * @return the enterprise for the terms of service.
327         */
328        public BoxEnterprise getEnterprise() {
329            return this.enterprise;
330        }
331
332
333        /**
334         * @return the text of the terms of service.
335         */
336        public String getText() {
337            return this.text;
338        }
339
340        /**
341         * Sets the text of the terms of service.
342         *
343         * @param text the new text of the terms of service.
344         */
345        public void setText(String text) {
346            this.text = text;
347            this.addPendingChange("text", text);
348        }
349
350
351        /**
352         * @return time the policy was created.
353         */
354        public Date getCreatedAt() {
355            return this.createdAt;
356        }
357
358        /**
359         * @return time the policy was modified.
360         */
361        public Date getModifiedAt() {
362            return this.modifiedAt;
363        }
364
365        /**
366         * {@inheritDoc}
367         */
368        @Override
369        void parseJSONMember(JsonObject.Member member) {
370            super.parseJSONMember(member);
371            String memberName = member.getName();
372            JsonValue value = member.getValue();
373            try {
374                if (memberName.equals("status")) {
375                    this.status = TermsOfServiceStatus.fromStatus(value.asString());
376                } else if (memberName.equals("enterprise")) {
377                    JsonObject jsonObject = value.asObject();
378                    this.enterprise = new BoxEnterprise(jsonObject);
379                } else if (memberName.equals("type")) {
380                    this.type = value.asString();
381                } else if (memberName.equals("tos_type")) {
382                    this.tosType = TermsOfServiceType.fromTosType(value.asString());
383                } else if (memberName.equals("text")) {
384                    this.text = value.asString();
385                } else if (memberName.equals("created_at")) {
386                    this.createdAt = BoxDateFormat.parse(value.asString());
387                } else if (memberName.equals("modified_at")) {
388                    this.modifiedAt = BoxDateFormat.parse(value.asString());
389                }
390            } catch (Exception e) {
391                throw new BoxDeserializationException(memberName, value.toString(), e);
392            }
393        }
394    }
395}