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 a device pin. 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("device_pin") 018public class BoxDevicePin extends BoxResource { 019 020 /** 021 * The URL template used for operation with the device pin. 022 */ 023 public static final URLTemplate DEVICE_PIN_URL_TEMPLATE = new URLTemplate("device_pinners/%s"); 024 025 /** 026 * The URL template used to get all the device pins within a given enterprise. 027 */ 028 public static final URLTemplate ENTERPRISE_DEVICE_PINS_TEMPLATE = new URLTemplate("enterprises/%s/device_pinners"); 029 030 /** 031 * Default limit of the device info entries per one response page. 032 */ 033 private static final int DEVICES_DEFAULT_LIMIT = 100; 034 035 /** 036 * Constructs a device pin for a resource with a given ID. 037 * 038 * @param api the API connection to be used by the resource. 039 * @param id the ID of the resource. 040 */ 041 public BoxDevicePin(BoxAPIConnection api, String id) { 042 super(api, id); 043 } 044 045 /** 046 * Gets information about the device pin. 047 * @param fields the fields to retrieve. 048 * @return info about the device pin. 049 */ 050 public Info getInfo(String ... fields) { 051 QueryStringBuilder builder = new QueryStringBuilder(); 052 if (fields.length > 0) { 053 builder.appendParam("fields", fields); 054 } 055 URL url = DEVICE_PIN_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), builder.toString(), this.getID()); 056 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET"); 057 BoxJSONResponse response = (BoxJSONResponse) request.send(); 058 JsonObject responseJSON = JsonObject.readFrom(response.getJSON()); 059 return new Info(responseJSON); 060 } 061 062 /** 063 * Returns iterable with all the device pins within a given enterprise. 064 * Must be an enterprise admin with the manage enterprise scope to make this call. 065 * @param api API used to connect the Box. 066 * @param enterpriseID ID of the enterprise to get all the device pins within. 067 * @param fields the optional fields to retrieve. 068 * @return iterable with all the device pins within a given enterprise. 069 */ 070 public static Iterable<BoxDevicePin.Info> getEnterpriceDevicePins(final BoxAPIConnection api, String enterpriseID, 071 String ... fields) { 072 return getEnterpriceDevicePins(api, enterpriseID, DEVICES_DEFAULT_LIMIT, fields); 073 } 074 075 /** 076 * Returns iterable with all the device pins within a given enterprise. 077 * Must be an enterprise admin with the manage enterprise scope to make this call. 078 * @param api API used to connect the Box. 079 * @param enterpriseID ID of the enterprise to get all the device pins within. 080 * @param limit the maximum number of items per single response. 081 * @param fields the optional fields to retrieve. 082 * @return iterable with all the device pins within a given enterprise. 083 */ 084 public static Iterable<BoxDevicePin.Info> getEnterpriceDevicePins(final BoxAPIConnection api, String enterpriseID, 085 int limit, String ... fields) { 086 QueryStringBuilder builder = new QueryStringBuilder(); 087 if (fields.length > 0) { 088 builder.appendParam("fields", fields); 089 } 090 return new BoxResourceIterable<BoxDevicePin.Info>(api, 091 ENTERPRISE_DEVICE_PINS_TEMPLATE.buildWithQuery(api.getBaseURL(), builder.toString(), enterpriseID), 092 limit) { 093 094 @Override 095 protected BoxDevicePin.Info factory(JsonObject jsonObject) { 096 BoxDevicePin pin = new BoxDevicePin(api, jsonObject.get("id").asString()); 097 return pin.new Info(jsonObject); 098 } 099 }; 100 } 101 102 /** 103 * Deletes the device pin. 104 */ 105 public void delete() { 106 URL url = DEVICE_PIN_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID()); 107 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "DELETE"); 108 BoxAPIResponse response = request.send(); 109 response.disconnect(); 110 } 111 112 /** 113 * Contains information about a task assignment. 114 */ 115 public class Info extends BoxResource.Info { 116 117 /** 118 * @see #getOwnedBy() 119 */ 120 private BoxUser.Info ownedBy; 121 122 /** 123 * @see #getProductName() 124 */ 125 private String productName; 126 127 /** 128 * @see #getCreatedAt() 129 */ 130 private Date createdAt; 131 132 /** 133 * @see #getModifiedAt() 134 */ 135 private Date modifiedAt; 136 137 /** 138 * Constructs an empty Info object. 139 */ 140 public Info() { 141 super(); 142 } 143 144 /** 145 * Constructs an Info object by parsing information from a JSON string. 146 * @param json the JSON string to parse. 147 */ 148 public Info(String json) { 149 super(json); 150 } 151 152 /** 153 * Constructs an Info object using an already parsed JSON object. 154 * @param jsonObject the parsed JSON object. 155 */ 156 Info(JsonObject jsonObject) { 157 super(jsonObject); 158 } 159 160 /** 161 * {@inheritDoc} 162 */ 163 @Override 164 public BoxResource getResource() { 165 return BoxDevicePin.this; 166 } 167 168 /** 169 * Gets ID of the user that the pin belongs to. 170 * @return ID of the user that the pin belongs to. 171 */ 172 public BoxUser.Info getOwnedBy() { 173 return this.ownedBy; 174 } 175 176 /** 177 * Gets the type of device being pinned. 178 * @return the type of device being pinned. 179 */ 180 public String getProductName() { 181 return this.productName; 182 } 183 184 /** 185 * Gets the time this pin was created. 186 * @return the time this pin was created. 187 */ 188 public Date getCreatedAt() { 189 return this.createdAt; 190 } 191 192 /** 193 * Gets the time this pin was modified. 194 * @return the time this pin was modified. 195 */ 196 public Date getModifiedAt() { 197 return this.modifiedAt; 198 } 199 200 /** 201 * {@inheritDoc} 202 */ 203 @Override 204 void parseJSONMember(JsonObject.Member member) { 205 super.parseJSONMember(member); 206 207 String memberName = member.getName(); 208 JsonValue value = member.getValue(); 209 try { 210 if (memberName.equals("owned_by")) { 211 JsonObject userJSON = value.asObject(); 212 if (this.ownedBy == null) { 213 String userID = userJSON.get("id").asString(); 214 BoxUser user = new BoxUser(getAPI(), userID); 215 this.ownedBy = user.new Info(userJSON); 216 } else { 217 this.ownedBy.update(userJSON); 218 } 219 } else if (memberName.equals("product_name")) { 220 this.productName = value.asString(); 221 } else if (memberName.equals("created_at")) { 222 this.createdAt = BoxDateFormat.parse(value.asString()); 223 } else if (memberName.equals("modified_at")) { 224 this.modifiedAt = BoxDateFormat.parse(value.asString()); 225 } 226 } catch (ParseException e) { 227 assert false : "A ParseException indicates a bug in the SDK."; 228 } 229 } 230 } 231}