CliWindSoil.java [tools/GetClimateWindSoil/src/getclimatewindsoil] Revision: af24fed480b2b8d52b03aa714e30b7a762e256e0  Date: Tue Dec 03 15:27:33 MST 2019
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package getclimatewindsoil;

import csip.Client;
import static csip.ModelDataService.KEY_METAINFO;
import static csip.ModelDataService.KEY_PARAMETER;
import csip.ServiceException;
import csip.utils.JSONUtils;
import java.io.File;
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;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
public class CliWindSoil extends ServiceCall {

    double latitude = Double.NaN, longitude = Double.NaN;
    String polyCoordinates = null;
    File data = null;

    public CliWindSoil(double lat, double lon, String URI) {
        super(URI);
        latitude = lat;
        longitude = lon;

        errorPrefix = "CliWindSoilBundle";
    }

    public CliWindSoil(String _coordinates, String URI) {
        super(URI);
        polyCoordinates = _coordinates;

        errorPrefix = "CliWindSoilBundle";
    }

    public File data() {
        return data;
    }

    @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);
            String fileName = getReturnMetainfo().getString("suid") + ".zip";
            String tURI = JSONUtils.getStringParam(resultMap, fileName, "");
            URI windURI = new URI(tURI);

            Client fileGet = new Client();

            data = fileGet.doGET(windURI.toString(), new File(fileName));

        } 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");
            request.put(KEY_METAINFO, requestMetainfoObject);

            JSONObject location = new JSONObject();
            JSONArray coordinates = new JSONArray();

            location.put("name", "aoa_geometry");
            if ((null != polyCoordinates) && !polyCoordinates.isEmpty()) {
                String tType = polyCoordinates.substring(0, polyCoordinates.indexOf("[") - 1).trim();
                String tCoordinates = polyCoordinates.substring(polyCoordinates.indexOf("[") - 1).trim();
                
                location.put("type", tType);
                coordinates = new JSONArray( tCoordinates);
                location.put("coordinates", coordinates);

            } else {
                if (!Double.isNaN(latitude) && !Double.isNaN(longitude)) {
                    location.put("type", "Point");
                    coordinates.put(longitude);
                    coordinates.put(latitude);
                    location.put("coordinates", coordinates);
                } else {
                    throw new ServiceException("No coordinates were specified.");
                }
            }

            dataArray.put(location);

            request.put(KEY_PARAMETER, dataArray);

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

}