StateCounty1_0.java [src/java/d/rusle2] Revision: 95faef4c0711cc06cca672ad885649ce96de8ec9  Date: Mon Sep 26 13:38:03 MDT 2016
/*
 * 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_Factory;
import c.PostGIS;
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.ServiceException;
import csip.utils.JSONUtils;

/**
 * REST Web Service
 *
 * @author wlloyd
 */
@Name("Cmz")
@Description("Climate Management Zone (CMZ) service")
@VersionInfo("1.0")
@Path("d/stateCounty/1.0")
public class StateCounty1_0 extends ModelDataService {

    static final Logger logger = Logger.getLogger(StateCounty1_0.class.getName());
    String rusle2db = "http://csip.engr.colostate.edu/r2";
    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(StateCounty1_0.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    public StateCounty1_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();

        log("get lat long from content input");
        double latitude = JSONUtils.getDoubleParam(getParamMap(), "latitude", 0);
        double longitude = JSONUtils.getDoubleParam(getParamMap(), "longitude", 0);

        try {
            GIS_DB.County countystate = null;

            try {
                log("find county for lat long");
                countystate = db.findCounty(latitude, longitude);
                log("found county for lat lng");
            } catch (SQLException | ServiceException ex) {
                log("exception finding county for lat long");
                Logger.getLogger(StateCounty1_0.class.getName()).log(Level.SEVERE, null, ex);
            }

            log("put county name into string");
            String countyName = countystate.name;
            String stateAbbr = countystate.st_abbr;

            log("build jsonobject to return");
            outputObj.put(JSONUtils.data("tstamp", new Date()));
            outputObj.put(JSONUtils.data("county", (String) countyName));
            outputObj.put(JSONUtils.data("state", (String) stateAbbr));
            log("write response");
        } catch (Exception ex) {
            log("Exception: " + ex.toString());
        }
        log("JSON object returned is=" + outputObj.toString());
        return EXEC_OK;
    }


    private void log(String text) {
        LOG.info(text);
    }
}