V1_0.java [src/java/m/svap/svap05a_svaphydroa] Revision: default  Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API, and application suite.
 *
 * 2012-2017, 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 m.svap.svap05a_svaphydroa;

import WaterLocations.Station;
import csip.annotations.Polling;
import csip.annotations.Resource;
import csip.ModelDataService;
import csip.ServiceException;
import WaterLocations.WaterLocations;
import WaterLocations.WaterLocationsInterface;
import csip.utils.JSONUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.ws.rs.Path;
import csip.annotations.Description;
import csip.annotations.Name;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import svap.utils.DBResources;

/**
 * SVAP-05a: Get Stream Gauging Station List
 *
 * This service returns a list of stream gauging stations within a bounding box
 * around a stream reach assessed using SVAP.
 *
 * @author Robert Streetman
 * @author Rumpal Sidhu
 * @version 1.0
 */
@Name("SVAP-05a: Get Stream Gauging Station List")
@Description("This service returns a list of stream gauging stations within a "
    + "bounding box around a stream reach assessed using SVAP.")
@Path("m/svap/svaphydroa/1.0")
@Polling(first = 10000, next = 2000)
@Resource(from = DBResources.class)

public class V1_0 extends ModelDataService {

  private int assessmentId;
  private String minLat, minLon, maxLat, maxLon;
  private String dataSource, description;

  private HashMap<String, Station> stationMap = new HashMap();


  @Override
  protected void preProcess() throws ServiceException {
    assessmentId = parameter().getInt("assessment_id");
    minLat = parameter().getString("latitude_minimum");
    maxLat = parameter().getString("latitude_maximum");
    minLon = parameter().getString("longitude_minimum");
    maxLon = parameter().getString("longitude_maximum");
  }


  @Override
  protected void doProcess() throws Exception {
    ArrayList<Station> stationList = getStationList();
    removeDuplicateStations(stationList);
  }


  @Override
  protected void postProcess() throws ServiceException, JSONException {
    results().put("assessment_id", assessmentId, "Assessment Identifier");
    if (stationMap.isEmpty()) {
      results().put("message", "No stream gauging stations were found within the bounding box.");
    } else {
      results().put("data_source", dataSource, description);
      results().put("number_stations_found", stationMap.size(), "Number of Stations in the Bounding Box");
      JSONArray stationArray = new JSONArray();

      for (String key : stationMap.keySet()) {
        JSONArray currentStationArray = new JSONArray();
        Station currentStation = stationMap.get(key);

        if (currentStation != null) {
          currentStationArray.put(JSONUtils.dataDesc("station_id", currentStation.StationId(), "Monitoring location identifier."));
          currentStationArray.put(JSONUtils.dataDesc("station_name", currentStation.StationName(), "Monitoring location name."));
          currentStationArray.put(JSONUtils.dataDesc("station_latitude", currentStation.Latitude(), "Monitoring location latitude."));
          currentStationArray.put(JSONUtils.dataDesc("station_longitude", currentStation.Longitude(), "Monitoring location longitude."));
        }
        stationArray.put(currentStationArray);
      }
      results().put("station_list", stationArray, "List of monitoring locations.");
    }
  }


  private ArrayList<Station> getStationList() throws IOException, Exception {
    WaterLocationsInterface usgs = WaterLocations.getNewWaterLocationsInterface("USGS");
    ArrayList<Station> stationList = usgs.getStations(minLat, maxLat, minLon, maxLon);

    //Check if stations were found
    if (stationList.isEmpty()) {    //If no USGS stations were found, try STORET
      WaterLocationsInterface storet = WaterLocations.getNewWaterLocationsInterface("STORET");
      stationList = storet.getStations(minLat, maxLat, minLon, maxLon);

      //Check if stations were found
      if (stationList.isEmpty()) {    //If no STORET stations were found, try CDSN
        WaterLocationsInterface cdsn = WaterLocations.getNewWaterLocationsInterface("CDSN");
        stationList = cdsn.getStations(minLat, maxLat, minLon, maxLon);

        if (stationList.isEmpty()) {    //No stations were found in any db, inform user
        } else {
          dataSource = "CDSN";
          description = "Colorado Data Sharing Network (coloradowaterdata.org)";
        }
      } else {
        dataSource = "STORET";
        description = "EPA Water Quality eXchange (WQX) STOrage and RETrieval database";
      }
    } else {
      dataSource = "USGS";
      description = "USGS National Water Information System";
    }
    return stationList;
  }


  private void removeDuplicateStations(ArrayList<Station> stationList) {
    for (Station station : stationList) {
      if (!stationMap.containsKey(station.StationId())) {
        stationMap.put(station.StationId(), station);
      }
    }
  }
}