V1_0.java [src/java/m/wqm/pesticidessrp] Revision: ee0b8b47273dff50b87bb8a75e4e6cad79761ead  Date: Mon Nov 30 08:34:28 MST 2015
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package m.wqm.pesticidessrp;
import csip.ModelDataService;
import static csip.ModelDataService.EXEC_OK;
import java.util.ArrayList;
import javax.ws.rs.Path;
import oms3.annotations.Description;
import oms3.annotations.Name;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import csip.utils.JSONUtils;
import java.util.Map;

/**
 *
 * @ Srinivas
 */
@Name("WQM-08: Pesticide Soil Solution Runoff Potential (PesticideSSRP)")
@Description("This service computes pesticide soil solution runoff potential for soil components in an area of analysis, and computes soil solution runoff potential representing the area of analysis (AoA). The service primarily will consume data from the WQM-2 WQMSoilAttributes service to compute soil solution runoff potential used later by the WQM-13 service to compute WQM threshold treatment level scores.")
@Path("m/pesticide_ssrp/1.0")

public class V1_0 extends ModelDataService{
    
    String comp_ssrp[]=new String[]{"","LOW","INTERMEDIATE","HIGH"};
    int comp_ssrp_number;
    String aoa_ssrp;
    //JSONArray getArray
    ArrayList<V1_0.Input> components=new ArrayList<>(); // store the set of all input soilcomponents as objects
    ArrayList<V1_0.Result1> result1=new ArrayList<>();  // store the result as objects
    @Override
        // reading the inputs from the json file into input object and placing it in the arraylist
        protected void preProcess() throws Exception {
        JSONArray groups = getJSONArrayParam("soilcomponents");
            for(int i=0;i<groups.length();i++){
                Map<String, JSONObject> group = JSONUtils.preprocess(groups.getJSONArray(i));
                int AoAId = JSONUtils.getIntParam(group, "AoAId", 0);
                String cokey=JSONUtils.getStringParam(group,"cokey","err");
                double aoa_comp_area=JSONUtils.getDoubleParam(group,"aoa_comp_area",0);
                String aoa_comp_hsg=JSONUtils.getStringParam(group,"aoa_comp_hsg","err");
                boolean aoa_comp_drained=JSONUtils.getBooleanParam(group,"aoa_comp_drained" ,false);
                components.add(new V1_0.Input(AoAId,cokey,aoa_comp_area,aoa_comp_hsg,aoa_comp_drained));
            }
        }

    @Override
        protected String process() throws Exception{
            for (V1_0.Input ip : components){
                if(ip.aoa_comp_hsg.equals("C")||ip.aoa_comp_hsg.equals("D")||ip.aoa_comp_hsg.equals("C/D")){
                    comp_ssrp_number=3;
                }
                else if(ip.aoa_comp_hsg.equals("A")||ip.aoa_comp_hsg.equals("A/D")){
                    if(ip.aoa_comp_drained){
                    
                        comp_ssrp_number=1;
                    }
                    else if(!ip.aoa_comp_drained){
                    
                        comp_ssrp_number=3;
                    }
                }
                else if(ip.aoa_comp_hsg.equals("B")||ip.aoa_comp_hsg.equals("B/D")){
                    if(ip.aoa_comp_drained){
                    
                        comp_ssrp_number=2;
                    }
                    else if(!ip.aoa_comp_drained){
                    
                        comp_ssrp_number=3;

                    }
                }
                result1.add(new V1_0.Result1(ip.AoAId,ip.cokey,ip.aoa_comp_area,comp_ssrp[comp_ssrp_number],comp_ssrp_number));
            }
            calAoANutSLP(result1);
            return EXEC_OK;       
        }
    @Override
        //writing the results back to JSON
        protected void postProcess() throws Exception {
            for(V1_0.Result1 temp:result1){
                
                putResult("AoAId",temp.AoAId,"Area of Analysis ID");
                putResult("cokey",temp.cokey,"Cokey of each soil component");
                putResult("aoa_comp_area",temp.aoa_comp_area,"Soil Component Area");
                putResult("comp_ssrp",temp.comp_ssrp,"Calculated SSRP for soil component");
                putResult("comp_ssrp_number",temp.comp_ssrp_number,"Number representing the SSRP");
            }    
            putResult("aoa_ssrp",aoa_ssrp,"Overall SSRP of the area of analysis");
        }
   // calculate the aoa_nslp 
        void calAoANutSLP(ArrayList<Result1> source){
            double cum_nslp_product=0;
            double aoa_area=0;
        
            for(V1_0.Result1 tmp:source){
                cum_nslp_product+=(tmp.comp_ssrp_number*tmp.aoa_comp_area);
                aoa_area+=tmp.aoa_comp_area;
            }
            double aoa_nslp_fract=cum_nslp_product/aoa_area;
            if(aoa_nslp_fract<=1.50){
                aoa_ssrp="LOW"; 
            }
            else if(aoa_nslp_fract>1.50&&aoa_nslp_fract<=2.50){
                aoa_ssrp="INTERMEDIATE";
            }

            else{
                aoa_ssrp="HIGH";
            }
        }
    public class Input {

        int AoAId;
        String cokey;
        double aoa_comp_area;
        String aoa_comp_hsg;
        boolean aoa_comp_drained;
        public Input(int AoAId,String cokey,double aoa_comp_area,String aoa_comp_hsg,boolean aoa_comp_drained){
            this.AoAId=AoAId;
            this.cokey=cokey;
            this.aoa_comp_area=aoa_comp_area;
            this.aoa_comp_hsg=aoa_comp_hsg;
            this.aoa_comp_drained=aoa_comp_drained;
        }
    }
    public class Result1 {
        int AoAId;
        String cokey;
        double aoa_comp_area;
        String comp_ssrp;
        int comp_ssrp_number;
        public Result1(int AoAId,String cokey,double aoa_comp_area,String comp_nslp,int comp_nslp_number){
            this.AoAId=AoAId;
            this.cokey=cokey;
            this.aoa_comp_area=aoa_comp_area;
            this.comp_ssrp=comp_nslp;
            this.comp_ssrp_number=comp_nslp_number;
        }
    }
}