001package com.box.sdk;
002
003import com.eclipsesource.json.JsonObject;
004import com.eclipsesource.json.JsonValue;
005import java.net.MalformedURLException;
006import java.net.URL;
007import java.text.ParseException;
008import java.util.Date;
009
010/**
011 * Represents an individual recent item.
012 *
013 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link
014 * BoxAPIException} (unchecked meaning that the compiler won't force you to handle it) if an error
015 * occurs. If you wish to implement custom error handling for errors related to the Box REST API,
016 * you should capture this exception explicitly.*
017 */
018public class BoxRecentItem extends BoxJSONObject {
019  private String type;
020  private String interactionType;
021  private BoxItem.Info item;
022  private Date interactedAt;
023  private URL interactionSharedLink;
024  private BoxAPIConnection api;
025
026  /**
027   * Construct a BoxRecentItem.
028   *
029   * @param jsonObject the parsed JSON object.
030   * @param api the API connection to be used to fetch interacted item
031   */
032  public BoxRecentItem(JsonObject jsonObject, BoxAPIConnection api) {
033    super(jsonObject);
034    this.api = api;
035  }
036
037  @Override
038  protected void parseJSONMember(JsonObject.Member member) {
039    super.parseJSONMember(member);
040
041    String memberName = member.getName();
042    JsonValue value = member.getValue();
043    try {
044      if (memberName.equals("type")) {
045        this.type = value.asString();
046      } else if (memberName.equals("interaction_type")) {
047        this.interactionType = value.asString();
048      } else if (memberName.equals("item")) {
049        String id = value.asObject().get("id").asString();
050        this.item = new BoxFile(this.api, id).new Info(value.asObject());
051      } else if (memberName.equals("interacted_at")) {
052        this.interactedAt = BoxDateFormat.parse(value.asString());
053      } else if (memberName.equals("interaction_shared_link")) {
054        this.interactionSharedLink = new URL(value.asString());
055      }
056    } catch (ParseException e) {
057      assert false : "A ParseException indicates a bug in the SDK.";
058    } catch (MalformedURLException e) {
059      assert false : "A ParseException indicates a bug in the SDK.";
060    }
061  }
062
063  /**
064   * Get item type.
065   *
066   * @return type of item
067   */
068  public String getType() {
069    return this.type;
070  }
071
072  /**
073   * Get interaction type.
074   *
075   * @return interaction type
076   */
077  public String getInteractionType() {
078    return this.interactionType;
079  }
080
081  /**
082   * Get the item which was interacted with.
083   *
084   * @return box item
085   */
086  public BoxItem.Info getItem() {
087    return this.item;
088  }
089
090  /**
091   * Get the interaction date.
092   *
093   * @return interaction date
094   */
095  public Date getInteractedAt() {
096    return this.interactedAt;
097  }
098
099  /**
100   * Get the shared link, if the item was accessed through a shared link.
101   *
102   * @return shared link
103   */
104  public URL getInteractionSharedLink() {
105    return this.interactionSharedLink;
106  }
107}