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