Baseflow_V1_0.java [src/java/m/cfa] Revision: 43efd7e0f3b8fe3c64f8229934ef0a7c85a69c01  Date: Fri Feb 14 14:19:18 MST 2014
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package m.cfa;

import cfa.guiBaseflow_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.FileUtils;
import org.apache.commons.io.IOUtils;
import org.codehaus.jettison.json.*;
import csip.utils.JSONUtils;
import csip.ServiceConst;

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

    guiBaseflow_Model model = new guiBaseflow_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.setDrainageArea(m.get("drainage_area").getDouble(ServiceConst.VALUE));
                model.setModelType(m.get("model_type").getString(ServiceConst.VALUE));
                model.setBeginDate(m.get("begin_date").getString(ServiceConst.VALUE));
                model.setEndDate(m.get("end_date").getString(ServiceConst.VALUE));
                model.setNDMIN(m.get("ndmin").getInt(ServiceConst.VALUE));
                model.setNDMAX(m.get("ndmax").getInt(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.getBaseflow_out().getName()};
    }
    
    @Override
    protected JSONArray createResults() throws Exception {
        String baseflowSummary = FileUtils.readFileToString(model.getBaseflow_dat());
        JSONArray result = new JSONArray();
        result.put(JSONUtils.data("len", model.getLen()));
        result.put(JSONUtils.data("start", model.getStart()));
        result.put(JSONUtils.data("end", model.getEnd()));
        result.put(JSONUtils.data("baseflow_summary", baseflowSummary));
        result.put(JSONUtils.data("baseflow_results", model.getBaseflow_out().getName()));
        result.put(JSONUtils.data("graph", model.getGraph()));
        return result;
    }

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