rusle2check.java [src/java/m/rusle2] Revision: 7f73ef958f686975828e1598660046c441b2c901  Date: Wed Mar 30 09:03:53 MDT 2016
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package m.rusle2;

import c.PostGIS;
import csip.ServiceException;
import java.io.File;
import java.util.*;
import javax.ws.rs.*;
import oms3.annotations.*;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import csip.ModelDataService;
import static csip.ModelDataService.EXEC_OK;
import static csip.ModelDataService.KEY_REQUEST_RESULTS;
import static csip.ModelDataService.VALUE;
import static util.ErosionConst.*;

import csip.utils.JSONUtils;
import csip.annotations.Polling;

/**
 * REST Web Service. Check if Operations and vegetation files are in place on
 * backend (nginx) to support R2 run If files are missing Rusle2 runs without
 * error using default operations and crops instead.
 *
 * @author wlloyd, od
 */
@Name("Rusle2Check")
@Description("R2 data input validator: validates if required inputs are provided and backend has necessary XML files.")
@VersionInfo("1.0")
@Path("m/rusle2check/1.3")
@Polling(first = 1000, next = 1000)
public class rusle2check extends ModelDataService {

    static final boolean PRODUCTION_MODE = true;
    //
    static final String R2_TMP_FILENAME = "r2";
    static final String R2_TMP_FILEEXT = ".rsh";
    static final String IO_TIMING_FILENAME = "/tmp/io-timing";
    static final String IO_TIMING_FILEEXT = ".txt";
    static final String REPORT_JSON_TEMPLATE_FILENAME = "Rusle2_Report.json";
    static final String REPORT_RSH_FILENAME = "rusle2_report.rsh";

    /*
     * The R2 Run
     */
    final R2Run r2run = new R2Run();
    // These are the allowable potential results returnable from the R2 service
    static final List<String> potResults = Collections.unmodifiableList(Arrays.asList(
            RES_SLOPE_DELIVERY,
            RES_SLOPE_T_VALUE,
            RES_SLOPE_DEGRAD,
            RES_SLOPE_EQUIV_DIESEL_USE_PER_AREA,
            RES_SOIL_COND_INDEX_STIR_VAL,
            RES_SOIL_COND_INDEX_RESULT,
            RES_SEG_SIM_DAY_LIVE_BIOMASS,
            RES_SEG_SIM_DAY_COVER_MASS_SUM,
            RES_SEG_SIM_DAY_STAND_MASS_SUM,
            RES_SEG_SIM_DAY_CANOPY_COVER,
            RES_SEG_SIM_DAY_PERENN_VEG_LIVE_HEIGHT,
            RES_SLOPE_SIM_DAY_DEGRAD,
            RES_SURF_RES_OUTPUTS_SURF_COV_AT_OP,
            RES_SOIL_COND_INDEX_OM_SUBFACTOR,
            RES_SOIL_COND_INDEX_FO_SUBFACTOR,
            RES_SOIL_COND_INDEX_ER_SUBFACTOR));

    String climate;
    String soil;
    String mgmt;
    List<String> operations;
    List<String> vegetations;
    List<String> residues;


    @Override
    protected void preProcess() throws Exception {
        // Validate that this is a Rulse 2 run
        try {
            LOG.info("\n\n\n\n\nMETAINFO:");
            LOG.info("\n" + getMetainfo().toString());
            LOG.info("\n\n\n");
            JSONUtils.checkValidResultRequest(getMetainfo(), potResults);
        } catch (ServiceException se) {
            LOG.severe("\n\n\nERROR!!!\n\n\n" + se.toString());
            LOG.warning("No Rusle2 return parameters requested! Will use defaults: SLOPE_DELIVERY, SLOPE_T_VALUE, SLOPE_DEGRAD");
            List<String> params = new LinkedList<String>();
            params.add(RES_SLOPE_DELIVERY);
            params.add(RES_SLOPE_T_VALUE);
            params.add(RES_SLOPE_DEGRAD);

            // check if these are being set
            params.add("CONTOUR_SYSTEM_PTR");
            params.add("STRIP_BARRIER_SYSTEM_PTR");
            params.add("HYD_ELEM_SYSTEM_PTR");

            getMetainfo().put(KEY_REQUEST_RESULTS, params);
        }

        // check if sufficient input is there.
        //JSONUtils.checkKeyExists(getParamMap(), KEY_CLIMATES);
        JSONUtils.checkKeyExists(getParamMap(), KEY_SOILS);
//        JSONUtils.checkKeyExists(getParamMap(), KEY_MANAGEMENTS);
        JSONUtils.checkKeyExists(getParamMap(), KEY_LENGTH);
        JSONUtils.checkKeyExists(getParamMap(), KEY_STEEPNESS);
        JSONUtils.checkKeyExists(getParamMap(), KEY_MGMTS);

        File r2script = new File(getWorkspaceDir(), R2_TMP_FILENAME + R2_TMP_FILEEXT);
        createInputFile(r2script, getMetainfo(), getParamMap());
    }


    @Override
    protected String process() throws Exception {
//                int result = r2run.execute(new File(getWorkspaceDir(), R2_TMP_FILENAME + R2_TMP_FILEEXT));
//                if (result != 0) {
//                    return EXEC_FAILED;
//                }
        return EXEC_OK;
    }


    @Override
    protected JSONArray createResults() throws Exception {
        JSONArray results = new JSONArray();

        int errors = 0;
        for (int i = 0; i < operations.size(); i++) {
            String op = operations.get(i);
            if (r2run.isUrlReachable(op)) {
                results.put(JSONUtils.dataDesc("Operation_" + (i + 1), op, "operation is valid"));
            } else {
                results.put(JSONUtils.dataDesc("OPERATION_INVALID_" + (i + 1), op, "OPERATION IS MISSING!"));
                errors++;
            }
        }
        for (int i = 0; i < vegetations.size(); i++) {
            String vege = vegetations.get(i);
            if (r2run.isUrlReachable(vege)) {
                results.put(JSONUtils.dataDesc("Vegetation_" + (i + 1), vege, "vegetation is valid"));
            } else {
                results.put(JSONUtils.dataDesc("VEGETATION_INVALID_" + (i + 1), vege, "VEGETATION IS MISSING"));
                errors++;
            }
        }
        for (int i = 0; i < residues.size(); i++) {
            String res = residues.get(i);
            if (r2run.isUrlReachable(res)) {
                results.put(JSONUtils.dataDesc("Residue_" + (i + 1), res, "residue is valid"));
            } else {
                results.put(JSONUtils.dataDesc("RESIDUE_INVALID_" + (i + 1), res, "RESIDUE IS MISSING"));
                errors++;
            }
        }

//        results.put(JSONUtils.data(KEY_CLIMATES, climate));
//        results.put(JSONUtils.data(KEY_SOILS, soil));
//        results.put(JSONUtils.data(KEY_MANAGEMENTS, mgmt));
        return results;
    }


    private void createInputFile(File file, JSONObject metainfo, Map<String, JSONObject> param)
            throws Exception {
        boolean resolveLoc = JSONUtils.getBooleanParam(param, KEY_RESOLVE_LOCATION, false);
        double latitude = JSONUtils.getDoubleParam(param, KEY_LATITUDE, 0.0);
        double longitude = JSONUtils.getDoubleParam(param, KEY_LONGITUDE, 0.0);

        String climatePtr = JSONUtils.getStringParam(param, KEY_CLIMATES, "");
        String soilPtr = JSONUtils.getStringParam(param, KEY_SOILS, "");
        String managementFormalName[] = new String[]{};

// to do
// Future: validate if these files are in place on back end
//        String contourSystem = JSONUtils.getStringParam(param, KEY_CONTOUR_SYSTEM_PTR, "");
//        String stripBarrierSystem = JSONUtils.getStringParam(param, KEY_STRIP_BARRIER_SYSTEM_PTR, "");
//        String hydElemSystem = JSONUtils.getStringParam(param, KEY_HYD_ELEM_SYSTEM_PTR, "");
//
        String mgmtPtr = JSONUtils.getStringParam(param, KEY_MANAGEMENTS, "");
        if (!mgmtPtr.startsWith("managements\\")) {
            mgmtPtr = "managements\\" + mgmtPtr;
        }
        // management conversion
        // Make file line separator unix compatible. ???? not sure if needed.
        System.setProperty("line.separator", "\n");
        //
        JSONArray managements = JSONUtils.getJSONArrayParam(param, KEY_MGMTS);

        LOG.info("managements array=" + managements.toString());
        managementFormalName = new String[managements.length()];

        for (int i = 0; i < managements.length(); i++) {
            LOG.info("management #" + i);

            JSONObject lmod = (JSONObject) managements.get(i);
            LOG.info(lmod.toString());

            LOG.info("creating Jim's translator.");
            lmod2rusle2.Rusle2Translator translator = new lmod2rusle2.Rusle2Translator();
            LOG.info("trying to put in the lmod json");
            translator.readJsonString(lmod.toString());
            LOG.info("trying to translate=" + translator.Translate());

            Document r2_xml = translator.getDocument();

            LOG.info("R2 XML FILE from trhe translator:\n\n" + translator.getRusle2Xml());
            LOG.info("R2 XML FILE:\n\n" + translator.getRusle2Xml());

            if ((r2_xml != null) && (r2_xml.getElementsByTagName("Filename") != null)) {
                NodeList nl = r2_xml.getElementsByTagName("Filename");
                for (int ii = 0; ii < nl.getLength(); ii++) {
                    LOG.info("filename node=" + nl.item(ii).getTextContent());
                    managementFormalName[i] = nl.item(ii).getTextContent();
                }
            }
            translator.writeRusle2Xml(getWorkspaceDir().toString(), "lmod_file" + i + ".xml");
            operations = translator.getOperationFiles();
            vegetations = translator.getVegetationFiles();
            residues = translator.getResidueFiles();

            for (String operation : translator.getOperationFiles()) {
                System.out.println("operation=" + operation);
            }
            for (String vege : translator.getVegetationFiles()) {
                System.out.println("vegetation=" + vege);
            }
            for (String residue : translator.getResidueFiles()) {
                System.out.println("residue=" + residue);
            }

        }

        String altR2db = null;
        if (param.get(KEY_ALT_R2DB) != null) {
            altR2db = param.get(KEY_ALT_R2DB).getString(VALUE);
        }

        PostGIS db = PostGIS.singleton();

        // If this is a Lat/Lng request, then we need to make a DB lookup
        // to obtain the Climate and Soil information and ignore whatever
        // may have been passed in
        LOG.info("Rulse 2 model request resolveLocation=" + resolveLoc);
        //if (resolveLoc) {

        if (db != null) {
            PostGIS.FileQryResult climate = db.findClimate(latitude, longitude);
            if (climate == null) {
                LOG.warning("no climate for lat=" + latitude + "\nfor long=" + longitude + "\n");
                climatePtr = "";
            } else {
                climatePtr = climate.file_path + "\\" + climate.file_name;
            }

            // to do
            // use only the first soil for now - later we need to use all of them
            String cokey = soilPtr.replace(",", " ");
            // strip first and last char
            String t2 = cokey.substring(1, cokey.length() - 1);
            String delims = "\"";
            String tokens[] = t2.split(delims);
            LinkedList<String> lst = new LinkedList();
            for (String token : tokens) {
                if (token.trim().length() > 0) {
                    lst.add(token);
                }
            }
            cokey = lst.get(0);  // first soil only for now
            PostGIS.FileQryResult soil = db.findSoilsByCokey(cokey, longitude);
            if (soil == null) {
                LOG.warning("No soil for lat=" + latitude + "\nfor long=" + longitude + "\n");
                appendMetainfoWarning(R2_NO_SOIL_FOR_PROVIDED_COKEY);
                soilPtr = "";
            } else {
                soilPtr = soil.file_path + "\\" + soil.file_name;
            }
        } else {
            LOG.warning("WARNING: Unable to access database to resolve the climate and soil information for this lat/long driven rusle2 model run.  Using deafults.");
        }

    }


    private String getFilenumber(String filename) {
        return filename.substring(R2_TMP_FILENAME.length(), filename.length() - R2_TMP_FILEEXT.length());
    }

//    @Override
//    protected JSONArray createReport() throws Exception {
//        JSONArray reportItemsOutput = new JSONArray();
//        return reportItemsOutput;
//    }
//    private JSONObject processReportElement(JSONObject obj, String type, String value) throws Exception {
//        if ((type.equals("TEXT")) || (type.equals("FILENAME")) || (type.equals("DATE"))) {
//            // because the RomeShell returns escaped strings, and the JSONObject in Java re-escapes them
//            // we have to remove one level of "escaping" !!! 
//            String newString = StringEscapeUtils.unescapeJava(value);
//            obj.put(REPORT_VALUE, Services.removeFirstLastChar(newString));
//        }
//        if (type.equals("INTEGER") && (value != null) && (!value.equals("null")) && (!value.equals("\"\""))) {
//            obj.put("value", new Double(value).intValue());
//        }
//        if (type.equals("REAL") && (value != null)) {
//            if ((!value.equals("null")) && (!value.equals("\"\""))) {
//                obj.put(REPORT_VALUE, new Double(value).doubleValue());
//            }
//            if (value.equals("\"\"")) {
//                obj.put(REPORT_VALUE, new Double(0).doubleValue());
//            }
//        }
//        if (type.equals("")) {
//            obj.put(REPORT_VALUE, value);
//        }
//        return obj;
//    }
}