LOADEST_V1_0.java [src/java/m/cfa] Revision: 43efd7e0f3b8fe3c64f8229934ef0a7c85a69c01  Date: Fri Feb 14 14:19:18 MST 2014
package m.cfa;

import cfa.guiLOADEST_Model;
import csip.AbstractModelService;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.Callable;
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.*;
import csip.utils.JSONUtils;
import csip.ServiceConst;

@Name("loadest")
@Description("loadest")
@VersionInfo("1.0")
@Path("m/cfa/loadest/1.0")
public class LOADEST_V1_0 extends AbstractModelService {

    guiLOADEST_Model model = new guiLOADEST_Model();

    @Override
    protected Callable<Integer> createCallable() throws Exception {
        return new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                Map<String, JSONObject> m = getParamMap();

                model.setMainFolder(getWorkspaceDir().toString());
                model.setOrganizationName(m.get("org").getString(ServiceConst.VALUE));
                model.setStationName(m.get("station_name").getString(ServiceConst.VALUE));
                model.setStationID(m.get("station_id").getString(ServiceConst.VALUE));
                model.setWQtest(m.get("wq_test").getString(ServiceConst.VALUE));
                model.setPTOPT(m.get("ptopt").getInt(ServiceConst.VALUE));
                model.setSEOPT(m.get("seopt").getInt(ServiceConst.VALUE));
                model.setLDOPT(m.get("ldopt").getInt(ServiceConst.VALUE));
                model.setmodelNumber(m.get("modelnumber").getInt(ServiceConst.VALUE));
                model.setPBMON(m.get("pbmon").getInt(ServiceConst.VALUE));
                model.setPEMON(m.get("pemon").getInt(ServiceConst.VALUE));
                model.setULFLAG(m.get("ulflag").getInt(ServiceConst.VALUE));
                model.setBeginDate(m.get("begin_date").getString(ServiceConst.VALUE));
                model.setEndDate(m.get("end_date").getString(ServiceConst.VALUE));
                model.setUserData(m.get("user_data").getString(ServiceConst.VALUE));
                model.setMergeDatasets(m.get("merge_datasets").getBoolean(ServiceConst.VALUE));
                model.setMergeMethod(m.get("merge_method").getString(ServiceConst.VALUE));
                model.run();
                return ServiceConst.EXEC_OK;
            }
        };
    }

    @Override
    public String[] postprocess() throws Exception {
        return new String[] {//Results files needed for JavaScript graphing with JHighCharts
                             model.getDailyTimeseries().getName(),
                             model.getDailyBoxplot().getName(),
                             model.getMonthlyTimeseries().getName(),
                             model.getMonthlyBoxplot().getName(),
                             model.getYearlyTimeseries().getName(),
                             model.getYearlyBoxplot().getName(),
                             //Graphs no longer handled with Java
//                             model.getDailyGraph(),
//                             model.getDailyBoxplotGraph(),
//                             model.getMonthlyGraph(),
//                             model.getMonthlyBoxplotGraph(),
//                             model.getYearlyGraph(),
//                             model.getYearlyBoxplotGraph(),
                             //Results files desired by the user as output of the model
                             model.getOutputResult1().getName(),
                             model.getOutputResult2().getName(),
                             model.getOutputResult3().getName()};
    }
    
    @Override
    protected JSONArray createResults() throws Exception {
        
        JSONArray result = new JSONArray();
        result.put(JSONUtils.data("flow_len", model.getFlowLen()));
        result.put(JSONUtils.data("wq_len", model.getWQLen()));
        result.put(JSONUtils.data("wq_units", model.getWQUnits()));
        result.put(JSONUtils.data("start", model.getStart()));
        result.put(JSONUtils.data("end", model.getEnd()));
        
//        result.put(JSONUtils.data("daily_graph", model.getDailyGraph()));
//        result.put(JSONUtils.data("daily_boxplot", model.getDailyBoxplotGraph()));
        result.put(JSONUtils.data("daily_max", model.getDailyMax()));
        result.put(JSONUtils.data("daily_min", model.getDailyMin()));
        result.put(JSONUtils.data("daily_upper_quartile", model.getDailyUpperQuartile()));
        result.put(JSONUtils.data("daily_lower_quartile", model.getDailyLowerQuartile()));
        result.put(JSONUtils.data("daily_median", model.getDailyMedian()));
        result.put(JSONUtils.data("daily_mean", model.getDailyMean()));
        result.put(JSONUtils.data("daily_standard_deviation", model.getDailyStandardDeviation()));
        
//        result.put(JSONUtils.data("monthly_graph", model.getMonthlyGraph()));
//        result.put(JSONUtils.data("monthly_boxplot", model.getMonthlyBoxplotGraph()));
        result.put(JSONUtils.data("monthly_max", model.getMonthlyMax()));
        result.put(JSONUtils.data("monthly_min", model.getMonthlyMin()));
        result.put(JSONUtils.data("monthly_upper_quartile", model.getMonthlyUpperQuartile()));
        result.put(JSONUtils.data("monthly_lower_quartile", model.getMonthlyLowerQuartile()));
        result.put(JSONUtils.data("monthly_median", model.getMonthlyMedian()));
        result.put(JSONUtils.data("monthly_mean", model.getMonthlyMean()));
        result.put(JSONUtils.data("monthly_standard_deviation", model.getMonthlyStandardDeviation()));
        
//        result.put(JSONUtils.data("yearly_graph", model.getYearlyGraph()));
//        result.put(JSONUtils.data("yearly_boxplot", model.getYearlyBoxplotGraph()));
        result.put(JSONUtils.data("yearly_max", model.getYearlyMax()));
        result.put(JSONUtils.data("yearly_min", model.getYearlyMin()));
        result.put(JSONUtils.data("yearly_upper_quartile", model.getYearlyUpperQuartile()));
        result.put(JSONUtils.data("yearly_lower_quartile", model.getYearlyLowerQuartile()));
        result.put(JSONUtils.data("yearly_median", model.getYearlyMedian()));
        result.put(JSONUtils.data("yearly_mean", model.getYearlyMean()));
        result.put(JSONUtils.data("yearly_standard_deviation", model.getYearlyStandardDeviation()));
        
        String graphDataFiles = model.getDailyTimeseries().getName() + "|" +  
                      model.getDailyBoxplot().getName() + "|" + 
                      model.getMonthlyTimeseries().getName() + "|" + 
                      model.getMonthlyBoxplot().getName() + "|" + 
                      model.getYearlyTimeseries().getName() + "|" + 
                      model.getYearlyBoxplot().getName();
        result.put(JSONUtils.data("graph_data_files", graphDataFiles));
        
//        result.put(JSONUtils.data("summaryFile", model.getOutputSummary().getName()));
        result.put(JSONUtils.data("user_resultFile1", model.getOutputResult1().getName()));
        result.put(JSONUtils.data("user_resultFile2", model.getOutputResult2().getName()));
        result.put(JSONUtils.data("user_resultFile3", model.getOutputResult3().getName()));
        return result;
    }

    @Override
    protected JSONObject describe() throws JSONException {
        try {
            String tmpl = IOUtils.toString(LOADEST_V1_0.class.getResource("/m/cfa/LOADEST_V1_0Req.json"));
            return new JSONObject(tmpl);
        } catch (IOException ex) {
            throw new RuntimeException("Not found: /m/cfa/LOADEST_V1_0Req.json");
        }
    }
}