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