001package com.box.sdk;
002
003import com.eclipsesource.json.JsonObject;
004import com.eclipsesource.json.JsonValue;
005import java.util.Date;
006
007/**
008 * The abstract base class for types that can be added to collaborations.
009 */
010public abstract class BoxCollaborator extends BoxResource {
011
012    /**
013     * Constructs a BoxCollaborator for a collaborator with a given ID.
014     *
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        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        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         *
101         * @param json the JSON string to parse.
102         */
103        public Info(String json) {
104            super(json);
105        }
106
107        /**
108         * Constructs an Info object using an already parsed JSON object.
109         *
110         * @param jsonObject the parsed JSON object.
111         */
112        Info(JsonObject jsonObject) {
113            super(jsonObject);
114        }
115
116        /**
117         * Gets the type of the collaborator.
118         *
119         * @return the type of the collaborator.
120         */
121        public CollaboratorType getType() {
122            return this.type;
123        }
124
125        /**
126         * Gets the name of the collaborator.
127         *
128         * @return the name of the collaborator.
129         */
130        public String getName() {
131            return this.name;
132        }
133
134        /**
135         * Sets the name of the collaborator.
136         *
137         * @param name the new name of the collaborator.
138         */
139        public void setName(String name) {
140            this.name = name;
141            this.addPendingChange("name", name);
142        }
143
144        /**
145         * Gets the date that the collaborator was created.
146         *
147         * @return the date that the collaborator was created.
148         */
149        public Date getCreatedAt() {
150            return this.createdAt;
151        }
152
153        /**
154         * Gets the date that the collaborator was modified.
155         *
156         * @return the date that the collaborator was modified.
157         */
158        public Date getModifiedAt() {
159            return this.modifiedAt;
160        }
161
162        /**
163         * Gets the login for the collaborator if the collaborator is a user.
164         *
165         * @return the login of the collaboraor.
166         */
167        public String getLogin() {
168            return this.login;
169        }
170
171        /**
172         * Gets the group type for the collaborator if the collaborator is a group.
173         *
174         * @return the group type of the collaboraor.
175         */
176        public GroupType getGroupType() {
177            return this.groupType;
178        }
179
180        @Override
181        protected void parseJSONMember(JsonObject.Member member) {
182            super.parseJSONMember(member);
183            JsonValue value = member.getValue();
184            String name = member.getName();
185
186            try {
187
188                if (name.equals("type")) {
189                    this.type = CollaboratorType.fromJSONValue(value.asString());
190                } else if (name.equals("name")) {
191                    this.name = value.asString();
192                } else if (name.equals("created_at")) {
193                    this.createdAt = BoxDateFormat.parse(value.asString());
194                } else if (name.equals("modified_at")) {
195                    this.modifiedAt = BoxDateFormat.parse(value.asString());
196                } else if (name.equals("login")) {
197                    this.login = value.asString();
198                } else if (name.equals("group_type")) {
199                    this.groupType = GroupType.fromJSONValue(value.asString());
200                }
201            } catch (Exception e) {
202                throw new BoxDeserializationException(name, value.toString(), e);
203            }
204        }
205    }
206}