Climate1_1.java [src/java/d/rusle2] Revision: 1e3f2b36363f0eeca9f70d1b10adc5e6aaf69e99  Date: Tue Nov 06 16:10:17 MST 2018
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package d.rusle2;

import c.GIS_DB.FileQryResult;
import c.SqlGIS;
import csip.Config;
import java.util.Date;
import javax.ws.rs.Path;
import csip.ModelDataService;
import csip.ServiceException;
import csip.annotations.*;
import csip.annotations.Resource;
import static csip.annotations.ResourceType.JDBC;
import static csip.annotations.State.RELEASED;
import static d.rusle2.Climate1_1.DB;

/**
 * EPA climate service
 *
 * @author wlloyd
 */
@Name("Climate")
@Description("EPA climate service")
@VersionInfo("1.1")
@State(RELEASED)
@Path("d/rusle2/climate/1.1")
@Resource(type = JDBC, file = "${conservation_resources.db}", id = DB)
public class Climate1_1 extends ModelDataService {

    static final String DB = "csip.erosion.sqlsvr";

    String rusle2db = Config.getString("r2.db", "http://csip.engr.colostate.edu/r2");


    @Override
    protected void doProcess() throws Exception {
        double lat = getDoubleParam("latitude");
        double lon = getDoubleParam("longitude");

        SqlGIS db = new SqlGIS(getResourceJDBC(DB));
        try {
            LOG.info("get lat long from content input");
            if (!db.IsValidCliCoord(lat, lon)) {
                throw new ServiceException("Invalid lat/lon : " + lat + "/" + lon);
            }

            FileQryResult climate = db.findClimate(lat, lon);
            if (climate == null) {
                throw new ServiceException("No climate found");
            }
            LOG.info("found climate for lat lng");

            String sClimate = rusle2db + "/epa/" + climate.file_path + "/" + climate.file_name + ".xml";
            sClimate = fixClimateUrl(sClimate);

            putResult("tstamp", new Date());
            putResult("climate", sClimate);
        } finally {
            db.close();
        }
    }


    private String fixClimateUrl(String text) {
        // remove \/ 's
        text = text.replace("\\/", "/");
        // convert \\ to /
        text = text.replace("\\\\", "/");
        // convert / to \
        text = text.replace("\\", "/");
        return text;
    }

}