V1_0.java [src/java/d/urbanareas/geometry] Revision: default  Date:
package d.urbanareas.geometry;

import csip.ModelDataService;
import csip.api.server.PayloadParameter;
import csip.api.server.PayloadResults;
import csip.api.server.ServiceException;
import csip.annotations.Description;
import csip.annotations.Name;
import csip.annotations.Resource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import javax.ws.rs.Path;
import cities.utils.DBResources;
import cities.utils.PostgresqlLocationsDatabase;
import m.utils.Layer;
import org.codehaus.jettison.json.JSONObject;

/**
* Last Updated: 6-December-2019
* @author Tyler Wible
* @since 22-November-2019
*/
@Name("Urban Areas Geometry")
@Description("Retrieve 2010 U.S. Census polygon geometries for Urban Areas that match a given OID")
@Path("d/urbanareas/geometry/1.0")
@Resource(from = DBResources.class)
public class V1_0 extends ModelDataService {

    protected PostgresqlLocationsDatabase db;
    protected int oid;
    protected JSONObject result;
    
    @Override
    protected void preProcess() throws Exception {
        PayloadParameter inputPayload = parameter();
        oid = inputPayload.getInt("oid");
    }

    @Override
    protected void doProcess() throws Exception {
        result = new JSONObject();
        try (Connection c = resources().getJDBC(DBResources.cities_ID)) {
            db = new PostgresqlLocationsDatabase(c);
            Layer lyr = db.getLocation("public", "urban_areas", "gid", oid);
            result = db.toJSON(lyr);
        } catch (SQLException | ServiceException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void postProcess() throws Exception {
        PayloadResults resultPayload = results();
        resultPayload.put("result", result);
    }
}