CMZ_01.java [src/java/d/rusle2] Revision: fe6c776942e0dd8c9659e726c32855185a5303c7 Date: Tue Feb 09 18:19:52 MST 2016
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package d.rusle2;
import c.PostGIS;
import c.PostGIS.FileQryResult;
import d.soils.V1_1;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Path;
import oms3.annotations.Description;
import oms3.annotations.Name;
import oms3.annotations.VersionInfo;
import org.apache.commons.io.IOUtils;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import csip.ModelDataService;
import static csip.ModelDataService.EXEC_OK;
import csip.utils.JSONUtils;
import static m.weps.SciEnergyParser.LOG;
/**
* REST Web Service
*
* @author wlloyd
*/
@Name("Cmz")
@Description("Climate Management Zone (CMZ) service")
@VersionInfo("1.0")
@Path("d/cmz/1.0")
public class CMZ_01 extends ModelDataService {
static final Logger logger = Logger.getLogger(CMZ_01.class.getName());
String rusle2db = "http://csip.engr.colostate.edu/r2";
JSONArray outputObj;
@Context
private UriInfo context;
PostGIS db;
@Context
HttpServletRequest inRequest;
@Override
protected void finalize() throws Throwable {
super.finalize();
try {
if (db != null) {
db.close();
}
} catch (SQLException ex) {
Logger.getLogger(CMZ_01.class.getName()).log(Level.SEVERE, null, ex);
}
}
public CMZ_01() throws Exception {
db = PostGIS.singleton();
}
// @Override
// String describeJSON() {
// try {
// String tmpl = IOUtils.toString(V1_1.class.getResource("/src/d/rusle2/climate.json"));
// JSONObject model = new JSONObject(tmpl);
// return model.toString();
// } catch (IOException ex) {
// throw new RuntimeException("tmpl");
// }
// }
@Override
protected JSONArray createResults() throws Exception {
return outputObj;
}
/**
* POST method for retrieving climates for a specific lat/long
*
* @param req representation for the resource
* @return an HTTP response with content of the updated or created resource.
*/
@Override
protected Callable<String> createCallable() throws Exception {
return new Callable<String>() {
@Override
public String call() throws Exception {
outputObj = new JSONArray(getParam().toString());
JSONArray aOutput = new JSONArray();
org.json.JSONObject res = new org.json.JSONObject();
double start = System.currentTimeMillis();
double end = 0;
String status = "Finished";
log("get lat long from content input");
double latitude = JSONUtils.getDoubleParam(getParamMap(), "latitude", 0);
double longitude = JSONUtils.getDoubleParam(getParamMap(), "longitude", 0);
try {
double cmz = 0.0;
try {
log("find cmz for lat long");
cmz = db.findCmz(latitude, longitude);
log("found cmzfor lat lng");
} catch (SQLException ex) {
log("exception finding cmz for lat long");
Logger.getLogger(CMZ_01.class.getName()).log(Level.SEVERE, null, ex);
}
log("put cmz into string");
String sCmz = Double.toString(cmz);
log("build jsonobject to return");
aOutput.put(JSONUtils.data("tstamp", new Date()));
aOutput.put(JSONUtils.data("cmz", (String) sCmz));
log("write response");
end = System.currentTimeMillis();
} catch (Exception ex) {
log("Exception: " + ex.toString());
status = "Failed";
}
outputObj.put(aOutput);
log("JSON object returned is=" + outputObj.toString());
return EXEC_OK;
}
};
}
private void log(String text) {
LOG.info(text);
}
}