001package com.box.sdk.internal.utils;
002
003import com.box.sdk.Metadata;
004import com.box.sdk.Representation;
005import com.eclipsesource.json.JsonObject;
006import com.eclipsesource.json.JsonValue;
007import java.util.ArrayList;
008import java.util.HashMap;
009import java.util.List;
010import java.util.Map;
011
012/**
013 * Utility class for constructing metadata map from json object.
014 */
015public class Parsers {
016
017    /**
018     * Only static members.
019     */
020    protected Parsers() {
021    }
022
023    /**
024     * Creates a map of metadata from json.
025     *
026     * @param jsonObject metadata json object for metadata field in get /files?fileds=,etadata.scope.template response
027     * @return Map of String as key a value another Map with a String key and Metadata value
028     */
029    public static Map<String, Map<String, Metadata>> parseAndPopulateMetadataMap(JsonObject jsonObject) {
030        Map<String, Map<String, Metadata>> metadataMap = new HashMap<String, Map<String, Metadata>>();
031        //Parse all templates
032        for (JsonObject.Member templateMember : jsonObject) {
033            if (templateMember.getValue().isNull()) {
034                continue;
035            } else {
036                String templateName = templateMember.getName();
037                Map<String, Metadata> scopeMap = metadataMap.get(templateName);
038                //If templateName doesn't yet exist then create an entry with empty scope map
039                if (scopeMap == null) {
040                    scopeMap = new HashMap<String, Metadata>();
041                    metadataMap.put(templateName, scopeMap);
042                }
043                //Parse all scopes in a template
044                for (JsonObject.Member scopeMember : templateMember.getValue().asObject()) {
045                    String scope = scopeMember.getName();
046                    Metadata metadataObject = new Metadata(scopeMember.getValue().asObject());
047                    scopeMap.put(scope, metadataObject);
048                }
049            }
050
051        }
052        return metadataMap;
053    }
054
055    /**
056     * Parse representations from a file object response.
057     *
058     * @param jsonObject representations json object in get response for /files/file-id?fields=representations
059     * @return list of representations
060     */
061    public static List<Representation> parseRepresentations(JsonObject jsonObject) {
062        List<Representation> representations = new ArrayList<Representation>();
063        for (JsonValue representationJson : jsonObject.get("entries").asArray()) {
064            Representation representation = new Representation(representationJson.asObject());
065            representations.add(representation);
066        }
067        return representations;
068    }
069}