001package com.box.sdk; 002 003import java.net.URL; 004 005import com.eclipsesource.json.JsonObject; 006import com.eclipsesource.json.JsonValue; 007 008/** 009 * Represents a Box user account. 010 * 011 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link BoxAPIException} (unchecked 012 * meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error 013 * handling for errors related to the Box REST API, you should capture this exception explicitly.</p> 014 */ 015public class BoxUser extends BoxCollaborator { 016 private static final URLTemplate GET_USER_URL = new URLTemplate("users/%s"); 017 private static final URLTemplate GET_ME_URL = new URLTemplate("users/me"); 018 019 /** 020 * Constructs a BoxUser for a user with a given ID. 021 * @param api the API connection to be used by the user. 022 * @param id the ID of the user. 023 */ 024 public BoxUser(BoxAPIConnection api, String id) { 025 super(api, id); 026 } 027 028 /** 029 * Gets the current user. 030 * @param api the API connection of the current user. 031 * @return the current user. 032 */ 033 public static BoxUser getCurrentUser(BoxAPIConnection api) { 034 URL url = GET_ME_URL.build(api.getBaseURL()); 035 BoxAPIRequest request = new BoxAPIRequest(api, url, "GET"); 036 BoxJSONResponse response = (BoxJSONResponse) request.send(); 037 JsonObject jsonObject = JsonObject.readFrom(response.getJSON()); 038 return new BoxUser(api, jsonObject.get("id").asString()); 039 } 040 041 /** 042 * Gets information about this user. 043 * @return info about this user. 044 */ 045 public BoxUser.Info getInfo() { 046 URL url = GET_USER_URL.build(this.getAPI().getBaseURL(), this.getID()); 047 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET"); 048 BoxJSONResponse response = (BoxJSONResponse) request.send(); 049 JsonObject jsonObject = JsonObject.readFrom(response.getJSON()); 050 return new Info(jsonObject); 051 } 052 053 /** 054 * Enumerates the possible roles that a user can have within an enterprise. 055 */ 056 public enum Role { 057 /** 058 * The user is an administrator of their enterprise. 059 */ 060 ADMIN, 061 062 /** 063 * The user is a co-administrator of their enterprise. 064 */ 065 COADMIN, 066 067 /** 068 * The user is a regular user within their enterprise. 069 */ 070 USER 071 } 072 073 /** 074 * Enumerates the possible statuses that a user's account can have. 075 */ 076 public enum Status { 077 /** 078 * The user's account is active. 079 */ 080 ACTIVE, 081 082 /** 083 * The user's account is inactive. 084 */ 085 INACTIVE, 086 087 /** 088 * The user's account cannot delete or edit content. 089 */ 090 CANNOT_DELETE_EDIT, 091 092 /** 093 * The user's account cannot delete, edit, or upload content. 094 */ 095 CANNOT_DELETE_EDIT_UPLOAD 096 } 097 098 /** 099 * Contains information about a BoxUser. 100 */ 101 public class Info extends BoxCollaborator.Info { 102 private String login; 103 private Role role; 104 private String language; 105 private String timezone; 106 private long spaceAmount; 107 private long spaceUsed; 108 private long maxUploadSize; 109 private Status status; 110 private String jobTitle; 111 private String phone; 112 private String address; 113 private String avatarURL; 114 115 Info(JsonObject jsonObject) { 116 super(jsonObject); 117 } 118 119 @Override 120 public BoxUser getResource() { 121 return BoxUser.this; 122 } 123 124 /** 125 * Gets the email address the user uses to login. 126 * @return the email address the user uses to login. 127 */ 128 public String getLogin() { 129 return this.login; 130 } 131 132 /** 133 * Gets the user's enterprise role. 134 * @return the user's enterprise role. 135 */ 136 public Role getRole() { 137 return this.role; 138 } 139 140 /** 141 * Gets the language of the user. 142 * @return the language of the user. 143 */ 144 public String getLanguage() { 145 return this.language; 146 } 147 148 /** 149 * Gets the timezone of the user. 150 * @return the timezone of the user. 151 */ 152 public String getTimezone() { 153 return this.timezone; 154 } 155 156 /** 157 * Gets the user's total available space in bytes. 158 * @return the user's total available space in bytes. 159 */ 160 public long getSpaceAmount() { 161 return this.spaceAmount; 162 } 163 164 /** 165 * Gets the amount of space the user has used in bytes. 166 * @return the amount of space the user has used in bytes. 167 */ 168 public long getSpaceUsed() { 169 return this.spaceUsed; 170 } 171 172 /** 173 * Gets the maximum individual file size in bytes the user can have. 174 * @return the maximum individual file size in bytes the user can have. 175 */ 176 public long getMaxUploadSize() { 177 return this.maxUploadSize; 178 } 179 180 /** 181 * Gets the user's current account status. 182 * @return the user's current account status. 183 */ 184 public Status getStatus() { 185 return this.status; 186 } 187 188 /** 189 * Gets the job title of the user. 190 * @return the job title of the user. 191 */ 192 public String getJobTitle() { 193 return this.jobTitle; 194 } 195 196 /** 197 * Gets the phone number of the user. 198 * @return the phone number of the user. 199 */ 200 public String getPhone() { 201 return this.phone; 202 } 203 204 /** 205 * Gets the address of the user. 206 * @return the address of the user. 207 */ 208 public String getAddress() { 209 return this.address; 210 } 211 212 /** 213 * Gets the URL of the user's avatar. 214 * @return the URL of the user's avatar. 215 */ 216 public String getAvatarURL() { 217 return this.avatarURL; 218 } 219 220 @Override 221 protected void parseJSONMember(JsonObject.Member member) { 222 super.parseJSONMember(member); 223 224 JsonValue value = member.getValue(); 225 String memberName = member.getName(); 226 if (memberName.equals("login")) { 227 this.login = value.asString(); 228 } else if (memberName.equals("role")) { 229 this.role = this.parseRole(value); 230 } else if (memberName.equals("language")) { 231 this.language = value.asString(); 232 } else if (memberName.equals("timezone")) { 233 this.timezone = value.asString(); 234 } else if (memberName.equals("space_amount")) { 235 this.spaceAmount = Double.valueOf(value.toString()).longValue(); 236 } else if (memberName.equals("space_used")) { 237 this.spaceUsed = Double.valueOf(value.toString()).longValue(); 238 } else if (memberName.equals("max_upload_size")) { 239 this.maxUploadSize = Double.valueOf(value.toString()).longValue(); 240 } else if (memberName.equals("status")) { 241 this.status = this.parseStatus(value); 242 } else if (memberName.equals("job_title")) { 243 this.jobTitle = value.asString(); 244 } else if (memberName.equals("phone")) { 245 this.phone = value.asString(); 246 } else if (memberName.equals("address")) { 247 this.address = value.asString(); 248 } else if (memberName.equals("avatar_url")) { 249 this.avatarURL = value.asString(); 250 } 251 } 252 253 private Role parseRole(JsonValue value) { 254 String roleString = value.asString().toUpperCase(); 255 return Role.valueOf(roleString); 256 } 257 258 private Status parseStatus(JsonValue value) { 259 String statusString = value.asString().toUpperCase(); 260 return Status.valueOf(statusString); 261 } 262 } 263}