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 user status on a custom Box Terms of Service object. 014 */ 015@BoxResourceType("terms_of_service_user_status") 016public class BoxTermsOfServiceUserStatus extends BoxResource { 017 /** 018 * All Terms of Services User Statuses URL Template. 019 */ 020 public static final URLTemplate TERMS_OF_SERVICE_USER_STATUSES_TEMPLATE = 021 new URLTemplate("terms_of_service_user_statuses/%s"); 022 /** 023 * Terms of Services User Statuses URL Template. 024 */ 025 public static final URLTemplate ALL_TERMS_OF_SERVICE_USER_STATUSES_TEMPLATE = 026 new URLTemplate("terms_of_service_user_statuses"); 027 028 /** 029 * Constructs a BoxTermsOfServiceUserStatus for a resource with a given ID. 030 * 031 * @param api the API connection to be used by the resource. 032 * @param id the ID of the resource. 033 */ 034 public BoxTermsOfServiceUserStatus(BoxAPIConnection api, String id) { 035 super(api, id); 036 } 037 038 /** 039 * Creates a User Status on a custom Terms of Service. 040 * 041 * @param api the API connection to be used by the resource. 042 * @param termsOfServiceID the ID of the terms of service. 043 * @param isAccepted the indicator for whether the terms of service has been accepted. 044 * @return information about the User Status for Terms of Service created. 045 */ 046 public static BoxTermsOfServiceUserStatus.Info create(final BoxAPIConnection api, String termsOfServiceID, 047 Boolean isAccepted) { 048 return create(api, termsOfServiceID, isAccepted, null); 049 } 050 051 /** 052 * Creates a User Status on a custom Terms of Service. 053 * 054 * @param api the API connection to be used by the resource. 055 * @param termsOfServiceID the ID of the terms of service. 056 * @param isAccepted the indicator for whether the terms of service has been accepted. 057 * @param userID the ID of the user for the terms of service. 058 * @return information about the User Status for Terms of Service created. 059 */ 060 public static BoxTermsOfServiceUserStatus.Info create(final BoxAPIConnection api, String termsOfServiceID, 061 Boolean isAccepted, String userID) { 062 URL url = ALL_TERMS_OF_SERVICE_USER_STATUSES_TEMPLATE.build(api.getBaseURL()); 063 BoxJSONRequest request = new BoxJSONRequest(api, url, "POST"); 064 JsonObject requestJSON = new JsonObject() 065 .add("tos", new JsonObject() 066 .add("type", "terms_of_service") 067 .add("id", termsOfServiceID)) 068 .add("is_accepted", isAccepted); 069 070 if (userID != null) { 071 requestJSON.add("user", new JsonObject() 072 .add("type", "user") 073 .add("id", userID)); 074 } 075 076 request.setBody(requestJSON.toString()); 077 BoxJSONResponse response = (BoxJSONResponse) request.send(); 078 JsonObject responseJSON = Json.parse(response.getJSON()).asObject(); 079 BoxTermsOfServiceUserStatus termsOfServiceUserStatus = new BoxTermsOfServiceUserStatus(api, 080 responseJSON.get("id").asString()); 081 082 return termsOfServiceUserStatus.new Info(responseJSON); 083 } 084 085 /** 086 * Retrieves a list of User Status for Terms of Service as an Iterable. 087 * 088 * @param api the API connection to be used by the resource. 089 * @param termsOfServiceID the ID of the terms of service. 090 * @return the Iterable of User Status for Terms of Service. 091 */ 092 public static List<BoxTermsOfServiceUserStatus.Info> getInfo(final BoxAPIConnection api, String termsOfServiceID) { 093 return getInfo(api, termsOfServiceID, null); 094 } 095 096 /** 097 * Retrieves a list of User Status for Terms of Service as an Iterable. 098 * 099 * @param api the API connection to be used by the resource. 100 * @param termsOfServiceID the ID of the terms of service. 101 * @param userID the ID of the user to retrieve terms of service for. 102 * @return the Iterable of User Status for Terms of Service. 103 */ 104 public static List<BoxTermsOfServiceUserStatus.Info> getInfo(final BoxAPIConnection api, 105 String termsOfServiceID, String userID) { 106 QueryStringBuilder builder = new QueryStringBuilder(); 107 builder.appendParam("tos_id", termsOfServiceID); 108 if (userID != null) { 109 builder.appendParam("user_id", userID); 110 } 111 112 URL url = ALL_TERMS_OF_SERVICE_USER_STATUSES_TEMPLATE.buildWithQuery(api.getBaseURL(), builder.toString()); 113 BoxAPIRequest request = new BoxAPIRequest(api, url, "GET"); 114 BoxJSONResponse response = (BoxJSONResponse) request.send(); 115 JsonObject responseJSON = Json.parse(response.getJSON()).asObject(); 116 117 int totalCount = responseJSON.get("total_count").asInt(); 118 List<BoxTermsOfServiceUserStatus.Info> termsOfServiceUserStatuses = new 119 ArrayList<>(totalCount); 120 JsonArray entries = responseJSON.get("entries").asArray(); 121 for (JsonValue value : entries) { 122 JsonObject termsOfServiceUserStatusJSON = value.asObject(); 123 BoxTermsOfServiceUserStatus termsOfServiceUserStatus = new 124 BoxTermsOfServiceUserStatus(api, termsOfServiceUserStatusJSON.get("id").asString()); 125 BoxTermsOfServiceUserStatus.Info info = termsOfServiceUserStatus.new Info(termsOfServiceUserStatusJSON); 126 termsOfServiceUserStatuses.add(info); 127 } 128 129 return termsOfServiceUserStatuses; 130 } 131 132 /** 133 * Updates the information about the user status for this terms of service with any info fields that have 134 * been modified locally. 135 * 136 * @param info the updated info. 137 */ 138 public void updateInfo(BoxTermsOfServiceUserStatus.Info info) { 139 URL url = TERMS_OF_SERVICE_USER_STATUSES_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID()); 140 BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT"); 141 request.setBody(info.getPendingChanges()); 142 143 BoxJSONResponse response = (BoxJSONResponse) request.send(); 144 JsonObject responseJSON = Json.parse(response.getJSON()).asObject(); 145 info.update(responseJSON); 146 } 147 148 /** 149 * Contains information about the user status on a terms of service. 150 */ 151 public class Info extends BoxResource.Info { 152 153 /** 154 * @see #getTermsOfService() 155 */ 156 private BoxTermsOfService.Info termsOfService; 157 158 /** 159 * @see #getUser() 160 */ 161 private BoxUser.Info user; 162 163 /** 164 * @see #getType() 165 */ 166 private String termsOfServiceUserStatusType; 167 168 169 /** 170 * @see #getIsAccepted() () 171 */ 172 private Boolean isAccepted; 173 174 /** 175 * @see #getCreatedAt() 176 */ 177 private Date createdAt; 178 179 /** 180 * @see #getModifiedAt() 181 */ 182 private Date modifiedAt; 183 184 /** 185 * Constructs an empty Info object. 186 */ 187 public Info() { 188 super(); 189 } 190 191 /** 192 * Constructs an Info object by parsing information from a JSON string. 193 * 194 * @param json the JSON string to parse. 195 */ 196 public Info(String json) { 197 super(json); 198 } 199 200 /** 201 * Constructs an Info object using an already parsed JSON object. 202 * 203 * @param jsonObject the parsed JSON object. 204 */ 205 Info(JsonObject jsonObject) { 206 super(jsonObject); 207 } 208 209 /** 210 * {@inheritDoc} 211 */ 212 @Override 213 public BoxResource getResource() { 214 return BoxTermsOfServiceUserStatus.this; 215 } 216 217 /** 218 * @return the terms of service. 219 */ 220 public BoxTermsOfService.Info getTermsOfService() { 221 return this.termsOfService; 222 } 223 224 /** 225 * @return the user. 226 */ 227 public BoxUser.Info getUser() { 228 return this.user; 229 } 230 231 /** 232 * @return the is_accepted state of the terms of service. 233 */ 234 public Boolean getIsAccepted() { 235 return this.isAccepted; 236 } 237 238 /** 239 * Accept or decline the terms of service. 240 * 241 * @param enabled the new user status of the terms of service. 242 */ 243 public void setIsAccepted(Boolean enabled) { 244 this.isAccepted = enabled; 245 this.addPendingChange("is_accepted", this.isAccepted); 246 } 247 248 /** 249 * @return the type of the terms of service user status. 250 */ 251 public String getType() { 252 return this.termsOfServiceUserStatusType; 253 } 254 255 /** 256 * @return time the policy was created. 257 */ 258 public Date getCreatedAt() { 259 return this.createdAt; 260 } 261 262 /** 263 * @return time the policy was modified. 264 */ 265 public Date getModifiedAt() { 266 return this.modifiedAt; 267 } 268 269 /** 270 * {@inheritDoc} 271 */ 272 @Override 273 void parseJSONMember(JsonObject.Member member) { 274 super.parseJSONMember(member); 275 String memberName = member.getName(); 276 JsonValue value = member.getValue(); 277 try { 278 if (memberName.equals("tos")) { 279 JsonObject tosJSON = value.asObject(); 280 String termsOfServiceID = tosJSON.get("id").asString(); 281 BoxTermsOfService termsOfService = new BoxTermsOfService(getAPI(), termsOfServiceID); 282 this.termsOfService = termsOfService.new Info(tosJSON); 283 } else if (memberName.equals("user")) { 284 JsonObject userJSON = value.asObject(); 285 String userID = userJSON.get("id").asString(); 286 BoxUser user = new BoxUser(getAPI(), userID); 287 this.user = user.new Info(userJSON); 288 } else if (memberName.equals("is_accepted")) { 289 this.isAccepted = value.asBoolean(); 290 } else if (memberName.equals("created_at")) { 291 this.createdAt = BoxDateFormat.parse(value.asString()); 292 } else if (memberName.equals("modified_at")) { 293 this.modifiedAt = BoxDateFormat.parse(value.asString()); 294 } else if (memberName.equals("type")) { 295 this.termsOfServiceUserStatusType = value.asString(); 296 } 297 } catch (Exception e) { 298 throw new BoxDeserializationException(memberName, value.toString(), e); 299 } 300 } 301 } 302}