SMWEPS.java [src/java/crp/utils] Revision:   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.WEPSMetaData;
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 SMWEPS extends ServiceCall {

  private String metaInfo;
  private WEPSMetaData modelData;
  private String ann_name;
  private double windErosion;

  public SMWEPS(String metaInfo, String ann_name, WEPSMetaData modelData, String URI) {
    super(URI);
    this.metaInfo = metaInfo;
    this.modelData = modelData;
    this.ann_name = ann_name;
    errorPrefix = "SMWEPSServiceCall";
  }

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

  public SMWEPS(String metaInfo, String ann_name, WEPSMetaData modelData, String URI, CheckedServiceResult<ServiceCallData> serviceCallback) {
    super(URI, serviceCallback);
    this.metaInfo = metaInfo;
    this.modelData = modelData;
    this.ann_name = ann_name;
    errorPrefix = "SMWEPSServiceCall";
  }

  @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("errosionRate")) {
        windErosion = JSONUtils.getDoubleParam(resultMap, "errosionRate", Double.NaN);
      } else {
        throwServiceCallException("Cannot find the errosionRate 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("annual_wind_energy", modelData.annualErosiveWindEnergy()));
      dataArray.put(JSONUtils.data("avg_annual_tmax", modelData.avgAnnualTMax()));
      dataArray.put(JSONUtils.data("avg_annual_tmin", modelData.avgAnnualTMin()));
      dataArray.put(JSONUtils.data("crustStability", modelData.crustStability()));
      dataArray.put(JSONUtils.data("days_with_precip_and_wind", modelData.daysWithPrecipAndErosiveWind()));
      dataArray.put(JSONUtils.data("mean_agg_diameter", modelData.aggGeomDiam()));
      dataArray.put(JSONUtils.data("soilWiltPoint", modelData.soilWiltPoint()));
      dataArray.put(JSONUtils.data("surfRockFrag", modelData.surfRockFrag()));
      dataArray.put(JSONUtils.data("surface_clay", modelData.fractionClay(0)));
      dataArray.put(JSONUtils.data("surface_sand", modelData.fractionSand(0)));

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

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

  public double windErosion() {
    return windErosion;
  }
}