SMWEPP.java [src/java/crp/utils] 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-2019, 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 crp.utils;

import static csip.ModelDataService.KEY_METAINFO;
import static csip.ModelDataService.KEY_PARAMETER;
import csip.ServiceException;
import csip.utils.JSONUtils;
import java.util.Map;
import data.models.WEPPMetaData;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
public class SMWEPP extends ServiceCall {

  private String metaInfo;
  private WEPPMetaData modelData;
  private String ann_name;
  private double waterErosion;

  public SMWEPP(String metaInfo, String ann_name, WEPPMetaData modelData, String URI) {
    super(URI);
    this.metaInfo = metaInfo;
    this.modelData = modelData;
    this.ann_name = ann_name;
    errorPrefix = "SMWEPPServiceCall";
  }

  public SMWEPP(String metaInfo, String ann_name, WEPPMetaData modelData, String URI, Object data, CheckedServiceResult<ServiceCallData> serviceCallback) {
    super(URI, data, serviceCallback);
    this.metaInfo = metaInfo;
    this.modelData = modelData;
    this.ann_name = ann_name;
    errorPrefix = "SMWEPPServiceCall";
  }

  public SMWEPP(String metaInfo, String ann_name, WEPPMetaData modelData, String URI, CheckedServiceResult<ServiceCallData> serviceCallback) {
    super(URI, serviceCallback);
    this.metaInfo = metaInfo;
    this.modelData = modelData;
    this.ann_name = ann_name;
    errorPrefix = "SMWEPPServiceCall";
  }

  @Override
  protected void parseResults() throws ServiceException {
    //   Error checking has already been done on these results.
    // If we get to this function, no further error
    // checking is necessary, except to look at the "results" array in the output, if desired.
    if (null == callResultSection) {
      throw new ServiceException((errorPrefix.isEmpty() ? "" : (errorPrefix + ":  ")) + "Cannot find results in the call to " + this.URI + " .");
    }

    try {
      //  Get the climate file and store it.
      Map<String, JSONObject> resultMap = JSONUtils.getResults(results);
      if (resultMap.containsKey("soil_loss")) {
        waterErosion = JSONUtils.getDoubleParam(resultMap, "soil_loss", Double.NaN);
      } else {
        throwServiceCallException("Cannot find the soil_loss result.");
      }
    } catch (JSONException ex) {
      throwServiceCallException("Error trying to find results section", ex);
    }
  }

  @Override
  protected void createRequest() throws ServiceException {
    JSONArray dataArray;

    requestMetainfoObject = new JSONObject();
    request = new JSONObject();

    dataArray = new JSONArray();
    try {
      requestMetainfoObject.put("MultipartRequest", "Bundled Service Request From csip-crp MetaModeling CliWindSoil Service");
      requestMetainfoObject.put("SMCRPAssessment_Metainfo", new JSONObject(metaInfo));
      if (asyncCall) {
        requestMetainfoObject.put("mode", "async");
      }

      request.put(KEY_METAINFO, requestMetainfoObject);

      dataArray.put(JSONUtils.data("ann_name", ann_name));
      dataArray.put(JSONUtils.data("annualPrecip", modelData.annualPrecip()));
      dataArray.put(JSONUtils.data("avg_annual_precip_duration", modelData.avgAnnualPrecipEventDuration()));
      dataArray.put(JSONUtils.data("effective_surface_conductivity", modelData.effectiveSurfaceConductivity()));
      dataArray.put(JSONUtils.data("rill_erodibility", modelData.rillErodibility()));
      dataArray.put(JSONUtils.data("slope_steepness", modelData.slopeSteepness()));
      dataArray.put(JSONUtils.data("wavg_clay_fraction", modelData.wAvgSoilClayFraction()));
      dataArray.put(JSONUtils.data("wavg_sand_fraction", modelData.wAvgSoilSandFraction()));

      request.put(KEY_PARAMETER, dataArray);
      requestJSON = request;
      //System.out.println(request.toString());

    } catch (JSONException ex) {
      throwServiceCallException("Cannot create the JSON request.", ex);
    }
  }

  public double waterErosion() {
    return waterErosion;
  }
}