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

import csip.Config;
import csip.ModelDataService;
import csip.annotations.Description;
import csip.annotations.Name;
import csip.annotations.Resource;
import csip.api.server.ServiceException;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
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-04:  WEPP Soil Input XML (weppsoilinput)")
@Description("Gets data from the NRCS Soil Data Mart and generates a soil component sol input file for the WEPP model. (Water Erosion Prediction Project)")
@Path("d/weppsoilinput/2.0")
@Resource(from = soils.db.DBResources.class)
public class V2_0 extends ModelDataService {

  static final String KEY_COKEY = "cokey";
  static final String STREAM_FILE = "stream_file";

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

  @Override
  protected void preProcess() throws Exception {
    cokey = parameter().getString(KEY_COKEY);
    streamFile = parameter().getBoolean(STREAM_FILE, false);
    // Validation.checkCokey(cokey); Does NOT actually validate a cokey, merely makes sure it is an interger...see actual validation function below
  }

  @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", "wwe-04 2.0");
      }
    };
  }

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

    //  Get the WEPP 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.findWEPPDataByCokey(comp))) {
          throw new ServiceException("Could not retrieve the proper SDM soils data for this cokey");
        }
      } else {
        throw new ServiceException("The cokey value provided, " + comp.cokey() + ", does not exist in the SDM database.  Cannot continue.");
      }
    }

    solFile = ((streamFile) ? null : (workspace().getFile(comp.compname() + ".sol")));
    fileOut = mapUnit.components().get(cokey).toSol();

    if (!streamFile) {
      try (BufferedWriter buffWriter = new BufferedWriter(new FileWriter(solFile))) {
        buffWriter.write(fileOut);
      }
    }
  }

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

}