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