CliWindSoil.java [tools/GetClimateWindSoil/src/getclimatewindsoil] Revision: 4b80f4d13c2abb8f0e218dfab214a33011391dcc  Date: Thu Oct 10 13:14:18 MDT 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, longitude;
    File data = null;

    public CliWindSoil( double lat, double lon, String URI) {
        super(URI);
        latitude = lat;
        longitude = lon;
        
        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");
            location.put("type", "Point");
            coordinates.put(longitude);
            coordinates.put(latitude);
            location.put("coordinates", coordinates);

            dataArray.put(location);
            
            request.put(KEY_PARAMETER, dataArray);

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