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