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 013 * BoxAPIException} (unchecked meaning that the compiler won't force you to handle it) if an error 014 * occurs. If you wish to implement custom error handling for errors related to the Box REST API, 015 * you should capture this exception explicitly. 016 */ 017@BoxResourceType("invite") 018public class BoxInvite extends BoxResource { 019 020 /** The URL template for invite creation requests. */ 021 public static final URLTemplate INVITE_CREATION_URL_TEMPLATE = new URLTemplate("invites"); 022 023 /** 024 * The URL template for invite retrieval requests. 025 * 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 * 033 * @param api the API connection to be used by the invite. 034 * @param id the ID of the invite. 035 */ 036 public BoxInvite(BoxAPIConnection api, String id) { 037 super(api, id); 038 } 039 040 /** 041 * Invite a user to an enterprise. 042 * 043 * @param api the API connection to use for the request. 044 * @param userLogin the login of the user to invite. 045 * @param enterpriseID the ID of the enterprise to invite the user to. 046 * @return the invite info. 047 */ 048 public static Info inviteUserToEnterprise( 049 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 /** Contains information about a BoxInvite. */ 090 public class Info extends BoxResource.Info { 091 092 /** @see #getInvitedTo() */ 093 private BoxEnterprise invitedTo; 094 095 /** @see #getActionableBy() */ 096 private BoxUser.Info actionableBy; 097 098 /** @see #getInvitedBy() */ 099 private BoxUser.Info invitedBy; 100 101 /** @see #getCreatedAt() */ 102 private Date createdAt; 103 104 /** @see #getModifiedAt() */ 105 private Date modifiedAt; 106 107 /** @see #getStatus() */ 108 private String status; 109 110 /** Constructs an empty Info object. */ 111 public Info() { 112 super(); 113 } 114 115 /** 116 * Constructs an Info object by parsing information from a JSON string. 117 * 118 * @param json the JSON string to parse. 119 */ 120 public Info(String json) { 121 super(json); 122 } 123 124 /** 125 * Constructs an Info object using an already parsed JSON object. 126 * 127 * @param jsonObject the parsed JSON object. 128 */ 129 Info(JsonObject jsonObject) { 130 super(jsonObject); 131 } 132 133 /** 134 * Gets the enterprise the user was invited to. 135 * 136 * @return the enterprise the user was invited to. 137 */ 138 public BoxEnterprise getInvitedTo() { 139 return this.invitedTo; 140 } 141 142 /** 143 * Gets the user that was invited to the enterprise. 144 * 145 * <p>Note: the BoxUser.Info returned by this method will only have the ID, name, and login 146 * fields populated. 147 * 148 * @return the invited user. 149 */ 150 public BoxUser.Info getActionableBy() { 151 return this.actionableBy; 152 } 153 154 /** 155 * Gets the user that made the invitation. 156 * 157 * <p>Note: the BoxUser.Info returned by this method will only have the ID, name, and login 158 * fields populated. 159 * 160 * @return the user that created the invitation. 161 */ 162 public BoxUser.Info getInvitedBy() { 163 return this.invitedBy; 164 } 165 166 /** 167 * Gets the status of the invitation. 168 * 169 * @return the invite status. 170 */ 171 public String getStatus() { 172 return this.status; 173 } 174 175 /** 176 * Gets the time the invite was created. 177 * 178 * @return the time the invite was created. 179 */ 180 public Date getCreatedAt() { 181 return this.createdAt; 182 } 183 184 /** 185 * Gets the time the invite was last modified. 186 * 187 * @return the time the invite was last modified. 188 */ 189 public Date getModifiedAt() { 190 return this.modifiedAt; 191 } 192 193 /** {@inheritDoc} */ 194 @Override 195 public BoxInvite getResource() { 196 return BoxInvite.this; 197 } 198 199 /** {@inheritDoc} */ 200 @Override 201 protected void parseJSONMember(JsonObject.Member member) { 202 super.parseJSONMember(member); 203 204 String memberName = member.getName(); 205 JsonValue value = member.getValue(); 206 207 try { 208 if (memberName.equals("invited_to")) { 209 JsonObject enterpriseJSON = value.asObject(); 210 this.invitedTo = new BoxEnterprise(enterpriseJSON); 211 } else if (memberName.equals("actionable_by")) { 212 JsonObject userJSON = value.asObject(); 213 if (this.actionableBy == null) { 214 String userID = userJSON.get("id").asString(); 215 BoxUser user = new BoxUser(getAPI(), userID); 216 this.actionableBy = user.new Info(userJSON); 217 } else { 218 this.actionableBy.update(userJSON); 219 } 220 } else if (memberName.equals("invited_by")) { 221 JsonObject userJSON = value.asObject(); 222 if (this.invitedBy == null) { 223 String userID = userJSON.get("id").asString(); 224 BoxUser user = new BoxUser(getAPI(), userID); 225 this.invitedBy = user.new Info(userJSON); 226 } else { 227 this.invitedBy.update(userJSON); 228 } 229 } else if (memberName.equals("status")) { 230 this.status = value.asString(); 231 } else if (memberName.equals("created_at")) { 232 this.createdAt = BoxDateFormat.parse(value.asString()); 233 234 } else if (memberName.equals("modified_at")) { 235 this.modifiedAt = BoxDateFormat.parse(value.asString()); 236 } 237 } catch (Exception e) { 238 throw new BoxDeserializationException(memberName, value.toString(), e); 239 } 240 } 241 } 242}