Climate1_1.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.FileQryResult;
import c.SqlGIS;
import csip.Config;
import java.util.Date;
import javax.ws.rs.Path;
import oms3.annotations.Description;
import oms3.annotations.Name;
import oms3.annotations.VersionInfo;
import csip.ModelDataService;
import csip.ServiceException;
import csip.annotations.Resource;
import static csip.annotations.ResourceType.JDBC;
import static d.rusle2.Climate1_1.DB;

/**
 * EPA climate service
 *
 * @author wlloyd
 */
@Name("Climate")
@Description("EPA climate service")
@VersionInfo("1.1")
@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;
    }

}