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

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

/**
 * COMET-01: Get Soil Parameters for Display in CDSI Application
 *
 * @author rumpal
 * @version 1.0
 */
@Name("COMET-01: Get Soil Parameters for Display in CDSI Application")
@Description("This service fetches parameters relevant to an air quality "
    + "resource assessment workflow from the NRCS Soil Data Mart in the "
    + "USDA Data Center through the CDSI csip-soils layer for display in a "
    + "CDSI application, such as the Conservation Desktop.")
@Path("m/comet/cometsoilparams/1.0")
@Polling(first = 10000, next = 2000)
@Resource(from = DBResources.class)
@Deprecated
public class V1_0 extends ModelDataService {

  private int aoaId, resourceAssessmentId;
  private JSONObject aoaGeometry;
  private ArrayList<MapUint> mapunitList = new ArrayList<>();


  @Override
  public void preProcess() throws ServiceException {
    aoaId = parameter().getInt("AoAId");
    resourceAssessmentId = parameter().getInt("resource_assessment_id");
    aoaGeometry = parameter().getParamJSON("aoa_geometry");
  }

  @Override
  public void doProcess() throws ServiceException {

    GISObject aoa_geometry;
    try (Connection connection = resources().getJDBC(CRDB)) {
      GISEngine gisEngine = createGISEngine(connection);
      aoa_geometry = GISObjectFactory.createGISObject(aoaGeometry, gisEngine);
    } catch (SQLException | GISObjectException | IOException | JSONException ex) {
      throw new ServiceException(ex);
    }

    try (Connection sdmConn = resources().getJDBC(SDM_REST);
        Statement stmt = sdmConn.createStatement();
        ResultSet resultSet = stmt.executeQuery(DBQueries.COMET01Query01(aoa_geometry));) {
      while (resultSet.next()) {
        int mukey = resultSet.getInt("mukey");
        String musym = resultSet.getString("musym");
        String muname = resultSet.getString("muname");
        double muarea = resultSet.getDouble("sizeIntersectionAcres");

        try (Statement stmt1 = sdmConn.createStatement();
            ResultSet rs = stmt1.executeQuery(DBQueries.COMET01Query02(mukey));) {

          ArrayList<Component> compList = new ArrayList<>();
          while (rs.next()) {
            int cokey = rs.getInt("cokey");
            String compname = rs.getString("compname");
            int comppct_r = rs.getInt("comppct_r");
            String taxorder = rs.getString("taxorder");
            compList.add(new Component(cokey, compname, comppct_r, taxorder));
          }
          mapunitList.add(new MapUint(mukey, muname, musym, muarea, compList));
        }
      }
    } catch (SQLException ex) {
      throw new ServiceException(ex);
    }
  }


  @Override
  public void postProcess() throws ServiceException {
    results().put("AoAId", aoaId, "Area of Analysis Identifier");
    results().put("resource_assessment_id", resourceAssessmentId, "Resource Assessment Identifier");
    results().put("aoa_geometry", aoaGeometry, "Area of Analysis Geometry");

    JSONArray mapunitArray = new JSONArray();
    try {
      for (MapUint mapUnit : mapunitList) {
        JSONArray mapunitArr = new JSONArray();
        mapunitArr.put(JSONUtils.dataDesc("mukey", mapUnit.mukey, "Soil Mapunit Key"));
        mapunitArr.put(JSONUtils.dataDesc("musym", mapUnit.musysm, "Soil Mapunit Symbol"));
        mapunitArr.put(JSONUtils.dataDesc("muname", mapUnit.muname, "Soil Mapunit Name"));
        mapunitArr.put(JSONUtils.dataDesc("muarea", mapUnit.muarea, "Soil Mapunit Area Within the Area of Analysis"));

        JSONArray compArray = new JSONArray();
        for (Component component : mapUnit.compList) {
          JSONArray compArr = new JSONArray();

          compArr.put(JSONUtils.dataDesc("cokey", component.cokey, "Soil Component Key"));
          compArr.put(JSONUtils.dataDesc("compname", component.compname, "Soil Component Name"));
          compArr.put(JSONUtils.dataDesc("comppct_r", component.comppct_r, "Soil Component Percentage of the Soil Mapunit"));
          compArr.put(JSONUtils.dataDesc("taxorder", component.taxorder, "Soil Taxonomic Order"));

          compArray.put(compArr);
        }

        mapunitArr.put(JSONUtils.data("Soil Component List", compArray));
        mapunitArray.put(mapunitArr);
      }
    } catch (JSONException ex) {
      throw new ServiceException(ex);
    }
    results().put("Soil Mapunit List", mapunitArray);
  }

  static class MapUint {

    protected int mukey;
    protected String muname;
    protected String musysm;
    protected double muarea;
    protected ArrayList<Component> compList;


    public MapUint(int mukey, String muname, String musysm, double muarea, ArrayList<Component> compList) {
      this.mukey = mukey;
      this.muname = muname;
      this.musysm = musysm;
      this.muarea = muarea;
      this.compList = compList;
    }
  }

  static class Component {

    protected int cokey;
    protected String compname;
    protected int comppct_r;
    protected String taxorder;


    public Component(int cokey, String compname, int comppct_r, String taxorder) {
      this.cokey = cokey;
      this.compname = compname;
      this.comppct_r = comppct_r;
      this.taxorder = taxorder;
    }
  }

}