CMZ1_0.java [src/java/d/rusle2] Revision: 7f16de24efff2958d5552d684892a367a9f8ef3d  Date: Tue Mar 29 13:19:23 MDT 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 CMZ1_0 extends ModelDataService {

    static final Logger logger = Logger.getLogger(CMZ1_0.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(CMZ1_0.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    public CMZ1_0() throws Exception {
        db = PostGIS.singleton();
    }

    @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 {
            String cmz = "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(CMZ1_0.class.getName()).log(Level.SEVERE, null, ex);
            }

            log("build jsonobject to return");
            outputObj.put(JSONUtils.data("tstamp", new Date()));
            outputObj.put(JSONUtils.data("cmz", (String) cmz));
            log("write response");
            end = System.currentTimeMillis();
        } catch (Exception ex) {
            log("Exception: " + ex.toString());
            status = "Failed";
        }
        log("JSON object returned is=" + outputObj.toString());
        return EXEC_OK;
    }


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