001package com.box.sdk;
002
003import com.eclipsesource.json.Json;
004import com.eclipsesource.json.JsonObject;
005import com.eclipsesource.json.JsonValue;
006import java.net.URL;
007import java.util.Date;
008
009/**
010 * Represents an invitation for a user to join an enterprise.
011 *
012 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link BoxAPIException} (unchecked
013 * meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error
014 * handling for errors related to the Box REST API, you should capture this exception explicitly.</p>
015 */
016@BoxResourceType("invite")
017public class BoxInvite extends BoxResource {
018
019    /**
020     * The URL template for invite creation requests.
021     */
022    public static final URLTemplate INVITE_CREATION_URL_TEMPLATE = new URLTemplate("invites");
023
024    /**
025     * The URL template for invite retrieval requests.
026     *
027     * @see #getInfo()
028     */
029    public static final URLTemplate INVITE_URL_TEMPLATE = new URLTemplate("invites/%s");
030
031    /**
032     * Constructs a BoxInvitee for an invite with a given ID.
033     *
034     * @param api the API connection to be used by the invite.
035     * @param id  the ID of the invite.
036     */
037    public BoxInvite(BoxAPIConnection api, String id) {
038        super(api, id);
039    }
040
041    /**
042     * Invite a user to an enterprise.
043     *
044     * @param api          the API connection to use for the request.
045     * @param userLogin    the login of the user to invite.
046     * @param enterpriseID the ID of the enterprise to invite the user to.
047     * @return the invite info.
048     */
049    public static Info inviteUserToEnterprise(BoxAPIConnection api, String userLogin, String enterpriseID) {
050
051        URL url = INVITE_CREATION_URL_TEMPLATE.build(api.getBaseURL());
052        BoxJSONRequest request = new BoxJSONRequest(api, url, "POST");
053
054        JsonObject body = new JsonObject();
055
056        JsonObject enterprise = new JsonObject();
057        enterprise.add("id", enterpriseID);
058        body.add("enterprise", enterprise);
059
060        JsonObject actionableBy = new JsonObject();
061        actionableBy.add("login", userLogin);
062        body.add("actionable_by", actionableBy);
063
064        request.setBody(body);
065        BoxJSONResponse response = (BoxJSONResponse) request.send();
066        JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
067
068        BoxInvite invite = new BoxInvite(api, responseJSON.get("id").asString());
069        return invite.new Info(responseJSON);
070    }
071
072    /**
073     * Gets information about this group membership.
074     *
075     * @return info about this group membership.
076     */
077    public Info getInfo() {
078        BoxAPIConnection api = this.getAPI();
079        URL url = INVITE_URL_TEMPLATE.build(api.getBaseURL(), this.getID());
080
081        BoxAPIRequest request = new BoxAPIRequest(api, url, "GET");
082        BoxJSONResponse response = (BoxJSONResponse) request.send();
083        JsonObject jsonObject = Json.parse(response.getJSON()).asObject();
084        return new Info(jsonObject);
085    }
086
087    /**
088     * Contains information about a BoxInvite.
089     */
090    public class Info extends BoxResource.Info {
091
092        /**
093         * @see #getInvitedTo()
094         */
095        private BoxEnterprise invitedTo;
096
097        /**
098         * @see #getActionableBy()
099         */
100        private BoxUser.Info actionableBy;
101
102        /**
103         * @see #getInvitedBy()
104         */
105        private BoxUser.Info invitedBy;
106
107        /**
108         * @see #getCreatedAt()
109         */
110        private Date createdAt;
111
112        /**
113         * @see #getModifiedAt()
114         */
115        private Date modifiedAt;
116
117        /**
118         * @see #getStatus()
119         */
120        private String status;
121
122        /**
123         * Constructs an empty Info object.
124         */
125        public Info() {
126            super();
127        }
128
129        /**
130         * Constructs an Info object by parsing information from a JSON string.
131         *
132         * @param json the JSON string to parse.
133         */
134        public Info(String json) {
135            super(json);
136        }
137
138        /**
139         * Constructs an Info object using an already parsed JSON object.
140         *
141         * @param jsonObject the parsed JSON object.
142         */
143        Info(JsonObject jsonObject) {
144            super(jsonObject);
145        }
146
147        /**
148         * Gets the enterprise the user was invited to.
149         *
150         * @return the enterprise the user was invited to.
151         */
152        public BoxEnterprise getInvitedTo() {
153            return this.invitedTo;
154        }
155
156        /**
157         * Gets the user that was invited to the enterprise.
158         *
159         * <p>Note: the BoxUser.Info returned by this method will only have the ID, name, and login fields
160         * populated.</p>
161         *
162         * @return the invited user.
163         */
164        public BoxUser.Info getActionableBy() {
165            return this.actionableBy;
166        }
167
168        /**
169         * Gets the user that made the invitation.
170         *
171         * <p>Note: the BoxUser.Info returned by this method will only have the ID, name, and login fields
172         * populated.</p>
173         *
174         * @return the user that created the invitation.
175         */
176        public BoxUser.Info getInvitedBy() {
177            return this.invitedBy;
178        }
179
180        /**
181         * Gets the status of the invitation.
182         *
183         * @return the invite status.
184         */
185        public String getStatus() {
186            return this.status;
187        }
188
189        /**
190         * Gets the time the invite was created.
191         *
192         * @return the time the invite was created.
193         */
194        public Date getCreatedAt() {
195            return this.createdAt;
196        }
197
198        /**
199         * Gets the time the invite was last modified.
200         *
201         * @return the time the invite was last modified.
202         */
203        public Date getModifiedAt() {
204            return this.modifiedAt;
205        }
206
207        /**
208         * {@inheritDoc}
209         */
210        @Override
211        public BoxInvite getResource() {
212            return BoxInvite.this;
213        }
214
215        /**
216         * {@inheritDoc}
217         */
218        @Override
219        protected void parseJSONMember(JsonObject.Member member) {
220            super.parseJSONMember(member);
221
222            String memberName = member.getName();
223            JsonValue value = member.getValue();
224
225            try {
226                if (memberName.equals("invited_to")) {
227                    JsonObject enterpriseJSON = value.asObject();
228                    this.invitedTo = new BoxEnterprise(enterpriseJSON);
229                } else if (memberName.equals("actionable_by")) {
230                    JsonObject userJSON = value.asObject();
231                    if (this.actionableBy == null) {
232                        String userID = userJSON.get("id").asString();
233                        BoxUser user = new BoxUser(getAPI(), userID);
234                        this.actionableBy = user.new Info(userJSON);
235                    } else {
236                        this.actionableBy.update(userJSON);
237                    }
238                } else if (memberName.equals("invited_by")) {
239                    JsonObject userJSON = value.asObject();
240                    if (this.invitedBy == null) {
241                        String userID = userJSON.get("id").asString();
242                        BoxUser user = new BoxUser(getAPI(), userID);
243                        this.invitedBy = user.new Info(userJSON);
244                    } else {
245                        this.invitedBy.update(userJSON);
246                    }
247                } else if (memberName.equals("status")) {
248                    this.status = value.asString();
249                } else if (memberName.equals("created_at")) {
250                    this.createdAt = BoxDateFormat.parse(value.asString());
251
252                } else if (memberName.equals("modified_at")) {
253                    this.modifiedAt = BoxDateFormat.parse(value.asString());
254
255                }
256            } catch (Exception e) {
257                throw new BoxDeserializationException(memberName, value.toString(), e);
258            }
259        }
260    }
261
262}