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

import csip.Config;
import csip.ModelDataService;
import csip.annotations.Resource;
import csip.api.server.PayloadResults;
import csip.api.server.ServiceException;
import static d.soils.wwe01_wwesoilparams.SoilParamsAoAFactory.WWE_SOIL_PARAMS_2_1;
import gisobjects.GISObject;
import gisobjects.GISObjectException;
import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import static soils.AoA.CORRECTED_GEOMETRY;
import static soils.AoA.EXCLUDED_LIST;
import soils.MapUnit;
import soils.SoilsData;
import soils.db.DBResources;
import static soils.db.DBResources.SDM;
import soils.db.SOILS_DATA;
import soils.db.SOILS_DB_Factory;
import static soils.utils.EvalResult.writeDouble;

/**
 *
 * @author scase
 */
@Resource(from = DBResources.class)
public class SoilParamsService extends ModelDataService {

  GISObject aoa_geometry;
  JSONObject aoa_input_geometry;
  double aoiArea;
  JSONArray outputObj;
  HashMap<String, MapUnit> soils;
  String newWKT = "";
  protected JSONArray excludeds;
  protected JSONArray results;
  protected String serviceVersion;
  protected String mupolygonkey = "";
  protected ArrayList<MapUnit> mapunits = new ArrayList<>();

  public SoilParamsService(String serviceVersion) {
    this.serviceVersion = serviceVersion;
  }

  @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("fpp.version", serviceVersion);
      }
    };
  }


    @Override
    protected void doProcess()  throws ServiceException, GISObjectException, IOException, Exception {
    try (SOILS_DATA soilsDb = SOILS_DB_Factory.createEngine(getClass(), LOG, Config.getString("soils.gis.database.source")); Connection gisDb = resources().getJDBC(SDM);) {
      SoilParamsAoA aoaObject;
      if (!mupolygonkey.isEmpty()) {
        aoaObject = SoilParamsAoAFactory.getAoA(serviceVersion, soilsDb, gisDb, mupolygonkey, LOG);
      } else {
        aoaObject = SoilParamsAoAFactory.getAoA(serviceVersion, soilsDb, gisDb, aoa_input_geometry, LOG);
        aoiArea = aoaObject.getArea();
      }

      aoaObject.findSoils();
      aoiArea = aoaObject.getArea();
      results = aoaObject.toJSON();
      excludeds = aoaObject.getExcluded();
      mapunits.addAll(aoaObject.getMapUnits().values());

      if (aoaObject.hasGeomChanged()) {
        newWKT = aoaObject.shapeWKT();
      }
    }
  }

  @Override
  protected void postProcess() throws Exception {
    PayloadResults result = results();
    result.put("AoA Area", writeDouble(aoiArea, "%.3f"), "acres");
    result.put(SoilsData.MAP_UNIT_LIST, results);
    result.put(EXCLUDED_LIST, excludeds);
    if (!newWKT.isEmpty()) {
      result.put(CORRECTED_GEOMETRY, newWKT, "If this section is present, the input geometry was invalid.  This service attempted to correct it, and was successful in creating a new geometry that could be utilized.  Please check this corrected geometry to be sure it represents what you originally intended.  If it does, please contact the source of your original geometry to have it corrected.  You may use this WKT to do so.");
    }
  }

  @Override
  protected void preProcess() throws ServiceException {
    if ((parameter().has("mupolygonkey")) && this.serviceVersion.equalsIgnoreCase(WWE_SOIL_PARAMS_2_1)) {
      mupolygonkey = parameter().getString("mupolygonkey", "");
    } else {
      aoa_input_geometry = parameter().getParamJSON("aoa_geometry");
    }
  }
}