001package com.box.sdk;
002
003import java.util.Date;
004
005import com.eclipsesource.json.JsonObject;
006import com.eclipsesource.json.JsonValue;
007
008/**
009 * The abstract base class for types that can be added to collaborations.
010 */
011public abstract class BoxCollaborator extends BoxResource {
012
013    /**
014     * Constructs a BoxCollaborator for a collaborator with a given ID.
015     * @param  api the API connection to be used by the collaborator.
016     * @param  id  the ID of the collaborator.
017     */
018    public BoxCollaborator(BoxAPIConnection api, String id) {
019        super(api, id);
020    }
021
022    /**
023     * Enumerates the possible types of collaborations.
024     */
025    public enum CollaboratorType {
026        /**
027         * A user.
028         */
029        USER ("user"),
030
031        /**
032         * A group.
033         */
034        GROUP ("group");
035
036        private final String jsonValue;
037
038        private CollaboratorType(String jsonValue) {
039            this.jsonValue = jsonValue;
040        }
041
042        static CollaboratorType fromJSONValue(String jsonValue) {
043            return CollaboratorType.valueOf(jsonValue.toUpperCase());
044        }
045
046        String toJSONValue() {
047            return this.jsonValue;
048        }
049    }
050
051    /**
052     * Enumerates the possible types of groups.
053     */
054    public enum GroupType {
055        /**
056         * A users group.
057         */
058        ALL_USERS_GROUP ("all_users_group"),
059
060        /**
061         * A managed group.
062         */
063        MANAGED_GROUP ("managed_group");
064
065        private final String jsonValue;
066
067        private GroupType(String jsonValue) {
068            this.jsonValue = jsonValue;
069        }
070
071        static GroupType fromJSONValue(String jsonValue) {
072            return GroupType.valueOf(jsonValue.toUpperCase());
073        }
074
075        String toJSONValue() {
076            return this.jsonValue;
077        }
078    }
079
080    /**
081     * Contains information about a BoxCollaborator.
082     */
083    public abstract class Info extends BoxResource.Info {
084        private CollaboratorType type;
085        private String name;
086        private Date createdAt;
087        private Date modifiedAt;
088        private String login;
089        private GroupType groupType;
090
091        /**
092         * Constructs an empty Info object.
093         */
094        public Info() {
095            super();
096        }
097
098        /**
099         * Constructs an Info object by parsing information from a JSON string.
100         * @param  json the JSON string to parse.
101         */
102        public Info(String json) {
103            super(json);
104        }
105
106        /**
107         * Constructs an Info object using an already parsed JSON object.
108         * @param  jsonObject the parsed JSON object.
109         */
110        Info(JsonObject jsonObject) {
111            super(jsonObject);
112        }
113
114        /**
115         * Gets the type of the collaborator.
116         * @return the type of the collaborator.
117         */
118        public CollaboratorType getType() {
119            return this.type;
120        }
121
122        /**
123         * Gets the name of the collaborator.
124         * @return the name of the collaborator.
125         */
126        public String getName() {
127            return this.name;
128        }
129
130        /**
131         * Sets the name of the collaborator.
132         * @param name the new name of the collaborator.
133         */
134        public void setName(String name) {
135            this.name = name;
136            this.addPendingChange("name", name);
137        }
138
139        /**
140         * Gets the date that the collaborator was created.
141         * @return the date that the collaborator was created.
142         */
143        public Date getCreatedAt() {
144            return this.createdAt;
145        }
146
147        /**
148         * Gets the date that the collaborator was modified.
149         * @return the date that the collaborator was modified.
150         */
151        public Date getModifiedAt() {
152            return this.modifiedAt;
153        }
154
155        /**
156         * Gets the login for the collaborator if the collaborator is a user.
157         * @return the login of the collaboraor.
158         */
159        public String getLogin() {
160            return this.login;
161        }
162
163        /**
164         * Gets the group type for the collaborator if the collaborator is a group.
165         * @return the group type of the collaboraor.
166         */
167        public GroupType getGroupType() {
168            return this.groupType;
169        }
170
171        @Override
172        protected void parseJSONMember(JsonObject.Member member) {
173            super.parseJSONMember(member);
174            JsonValue value = member.getValue();
175            String name = member.getName();
176
177            try {
178
179                if (name.equals("type")) {
180                    this.type = CollaboratorType.fromJSONValue(value.asString());
181                } else if (name.equals("name")) {
182                    this.name = value.asString();
183                } else if (name.equals("created_at")) {
184                    this.createdAt = BoxDateFormat.parse(value.asString());
185                } else if (name.equals("modified_at")) {
186                    this.modifiedAt = BoxDateFormat.parse(value.asString());
187                } else if (name.equals("login")) {
188                    this.login = value.asString();
189                } else if (name.equals("group_type")) {
190                    this.groupType = GroupType.fromJSONValue(value.asString());
191                }
192            } catch (Exception e) {
193                throw new BoxDeserializationException(name, value.toString(), e);
194            }
195        }
196    }
197}