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