From 43cc4a19c50512e5435ddd4a0946c8577586b864 Mon Sep 17 00:00:00 2001
From: Tor-Einar Skog <tor-einar.skog@nibio.no>
Date: Tue, 27 Aug 2024 15:20:27 +0200
Subject: [PATCH] Add admin for weather station data sources: Main menu item
 and default page (list)

---
 .../WeatherStationDataSourceController.java   | 108 ++++++++++++++++++
 .../vips/logic/i18n/vipslogictexts.properties |   2 +
 .../logic/i18n/vipslogictexts_bs.properties   |   2 +
 .../logic/i18n/vipslogictexts_hr.properties   |   2 +
 .../logic/i18n/vipslogictexts_nb.properties   |   2 +
 .../logic/i18n/vipslogictexts_sr.properties   |   2 +
 .../i18n/vipslogictexts_zh_CN.properties      |   2 +
 src/main/webapp/WEB-INF/web.xml               |   8 ++
 src/main/webapp/templates/master.ftl          |   3 +
 .../weatherStationDataSourceList.ftl          |  39 +++++++
 10 files changed, 170 insertions(+)
 create mode 100644 src/main/java/no/nibio/vips/logic/controller/servlet/WeatherStationDataSourceController.java
 create mode 100644 src/main/webapp/templates/weatherStationDataSourceList.ftl

diff --git a/src/main/java/no/nibio/vips/logic/controller/servlet/WeatherStationDataSourceController.java b/src/main/java/no/nibio/vips/logic/controller/servlet/WeatherStationDataSourceController.java
new file mode 100644
index 00000000..3efd4961
--- /dev/null
+++ b/src/main/java/no/nibio/vips/logic/controller/servlet/WeatherStationDataSourceController.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2024 NIBIO <http://www.nibio.no/>. 
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+package no.nibio.vips.logic.controller.servlet;
+
+import java.io.IOException;
+
+import java.util.List;
+
+import javax.ejb.EJB;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import no.nibio.vips.logic.controller.session.PointOfInterestBean;
+import no.nibio.vips.logic.entity.VipsLogicUser;
+import no.nibio.vips.logic.entity.WeatherStationDataSource;;
+
+/**
+ * Handles actions regarding listing and modifying weather station data sources
+ * 
+ * @copyright 2024 <a href="http://www.nibio.no/">NIBIO</a>
+ * @author Tor-Einar Skog <tor-einar.skog@nibio.no>
+ */
+public class WeatherStationDataSourceController  extends HttpServlet{
+
+    @EJB
+    PointOfInterestBean poiBean;
+
+    /** 
+     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
+     * @param request servlet request
+     * @param response servlet response
+     * @throws ServletException if a servlet-specific error occurs
+     * @throws IOException if an I/O error occurs
+     */
+    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
+    throws ServletException, IOException {
+
+        response.setContentType("text/html;charset=UTF-8");
+        
+        VipsLogicUser user = (VipsLogicUser) request.getSession().getAttribute("user");
+        // Basic authorization
+        if(!user.isSuperUser() && ! user.isOrganizationAdmin()){
+            response.sendError(403,"Access not authorized");
+            return;
+        }
+        String action = request.getParameter("action");
+
+        if(action == null)
+        {
+            List<WeatherStationDataSource> wsDataSources = poiBean.getWeatherStationDataSources();
+            request.setAttribute("weatherStationDataSources", wsDataSources);
+            request.getRequestDispatcher("/weatherStationDataSourceList.ftl").forward(request, response);
+        }
+    }
+
+    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
+    /** 
+     * Handles the HTTP <code>GET</code> method.
+     * @param request servlet request
+     * @param response servlet response
+     * @throws ServletException if a servlet-specific error occurs
+     * @throws IOException if an I/O error occurs
+     */
+    @Override
+    protected void doGet(HttpServletRequest request, HttpServletResponse response)
+    throws ServletException, IOException {
+        processRequest(request, response);
+    } 
+
+    /** 
+     * Handles the HTTP <code>POST</code> method.
+     * @param request servlet request
+     * @param response servlet response
+     * @throws ServletException if a servlet-specific error occurs
+     * @throws IOException if an I/O error occurs
+     */
+    @Override
+    protected void doPost(HttpServletRequest request, HttpServletResponse response)
+    throws ServletException, IOException {
+        processRequest(request, response);
+    }
+
+    /** 
+     * Returns a short description of the servlet.
+     * @return a String containing servlet description
+     */
+    @Override
+    public String getServletInfo() {
+        return "Short description";
+    }// </editor-fold>
+}
diff --git a/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts.properties b/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts.properties
index 849d6e2f..3c388d81 100755
--- a/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts.properties
+++ b/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts.properties
@@ -1055,3 +1055,5 @@ thresholdDSVTempMin=Minimum temperature for DSV calculation
 useGridWeatherData=Use grid weather data
 doNotUse=Do not use
 defaultGridWeatherStationDataSource=Gridded weather data source
+weatherStationDataSources=Weather station data sources
+newWeatherStationDataSource=New weather (station) data source
diff --git a/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_bs.properties b/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_bs.properties
index 3fb8bdfe..5911156f 100755
--- a/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_bs.properties
+++ b/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_bs.properties
@@ -1049,3 +1049,5 @@ thresholdDSVTempMin=Minimum temperature for DSV calculation
 useGridWeatherData=Use grid weather data
 doNotUse=Do not use
 defaultGridWeatherStationDataSource=Gridded weather data source
+weatherStationDataSources=Weather station data sources
+newWeatherStationDataSource=New weather (station) data source
diff --git a/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_hr.properties b/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_hr.properties
index 3f488790..00d9452c 100755
--- a/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_hr.properties
+++ b/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_hr.properties
@@ -1047,3 +1047,5 @@ thresholdDSVTempMin=Minimum temperature for DSV calculation
 useGridWeatherData=Use grid weather data
 doNotUse=Do not use
 defaultGridWeatherStationDataSource=Gridded weather data source
+weatherStationDataSources=Weather station data sources
+newWeatherStationDataSource=New weather (station) data source
diff --git a/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_nb.properties b/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_nb.properties
index 0a3f9103..5a472851 100755
--- a/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_nb.properties
+++ b/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_nb.properties
@@ -1055,3 +1055,5 @@ thresholdDSVTempMin=Minimumstemperatur for beregning av DSV
 useGridWeatherData=Bruk v\u00e6rdata fra rutenett
 doNotUse=Ikke bruk
 defaultGridWeatherStationDataSource=GRID-basert v\u00e6rdatakilde
+weatherStationDataSources=V\u00e6r(stasjons)datakilder
+newWeatherStationDataSource=Ny v\u00e6r(stasjons)datakilde
diff --git a/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_sr.properties b/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_sr.properties
index be4ee828..c3a70f06 100755
--- a/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_sr.properties
+++ b/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_sr.properties
@@ -1049,3 +1049,5 @@ thresholdDSVTempMin=Minimum temperature for DSV calculation
 useGridWeatherData=Use grid weather data
 doNotUse=Do not use
 defaultGridWeatherStationDataSource=Gridded weather data source
+weatherStationDataSources=Weather station data sources
+newWeatherStationDataSource=New weather (station) data source
diff --git a/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_zh_CN.properties b/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_zh_CN.properties
index 62560ca5..47043bc4 100755
--- a/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_zh_CN.properties
+++ b/src/main/resources/no/nibio/vips/logic/i18n/vipslogictexts_zh_CN.properties
@@ -1043,3 +1043,5 @@ thresholdDSVTempMin=Minimum temperature for DSV calculation
 useGridWeatherData=Use grid weather data
 doNotUse=Do not use
 defaultGridWeatherStationDataSource=Gridded weather data source
+weatherStationDataSources=Weather station data sources
+newWeatherStationDataSource=New weather (station) data source
diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml
index 62d85f54..2f2ca6f0 100755
--- a/src/main/webapp/WEB-INF/web.xml
+++ b/src/main/webapp/WEB-INF/web.xml
@@ -79,6 +79,10 @@
         <servlet-name>CropCategoryController</servlet-name>
         <servlet-class>no.nibio.vips.logic.controller.servlet.CropCategoryController</servlet-class>
     </servlet>
+    <servlet>
+        <servlet-name>WeatherStationDataSourceController</servlet-name>
+        <servlet-class>no.nibio.vips.logic.controller.servlet.WeatherStationDataSourceController</servlet-class>
+    </servlet>
     <servlet>
         <servlet-name>JSEnvironment</servlet-name>
         <servlet-class>no.nibio.vips.logic.web.js.JSEnvironment</servlet-class>
@@ -156,6 +160,10 @@
         <servlet-name>CropCategoryController</servlet-name>
         <url-pattern>/organism/cropcategory</url-pattern>
     </servlet-mapping>
+    <servlet-mapping>
+        <servlet-name>WeatherStationDataSourceController</servlet-name>
+        <url-pattern>/weatherstationdatasource</url-pattern>
+    </servlet-mapping>
     <servlet-mapping>
         <servlet-name>JSEnvironment</servlet-name>
         <url-pattern>/js/environment.js</url-pattern>
diff --git a/src/main/webapp/templates/master.ftl b/src/main/webapp/templates/master.ftl
index 0027c08c..712e6aaf 100755
--- a/src/main/webapp/templates/master.ftl
+++ b/src/main/webapp/templates/master.ftl
@@ -59,6 +59,9 @@
 		  <#else>
 		  	<li><a href="/user?action=viewUser&userId=${user.userId}">${i18nBundle.myAccount}</a></li>
 		  </#if>
+		  <#if user.isOrganizationAdmin() || user.isSuperUser()>
+		  	<li><a href="/weatherstationdatasource">${i18nBundle.weatherStationDataSources}</a></li>
+		  </#if>
 		  <li><a href="/poi">${i18nBundle.pois}</a></li>
 		  <#if user.isOrganizationAdmin() || user.isSuperUser() || user.isMessageAuthor()>
 		  	<li><a href="/message">${i18nBundle.messages}</a></li>
diff --git a/src/main/webapp/templates/weatherStationDataSourceList.ftl b/src/main/webapp/templates/weatherStationDataSourceList.ftl
new file mode 100644
index 00000000..3645a22c
--- /dev/null
+++ b/src/main/webapp/templates/weatherStationDataSourceList.ftl
@@ -0,0 +1,39 @@
+<#-- 
+    Copyright (c) 2024 NIBIO <http://www.nibio.no/>. 
+  
+  This file is part of VIPSLogic.
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU Affero General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU Affero General Public License for more details.
+
+  You should have received a copy of the GNU Affero General Public License
+  along with this program.  If not, see <https://www.gnu.org/licenses/>.
+--><#include "master.ftl">
+<#macro page_head>
+        <title>${i18nBundle.weatherStationDataSources}</title>
+</#macro>
+<#macro custom_css>
+</#macro>
+<#macro custom_js>
+</#macro>
+<#macro page_contents>
+<div class="singleBlockContainer">
+<h1>${i18nBundle.weatherStationDataSources}</h1>
+<#if messageKey?has_content>
+        <div class="alert alert-success">${i18nBundle(messageKey)}</div>
+</#if>
+<ul>
+<#list weatherStationDataSources as dSource>
+<li><a href="/weatherstationdatasource?action=editWeatherStationDataSource&weatherStationDataSourceId=${dSource.weatherStationDataSourceId}">${dSource.name}</a></li>
+</#list>
+</ul>
+<p><a href="/weatherstationdatasource??action=newWeatherStationDataSource" class="btn btn-default back" role="button">${i18nBundle.newWeatherStationDataSource}</a></p>
+</div>
+</#macro>
+<@page_html/>
-- 
GitLab