From 9c5d10a30bf60a0e7c495543faaed42e5d2ddf2a Mon Sep 17 00:00:00 2001
From: Bhabesh <bhabesh.mukhopadhyay@nibio.no>
Date: Thu, 12 Nov 2020 14:50:34 +0100
Subject: [PATCH] Multiple types included but unchange the pojo structure

Pojo data structure (i.e. Plant and Plantation) not changed. It helps modelconfiguration simple. Business logic implemented in preprocessor level to understand different types (i.e. varieties) in json file (used as temporary small database)
---
 .../PhenologyModelPreprocessor.java           | 78 +++++++++----------
 .../java/no/nibio/vips/logic/util/Plant.java  | 32 +++++---
 .../no/nibio/vips/logic/util/Plantation.java  | 74 ++++++------------
 3 files changed, 82 insertions(+), 102 deletions(-)

diff --git a/src/main/java/no/nibio/vips/logic/scheduling/model/preprocessor/PhenologyModelPreprocessor.java b/src/main/java/no/nibio/vips/logic/scheduling/model/preprocessor/PhenologyModelPreprocessor.java
index c31b206a..9d771d92 100644
--- a/src/main/java/no/nibio/vips/logic/scheduling/model/preprocessor/PhenologyModelPreprocessor.java
+++ b/src/main/java/no/nibio/vips/logic/scheduling/model/preprocessor/PhenologyModelPreprocessor.java
@@ -157,24 +157,13 @@ public class PhenologyModelPreprocessor extends ModelRunPreprocessor{
             LOGGER.log(Level.CONFIG, "Observations=" + observations.toString());
         }
     
-        Plant   plantDBData   =   getConfigurationPlantation(FILE_PLANTATION);
+        Plant   plantDBData   =   getConfigurationPlantation(FILE_PLANTATION,paramPhenologyTypeName);
         
-        if(!plantDBData.getTypeNames().isEmpty())
+        if(!plantDBData.getPlantations().isEmpty())
         {
-            for(Plantation objTypeNames: plantDBData.getTypeNames())
-            {
-                if(objTypeNames.getTypeName().equals((paramPhenologyTypeName)))
-                {
-                    
-                    plant.setSpeciesLatinName(plantDBData.getSpeciesLatinName());
-                    plant.setBaseTemp(objTypeNames.getBaseTemp());
-                    List<Plantation>  typeNames   = new ArrayList<Plantation>();
-                    typeNames.add(objTypeNames);
-                    plant.setTypeNames(typeNames);
-                    plant.setStartDate(paramSowingDate);
-                    break;
-                }
-            }
+            plant = plantDBData;
+            plant.setStartDate(paramSowingDate);
+
         }
         
         /*
@@ -326,7 +315,7 @@ public class PhenologyModelPreprocessor extends ModelRunPreprocessor{
         return retVal;
     }
     
-    private Plant getConfigurationPlantation(String fileName)
+    private Plant getConfigurationPlantation(String fileName, String strParamTypeName)
     {
         Plant   plant   = new Plant();
                 
@@ -338,53 +327,62 @@ public class PhenologyModelPreprocessor extends ModelRunPreprocessor{
             BufferedInputStream inputStream = new BufferedInputStream(this.getClass().getResourceAsStream(fileName));
             JsonFactory f = new MappingJsonFactory();
             JsonParser jp = f.createParser(inputStream);
-            JsonNode all = jp.readValueAsTree();
+            JsonNode nodeSpecies = jp.readValueAsTree();
+            
+            JsonNode nodeTypeNames = nodeSpecies.path("typeNames");
+            
+            plant.setSpeciesLatinName(nodeSpecies.get("speciesLatinName").asText());
+            plant.setBaseTemp(nodeSpecies.get("baseTemp").asDouble());
+            
             
-            JsonNode nodeTypeNames = all.path("typeNames");
             if(nodeTypeNames.isArray())
             {
                  for(JsonNode nodeTypeName : nodeTypeNames)
                  {
-                     Plantation plantation  = new Plantation();
-                     plantation.setTypeName(nodeTypeName.get("typeName").asText());
-                     if(null != nodeTypeName.get("baseTemp").asText())
+                     if(nodeTypeName.get("typeName").asText().trim().equalsIgnoreCase(strParamTypeName))
                      {
-                         plantation.setBaseTemp(nodeTypeName.get("baseTemp").asDouble());
-                     }
+                         plant.setTypeName(strParamTypeName);
+                        if(null != nodeTypeName.get("baseTemp").asText())
+                        {
+                            plant.setBaseTemp(nodeTypeName.get("baseTemp").asDouble());
+                        }
+                         
                      JsonNode nodeAllPhaseInfo = nodeTypeName.path("phaseInfo");
                      if(nodeAllPhaseInfo.isArray())
                      {
-                         List<Plantation.PhaseInfo> listPhaseInfo = new ArrayList<Plantation.PhaseInfo>();
                          for(JsonNode nodePhaseInfo : nodeAllPhaseInfo)
                          {
-                             Plantation.PhaseInfo phaseInfo = new Plantation().new PhaseInfo();
-                             phaseInfo.setPhaseName(nodePhaseInfo.get("phaseName").asText());
-                             phaseInfo.setHeatReq(nodePhaseInfo.get("heatReq").asDouble());
-                             listPhaseInfo.add(phaseInfo);
-                         }
-                         if(listPhaseInfo.size() != 0)
-                         {
-                             plantation.setPhaseInfo(listPhaseInfo);
+                            Plantation plantation  = new Plantation();
+                            plantation.setTypeName(nodeTypeName.get("typeName").asText());
+
+
+                             plantation.setPhaseName(nodePhaseInfo.get("phaseName").asText());
+                             plantation.setHeatReq(nodePhaseInfo.get("heatReq").asDouble());
+                             
+                             plantations.add(plantation);
                          }
+
                      }
-                     plantations.add(plantation);
+                     
+                     } 
+                     
                  }
                  
             }
             
             
         /*  
-            JsonNode nodePhaseInfo = all.path("phaseInfo");
-            plant.setSpeciesLatinName(all.get("speciesLatinName").asText());
-            plant.setTypeName(all.get("typeName").asText());
-            plant.setBaseTemp(all.get("baseTemp").asDouble());
+            JsonNode nodePhaseInfo = nodeSpecies.path("phaseInfo");
+            plant.setSpeciesLatinName(nodeSpecies.get("speciesLatinName").asText());
+            plant.setTypeName(nodeSpecies.get("typeName").asText());
+            plant.setBaseTemp(nodeSpecies.get("baseTemp").asDouble());
             
             if(nodePhaseInfo.isArray())
             {
                 for(JsonNode node : nodePhaseInfo)
                 {
                      Plantation   plantation  =   new Plantation
-                                                (           all.get("typeName").asText()
+                                                (           nodeSpecies.get("typeName").asText()
                                                         ,   node.get("phaseName").asText()
                                                        ,    node.get("heatReq").asDouble()
                                                 );
@@ -392,7 +390,7 @@ public class PhenologyModelPreprocessor extends ModelRunPreprocessor{
                 }
             }
         */    
-            plant.setTypeNames(plantations);
+            plant.setPlantations(plantations);
             
             
         } catch (IOException ex) {
diff --git a/src/main/java/no/nibio/vips/logic/util/Plant.java b/src/main/java/no/nibio/vips/logic/util/Plant.java
index ce4230d8..780e67e4 100644
--- a/src/main/java/no/nibio/vips/logic/util/Plant.java
+++ b/src/main/java/no/nibio/vips/logic/util/Plant.java
@@ -11,38 +11,50 @@ import java.util.List;
 
 public class Plant {
     private String              speciesLatinName;
+    private String              typeName;
     private Double              baseTemp;
     private Date                startDate;
-    private List<Plantation>    typeNames;
+    
+    private List<Plantation>    plantations;
 
     public String getSpeciesLatinName() {
         return speciesLatinName;
     }
+
     public void setSpeciesLatinName(String speciesLatinName) {
         this.speciesLatinName = speciesLatinName;
     }
+
+    public String getTypeName() {
+        return typeName;
+    }
+
+    public void setTypeName(String typeName) {
+        this.typeName = typeName;
+    }
+
     public Double getBaseTemp() {
         return baseTemp;
     }
+
     public void setBaseTemp(Double baseTemp) {
         this.baseTemp = baseTemp;
     }
+
     public Date getStartDate() {
         return startDate;
     }
+
     public void setStartDate(Date startDate) {
         this.startDate = startDate;
     }
-    public List<Plantation> getTypeNames() {
-        return typeNames;
-    }
-    public void setTypeNames(List<Plantation> typeNames) {
-        this.typeNames = typeNames;
+
+    public List<Plantation> getPlantations() {
+        return plantations;
     }
 
-    @Override
-    public String toString() {
-        return "Plant{" + "speciesLatinName=" + speciesLatinName + ", baseTemp=" + baseTemp + ", startDate=" + startDate + ", typeNames=" + typeNames + '}';
+    public void setPlantations(List<Plantation> plantations) {
+        this.plantations = plantations;
     }
-    
+        
 }
diff --git a/src/main/java/no/nibio/vips/logic/util/Plantation.java b/src/main/java/no/nibio/vips/logic/util/Plantation.java
index 09448b19..42fd0feb 100644
--- a/src/main/java/no/nibio/vips/logic/util/Plantation.java
+++ b/src/main/java/no/nibio/vips/logic/util/Plantation.java
@@ -2,7 +2,6 @@
 package no.nibio.vips.logic.util;
 
 import java.util.Date;
-import java.util.List;
 
 /**
  *
@@ -12,23 +11,25 @@ import java.util.List;
 
 public class Plantation {
     private String  typeName;
-    private Double  baseTemp;
-    List<PhaseInfo>   phaseInfo;
-    
+    private String  phaseName;
+    //private String  baseTemp;
+    private Double  heatReq;
+    //private Date    startDate;
+
     public Plantation() {
     }
 
-    public Plantation(String typeName, List<PhaseInfo> phaseInfo) {
-        this.typeName   = typeName;
-        this.phaseInfo  = phaseInfo;
+    public Plantation(String typeName, String phaseName, Double heatReq) {
+        this.typeName = typeName;
+        this.phaseName = phaseName;
+        this.heatReq = heatReq;
         } 
     
-      public Plantation(String typeName,Double baseTemp, List<PhaseInfo> phaseInfo) {
-        this.typeName   = typeName;
-        this.baseTemp  = baseTemp;
-        this.phaseInfo  = phaseInfo;
+      public Plantation(String phaseName, Double heatReq) {
+        this.phaseName = phaseName;
+        this.heatReq = heatReq;
         }  
-
+    
     public String getTypeName() {
         return typeName;
     }
@@ -37,56 +38,25 @@ public class Plantation {
         this.typeName = typeName;
     }
 
-    public Double getBaseTemp() {
-        return baseTemp;
+    public String getPhaseName() {
+        return phaseName;
     }
 
-    public void setBaseTemp(Double baseTemp) {
-        this.baseTemp = baseTemp;
+    public void setPhaseName(String phaseName) {
+        this.phaseName = phaseName;
     }
 
-    public List<PhaseInfo> getPhaseInfo() {
-        return phaseInfo;
+    public Double getHeatReq() {
+        return heatReq;
     }
 
-    public void setPhaseInfo(List<PhaseInfo> phaseInfo) {
-        this.phaseInfo = phaseInfo;
+    public void setHeatReq(Double heatReq) {
+        this.heatReq = heatReq;
     }
 
-      
     @Override
     public String toString() {
-        return "Plantation{" + "typeName=" + typeName + ", baseTemp=" + baseTemp + ", phaseInfo=" + phaseInfo + '}';
+        return "Plantation{" + "typeName=" + typeName + ", phaseName=" + phaseName + ", heatReq=" + heatReq + '}';
     }
-    
-    public class PhaseInfo
-    {
-            private String  phaseName;
-            private Double  heatReq;
-
-        public String getPhaseName() {
-            return phaseName;
-        }
 
-        public void setPhaseName(String phaseName) {
-            this.phaseName = phaseName;
-        }
-
-        public Double getHeatReq() {
-            return heatReq;
-        }
-
-        public void setHeatReq(Double heatReq) {
-            this.heatReq = heatReq;
-        }
-
-        @Override
-        public String toString() {
-            return "PhaseInfo{" + "phaseName=" + phaseName + ", heatReq=" + heatReq + '}';
-        }
-        
-    }
-    
-    
 }
-
-- 
GitLab