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

  private String metaInfo;
  private String climateData;
  double windErosion;
  private JSONObject rotation;
  private double latitude, longitude, regionLength, regionWidth, regionOrientation;
  private String cokey;
  private WEPSFallowRotation fallowRotation;

  public WEPS(String metaInfo, double _latitude, double _longitude, double _regionLength, double _regionWidth,
      double _regionOrientation, String _cokey, JSONObject _rotation, String URI) throws JSONException {
    super(URI);
    this.metaInfo = metaInfo;
    if (null == _rotation) {
      rotation = new WEPSFallowRotation(0).rotation();
    } else {
      rotation = _rotation;
    }
    latitude = _latitude;
    longitude = _longitude;
    cokey = _cokey;
    regionLength = _regionLength;
    regionWidth = _regionWidth;
    regionOrientation = _regionOrientation;
    asyncCall = true;

    errorPrefix = "WEPSServiceCall";
  }

  public WEPS(String metaInfo, double _latitude, double _longitude, double _regionLength, double _regionWidth,
      double _regionOrientation, String _cokey, JSONObject _rotation, String URI,
      Object data, CheckedServiceResult<ServiceCallData> serviceCallback) throws JSONException {
    super(URI, data, serviceCallback);
    this.metaInfo = metaInfo;
    if (null == _rotation) {
      rotation = new WEPSFallowRotation(0).rotation();
    } else {
      rotation = _rotation;
    }
    latitude = _latitude;
    longitude = _longitude;
    cokey = _cokey;
    regionLength = _regionLength;
    regionWidth = _regionWidth;
    regionOrientation = _regionOrientation;
    asyncCall = true;

    errorPrefix = "WEPSServiceCall";
  }

  public WEPS(String metaInfo, double _latitude, double _longitude, double _regionLength, double _regionWidth,
      double _regionOrientation, String _cokey, JSONObject _rotation, String URI,
      CheckedServiceResult<ServiceCallData> serviceCallback) throws JSONException {
    super(URI, serviceCallback);
    this.metaInfo = metaInfo;
    if (null == _rotation) {
      rotation = new WEPSFallowRotation(0).rotation();
    } else {
      rotation = _rotation;
    }
    latitude = _latitude;
    longitude = _longitude;
    cokey = _cokey;
    regionLength = _regionLength;
    regionWidth = _regionWidth;
    regionOrientation = _regionOrientation;
    asyncCall = true;

    errorPrefix = "WEPSServiceCall";
  }

  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("wind_eros")) {
        windErosion = JSONUtils.getDoubleParam(resultMap, "wind_eros", Double.NaN);
      } else {
        throwServiceCallException("Cannot find the wind_eros 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);

      JSONArray windBarriers = new JSONArray();
      JSONObject windBarrier = new JSONObject("{\"name\": \"wind_barrier\",\"value\": null,\"properties\": {} }");

      windBarriers.put(windBarrier);
      windBarriers.put(windBarrier);
      windBarriers.put(windBarrier);
      windBarriers.put(windBarrier);

      //  Non-Changing values
      dataArray.put(JSONUtils.data("wind_barriers", windBarriers));
      dataArray.put(JSONUtils.data("crop_calibration_mode", false));
      dataArray.put(JSONUtils.data("usePRISM", true));
      dataArray.put(JSONUtils.data("field_shape", "rectangle"));
      dataArray.put(JSONUtils.data("elevation", 0));
      dataArray.put(JSONUtils.data("field_radius", 0));
      dataArray.put(JSONUtils.data("soil_rock_fragments", 0));

      //  User or other service provided values
      dataArray.put(JSONUtils.data("latitude", latitude));
      dataArray.put(JSONUtils.data("longitude", longitude));
      dataArray.put(JSONUtils.data("soil", cokey));
      dataArray.put(JSONUtils.data("field_length", regionLength));
      dataArray.put(JSONUtils.data("field_width", regionWidth));
      dataArray.put(JSONUtils.data("field_orientation", regionOrientation));

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

      request.put(KEY_PARAMETER, dataArray);
      this.requestJSON = request;

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

  public double windErosion() {
    return windErosion;
  }
}