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

import static adb.DBResources.R2GIS_SQLSVR;
import csip.Config;
import csip.ModelDataService;
import csip.annotations.Description;
import csip.annotations.Name;
import csip.annotations.Resource;
import csip.api.server.ServiceException;
import csip.utils.Validation;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.ws.rs.Path;
import soils.Component;
import soils.MapUnit;
import static soils.db.DBResources.SDM;
import soils.db.SOILS_DATA;
import soils.db.SOILS_DB_Factory;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
@Name("WWE-02:  WEPS Soil Input IFC (wepssoilinput)")
@Description("Gets data from the NRCS Soil Data Mart and generates a soil IFC input file for the WEPS model. (Wind Erosion Prediction System)")
@Path("d/wepssoilinput/3.0")
@Resource(from = soils.db.DBResources.class)

public class V3_0 extends ModelDataService {

  protected static final String KEY_COKEY = "cokey";
  protected static final String STREAM_FILE = "stream_file";
  protected static final String DEFAULT_SDM_SOURCE = "SDM_REST";
  protected static final String KEY_AVG_STRATIFIED = "avg_stratified_layers";
  protected static final String KEY_MAX_ORGANIC_DEPTH = "max_organic_depth";
  public static final String KEY_ORGANIC_LAYER_THRESHOLD = "organic_layer_threshold";
  protected static final String KEY_CHECK_ORGANIC_SURFACE = "check_organic_surface";
  protected final String KEY_ESTIMATE_NULLS = "estimate_nulls";

  protected boolean streamFile = false;
  protected Component comp = null;
  protected MapUnit mapUnit = null;
  protected File ifcFile = null;
  protected String fileOut = "";
  protected String cokey;

  @Override
  protected void preProcess() throws Exception {
    cokey = parameter().getString(KEY_COKEY);
    streamFile = parameter().getBoolean(STREAM_FILE, false);
    Validation.checkCokey(cokey);
  }

  @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", "wwe-02 3.0");
      }
    };
  }

  @Override
  protected void doProcess() throws Exception {
    comp = new Component();
    comp.cokey(cokey);

    //  Get the WEPS filtered SOILS data for the cokey provided.
    try (SOILS_DATA soilsDb = SOILS_DB_Factory.createEngine(getClass(), LOG, Config.getString("soils.gis.database.source"))) {
      if (soilsDb.validateComponent(Integer.parseInt(comp.cokey()))) {
        if (null == (mapUnit = soilsDb.findWEPSDataByCokey(comp, Config.getBoolean(KEY_AVG_STRATIFIED, true), Config.getBoolean(KEY_CHECK_ORGANIC_SURFACE, false), Config.getBoolean(KEY_ESTIMATE_NULLS, true)))) {
          throw new ServiceException("Could not retrieve the proper SDM soils data for this cokey");
        }
      } else {
        throw new ServiceException("The cokey value provided does not exist in the SDM database.  Cannot continue.");
      }
    }

    //  Don't think these two are needed, test...
    if (!comp.isOrganicSoil() && comp.isExcluded()) {
      throw new ServiceException("Component: " + comp.compname() + " has been excluded. Reason: " + comp.getExcludedReason());
    }

    ifcFile = (streamFile ? null : (workspace().getFile(comp.compname() + ".ifc")));
    fileOut = mapUnit.toIfc(cokey);

    if (!streamFile) {
      workspace().writeString(comp.compname() + ".ifc", fileOut);
    }

  }

  @Override
  protected void postProcess() throws Exception {
    if (!streamFile && ifcFile.exists()) {
      results().put(ifcFile);
    } else {
      if (!streamFile) {
        throw new ServiceException("File " + ifcFile.getName() + " could not be created.");
      } else {
        if ((null != fileOut) && (!fileOut.isEmpty()) && (fileOut.length() > 0)) {
          results().put(comp.compname(), fileOut);
        } else {
          throw new ServiceException("Resulting ifc file was empty, cannot proceed");
        }
      }
    }
  }
}