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

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

The newest version!
package com.factual.driver;

import java.util.List;
import java.util.Map;

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

import com.google.common.collect.Maps;



/**
 * Represents the response from running a schema request against Factual.
 *
 * @author aaron
 */
public class SchemaResponse extends Response implements Tabular {
  private final Map columnSchemas;
  private final String title;
  private final boolean searchEnabled;
  private final boolean geoEnabled;
  private final String description;
  private final List> data;

  /**
   * Constructor, parses from a JSON response String.
   */
  public SchemaResponse(InternalResponse resp) {
    super(resp);
    try{
      JSONObject rootJsonObj = new JSONObject(resp.getContent());
      Response.withMeta(this, rootJsonObj);
      JSONObject respObj = rootJsonObj.getJSONObject(Constants.RESPONSE);
      JSONObject view = respObj.getJSONObject(Constants.SCHEMA_VIEW);
      data = JsonUtil.data(view.getJSONArray(Constants.SCHEMA_FIELDS));
      columnSchemas = makeColumnSchemas(data);
      title = view.getString(Constants.SCHEMA_TITLE);
      description = view.getString(Constants.SCHEMA_DESCRIPTION);
      searchEnabled = view.getBoolean(Constants.SCHEMA_SEARCH_ENABLED);
      geoEnabled = view.getBoolean(Constants.SCHEMA_GEO_ENABLED);
    } catch (JSONException e) {
      throw new RuntimeException(e);
    }
  }

  private Map makeColumnSchemas(List> data) {
    Map schemas = Maps.newHashMap();
    for(Map smap : data) {
      schemas.put(smap.get(Constants.SCHEMA_COLUMN_NAME).toString(), new ColumnSchema(smap));
    }
    return schemas;
  }

  public String getTitle() {
    return title;
  }

  public String getDescription() {
    return description;
  }

  public boolean isSearchEnabled() {
    return searchEnabled;
  }

  public boolean isGeoEnabled() {
    return geoEnabled;
  }

  /**
   * @return The full JSON response from Factual
   */
  @Override
  public String getJson() {
    return resp.getContent();
  }

  /**
   * @return the size of the schema (that is, the number of columns in the
   *         table)
   */
  public int size() {
    return columnSchemas.size();
  }

  public Map getColumnSchemas() {
    return columnSchemas;
  }

  public ColumnSchema getColumnSchema(String columnName) {
    return columnSchemas.get(columnName);
  }

  @Override
  public List> getData() {
    return data;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy