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