DurationCurve_V1_0.java [src/java/m/cfa] Revision: 4daefd1ac3a5cce6d2af07d219b133db7ce0b7a4  Date: Thu Sep 26 16:17:42 MDT 2013
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package m.cfa;

import cfa.guiDC_Model;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.Callable;
import javax.ws.rs.Path;
import services.AbstractModelService;
import oms3.annotations.Description;
import oms3.annotations.Name;
import oms3.annotations.VersionInfo;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.codehaus.jettison.json.*;
import utils.JSONUtils;
import csip.ServiceConst;

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

    guiDC_Model model = new guiDC_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.setStationID(m.get("station_id").getString(ServiceConst.VALUE));
                model.setStationName(m.get("station_name").getString(ServiceConst.VALUE));
                model.setModelType(m.get("model_type").getString(ServiceConst.VALUE));
                model.setWaterQualityTest(m.get("wq_test").getString(ServiceConst.VALUE));
                model.setWaterQualityTarget(m.get("wq_target").getDouble(ServiceConst.VALUE));
                model.setBeginDate(m.get("begin_date").getString(ServiceConst.VALUE));
                model.setEndDate(m.get("end_date").getString(ServiceConst.VALUE));
                model.setSeasonBegin(m.get("season_begin").getString(ServiceConst.VALUE));
                model.setSeasonEnd(m.get("season_end").getString(ServiceConst.VALUE));
                model.setMQNperiod(m.get("mqn_period").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[] {model.getGraph(), model.getDurationCurve_results().getName()};
    }

    @Override
    protected JSONArray createResults() throws Exception {
        String summary = FileUtils.readFileToString(model.getDurationCurve_summary());
        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("mqn_val", model.getMQNval()));
        result.put(JSONUtils.data("low_flow_error_message", model.getLowFlowErrorMessage()));
        result.put(JSONUtils.data("duration_curve_summary", summary));
        result.put(JSONUtils.data("graph", model.getGraph()));
        result.put(JSONUtils.data("result_file", model.getDurationCurve_results().getName()));
        return result;
    }

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