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        try (BoxJSONResponse response = 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    /**
074     * Gets information about this group membership.
075     *
076     * @return info about this group membership.
077     */
078    public Info getInfo() {
079        BoxAPIConnection api = this.getAPI();
080        URL url = INVITE_URL_TEMPLATE.build(api.getBaseURL(), this.getID());
081
082        BoxJSONRequest request = new BoxJSONRequest(api, url, "GET");
083        try (BoxJSONResponse response = request.send()) {
084            JsonObject jsonObject = Json.parse(response.getJSON()).asObject();
085            return new Info(jsonObject);
086        }
087    }
088
089    /**
090     * Contains information about a BoxInvite.
091     */
092    public class Info extends BoxResource.Info {
093
094        /**
095         * @see #getInvitedTo()
096         */
097        private BoxEnterprise invitedTo;
098
099        /**
100         * @see #getActionableBy()
101         */
102        private BoxUser.Info actionableBy;
103
104        /**
105         * @see #getInvitedBy()
106         */
107        private BoxUser.Info invitedBy;
108
109        /**
110         * @see #getCreatedAt()
111         */
112        private Date createdAt;
113
114        /**
115         * @see #getModifiedAt()
116         */
117        private Date modifiedAt;
118
119        /**
120         * @see #getStatus()
121         */
122        private String status;
123
124        /**
125         * Constructs an empty Info object.
126         */
127        public Info() {
128            super();
129        }
130
131        /**
132         * Constructs an Info object by parsing information from a JSON string.
133         *
134         * @param json the JSON string to parse.
135         */
136        public Info(String json) {
137            super(json);
138        }
139
140        /**
141         * Constructs an Info object using an already parsed JSON object.
142         *
143         * @param jsonObject the parsed JSON object.
144         */
145        Info(JsonObject jsonObject) {
146            super(jsonObject);
147        }
148
149        /**
150         * Gets the enterprise the user was invited to.
151         *
152         * @return the enterprise the user was invited to.
153         */
154        public BoxEnterprise getInvitedTo() {
155            return this.invitedTo;
156        }
157
158        /**
159         * Gets the user that was invited to the enterprise.
160         *
161         * <p>Note: the BoxUser.Info returned by this method will only have the ID, name, and login fields
162         * populated.</p>
163         *
164         * @return the invited user.
165         */
166        public BoxUser.Info getActionableBy() {
167            return this.actionableBy;
168        }
169
170        /**
171         * Gets the user that made the invitation.
172         *
173         * <p>Note: the BoxUser.Info returned by this method will only have the ID, name, and login fields
174         * populated.</p>
175         *
176         * @return the user that created the invitation.
177         */
178        public BoxUser.Info getInvitedBy() {
179            return this.invitedBy;
180        }
181
182        /**
183         * Gets the status of the invitation.
184         *
185         * @return the invite status.
186         */
187        public String getStatus() {
188            return this.status;
189        }
190
191        /**
192         * Gets the time the invite was created.
193         *
194         * @return the time the invite was created.
195         */
196        public Date getCreatedAt() {
197            return this.createdAt;
198        }
199
200        /**
201         * Gets the time the invite was last modified.
202         *
203         * @return the time the invite was last modified.
204         */
205        public Date getModifiedAt() {
206            return this.modifiedAt;
207        }
208
209        /**
210         * {@inheritDoc}
211         */
212        @Override
213        public BoxInvite getResource() {
214            return BoxInvite.this;
215        }
216
217        /**
218         * {@inheritDoc}
219         */
220        @Override
221        protected void parseJSONMember(JsonObject.Member member) {
222            super.parseJSONMember(member);
223
224            String memberName = member.getName();
225            JsonValue value = member.getValue();
226
227            try {
228                if (memberName.equals("invited_to")) {
229                    JsonObject enterpriseJSON = value.asObject();
230                    this.invitedTo = new BoxEnterprise(enterpriseJSON);
231                } else if (memberName.equals("actionable_by")) {
232                    JsonObject userJSON = value.asObject();
233                    if (this.actionableBy == null) {
234                        String userID = userJSON.get("id").asString();
235                        BoxUser user = new BoxUser(getAPI(), userID);
236                        this.actionableBy = user.new Info(userJSON);
237                    } else {
238                        this.actionableBy.update(userJSON);
239                    }
240                } else if (memberName.equals("invited_by")) {
241                    JsonObject userJSON = value.asObject();
242                    if (this.invitedBy == null) {
243                        String userID = userJSON.get("id").asString();
244                        BoxUser user = new BoxUser(getAPI(), userID);
245                        this.invitedBy = user.new Info(userJSON);
246                    } else {
247                        this.invitedBy.update(userJSON);
248                    }
249                } else if (memberName.equals("status")) {
250                    this.status = value.asString();
251                } else if (memberName.equals("created_at")) {
252                    this.createdAt = BoxDateFormat.parse(value.asString());
253
254                } else if (memberName.equals("modified_at")) {
255                    this.modifiedAt = BoxDateFormat.parse(value.asString());
256
257                }
258            } catch (Exception e) {
259                throw new BoxDeserializationException(memberName, value.toString(), e);
260            }
261        }
262    }
263
264}