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
031        /**
032         * Constructs an empty Info object.
033         */
034        public Info() {
035            super();
036        }
037
038        /**
039         * Constructs an Info object by parsing information from a JSON string.
040         * @param  json the JSON string to parse.
041         */
042        public Info(String json) {
043            super(json);
044        }
045
046        /**
047         * Constructs an Info object using an already parsed JSON object.
048         * @param  jsonObject the parsed JSON object.
049         */
050        Info(JsonObject jsonObject) {
051            super(jsonObject);
052        }
053
054        /**
055         * Gets the name of the collaborator.
056         * @return the name of the collaborator.
057         */
058        public String getName() {
059            return this.name;
060        }
061
062        /**
063         * Sets the name of the collaborator.
064         * @param name the new name of the collaborator.
065         */
066        public void setName(String name) {
067            this.name = name;
068            this.addPendingChange("name", name);
069        }
070
071        /**
072         * Gets the date that the collaborator was created.
073         * @return the date that the collaborator was created.
074         */
075        public Date getCreatedAt() {
076            return this.createdAt;
077        }
078
079        /**
080         * Gets the date that the collaborator was modified.
081         * @return the date that the collaborator was modified.
082         */
083        public Date getModifiedAt() {
084            return this.modifiedAt;
085        }
086
087        @Override
088        protected void parseJSONMember(JsonObject.Member member) {
089            super.parseJSONMember(member);
090
091            try {
092                JsonValue value = member.getValue();
093                String name = member.getName();
094                if (name.equals("name")) {
095                    this.name = value.asString();
096                } else if (name.equals("created_at")) {
097                    this.createdAt = BoxDateFormat.parse(value.asString());
098                } else if (name.equals("modified_at")) {
099                    this.modifiedAt = BoxDateFormat.parse(value.asString());
100                }
101            } catch (ParseException e) {
102                assert false : "A ParseException indicates a bug in the SDK.";
103            }
104        }
105    }
106}