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