Displaying differences for changeset
 
display as  

src/java/m/wqm/nutappmgtscores/Crop.java

@@ -6,26 +6,29 @@
  */
 import java.util.Date;
 import csip.utils.Dates;
+import java.util.ArrayList;
 
 public class Crop {
 
-    private int cropId;
+    private int mgtCropId;
     private boolean lmod;
     private String cropPlantDate;
     private double cropYield;
     private String cropYieldUnits;
+    private ArrayList<Nutrient> nutrientApplicationList;
 
-    public Crop(int cropId, boolean lmod, String cropPlantDate, double cropYield,
-            String cropYieldUnits) {
-        this.cropId = cropId;
+    public Crop(int mgtCropId, boolean lmod, String cropPlantDate, double cropYield,
+            String cropYieldUnits, ArrayList<Nutrient> nutrientApplicationList) {
+        this.mgtCropId = mgtCropId;
         this.lmod = lmod;
         this.cropPlantDate = cropPlantDate;
         this.cropYield = cropYield;
         this.cropYieldUnits = cropYieldUnits;
+        this.nutrientApplicationList = nutrientApplicationList;
     }
 
-    public int getCropId() {
-        return cropId;
+    public int getMgtCropId() {
+        return mgtCropId;
     }
 
     public boolean getLmod() {
@@ -44,4 +47,8 @@
     public String getCropYieldUnits() {
         return cropYieldUnits;
     }
+
+    public ArrayList getNutrientApplicationList() {
+        return nutrientApplicationList;
+    }
 }

src/java/m/wqm/nutappmgtscores/Input.java

@@ -4,36 +4,30 @@
  *
  * @author RUMPAL SIDHU
  */
-import java.util.Date;
+
+import java.util.ArrayList;
 
 public class Input {
 
-    int AoAId;
-    int mgt_crop_id;
-    boolean from_lmod;
-    Date crop_plant_date;
-    double crop_yield;
-    String crop_yield_units;
-    String [] nutrient_application_date_array;
-    boolean incorporated;
-    String [] nutrient_applied_array;
-    double application_rate;
-    String p_soil_test_result;
+    private int AoAId;
+    private ArrayList<Crop> cropList;
+    private String pSoilTestResult;
 
-    public Input(int AoAId, int mgt_crop_id, boolean from_lmod, Date crop_plant_date,
-            double crop_yield, String crop_yield_units, String [] nutrient_application_date_array,
-            boolean incorporated, String [] nutrient_applied_array, double application_rate,
-            String p_soil_test_result) {
+    public Input(int AoAId, ArrayList<Crop> cropList, String pSoilTestResult) {
         this.AoAId = AoAId;
-        this.mgt_crop_id = mgt_crop_id;
-        this.from_lmod = from_lmod;
-        this.crop_plant_date = crop_plant_date;
-        this.crop_yield = crop_yield;
-        this.crop_yield_units = crop_yield_units;
-        this.nutrient_application_date_array = nutrient_application_date_array;
-        this.incorporated = incorporated;
-        this.nutrient_applied_array = nutrient_applied_array;
-        this.application_rate = application_rate;
-        this.p_soil_test_result = p_soil_test_result;
+        this.cropList = cropList;
+        this.pSoilTestResult = pSoilTestResult;
     }
-}
\ No newline at end of file
+
+    public int getAoAId() {
+        return AoAId;
+    }
+
+    public ArrayList getCropList() {
+        return cropList;
+    }
+
+    public String getPSoilTestResult() {
+        return pSoilTestResult;
+    }
+}

src/java/m/wqm/nutappmgtscores/Nutrient.java

@@ -4,9 +4,32 @@
  *
  * @author RUMPAL SIDHU
  */
+import csip.utils.Dates;
+import java.util.ArrayList;
+import java.util.Date;
 
 public class Nutrient {
-    
-    
-    public Nutrient () {}
+
+    private String applicationDate;
+    private boolean incorporated;
+    private ArrayList<NutrientApplied> nutrientAppliedList;
+
+    public Nutrient(String applicationDate, boolean incorporated, ArrayList<NutrientApplied> nutrientAppliedList) {
+        this.applicationDate = applicationDate;
+        this.incorporated = incorporated;
+        this.nutrientAppliedList = nutrientAppliedList;
+    }
+
+    public Date getApplicationDate() throws Exception {
+        Date date = Dates.parse(applicationDate);
+        return date;
+    }
+
+    public boolean isIncorporated() {
+        return incorporated;
+    }
+
+    public ArrayList getNutrientAppliedList() {
+        return nutrientAppliedList;
+    }
 }

src/java/m/wqm/nutappmgtscores/V1_0.java

@@ -5,11 +5,13 @@
  * @author RUMPAL SIDHU
  */
 import csip.ModelDataService;
+import static csip.ModelDataService.EXEC_OK;
 import csip.utils.JSONUtils;
 import csip.utils.Dates;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.ArrayList;
 import java.util.Map;
@@ -27,8 +29,15 @@
 
 public class V1_0 extends ModelDataService {
 
-    ArrayList<m.wqm.nutappmgtscores.Input> components = new ArrayList<>(); // store the set of all input soilcomponents as objects
-    ArrayList<m.wqm.nutappmgtscores.Result> result = new ArrayList<>();  // store the result as objects
+    //SQL params here for quick modification
+    private final String USER = "postgres";
+    private final String PASS = "postgresql";
+    private final String SERVER = "localhost:5432/postgres";
+    private final String JDBC_TYPE = "jdbc:postgresql://";
+    private final String CLASS_NAME = "org.postgresql.Driver";
+
+    ArrayList<m.wqm.nutappmgtscores.Input> input = new ArrayList<>(); //Store input
+    ArrayList<m.wqm.nutappmgtscores.Result> result = new ArrayList<>(); //Store result
 
     @Override
     // reading the inputs from the json file into input object and placing it in the arraylist
@@ -36,34 +45,39 @@
 
         int AoAId = getIntParam("AoAId", 0);
         String p_soil_test_result = getStringParam("p_soil_test_result", "err");
-        JSONArray mgtCropIds = getJSONArrayParam("mgtCropIds");
-        ArrayList mgmCrop = new ArrayList();
-        for (int i = 0; i < mgtCropIds.length(); i++) {
-            Map<String, JSONObject> mgtCropId = JSONUtils.preprocess(mgtCropIds.getJSONArray(i));            
+        ArrayList<Crop> cropList = new ArrayList<>();
+
+        JSONArray cropIds = getJSONArrayParam("cropIds");
+        for (int i = 0; i < cropIds.length(); i++) {
+            Map<String, JSONObject> mgtCropId = JSONUtils.preprocess(cropIds.getJSONArray(i));
+
             int cropId = JSONUtils.getIntParam(mgtCropId, "mgt_crop_id", 0);
             boolean lmod = JSONUtils.getBooleanParam(mgtCropId, "from_lmod", false);
             String cropPlantDate = JSONUtils.getStringParam(mgtCropId, "crop_plant_date", "err");
             //Date crop_plant_date = Dates.parse(crop_plant_date_string);
             double cropYield = JSONUtils.getDoubleParam(mgtCropId, "crop_yield", 0);
             String cropYieldUnits = JSONUtils.getStringParam(mgtCropId, "crop_yield_units", "err");
+
+            ArrayList<Nutrient> nutrientApplicationList = new ArrayList<>();
             JSONArray applicationList = JSONUtils.getJSONArrayParam(mgtCropId, "applicationList");
             for (int j = 0; j < applicationList.length(); j++) {
                 Map<String, JSONObject> application = JSONUtils.preprocess(applicationList.getJSONArray(j));
                 String nutrient_application_date = JSONUtils.getStringParam(application, "nutrient_application_date", "err");
                 boolean incorporated = JSONUtils.getBooleanParam(application, "incorporated", false);
+
+                ArrayList<NutrientApplied> nutrientAppliedList = new ArrayList<>();
                 JSONArray applicate = JSONUtils.getJSONArrayParam(application, "application");
                 for (int k = 0; k < applicate.length(); k++) {
                     Map<String, JSONObject> a = JSONUtils.preprocess(applicate.getJSONArray(k));
                     String nutrient_applied = JSONUtils.getStringParam(a, "nutrient_applied", "err");
                     double application_rate = JSONUtils.getDoubleParam(a, "application_rate", 0);
+                    nutrientAppliedList.add(new NutrientApplied(nutrient_applied, application_rate));
                 }
+                nutrientApplicationList.add(new Nutrient(nutrient_application_date, incorporated, nutrientAppliedList));
             }
-            
+            cropList.add(new Crop(cropId, lmod, cropPlantDate, cropYield, cropYieldUnits, nutrientApplicationList));
         }
-
-//        m.wqm.nutappmgtscores.Input input = new m.wqm.nutappmgtscores.Input(AoAId, mgt_crop_id, from_lmod, crop_plant_date, crop_yield, crop_yield_units, nutrient_application_date_array, incorporated, nutrient_applied_array, application_rate, p_soil_test_result);
-    //    components.add(input);
-
+        input.add(new m.wqm.nutappmgtscores.Input(AoAId, cropList, p_soil_test_result));
     }
 
     @Override
@@ -71,138 +85,155 @@
 
         Connection conn = null;
         Statement statement = null;
-        Class.forName("org.postgresql.Driver");
-        conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/WQM Data Mart 20150324", "postgres", "postgresql");
-        conn.setAutoCommit(false);
-        statement = conn.createStatement();
+        String query;
+        ResultSet resultSet = null;
 
-        for (m.wqm.nutappmgtscores.Input ip : components) {
-            //if (ip.AoAId > 0) {
-                int n_app_timing_score = 0;
-                int p_app_timing_score = 0;
+        try {
+            Class.forName(CLASS_NAME);
+            conn = DriverManager.getConnection(JDBC_TYPE + SERVER, USER, PASS);
+            conn.setAutoCommit(false);
+
+            statement = conn.createStatement();
+
+            for (m.wqm.nutappmgtscores.Input ip : input) {
+                int n_app_timing_score = 100;
+                int p_app_timing_score = 100;
+
                 int app_method_score = 0;
                 int n_app_rate_score = 0;
                 int p_app_rate_score = 0;
-                //for (int i = 0; i < length(ip.mgt_crop_id); i++) {}
-                int this_crop_id;
-                if (ip.from_lmod) {
-                    String query = "SELECT wqm_crop_id FROM wqm_lmod_crop_link WHERE lmod_crop_id=" + ip.mgt_crop_id + ";";
-                    ResultSet results = statement.executeQuery(query);
-                    this_crop_id = 0;
-                    while (results.next()) {
-                        this_crop_id = results.getInt("wqm_crop_id");
+                int this_crop_id = 0;
+
+                ArrayList<Crop> cropList = ip.getCropList();
+                for (Crop crop : cropList) {
+                    ArrayList<Nutrient> nutrientApplicationList = crop.getNutrientApplicationList();
+
+                    //#If request payload crop is an LMOD vegetation, then convert it to a wqm_crop using the link table
+                    if (crop.getLmod()) {
+                        query = "SELECT wqm_crop_id FROM wqm_lmod_crop_link WHERE lmod_crop_id=" + crop.getMgtCropId() + ";";
+                        resultSet = statement.executeQuery(query);
+                        while (resultSet.next()) {
+                            this_crop_id = resultSet.getInt("wqm_crop_id");
+                        }
+                    } else {
+                        this_crop_id = crop.getMgtCropId();
                     }
-                } else {
-                    this_crop_id = ip.mgt_crop_id;
-                }
 
-                //#Determine whether split nutrient applications or not
-                int app_count = 0;
-                String app_type;
-                //For each nutrient_application_date in the crop period
-                for (String i : ip.nutrient_application_date_array) {
-                    app_count = app_count + 1;
-                }
-                if (app_count == 1) {
-                    app_type = "nosplit";
-                } else {
-                    app_type = "split";
-                }
+                    //#Determine crop type of the crop
+                    query = "SELECT crop_type FROM wqm_crops WHERE wqm_crop_id=" + this_crop_id + ";";
+                    resultSet = statement.executeQuery(query);
+                    String crop_type = "err";
+                    while (resultSet.next()) {
+                        crop_type = resultSet.getString("crop_type");
+                    }
 
-                //#Determine crop type of the crop
-                String query = "SELECT crop_type FROM wqm_crops WHERE wqm_crop_id=" + this_crop_id + ";";
-                ResultSet results = statement.executeQuery(query);
-                String crop_type = "err";
-                while (results.next()) {
-                    crop_type = results.getString("crop_type");
-                }
+                    //#Determine whether split nutrient applications or not
+                    int app_count = 0;
+                    String app_type;
+                    //For each nutrient_application_date in the crop period
+                    //for (Nutrient i : nutrientApplication) {
+                    //    app_count = app_count + 1;
+                    //}
+                    app_count = nutrientApplicationList.size();
+                    if (app_count == 1) {
+                        app_type = "nosplit";
+                    } else {
+                        app_type = "split";
+                    }
 
-                int ncrop_app_rate_score = 0;
-                int pcrop_app_rate_score = 0;
-                //If no nutrient_application_date (no nutrient applications for the crop)
-                //#Compute score for not fertilizing.
-                if (ip.nutrient_application_date_array.length == 0) {
-                    query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Nitrogen" + "' AND app_mgt_kind='" + "Rate" + "' AND app_mgt_factor='" + "None" + "';";
-                    results = statement.executeQuery(query);
-                    while (results.next()) {
-                        n_app_rate_score = results.getInt("app_mgt_score");
-                    }
-                    switch (ip.p_soil_test_result) {
-                        case "High": {
-                            query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Phosphorus" + "' AND app_mgt_kind='" + "Rate" + "' AND app_mgt_factor='" + "None" + "' AND soil_test_result='" + "High" + "';";
-                            results = statement.executeQuery(query);
-
-                            while (results.next()) {
-                                p_app_rate_score = results.getInt("app_mgt_score");
+                    //If no nutrient_application_date (no nutrient applications for the crop)
+                    int ncrop_app_rate_score = 0;
+                    int pcrop_app_rate_score = 0;
+                    if (nutrientApplicationList.isEmpty()) {
+                        //#Compute score for not fertilizing.
+                        query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Nitrogen" + "' AND app_mgt_kind='" + "Rate" + "' AND app_mgt_factor='" + "None" + "';";
+                        resultSet = statement.executeQuery(query);
+                        while (resultSet.next()) {
+                            n_app_rate_score = resultSet.getInt("app_mgt_score");
+                        }
+                        switch (ip.getPSoilTestResult()) {
+                            case "High": {
+                                query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Phosphorus" + "' AND app_mgt_kind='" + "Rate" + "' AND app_mgt_factor='" + "None" + "' AND soil_test_result='" + "High" + "';";
+                                resultSet = statement.executeQuery(query);
+                                while (resultSet.next()) {
+                                    p_app_rate_score = resultSet.getInt("app_mgt_score");
+                                }
+                                break;
                             }
-                            break;
-                        }
-                        case "Medium": {
-                            query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Phosphorus" + "' AND app_mgt_kind='" + "Rate" + "' AND app_mgt_factor='" + "None" + "' AND soil_test_result='" + "Medium" + "';";
-                            results = statement.executeQuery(query);
-                            while (results.next()) {
-                                p_app_rate_score = results.getInt("app_mgt_score");
+                            case "Medium": {
+                                query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Phosphorus" + "' AND app_mgt_kind='" + "Rate" + "' AND app_mgt_factor='" + "None" + "' AND soil_test_result='" + "Medium" + "';";
+                                resultSet = statement.executeQuery(query);
+                                while (resultSet.next()) {
+                                    p_app_rate_score = resultSet.getInt("app_mgt_score");
+                                }
+                                break;
                             }
-                            break;
-                        }
-                        case "None": {
-                            query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Phosphorus" + "' AND app_mgt_kind='" + "Rate" + "' AND app_mgt_factor='" + "None" + "' AND soil_test_result='" + "None" + "';";
-                            results = statement.executeQuery(query);
-                            while (results.next()) {
-                                p_app_rate_score = results.getInt("app_mgt_score");
+                            case "None": {
+                                query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Phosphorus" + "' AND app_mgt_kind='" + "Rate" + "' AND app_mgt_factor='" + "None" + "' AND soil_test_result='" + "None" + "';";
+                                resultSet = statement.executeQuery(query);
+                                while (resultSet.next()) {
+                                    p_app_rate_score = resultSet.getInt("app_mgt_score");
+                                }
                             }
                         }
-                    }
-                } else { //Else for each nutrient_application_date (nutrient application) in the crop period
-                    for (String i : ip.nutrient_application_date_array) { //#Compute N and P removal ratios
+                    } else { //Else #Compute N and P removal ratios
+
                         double nrate = 0.0;
                         double prate = 0.0;
-                        for (String j : ip.nutrient_applied_array) {
-                            if (j.equals("Nitrogen")) {
-                                nrate = nrate + ip.application_rate;
-                            } else {
-                                prate = prate + ip.application_rate;
+
+                        for (Nutrient nutrient : nutrientApplicationList) {
+                            ArrayList<NutrientApplied> nutrientAppliedList = nutrient.getNutrientAppliedList();
+                            for (NutrientApplied nApplied : nutrientAppliedList) {
+                                if (nApplied.getNutrientApplied().equals("Nitrogen")) {
+                                    nrate = nrate + nApplied.getApplicationRate();
+                                } else {
+                                    prate = prate + nApplied.getApplicationRate();
+                                }
                             }
                         }
+
                         double wqm_crop_pct_dmat = 0.0;
                         double wqm_pct_nitrogen = 0.0;
                         double wqm_pct_phosphorus = 0.0;
                         query = "SELECT wqm_crop_pct_dmat, wqm_pct_nitrogen, wqm_pct_phosphorus FROM wqm_crops WHERE wqm_crop_id=" + this_crop_id + ";";
-                        results = statement.executeQuery(query);
-                        while (results.next()) {
-                            wqm_crop_pct_dmat = results.getDouble("wqm_crop_pct_dmat");
-                            wqm_pct_nitrogen = results.getDouble("wqm_pct_nitrogen");
-                            wqm_pct_phosphorus = results.getDouble("wqm_pct_phosphorus");
+                        resultSet = statement.executeQuery(query);
+                        while (resultSet.next()) {
+                            wqm_crop_pct_dmat = resultSet.getDouble("wqm_crop_pct_dmat");
+                            wqm_pct_nitrogen = resultSet.getDouble("wqm_pct_nitrogen");
+                            wqm_pct_phosphorus = resultSet.getDouble("wqm_pct_phosphorus");
                         }
-                        double n_growout = ip.crop_yield * wqm_crop_pct_dmat * wqm_pct_nitrogen;
-                        double p_growout = ip.crop_yield * wqm_crop_pct_dmat * wqm_pct_phosphorus;
+
+                        double n_growout = crop.getCropYield() * wqm_crop_pct_dmat * wqm_pct_nitrogen;
+                        double p_growout = crop.getCropYield() * wqm_crop_pct_dmat * wqm_pct_phosphorus;
+
                         double n_remove_ratio = nrate / n_growout;
                         double p_remove_ratio = prate / p_growout;
+
                         //#Compute N application management rate score based on removal ratio and whether small grain or not
                         if (crop_type.equals("small grain")) {
                             query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Nitrogen" + "' AND app_mgt_kind='" + "Rate" + "' AND app_mgt_factor='" + "small grain" + "' AND remove_ratio_1 <= " + n_remove_ratio + "AND remove_ratio_2 > " + n_remove_ratio + ";";
-                            results = statement.executeQuery(query);
-                            while (results.next()) {
-                                ncrop_app_rate_score = results.getInt("app_mgt_score");
+                            resultSet = statement.executeQuery(query);
+                            while (resultSet.next()) {
+                                ncrop_app_rate_score = resultSet.getInt("app_mgt_score");
                             }
                         } else {
                             query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Nitrogen" + "' AND app_mgt_kind='" + "Rate" + "' AND app_mgt_factor='" + "other" + "' AND remove_ratio_1 <= " + n_remove_ratio + "AND remove_ratio_2 > " + n_remove_ratio + ";";
-                            results = statement.executeQuery(query);
-                            while (results.next()) {
-                                ncrop_app_rate_score = results.getInt("app_mgt_score");
+                            resultSet = statement.executeQuery(query);
+                            while (resultSet.next()) {
+                                ncrop_app_rate_score = resultSet.getInt("app_mgt_score");
                             }
                         }
+
                         //#Compute P application management rate scores based on removal ratio and soil test result
-                        switch (ip.p_soil_test_result) {
+                        switch (ip.getPSoilTestResult()) {
                             case "High": {
                                 if (p_remove_ratio >= 1.2) {
                                     pcrop_app_rate_score = 0;
                                 } else {
                                     query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Phosphorus" + "' AND app_mgt_kind='" + "Rate" + "' AND app_mgt_factor='" + "app" + "' AND soil_test_result'=" + "High" + "'AND remove_ratio_1 <= " + p_remove_ratio + "AND remove_ratio_2 > " + p_remove_ratio + ";";
-                                    pcrop_app_rate_score = 0;
-                                    results = statement.executeQuery(query);
-                                    while (results.next()) {
-                                        pcrop_app_rate_score = results.getInt("app_mgt_score");
+                                    resultSet = statement.executeQuery(query);
+                                    while (resultSet.next()) {
+                                        pcrop_app_rate_score = resultSet.getInt("app_mgt_score");
                                     }
                                 }
                                 break;
@@ -212,10 +243,9 @@
                                     pcrop_app_rate_score = 0;
                                 } else {
                                     query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Phosphorus" + "' AND app_mgt_kind='" + "Rate" + "' AND app_mgt_factor='" + "app" + "' AND soil_test_result'=" + "Medium" + "'AND remove_ratio_1 <= " + p_remove_ratio + "AND remove_ratio_2 > " + p_remove_ratio + ";";
-                                    pcrop_app_rate_score = 0;
-                                    results = statement.executeQuery(query);
-                                    while (results.next()) {
-                                        pcrop_app_rate_score = results.getInt("app_mgt_score");
+                                    resultSet = statement.executeQuery(query);
+                                    while (resultSet.next()) {
+                                        pcrop_app_rate_score = resultSet.getInt("app_mgt_score");
                                     }
                                 }
                                 break;
@@ -225,113 +255,139 @@
                                     pcrop_app_rate_score = 0;
                                 } else {
                                     query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Phosphorus" + "' AND app_mgt_kind='" + "Rate" + "' AND app_mgt_factor='" + "app" + "' AND soil_test_result'=" + "Low" + "'AND remove_ratio_1 <= " + p_remove_ratio + "AND remove_ratio_2 > " + p_remove_ratio + ";";
-                                    pcrop_app_rate_score = 0;
-                                    results = statement.executeQuery(query);
-                                    while (results.next()) {
-                                        pcrop_app_rate_score = results.getInt("app_mgt_score");
+                                    resultSet = statement.executeQuery(query);
+                                    while (resultSet.next()) {
+                                        pcrop_app_rate_score = resultSet.getInt("app_mgt_score");
                                     }
                                 }
                                 break;
                             }
-
                             case "None": {
                                 if (p_remove_ratio >= 1.2) {
                                     pcrop_app_rate_score = 0;
                                 } else {
                                     query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Phosphorus" + "' AND app_mgt_kind='" + "Rate" + "' AND app_mgt_factor='" + "app" + "' AND soil_test_result'=" + "None" + "'AND remove_ratio_1 <= " + p_remove_ratio + "AND remove_ratio_2 > " + p_remove_ratio + ";";
-                                    pcrop_app_rate_score = 0;
-                                    results = statement.executeQuery(query);
-                                    while (results.next()) {
-                                        pcrop_app_rate_score = results.getInt("app_mgt_score");
+                                    resultSet = statement.executeQuery(query);
+                                    while (resultSet.next()) {
+                                        pcrop_app_rate_score = resultSet.getInt("app_mgt_score");
                                     }
                                 }
                                 break;
                             }
                         }
 
+                        //#Update N and P application management rate scores for the AoA
+                        n_app_rate_score = n_app_rate_score + ncrop_app_rate_score;
+                        p_app_rate_score = p_app_rate_score + pcrop_app_rate_score;
                     }
-                    //#Increment N and P application management rate scores
-                    n_app_rate_score = n_app_rate_score + ncrop_app_rate_score;
-                    p_app_rate_score = p_app_rate_score + pcrop_app_rate_score;
-                }
-                int ncrop_app_timing_score = 0;
-                int pcrop_app_timing_score = 0;
-                for (String i : ip.nutrient_application_date_array) { //#Compute N and P application timing scores for the crop
-                    Date application_date = Dates.parse(i);
-                    long app_day_diff = Dates.diffInMillis(application_date, ip.crop_plant_date);
-                    app_day_diff = TimeUnit.MICROSECONDS.convert(app_day_diff, TimeUnit.DAYS);
-                    for (String j : ip.nutrient_applied_array) {
-                        switch (j) {
-                            case "Nitrogen": {
-                                if (app_type.equals("split")) {
-                                    query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Nitrogen" + "' AND app_mgt_kind='" + "Timing" + "' AND app_mgt_factor='" + "split" + "'AND days_fr_plant_1 <= " + app_day_diff + "AND days_fr_plant_2 > " + app_day_diff + ";";
-                                    results = statement.executeQuery(query);
-                                    while (results.next()) {
-                                        ncrop_app_timing_score = results.getInt("app_mgt_score");
+
+                    //#Compute N and P application timing scores for the crop and update timin scores for the AoA
+                    int ncrop_app_timing_score = 0;
+                    int pcrop_app_timing_score = 0;
+                    for (Nutrient nutrient : nutrientApplicationList) {
+                        Date application_date = nutrient.getApplicationDate();
+                        long app_day_diff = Dates.diffInMillis(application_date, crop.getCropPlantDate());
+                        app_day_diff = TimeUnit.MICROSECONDS.convert(app_day_diff, TimeUnit.DAYS);
+
+                        ArrayList<NutrientApplied> nutrientAppliedList = nutrient.getNutrientAppliedList();
+                        for (NutrientApplied nApplied : nutrientAppliedList) {
+                            switch (nApplied.getNutrientApplied()) {
+                                case "Nitrogen": {
+                                    if (app_type.equals("split")) {
+                                        query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Nitrogen" + "' AND app_mgt_kind='" + "Timing" + "' AND app_mgt_factor='" + "split" + "'AND days_fr_plant_1 <= " + app_day_diff + "AND days_fr_plant_2 > " + app_day_diff + ";";
+                                        resultSet = statement.executeQuery(query);
+                                        while (resultSet.next()) {
+                                            ncrop_app_timing_score = resultSet.getInt("app_mgt_score");
+                                            //Doubt
+                                            if (resultSet.wasNull()) {
+                                                n_app_timing_score = 0;
+                                            } else if (ncrop_app_timing_score < n_app_timing_score) {
+                                                n_app_timing_score = ncrop_app_timing_score;
+                                            }
+                                        }
+
+                                    } else {
+                                        query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Nitrogen" + "' AND app_mgt_kind='" + "Timing" + "' AND app_mgt_factor='" + "nosplit" + "'AND days_fr_plant_1 <= " + app_day_diff + "AND days_fr_plant_2 > " + app_day_diff + ";";
+                                        resultSet = statement.executeQuery(query);
+                                        while (resultSet.next()) {
+                                            ncrop_app_timing_score = resultSet.getInt("app_mgt_score");
+                                            //Doubt
+                                            if (resultSet.wasNull()) {
+                                                n_app_timing_score = 0;
+                                            } else if (ncrop_app_timing_score < n_app_timing_score) {
+                                                n_app_timing_score = ncrop_app_timing_score;
+                                            }
+                                        }
                                     }
-                                } else {
-                                    query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Nitrogen" + "' AND app_mgt_kind='" + "Timing" + "' AND app_mgt_factor='" + "nosplit" + "'AND days_fr_plant_1 <= " + app_day_diff + "AND days_fr_plant_2 > " + app_day_diff + ";";
-                                    results = statement.executeQuery(query);
-                                    while (results.next()) {
-                                        ncrop_app_timing_score = results.getInt("app_mgt_score");
-                                    }
+                                    break;
                                 }
-                                break;
-                            }
-                            case "Phosphorus": {
-                                if (app_type.equals("split")) {
-                                    query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Phosphorus" + "' AND app_mgt_kind='" + "Timing" + "' AND app_mgt_factor='" + "split" + "'AND days_fr_plant_1 <= " + app_day_diff + "AND days_fr_plant_2 > " + app_day_diff + ";";
-                                    results = statement.executeQuery(query);
-                                    while (results.next()) {
-                                        pcrop_app_timing_score = results.getInt("app_mgt_score");
-                                    }
-                                } else {
-                                    query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Phosphorus" + "' AND app_mgt_kind='" + "Timing" + "' AND app_mgt_factor='" + "nosplit" + "'AND days_fr_plant_1 <= " + app_day_diff + "AND days_fr_plant_2 > " + app_day_diff + ";";
-                                    results = statement.executeQuery(query);
-                                    while (results.next()) {
-                                        pcrop_app_timing_score = results.getInt("app_mgt_score");
+                                case "Phosphorus": {
+                                    if (app_type.equals("split")) {
+                                        query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Phosphorus" + "' AND app_mgt_kind='" + "Timing" + "' AND app_mgt_factor='" + "split" + "'AND days_fr_plant_1 <= " + app_day_diff + "AND days_fr_plant_2 > " + app_day_diff + ";";
+                                        resultSet = statement.executeQuery(query);
+                                        while (resultSet.next()) {
+                                            pcrop_app_timing_score = resultSet.getInt("app_mgt_score");
+                                            //Doubt
+                                            if (resultSet.wasNull()) {
+                                                p_app_timing_score = 0;
+                                            } else if (pcrop_app_timing_score < p_app_timing_score) {
+                                                p_app_timing_score = ncrop_app_timing_score;
+                                            }
+                                        }
+                                    } else {
+                                        query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE nutrient='" + "Phosphorus" + "' AND app_mgt_kind='" + "Timing" + "' AND app_mgt_factor='" + "nosplit" + "'AND days_fr_plant_1 <= " + app_day_diff + "AND days_fr_plant_2 > " + app_day_diff + ";";
+                                        resultSet = statement.executeQuery(query);
+                                        while (resultSet.next()) {
+                                            pcrop_app_timing_score = resultSet.getInt("app_mgt_score");
+                                            //Doubt
+                                            if (resultSet.wasNull()) {
+                                                p_app_timing_score = 0;
+                                            } else if (pcrop_app_timing_score < p_app_timing_score) {
+                                                p_app_timing_score = pcrop_app_timing_score;
+                                            }
+                                        }
                                     }
                                 }
                             }
                         }
                     }
+                    for (Nutrient nutrient : nutrientApplicationList) {
+                        //#If any nutrient application for any crop is not incorporated, the method score for the AoA is zero
+                        if (!nutrient.isIncorporated()) {
+                            app_method_score = 0;
+                            break;
+                        } else if (nutrient.isIncorporated()) { //#If all nutrient applications for all crops are incorporated, the method score for incorporation applies to the AoA
+                            query = "SELECT app_mgt_score FROM wqm_nutrient_application_mgt_scores WHERE app_mgt_kind='" + "Method" + "' AND app_mgt_factor='" + "incorporate" + ";";
+                            resultSet = statement.executeQuery(query);
+                            while (resultSet.next()) {
+                                app_method_score = resultSet.getInt("app_method_score");
+
+                            }
+
+                        }
+
+                    }
 
                 }
-
-                //#Application timing score for AoA is lowest nutrient application timing score for any crop
-                if (n_app_timing_score == 0) {
-                    n_app_timing_score = ncrop_app_timing_score;
-                } else if (n_app_timing_score > ncrop_app_timing_score) {
-                    n_app_timing_score = ncrop_app_timing_score;
-                }
-                if (p_app_timing_score == 0) {
-                    p_app_timing_score = pcrop_app_timing_score;
-                } else if (p_app_timing_score > pcrop_app_timing_score) {
-                    p_app_timing_score = pcrop_app_timing_score;
-                }
-
-                for (String i : ip.nutrient_application_date_array) {
-                    /*
-                     For each nutrient_application_date in the crop period
-                     #If any nutrient application for any crop is not incorporated, the method score for the AoA is zero
-                     If incorporated FALSE
-                     app_method_score = 0
-                     Break For loop
-                     #If all nutrient applications for all crops are incorporated, the method score for incorporation applies to the AoA
-                     Else if incorporated TRUE
-                     Select
-                     app_mgt_score As app_method_score
-                     Where app_mgt_kind == method and app_mgt_factor == incorporate
-                     */
-                }
+                //#Compute application management scores for nitrogen in groundwater, nitrogen in surface water, and phosphorus in surface water
                 int nleach_app_mgt_score = n_app_rate_score + n_app_timing_score + app_method_score;
                 int nsurf_app_mgt_score = n_app_rate_score + n_app_timing_score + app_method_score;
                 int psurf_app_mgt_score = p_app_rate_score + p_app_timing_score + app_method_score;
-                m.wqm.nutappmgtscores.Result result1 = new m.wqm.nutappmgtscores.Result(ip.AoAId, nleach_app_mgt_score, nsurf_app_mgt_score, psurf_app_mgt_score, n_app_rate_score, n_app_timing_score, p_app_rate_score, p_app_timing_score, app_method_score);
+                m.wqm.nutappmgtscores.Result result1 = new m.wqm.nutappmgtscores.Result(ip.getAoAId(), nleach_app_mgt_score, nsurf_app_mgt_score, psurf_app_mgt_score, n_app_rate_score, n_app_timing_score, p_app_rate_score, p_app_timing_score, app_method_score);
                 result.add(result1);
             }
-//        }
-
+            conn.close();
+        } catch (SQLException se) {
+            LOG.info("Did not open database for WQM-16!");
+            LOG.info(se.getMessage());
+        } finally {
+            if (statement != null) {
+                statement.close();
+            }
+            if (conn != null) {
+                conn.close();
+            }
+        }
         return EXEC_OK;
     }
 

src/java/m/wqm/nutappmgtscores/V1_0.json

@@ -13,7 +13,7 @@
             "Description": "Soil Test Result for Phosphorus, values are High, Medium, Low, None"
         },
         {
-            "name": "mgtCropIds",
+            "name": "cropIds",
             "description": "List of mgt crop id",
             "value": [
                 [