001package com.box.sdk;
002
003import com.eclipsesource.json.JsonObject;
004import com.eclipsesource.json.JsonValue;
005import java.util.ArrayList;
006import java.util.HashMap;
007
008/**
009 * Represents an individual item returned by a metadata query item.
010 *
011 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link
012 * BoxAPIException} (unchecked meaning that the compiler won't force you to handle it) if an error
013 * occurs. If you wish to implement custom error handling for errors related to the Box REST API,
014 * you should capture this exception explicitly.
015 */
016public class BoxMetadataQueryItem extends BoxJSONObject {
017  private BoxItem.Info item;
018  private HashMap<String, ArrayList<Metadata>> metadata;
019  private BoxAPIConnection api;
020
021  /**
022   * Construct a BoxMetadataQueryItem.
023   *
024   * @param jsonObject the parsed JSON object.
025   * @param api the API connection to be used to fetch interacted item
026   */
027  public BoxMetadataQueryItem(JsonObject jsonObject, BoxAPIConnection api) {
028    super(jsonObject);
029    this.api = api;
030  }
031
032  @Override
033  protected void parseJSONMember(JsonObject.Member member) {
034    super.parseJSONMember(member);
035
036    String memberName = member.getName();
037    JsonValue value = member.getValue();
038    if (memberName.equals("item")) {
039      String id = value.asObject().get("id").asString();
040      String type = value.asObject().get("type").asString();
041      this.item = new BoxFile(this.api, id).new Info(value.asObject());
042      if (type.equals("folder")) {
043        BoxFolder folder = new BoxFolder(this.api, id);
044        this.item = folder.new Info(value.asObject());
045      } else if (type.equals("file")) {
046        BoxFile file = new BoxFile(this.api, id);
047        this.item = file.new Info(value.asObject());
048      } else if (type.equals("web_link")) {
049        BoxWebLink link = new BoxWebLink(this.api, id);
050        this.item = link.new Info(value.asObject());
051      } else {
052        assert false : "Unsupported item type: " + type;
053        throw new BoxAPIException("Unsupported item type: " + type);
054      }
055    } else if (memberName.equals("metadata")) {
056      this.metadata = new HashMap<String, ArrayList<Metadata>>();
057      JsonObject metadataObject = value.asObject();
058      for (JsonObject.Member enterprise : metadataObject) {
059        String enterpriseName = enterprise.getName();
060        JsonObject templates = enterprise.getValue().asObject();
061        ArrayList<Metadata> enterpriseMetadataArray = new ArrayList<Metadata>();
062        for (JsonObject.Member template : templates) {
063          String templateName = template.getName();
064          JsonObject templateValue = template.getValue().asObject();
065          Metadata metadataOfTemplate = new Metadata(templateValue);
066          metadataOfTemplate.add("/$scope", enterpriseName);
067          metadataOfTemplate.add("/$template", templateName);
068          enterpriseMetadataArray.add(metadataOfTemplate);
069        }
070        this.metadata.put(enterpriseName, enterpriseMetadataArray);
071      }
072    }
073  }
074
075  /**
076   * Get the item which was interacted with.
077   *
078   * @return box item
079   */
080  public BoxItem.Info getItem() {
081    return this.item;
082  }
083
084  /**
085   * Get the metadata on the item.
086   *
087   * @return HashMap of metadata
088   */
089  public HashMap<String, ArrayList<Metadata>> getMetadata() {
090    return this.metadata;
091  }
092}