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