V2_1.java [src/java/d/soils/wqm02_wqmsoilparams] 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 d.soils.wqm02_wqmsoilparams;

/**
 *
 * @author Shaun Case
 */
import static adb.DBResources.EROSION_SQLSVR;
import static adb.DBResources.R2GIS_SQLSVR;
import csip.Config;
import csip.api.server.ServiceException;
import csip.SessionLogger;
import csip.annotations.Resource;
import gisobjects.GISObjectException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import javax.ws.rs.Path;
import csip.annotations.Description;
import csip.annotations.Name;
import java.util.LinkedHashMap;
import java.util.Map;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import soils.Component;
import soils.MapUnit;
import soils.db.DBResources;
import static soils.db.DBResources.SDM;
import soils.db.SOILS_DATA;
import soils.db.SOILS_DB_Factory;

/**
 * WQM-02: WQMSoilAttributes
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 *
 */
@Name("WQM-02: Water Quality Soil Parameters (wqmsoilparams)")
@Description("Intersects area of analysis (AoA) geometry with NRCS Soil Data Mart (SDM) mapunit geometry, derives a list of distinct soil components for the AoA, and gets parameters from SDM tables for computing nutrient and pesticide loss potentials")
@Path("d/wqmsoilparams/2.1")

@Resource(from = DBResources.class)
public class V2_1 extends V2_0 {

  @Override
  protected Map<String, Object> getConfigInfo() {
    return new LinkedHashMap<String, Object>() {
      {
        put("soils.gis.database.source", resources().getResolved("soils.gis.database.source"));
        put(SDM, resources().getResolved(SDM));
        put(R2GIS_SQLSVR, resources().getResolved(R2GIS_SQLSVR));
        put("fpp.version", "wqm-02 2.1");
      }
    };
  }

  @Override
  protected void doProcess() throws ServiceException, SQLException, GISObjectException, JSONException, IOException, Exception {
    if (null != aoa_geometry) {
      try (SOILS_DATA soilsDb = SOILS_DB_Factory.createEngine(getClass(), LOG, Config.getString("soils.gis.database.source")); Connection gisDb = resources().getJDBC(EROSION_SQLSVR);) {

        V2_1.AoA aoaObject = new V2_1.AoA(soilsDb, gisDb, aoa_geometry, LOG, minimumPercentage);

        aoaObject.getIntersectionsAndComponents();
        aoa_area = aoaObject.getArea();
        results = aoaObject.toJSON();
        excludeds = aoaObject.getExcluded();
        if (aoaObject.hasGeomChanged()) {
          newWKT = aoaObject.shapeWKT();
        }
      }
    }
  }

  public class AoA extends V2_0.AoA {

    public AoA(SOILS_DATA soilsDb, Connection gisDb, JSONObject geometry, SessionLogger LOG, double minimumPercentage) throws GISObjectException, SQLException, JSONException, IOException, ServiceException {
      super(soilsDb, gisDb, geometry, LOG, minimumPercentage);
    }

    /**
     *
     * @throws ServiceException
     * @throws GISObjectException
     */
    @Override
    public void getIntersectionsAndComponents() throws ServiceException, GISObjectException {
      if (findIntersectedMapUnitsWithAreas()) {
        if (soilsDb.findAllBasicComponentHorizonFragTextureData(map_units)) {
          for (MapUnit mapUnit : map_units.values()) {
            for (Component tComponent : mapUnit.components().values()) {
              tComponent.applyHorizonFilter("WATER");
            }
          }

          removeComponentsByAreaSize();
          findComponentSoilMoistures();
          for (MapUnit mapUnit : map_units.values()) {
            if (!mapUnit.isExcluded()) {
              for (Component component : mapUnit.components().values()) {
                if (!component.isExcluded()) {
                  computeHorizonResults(component);
                }
              }
            }
          }
        } else {
          throw new ServiceException("Cannot find components for the map units interesected by this AoA");
        }
      } else {
        throw new ServiceException("Cannot find the mapunits intersected by this AoA");
      }
    }
  }
}