Region.java [src/java/m/crp/services] Revision: 57250261f8dd22e26a3b05bbd9b109c64c04d323  Date: Fri Dec 06 13:37:31 MST 2019
/*
 * $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 m.crp.services;

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;
import m.crp.utils.ServiceCall;

/**
 *
 * @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 JSONObject 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(JSONObject _metaInfo, JSONObject shape, String URI) {
        super(URI);
        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", metaInfo);
            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;
    }
}