StateCounty.java [src/java/crp/utils] Revision:   Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API, and application suite.
 *
 * 2012-2019, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package crp.utils;

import static csip.ModelDataService.KEY_METAINFO;
import static csip.ModelDataService.KEY_PARAMETER;
import csip.ServiceException;
import csip.utils.JSONUtils;
import java.util.Map;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
public class StateCounty extends ServiceCall {

  private double latitude, longitude;
  protected String metaInfo;
  protected String countyCode, county, stateAbbrev;

  public StateCounty(String _metaInfo, double latitude, double longitude, String URI) {
    super(URI);
    this.latitude = latitude;
    this.longitude = longitude;
    metaInfo = _metaInfo;
    errorPrefix = "StateCounty_ServiceCall";

  }

  public String countyCode() {
    return countyCode;
  }

  public String county() {
    return county;
  }

  public String stateAbbrev() {
    return stateAbbrev;
  }

  @Override
  protected void createRequest() throws ServiceException {
    JSONArray dataArray;
    JSONObject requestData;

    requestMetainfoObject = new JSONObject();
    request = new JSONObject();
    requestData = new JSONObject();
    dataArray = new JSONArray();

    try {
      requestMetainfoObject.put("MultipartRequest", "Bundled Service Request From csip-crp MetaModeling CRP Assessment Service");
      requestMetainfoObject.put("CRPAssessment_Metainfo", new JSONObject(metaInfo));
      if (asyncCall) {
        requestMetainfoObject.put("mode", "async");
      }
      request.put(KEY_METAINFO, requestMetainfoObject);

      dataArray.put(JSONUtils.data("latitude", latitude));
      dataArray.put(JSONUtils.data("longitude", longitude));
      request.put(KEY_PARAMETER, dataArray);

    } catch (JSONException ex) {
      throwServiceCallException("Cannot create the JSON request for Region call.", ex);
    }
  }

  @Override
  protected void parseResults() throws ServiceException {
    //   Error checking has already been done on these results.
    // If we get to this function, no further error
    // checking is necessary, except to look at the "results" array in the output, if desired.
    if (null == callResultSection) {
      throw new ServiceException((errorPrefix.isEmpty() ? "" : (errorPrefix + ":  ")) + "Cannot find results in the call to " + this.URI + " .");
    }

    try {
      //  Get the region results and store them.
      Map<String, JSONObject> resultMap = JSONUtils.getResults(results);
      countyCode = JSONUtils.getStringParam(resultMap, "county_code", "");
      county = JSONUtils.getStringParam(resultMap, "county", "");
      stateAbbrev = JSONUtils.getStringParam(resultMap, "state", "");

      if (countyCode.isEmpty() || county.isEmpty() || stateAbbrev.isEmpty()) {
        throwServiceCallException("Missing results for all or some of the result "
            + "JSON for this call. CountyCode:'" + countyCode + "', County:'"
            + county + "', StateAbbreviation:'" + stateAbbrev + "'");
      }

    } catch (JSONException ex) {
      throwServiceCallException("Could not build the results map: " + ex.getMessage(), ex);
    }
  }
}