WindGen.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 csip.Client;
import static csip.ModelDataService.KEY_METAINFO;
import static csip.ModelDataService.KEY_PARAMETER;
import csip.ServiceException;
import csip.utils.JSONUtils;
import gisobjects.GISObject;
import gisobjects.GISObjectException;
import java.net.URI;
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 WindGen extends ServiceCall {

    GISObject shape;
    JSONObject metaInfo;
    String windData;

    public WindGen(JSONObject metaInfo, GISObject shape, String URI) {
        super(URI);
        this.shape = shape;
        this.metaInfo = metaInfo;

        errorPrefix = "WindgenServiceCall";
    }

    public String data(){
        return windData;
    }
    
    @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);
            URI windURI = new URI(JSONUtils.getStringParam(resultMap, metaInfo.getString("suid") + ".win", ""));
            
            Client fileGet = new Client();

            windData = fileGet.doGET(windURI.toString());

        } catch (Exception ex) {
            throwServiceCallException("Could not retrieve the wind file: ", 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("CliWindSoil_Metainfo", metaInfo);
            request.put(KEY_METAINFO, requestMetainfoObject);

            dataArray.put(JSONUtils.data("latitude", shape.getLatitude()));
            dataArray.put(JSONUtils.data("longitude", shape.getLongitude()));
            dataArray.put(JSONUtils.data("outputFile", metaInfo.get("suid") + ".win"));
            dataArray.put(JSONUtils.data("duration", 100));
            dataArray.put(JSONUtils.data("dataVersion", 2015));
            dataArray.put(JSONUtils.data("usePRISM", true));

            request.put(KEY_PARAMETER, dataArray);

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