001package com.box.sdk; 002 003import com.eclipsesource.json.JsonObject; 004import com.eclipsesource.json.JsonValue; 005import java.net.URL; 006import java.util.Date; 007 008/** 009 * Represents an invitation for a user to join an enterprise. 010 * 011 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link BoxAPIException} (unchecked 012 * meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error 013 * handling for errors related to the Box REST API, you should capture this exception explicitly.</p> 014 */ 015@BoxResourceType("invite") 016public class BoxInvite extends BoxResource { 017 018 /** 019 * The URL template for invite creation requests. 020 */ 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(BoxAPIConnection api, String userLogin, String enterpriseID) { 049 050 URL url = INVITE_CREATION_URL_TEMPLATE.build(api.getBaseURL()); 051 BoxJSONRequest request = new BoxJSONRequest(api, url, "POST"); 052 053 JsonObject body = new JsonObject(); 054 055 JsonObject enterprise = new JsonObject(); 056 enterprise.add("id", enterpriseID); 057 body.add("enterprise", enterprise); 058 059 JsonObject actionableBy = new JsonObject(); 060 actionableBy.add("login", userLogin); 061 body.add("actionable_by", actionableBy); 062 063 request.setBody(body); 064 BoxJSONResponse response = (BoxJSONResponse) request.send(); 065 JsonObject responseJSON = JsonObject.readFrom(response.getJSON()); 066 067 BoxInvite invite = new BoxInvite(api, responseJSON.get("id").asString()); 068 return invite.new Info(responseJSON); 069 } 070 071 /** 072 * Gets information about this group membership. 073 * 074 * @return info about this group membership. 075 */ 076 public Info getInfo() { 077 BoxAPIConnection api = this.getAPI(); 078 URL url = INVITE_URL_TEMPLATE.build(api.getBaseURL(), this.getID()); 079 080 BoxAPIRequest request = new BoxAPIRequest(api, url, "GET"); 081 BoxJSONResponse response = (BoxJSONResponse) request.send(); 082 JsonObject jsonObject = JsonObject.readFrom(response.getJSON()); 083 return new Info(jsonObject); 084 } 085 086 /** 087 * Contains information about a BoxInvite. 088 */ 089 public class Info extends BoxResource.Info { 090 091 /** 092 * @see #getInvitedTo() 093 */ 094 private BoxEnterprise invitedTo; 095 096 /** 097 * @see #getActionableBy() 098 */ 099 private BoxUser.Info actionableBy; 100 101 /** 102 * @see #getInvitedBy() 103 */ 104 private BoxUser.Info invitedBy; 105 106 /** 107 * @see #getCreatedAt() 108 */ 109 private Date createdAt; 110 111 /** 112 * @see #getModifiedAt() 113 */ 114 private Date modifiedAt; 115 116 /** 117 * @see #getStatus() 118 */ 119 private String status; 120 121 /** 122 * Constructs an empty Info object. 123 */ 124 public Info() { 125 super(); 126 } 127 128 /** 129 * Constructs an Info object by parsing information from a JSON string. 130 * 131 * @param json the JSON string to parse. 132 */ 133 public Info(String json) { 134 super(json); 135 } 136 137 /** 138 * Constructs an Info object using an already parsed JSON object. 139 * 140 * @param jsonObject the parsed JSON object. 141 */ 142 Info(JsonObject jsonObject) { 143 super(jsonObject); 144 } 145 146 /** 147 * Gets the enterprise the user was invited to. 148 * 149 * @return the enterprise the user was invited to. 150 */ 151 public BoxEnterprise getInvitedTo() { 152 return this.invitedTo; 153 } 154 155 /** 156 * Gets the user that was invited to the enterprise. 157 * 158 * <p>Note: the BoxUser.Info returned by this method will only have the ID, name, and login fields 159 * populated.</p> 160 * 161 * @return the invited user. 162 */ 163 public BoxUser.Info getActionableBy() { 164 return this.actionableBy; 165 } 166 167 /** 168 * Gets the user that made the invitation. 169 * 170 * <p>Note: the BoxUser.Info returned by this method will only have the ID, name, and login fields 171 * populated.</p> 172 * 173 * @return the user that created the invitation. 174 */ 175 public BoxUser.Info getInvitedBy() { 176 return this.invitedBy; 177 } 178 179 /** 180 * Gets the status of the invitation. 181 * 182 * @return the invite status. 183 */ 184 public String getStatus() { 185 return this.status; 186 } 187 188 /** 189 * Gets the time the invite was created. 190 * 191 * @return the time the invite was created. 192 */ 193 public Date getCreatedAt() { 194 return this.createdAt; 195 } 196 197 /** 198 * Gets the time the invite was last modified. 199 * 200 * @return the time the invite was last modified. 201 */ 202 public Date getModifiedAt() { 203 return this.modifiedAt; 204 } 205 206 /** 207 * {@inheritDoc} 208 */ 209 @Override 210 public BoxInvite getResource() { 211 return BoxInvite.this; 212 } 213 214 /** 215 * {@inheritDoc} 216 */ 217 @Override 218 protected void parseJSONMember(JsonObject.Member member) { 219 super.parseJSONMember(member); 220 221 String memberName = member.getName(); 222 JsonValue value = member.getValue(); 223 224 try { 225 if (memberName.equals("invited_to")) { 226 JsonObject enterpriseJSON = value.asObject(); 227 BoxEnterprise enterprise = new BoxEnterprise(enterpriseJSON); 228 this.invitedTo = enterprise; 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}