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

  private double length, width, orientation;
  protected JSONObject feature;
  protected String metaInfo;
  protected String regionObject = "        {\n"
      + "            \"name\": \"field_boundary\",\n"
      + "            \"value\": {\n"
      + "                \"type\": \"FeatureCollection\",\n"
      + "                \"features\": [\n"
      + "                    {\n"
      + "                        \"type\": \"Feature\","
      + "                          \"geometry\":";

  protected String regionObjectFinish = ",\n"
      + "                        \"properties\": {}\n"
      + "                    }\n"
      + "                ]\n"
      + "            }\n"
      + "        }";

  public Region(String _metaInfo, JSONObject shape, String URI) {
    super(URI);
    feature = shape;
    metaInfo = _metaInfo;
    errorPrefix = "Region_ServiceCall";

  }

  public Region(String _metaInfo, JSONObject shape, String URI, Object data, CheckedServiceResult<ServiceCallData> serviceCallback) {
    super(URI, data, serviceCallback);
    feature = shape;
    metaInfo = _metaInfo;
    errorPrefix = "Region_ServiceCall";

  }

  public Region(String _metaInfo, JSONObject shape, String URI, CheckedServiceResult<ServiceCallData> serviceCallback) {
    super(URI, serviceCallback);
    feature = shape;
    metaInfo = _metaInfo;
    errorPrefix = "Region_ServiceCall";

  }

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

    requestMetainfoObject = new JSONObject();
    request = new JSONObject();
    requestData = new JSONObject();
    dataArray = new JSONArray();

    try {
      requestMetainfoObject.put("MultipartRequest", "Bundled Service Request From csip-crp MetaModeling CRP Assessment Service");
      requestMetainfoObject.put("CRPAssessment_Metainfo", new JSONObject(metaInfo));
      if (asyncCall) {
        requestMetainfoObject.put("mode", "async");
      }
      request.put(KEY_METAINFO, requestMetainfoObject);

      if (null != feature) {
        regionObject += feature.toString() + regionObjectFinish;
        requestData = new JSONObject(regionObject);
        dataArray.put(requestData);
        request.put(KEY_PARAMETER, dataArray);
      } else {
        throwServiceCallException("No input shape was specified to the region service call object.");
      }
    } catch (JSONException ex) {
      throwServiceCallException("Cannot create the JSON request for Region 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) {
      throw new ServiceException((errorPrefix.isEmpty() ? "" : (errorPrefix + ":  ")) + "Cannot find results in the call to " + this.URI + " .");
    }

    try {
      //  Get the region results and store them.
      Map<String, JSONObject> resultMap = JSONUtils.getResults(results);

      if (resultMap.containsKey("field_length")) {
        length = JSONUtils.getDoubleParam(resultMap, "field_length", Double.NaN);
      } else {
        throwServiceCallException("Results from the region service did not contain a field_length object.");
      }

      if (resultMap.containsKey("field_width")) {
        width = JSONUtils.getDoubleParam(resultMap, "field_width", Double.NaN);
      } else {
        throwServiceCallException("Results from the region service did not contain a field_width object.");
      }

      if (resultMap.containsKey("field_orientation")) {
        orientation = JSONUtils.getDoubleParam(resultMap, "field_orientation", Double.NaN);
      } else {
        throwServiceCallException("Results from the region service did not contain a field_orientation object.");
      }

    } catch (JSONException ex) {
      throwServiceCallException("Could not build the results map: " + ex.getMessage(), ex);
    }
  }

  public double getWidth() {
    return width;
  }

  public double getLength() {
    return length;
  }

  public double getOrientation() {
    return orientation;
  }
}