Climate1_0.java [src/java/d/rusle2] Revision: 57d2b78912cf87f99bd814df3c90cb61873e5fe4 Date: Mon Apr 10 21:28:56 MDT 2017
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package d.rusle2;
import c.GIS_DB;
import c.GIS_DB.FileQryResult;
import c.GIS_DB_Factory;
import java.sql.SQLException;
import java.util.Date;
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.codehaus.jettison.json.JSONArray;
import csip.ModelDataService;
import static csip.ModelDataService.EXEC_OK;
import csip.utils.JSONUtils;
/**
* REST Web Service
*
* @author wlloyd
*/
@Name("Climate")
@Description("EPA climate service")
@VersionInfo("1.0")
@Path("d/rusle2/climate/1.0")
public class Climate1_0 extends ModelDataService {
static final Logger logger = Logger.getLogger(Climate1_0.class.getName());
String rusle2db = "http://csip.engr.colostate.edu/r2";
static final String CLIMATE_FILE_EXT = ".xml";
JSONArray outputObj;
@Context
private UriInfo context;
GIS_DB db;
@Context
HttpServletRequest inRequest;
@Override
protected void finalize() throws Throwable {
super.finalize();
try {
if (db != null) {
db.close();
}
} catch (SQLException ex) {
Logger.getLogger(Climate1_0.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Climate1_0() throws Exception {
db = GIS_DB_Factory.createGISEngine();
}
@Override
protected JSONArray createResults() throws Exception {
return outputObj;
}
@Override
protected String process() 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 {
FileQryResult climate = null;
try {
log("find climate for lat long");
climate = db.findClimate(latitude, longitude);
log("found climate for lat lng");
} catch (SQLException ex) {
log("exception finding climate for lat long");
Logger.getLogger(Climate1_0.class.getName()).log(Level.SEVERE, null, ex);
}
log("put climate into array list");
String sClimate;
sClimate = rusle2db + "/epa/" + climate.file_path + "/" + climate.file_name + CLIMATE_FILE_EXT;
sClimate = fixClimateUrl(sClimate);
log("build jsonobject to return");
aOutput.put(JSONUtils.data("tstamp", new Date()));
aOutput.put(JSONUtils.data("climate", (String) sClimate));
// res.put("name", "climate");
// res.put("value", (String) sClimate);
// aOutput.put(res); aOutput.
// res.put("tstamp", new Date());
// res.put("climate", (String) sClimate);
log("JSON object returned is=" + res.toString());
log("write response");
end = System.currentTimeMillis();
} catch (Exception ex) {
log("Exception: " + ex.toString());
status = "Failed";
}
//outputObj.put(res);
outputObj.put(aOutput);
//return res.toString();
return EXEC_OK;
}
private String fixClimateUrl(String text) {
log("fixing string=" + text);
// remove \/ 's
text = text.replace("\\/", "/");
// convert \\ to /
text = text.replace("\\\\", "/");
// convert / to \
text = text.replace("\\", "/");
log("fixed string is=" + text);
return text;
}
private void log(String text) {
LOG.info(text);
}
}