001package com.box.sdk;
002
003import java.text.ParseException;
004import java.util.Date;
005
006import com.eclipsesource.json.JsonObject;
007import com.eclipsesource.json.JsonValue;
008
009/**
010 * The abstract base class for types that can be added to collaborations.
011 */
012public abstract class BoxCollaborator extends BoxResource {
013
014    /**
015     * Constructs a BoxCollaborator for a collaborator with a given ID.
016     * @param  api the API connection to be used by the collaborator.
017     * @param  id  the ID of the collaborator.
018     */
019    public BoxCollaborator(BoxAPIConnection api, String id) {
020        super(api, id);
021    }
022
023    /**
024     * Contains information about a BoxCollaborator.
025     */
026    public abstract class Info extends BoxResource.Info {
027        private String name;
028        private Date createdAt;
029        private Date modifiedAt;
030        private String login;
031
032        /**
033         * Constructs an empty Info object.
034         */
035        public Info() {
036            super();
037        }
038
039        /**
040         * Constructs an Info object by parsing information from a JSON string.
041         * @param  json the JSON string to parse.
042         */
043        public Info(String json) {
044            super(json);
045        }
046
047        /**
048         * Constructs an Info object using an already parsed JSON object.
049         * @param  jsonObject the parsed JSON object.
050         */
051        Info(JsonObject jsonObject) {
052            super(jsonObject);
053        }
054
055        /**
056         * Gets the name of the collaborator.
057         * @return the name of the collaborator.
058         */
059        public String getName() {
060            return this.name;
061        }
062
063        /**
064         * Sets the name of the collaborator.
065         * @param name the new name of the collaborator.
066         */
067        public void setName(String name) {
068            this.name = name;
069            this.addPendingChange("name", name);
070        }
071
072        /**
073         * Gets the date that the collaborator was created.
074         * @return the date that the collaborator was created.
075         */
076        public Date getCreatedAt() {
077            return this.createdAt;
078        }
079
080        /**
081         * Gets the date that the collaborator was modified.
082         * @return the date that the collaborator was modified.
083         */
084        public Date getModifiedAt() {
085            return this.modifiedAt;
086        }
087
088        /**
089         * Gets the login for the collaborator.
090         * @return the login of the collaboraor.
091         */
092        public String getLogin() {
093            return this.login;
094        }
095
096        @Override
097        protected void parseJSONMember(JsonObject.Member member) {
098            super.parseJSONMember(member);
099
100            try {
101                JsonValue value = member.getValue();
102                String name = member.getName();
103                if (name.equals("name")) {
104                    this.name = value.asString();
105                } else if (name.equals("created_at")) {
106                    this.createdAt = BoxDateFormat.parse(value.asString());
107                } else if (name.equals("modified_at")) {
108                    this.modifiedAt = BoxDateFormat.parse(value.asString());
109                } else if (name.equals("login")) {
110                    this.login = value.asString();
111                }
112            } catch (ParseException e) {
113                assert false : "A ParseException indicates a bug in the SDK.";
114            }
115        }
116    }
117}