All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.factual.driver.MultiResponse Maven / Gradle / Ivy

The newest version!
package com.factual.driver;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.json.JSONException;
import org.json.JSONObject;

import com.factual.driver.Factual.RequestImpl;

/**
 * Represents a Factual Multi response.
 * 
 * @author brandon
 *
 */
public class MultiResponse extends Response {
  private String json = null;
  private final Map data = new HashMap();

  private Map requestMapping = null;

  /**
   * 
   * @param requestMapping
   */
  public MultiResponse(Map requestMapping) {
    super(null);
    this.requestMapping = requestMapping;
  }

  /**
   * Parses from a json response string
   * @param json json response string to parse from
   */
  public void setJson(String json) {
    this.json = json;
    try {
      JSONObject rootJsonObj = new JSONObject(json);
      parseResponse(rootJsonObj);
    } catch (JSONException e) {
      throw new RuntimeException(e);
    }
  }

  private void parseResponse(JSONObject jo) throws JSONException {
    data.clear();
    for (Entry entry : requestMapping.entrySet()) {
      String responseJson = jo.getJSONObject(entry.getKey()).toString();
      RequestImpl query = entry.getValue();
      InternalResponse internalResp = new InternalResponse(responseJson);
      Response resp = query.getResponse(internalResp);
      if (resp != null)
        data.put(entry.getKey(), resp);
    }
  }

  /**
   * A collection of the responses returned by Factual for a multi query.
   * 
   * @return the multi query data returned by Factual.
   */
  public Map getData() {
    return data;
  }

  @Override
  public String getJson() {
    return json;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy