WEPSSoilInput.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_NAME;
import static csip.ModelDataService.KEY_PARAMETER;
import static csip.ModelDataService.KEY_RESULT;
import static csip.ModelDataService.KEY_VALUE;
import csip.ServiceException;
import csip.utils.JSONUtils;
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 WEPSSoilInput extends ServiceCall {

  protected String _cokey;
  protected String _callingMetaInfo;
  protected String ifcFile = "";
  protected String ifcFilename = "";

  public WEPSSoilInput(String metaInfo, String cokey, String URI) {
    super(URI);
    _callingMetaInfo = metaInfo;
    errorPrefix = "WEPSSoilParams_ServiceCall";
    _cokey = cokey;
  }

  public WEPSSoilInput(String metaInfo, String cokey, String URI, Object data, CheckedServiceResult<ServiceCallData> serviceCallback) {
    super(URI, data, serviceCallback);
    _callingMetaInfo = metaInfo;
    errorPrefix = "WEPSSoilParams_ServiceCall";
    _cokey = cokey;
  }

  public WEPSSoilInput(String metaInfo, String cokey, String URI, CheckedServiceResult<ServiceCallData> serviceCallback) {
    super(URI, serviceCallback);
    _callingMetaInfo = metaInfo;
    errorPrefix = "WEPSSoilParams_ServiceCall";
    _cokey = cokey;
  }

  public String getIFCFileData() {
    return ifcFile;
  }

  public String getIFCFilename() {
    return ifcFilename;
  }

  @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("SMWEPS_Metainfo", new JSONObject(_callingMetaInfo));
      if (asyncCall) {
        requestMetainfoObject.put("mode", "async");
      }
      request.put(KEY_METAINFO, requestMetainfoObject);

      if ((null != _cokey) && (!_cokey.isEmpty())) {
        dataArray.put(JSONUtils.data("cokey", _cokey));
        dataArray.put(JSONUtils.data("stream_file", true));

        request.put(KEY_PARAMETER, dataArray);
      } else {
        throwServiceCallException("No valid cokey value found.  Cannot proceed with call.");
      }

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

  @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) {
      throwServiceCallException("Cannot find results in the call to " + this.URI + " .");
    }

    try {
      JSONArray resultSection = results.getJSONArray(KEY_RESULT);

      if (resultSection.length() > 0) {
        JSONObject ifcObject = resultSection.getJSONObject(0);

        ifcFilename = ifcObject.getString(KEY_NAME);
        ifcFile = ifcObject.getString(KEY_VALUE);

      } else {
        throwServiceCallException("No IFC file data was returned from this call.");
      }
    } catch (JSONException ex) {
      throwServiceCallException("Could not parse the results of this call: " + ex.getMessage(), ex);
    }
  }

}