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