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