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