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