Climate.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 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 java.io.IOException;
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 Climate extends ServiceCall {
GISObject shape;
String metaInfo;
String climateData;
int duration;
public Climate(String metaInfo, GISObject shape, String URI, int duration) {
super(URI);
this.shape = shape;
this.metaInfo = metaInfo;
this.duration = duration;
errorPrefix = "CligenServiceCall";
}
public Climate(String metaInfo, GISObject shape, String URI, int duration, Object data, CheckedServiceResult<ServiceCallData> serviceCallback) {
super(URI, data, serviceCallback);
this.shape = shape;
this.metaInfo = metaInfo;
this.duration = duration;
errorPrefix = "CligenServiceCall";
}
public Climate(String metaInfo, GISObject shape, String URI, int duration, CheckedServiceResult<ServiceCallData> serviceCallback) {
super(URI, serviceCallback);
this.shape = shape;
this.metaInfo = metaInfo;
this.duration = duration;
errorPrefix = "CligenServiceCall";
}
public String data() {
return climateData;
}
public int duration() {
return duration;
}
@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 cliURI = new URI(JSONUtils.getStringParam(resultMap, new JSONObject(metaInfo).getString("suid") + ".cli", ""));
Client fileGet = new Client();
climateData = fileGet.doGET(cliURI.toString());
} catch (Exception ex) {
throwServiceCallException("Could not retrieve the climate 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", new JSONObject(metaInfo));
if (asyncCall) {
requestMetainfoObject.put("mode", "async");
}
request.put(KEY_METAINFO, requestMetainfoObject);
try {
JSONObject geometry = shape.toJSON();
JSONObject feature = new JSONObject();
JSONObject properties = new JSONObject();
properties.put("name", "pt one");
properties.put("gid", 1);
feature.put("type", "feature");
feature.put("geometry", geometry);
feature.put("properties", properties);
JSONArray features = new JSONArray();
features.put(feature);
JSONObject zoneFeatures = new JSONObject();
zoneFeatures.put("type", "FeatureCollection");
zoneFeatures.put("features", features);
dataArray.put(JSONUtils.data("input_zone_features", zoneFeatures));
dataArray.put(JSONUtils.data("outputFile", new JSONObject(metaInfo).get("suid") + ".cli"));
dataArray.put(JSONUtils.data("duration", duration));
dataArray.put(JSONUtils.data("dataVersion", 2015));
dataArray.put(JSONUtils.data("usePRISM", true));
request.put(KEY_PARAMETER, dataArray);
} catch (IOException ex) {
throwServiceCallException("Cannot create input to cligen_prism", ex);
}
} catch (JSONException ex) {
throwServiceCallException("Cannot create the JSON request.", ex);
}
}
}