WEPP.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 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 WEPP extends ServiceCall {

  private String metaInfo;
  private String climateData;
  private double waterErosion;
  private JSONObject rotation;
  private double latitude, longitude;
  private String cokey;
  private double length;
  private double steepness;

  public WEPP(String metaInfo, double _latitude, double _longitude, String _cokey, double _length, double _steepness, JSONObject _rotation, String URI) {
    super(URI);
    this.metaInfo = metaInfo;
    rotation = _rotation;
    latitude = _latitude;
    longitude = _longitude;
    cokey = _cokey;
    length = _length;
    steepness = _steepness;
    asyncCall = true;

    errorPrefix = "WEPPServiceCall";
  }

  public WEPP(String metaInfo, double _latitude, double _longitude, String _cokey,
      double _length, double _steepness, JSONObject _rotation, String URI,
      Object data, CheckedServiceResult<ServiceCallData> serviceCallback) {
    super(URI, data, serviceCallback);
    this.metaInfo = metaInfo;
    rotation = _rotation;
    latitude = _latitude;
    longitude = _longitude;
    cokey = _cokey;
    length = _length;
    steepness = _steepness;
    asyncCall = true;

    errorPrefix = "WEPPServiceCall";
  }

  public WEPP(String metaInfo, double _latitude, double _longitude, String _cokey,
      double _length, double _steepness, JSONObject _rotation, String URI,
      CheckedServiceResult<ServiceCallData> serviceCallback) {
    super(URI, serviceCallback);
    this.metaInfo = metaInfo;
    rotation = _rotation;
    latitude = _latitude;
    longitude = _longitude;
    cokey = _cokey;
    length = _length;
    steepness = _steepness;
    asyncCall = true;

    errorPrefix = "WEPPServiceCall";
  }

  public String data() {
    return climateData;
  }

  @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("SoilLoss")) {
        waterErosion = JSONUtils.getDoubleParam(resultMap, "SoilLoss", Double.NaN);
      } else {
        throwServiceCallException("Cannot find the SoilLoss 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("CRPAssessment_Metainfo", new JSONObject(metaInfo));
      if (asyncCall) {
        requestMetainfoObject.put("mode", "async");
      }
      requestMetainfoObject.put("report", false);
      request.put(KEY_METAINFO, requestMetainfoObject);

      //  Non-Changing values
      dataArray.put(JSONUtils.data("climateDataVersion", "2015"));
      dataArray.put(JSONUtils.data("usePRISM", true));
      dataArray.put(JSONUtils.data("aspect", 180));
      dataArray.put(JSONUtils.data("width", 50));
      dataArray.put(JSONUtils.data("slope_type", "Uniform"));
      dataArray.put(JSONUtils.data("contour", "(none)"));
      dataArray.put(JSONUtils.data("strip_barrier", "(none)"));

      //  User or other service provided values            
      dataArray.put(JSONUtils.data("latitude", latitude));
      dataArray.put(JSONUtils.data("longitude", longitude));
      dataArray.put(JSONUtils.data("soilPtr", new JSONArray().put(cokey)));
      dataArray.put(JSONUtils.data("length", length));
      dataArray.put(JSONUtils.data("slope_steepness", steepness));

      //  Rotation object retrieved from CRP Database            
      dataArray.put(rotation);

      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;
  }
}