V1_0.java [src/java/m/svap/svap01c_getmlra] 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.svap01c_getmlra;

import csip.annotations.Polling;
import csip.annotations.Resource;
import csip.ModelDataService;
import csip.ServiceException;
import gisobjects.GISObject;
import gisobjects.GISObjectException;
import gisobjects.GISObjectFactory;
import gisobjects.db.GISEngine;
import static gisobjects.db.GISEngineFactory.createGISEngine;
import java.io.IOException;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.ws.rs.Path;
import csip.annotations.Description;
import csip.annotations.Name;
import org.codehaus.jettison.json.JSONObject;
import org.codehaus.jettison.json.JSONException;
import svap.utils.DBQueries;
import svap.utils.DBResources;
import static svap.utils.DBResources.CRDB;

/**
 * SVAP-01c: Get Major Land Resource Area for Watershed Description
 *
 * This service returns the identifier and name of the Major Land Resource Area
 * (MLRA) for the stream reach to be assessed using SVAP.
 *
 * @author Robert Streetman
 * @author Rumpal Sidhu
 * @version 1.0
 */
@Name("SVAP-01c: Get Major Land Resource Area for Watershed Description")
@Description("This service returns the identifier and name of the Major "
    + "Land Resource Area (MLRA) for the stream reach to be assessed using SVAP.")
@Path("m/svap/getmlra/1.0")
@Polling(first = 10000, next = 2000)
@Resource(from = DBResources.class)

public class V1_0 extends ModelDataService {

  private JSONObject assessmentLocation;
  private int assessmentId;

  private String mlraName, mlraSym, lrrsym, lrrname;
  private int mlraID;


  @Override
  protected void preProcess() throws ServiceException {
    assessmentId = parameter().getInt("assessment_id");
    assessmentLocation = parameter().getJSON("assessment_location");
  }


  @Override
  protected void doProcess() throws ServiceException, SQLException, GISObjectException, JSONException, IOException, URISyntaxException {
    try (Connection connection = resources().getJDBC(CRDB);
        GISEngine gisEngine = createGISEngine(connection)) {
      GISObject hucGIS = GISObjectFactory.createGISObject(assessmentLocation, gisEngine);
      try (Statement statement = connection.createStatement();
          ResultSet resultSet = statement.executeQuery(DBQueries.SVAP1cQuery01(hucGIS.toWKT()));) {
        while (resultSet.next()) {
          mlraName = resultSet.getString("mlra_name");
          mlraSym = resultSet.getString("mlrarsym");
          mlraID = resultSet.getInt("mlra_id");
          lrrsym = resultSet.getString("lrrsym");
          lrrname = resultSet.getString("lrr_name");
        }
      }
    }
  }


  @Override
  protected void postProcess() throws ServiceException {
    results().put("assessment_id", assessmentId, "Assessment Identifier");
    results().put("mlra_name", mlraName, "Major Land Resource Area Name");
    results().put("mlra_sym", mlraSym, "Major Land Resource Area Symbol");
    results().put("mlra_identifier", mlraID, "Major Land Resource Area Identifier");
    results().put("lrrsym", lrrsym, "Land Resource Region Symbol");
    results().put("lrrname", lrrname, "Land Resource Region Name");
  }

}