StationMetaDataService.java [src/java/svap/utils] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package svap.utils;

import csip.Config;
import csip.ServiceException;
import gisobjects.GISObjectException;
import java.util.ArrayList;
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 StationMetaDataService extends ACIS {

  private static final String url;
  private static final String BBOX_TYPE = "bbox";
  private static final String COUNTY_TYPE = "county";
  private static final String STATE_TYPE = "state";

  private static final String ACIS_STNMETA_URL = "http://data.rcc-acis.org/StnMeta";
  private JSONObject metaInfo;
  private String bbox, state_county_code, state_code;
  private String searchType = BBOX_TYPE;


  static {
    url = Config.getString("service.acis.stnmeta.url", ACIS_STNMETA_URL);
  }


  public StationMetaDataService() {
    super(url);
  }


  public final void setSearchType(String type) throws ServiceException {
    switch (type.toLowerCase()) {
      case BBOX_TYPE:
        searchType = BBOX_TYPE;
        break;
      case COUNTY_TYPE:
        searchType = COUNTY_TYPE;
        break;
      case STATE_TYPE:
        searchType = STATE_TYPE;
        break;
      default:
        throw new ServiceException("Cannot set that service request type, " + type + ", for the ACIS call.");
    }
  }


  @Override
  protected void createRequest() throws ServiceException {
    try {
      request = new JSONObject();
      switch (searchType) {
        case BBOX_TYPE:
          request.put(BBOX_TYPE, bbox);
          break;
        case COUNTY_TYPE:
          request.put(COUNTY_TYPE, state_county_code);
          break;
        case STATE_TYPE:
          request.put(COUNTY_TYPE, state_county_code);
          break;
        default:
          throw new ServiceException("Invalid search type, " + searchType + ", specified to the ACIS service layer.");
      }

      request.put("meta", meta);
      request.put("elems", new JSONArray(elems));
      request.put("sdate", sDate);
      request.put("edate", eDate);

    } catch (JSONException ex) {
      throw new ServiceException("Cannot build the request JSON to send to ACIS: " + ex.getMessage(), ex);
    }
  }


  @Override
  protected void parseResults() throws ServiceException {
    //  For ACIS, the results are stored in a JSONArray under the name "Meta".
    try {
      JSONArray stationList = results.getJSONArray("meta");

      for (int i = 0; i < stationList.length(); i++) {
        ClimateStation station = new ClimateStation();
        JSONObject stationData = null;
        try {
          stationData = stationList.getJSONObject(i);
          station.getDataFromJSON(stationData);
        } catch (JSONException | GISObjectException ex) {
          throw new ServiceException("Error reading station data returned from ACIS: " + ex.getMessage(), ex);
        }
        stations.add(station);
      }
    } catch (JSONException ex) {
      throw new ServiceException("Cannot find result data, 'meta', in return from ACIS: " + ex.getMessage(), ex);
    }
  }


  public ArrayList<ClimateStation> searchBBox(String coordinates, String sDate, String eDate) throws ServiceException {
    setSearchType(BBOX_TYPE);
    bbox = coordinates;
    this.sDate = sDate;
    this.eDate = eDate;

    return getStationList();
  }


  public ArrayList<ClimateStation> searchCounty(String state_county_code, String sDate, String eDate) throws ServiceException {
    setSearchType(COUNTY_TYPE);
    this.state_county_code = state_county_code;
    this.sDate = sDate;
    this.eDate = eDate;

    return getStationList();
  }


  public ArrayList<ClimateStation> searchState(String state_abbr, String sDate, String eDate) throws ServiceException {
    setSearchType(STATE_TYPE);
    this.state_code = state_abbr;
    this.sDate = sDate;
    this.eDate = eDate;

    return getStationList();
  }


  private ArrayList<ClimateStation> getStationList() throws ServiceException {
    stations.clear();
    call();

    return stations;
  }
}