From 786facf4dbe8f98a6583d7fb20522b3240ed764a Mon Sep 17 00:00:00 2001
From: Tor-Einar Skog <tor-einar.skog@nibio.no>
Date: Thu, 9 Jan 2020 14:48:32 +0100
Subject: [PATCH] Added dynamic update of crop organism id for winter wheat

---
 .../nordic_septoria_map.js                    | 24 ++++++++++++++-----
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/src/main/webapp/public/nordic_septoria_map/nordic_septoria_map.js b/src/main/webapp/public/nordic_septoria_map/nordic_septoria_map.js
index 27b8ce96..117feec1 100644
--- a/src/main/webapp/public/nordic_septoria_map/nordic_septoria_map.js
+++ b/src/main/webapp/public/nordic_septoria_map/nordic_septoria_map.js
@@ -41,12 +41,25 @@ for (var i in views)
 }
 var maps = {mainMap:null, subMap1: null, subMap2: null, subMap3: null, subMap4: null};
 var language = "en";
-
+var cropNorwegianName = "Høsthvete";
+var cropId = -1;
 
 /**
  * After all libraries have been loaded (added to the containing page), this
- * function sets up all the maps and displays the initial view
- * @returns {undefined}
+ * function configures anything that needs dynamic config and then inits the map
+ * setup
+ */
+var initApp = function()
+{
+    ajax(hostName + "/rest/organism/search/localnames/nb?keywords=" + cropNorwegianName, function(e){
+        var cropList = JSON.parse(e.target.responseText);
+        cropId = cropList[0].organismId;
+        initMap();
+    });
+};
+
+/**
+ * Set up the map and all that goes with it
  */
 var initMap = function ()
 {
@@ -439,7 +452,7 @@ var getResultsChain = function(season, layerName)
                         }
                     }
                     
-                    ajax(hostName + "/rest/gs/date/32,71/25/?season=" + season + "&location=" + currentForecast.locationPointOfInterestId.longitude + "," + currentForecast.locationPointOfInterestId.latitude, function(e){
+                    ajax(hostName + "/rest/gs/date/32,71/" + cropId + "/?season=" + season + "&location=" + currentForecast.locationPointOfInterestId.longitude + "," + currentForecast.locationPointOfInterestId.latitude, function(e){
                         // This callback interprets the data returned from 
                         // the VIPSLogic GrowthStageService
                         // It then forwards results and GS dates to displayResults
@@ -457,7 +470,6 @@ var getResultsChain = function(season, layerName)
                                 GS71Date = moment(GSResults[i]["71"]);
                             }
                         }
-                        
                         var startDate = document.getElementById("startDate").value != "" ? moment(document.getElementById("startDate").value) : GS32Date;
                         var endDate = document.getElementById("endDate").value != "" ? moment(document.getElementById("endDate").value) : GS71Date;
                         //console.info("startDate=" + startDate.format("YYYY-MM-DD"));
@@ -912,7 +924,7 @@ document.addEventListener("DOMContentLoaded", function() {
                                             { 
                                                 src: "https://kit.fontawesome.com/567c8b1eb5.js" 
                                             }, 
-                                            initMap
+                                            initApp
                                         ); 
                                     }
                                 ); 
-- 
GitLab