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 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.
014 */
015public class BoxMetadataQueryItem extends BoxJSONObject {
016    private BoxItem.Info item;
017    private HashMap<String, ArrayList<Metadata>> metadata;
018    private BoxAPIConnection api;
019
020    /**
021     * Construct a BoxMetadataQueryItem.
022     *
023     * @param jsonObject the parsed JSON object.
024     * @param api        the API connection to be used to fetch interacted item
025     */
026    public BoxMetadataQueryItem(JsonObject jsonObject, BoxAPIConnection api) {
027        super(jsonObject);
028        this.api = api;
029    }
030
031    @Override
032    protected void parseJSONMember(JsonObject.Member member) {
033        super.parseJSONMember(member);
034
035        String memberName = member.getName();
036        JsonValue value = member.getValue();
037        if (memberName.equals("item")) {
038            String id = value.asObject().get("id").asString();
039            String type = value.asObject().get("type").asString();
040            this.item = new BoxFile(this.api, id).new Info(value.asObject());
041            if (type.equals("folder")) {
042                BoxFolder folder = new BoxFolder(this.api, id);
043                this.item = folder.new Info(value.asObject());
044            } else if (type.equals("file")) {
045                BoxFile file = new BoxFile(this.api, id);
046                this.item = file.new Info(value.asObject());
047            } else if (type.equals("web_link")) {
048                BoxWebLink link = new BoxWebLink(this.api, id);
049                this.item = link.new Info(value.asObject());
050            } else {
051                assert false : "Unsupported item type: " + type;
052                throw new BoxAPIException("Unsupported item type: " + type);
053            }
054        } else if (memberName.equals("metadata")) {
055            this.metadata = new HashMap<String, ArrayList<Metadata>>();
056            JsonObject metadataObject = value.asObject();
057            for (JsonObject.Member enterprise : metadataObject) {
058                String enterpriseName = enterprise.getName();
059                JsonObject templates = enterprise.getValue().asObject();
060                ArrayList<Metadata> enterpriseMetadataArray = new ArrayList<Metadata>();
061                for (JsonObject.Member template : templates) {
062                    String templateName = template.getName();
063                    JsonObject templateValue = template.getValue().asObject();
064                    Metadata metadataOfTemplate = new Metadata(templateValue);
065                    metadataOfTemplate.add("/$scope", enterpriseName);
066                    metadataOfTemplate.add("/$template", templateName);
067                    enterpriseMetadataArray.add(metadataOfTemplate);
068                }
069                this.metadata.put(enterpriseName, enterpriseMetadataArray);
070            }
071        }
072    }
073
074    /**
075     * Get the item which was interacted with.
076     *
077     * @return box item
078     */
079    public BoxItem.Info getItem() {
080        return this.item;
081    }
082
083    /**
084     * Get the metadata on the item.
085     *
086     * @return HashMap of metadata
087     */
088    public HashMap<String, ArrayList<Metadata>> getMetadata() {
089        return this.metadata;
090    }
091
092}