На работе попытался поработать :-)

This commit is contained in:
Igor I
2025-01-06 17:55:54 +05:00
parent 8aaf1c7819
commit 7cf3d879e4
7 changed files with 208 additions and 191 deletions

View File

@ -10,19 +10,21 @@ spring:
cache:
type: none
#custom.config.db_url=jdbc:postgresql://192.168.0.90:5432/weather
#custom.config.db_url=jdbc:postgresql://192.168.0.90:5432/weather?ApplicationName=weather&sslmode=require
custom:
config:
data_dir: O:\\temp\\CCALM\\
#data_dir: /temp/CCALM/
db_all:
#url: jdbc:postgresql://127.0.0.1:5432/weather?ApplicationName=kz_mcp_weather
url: jdbc:postgresql://92.46.48.43:5433/weather?ApplicationName=kz_mcp_weather
url: jdbc:postgresql://192.168.0.90:5432/weather?ApplicationName=kz_mcp_weather&sslmode=require
#url: jdbc:postgresql://127.0.0.1:5432/weather?ApplicationName=kz_mcp_weather&sslmode=require
#url: jdbc:postgresql://92.46.48.43:5433/weather?ApplicationName=kz_mcp_weather&sslmode=require
login: postgres
password: PasSecrKey10
password: PasSecrKey1
db_ru:
#url: jdbc:postgresql://92.46.48.43:5433/weather_ru?ApplicationName=kz_mcp_weather
url: jdbc:postgresql://127.0.0.1:5432/weather_ru?ApplicationName=kz_mcp_weather
url: jdbc:postgresql://192.168.0.90:5433/weather_ru?ApplicationName=kz_mcp_weather&sslmode=require
#url: jdbc:postgresql://92.46.48.43:5433/weather_ru?ApplicationName=kz_mcp_weather&sslmode=require
#url: jdbc:postgresql://127.0.0.1:5432/weather_ru?ApplicationName=kz_mcp_weather&sslmode=require
login: postgres
password: PasSecrKey1

24
pom.xml
View File

@ -12,10 +12,9 @@
<artifactId>weather</artifactId>
<version>0.0.7-SNAPSHOT</version>
<name>weather</name>
<description>Demo project for Spring Boot</description>
<description>Weather APP</description>
<properties>
<java.version>17</java.version>
<start-class>org.ccalm.weather.WeatherApplication</start-class>
<java.version>21</java.version>
</properties>
<repositories>
@ -24,11 +23,6 @@
<name>Unidata UCAR Repository</name>
<url>https://artifacts.unidata.ucar.edu/content/repositories/unidata-releases/</url>
</repository>
<!--repository>
<id>boundless</id>
<name>Boundless Repository</name>
<url>https://repo.boundlessgeo.com/main/</url>
</repository-->
</repositories>
<dependencies>
@ -36,7 +30,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
@ -47,22 +40,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>6.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.gdal/gdal -->
<!--dependency>
<groupId>org.gdal</groupId>
<artifactId>gdal</artifactId>
<version>2.4.0</version>
<scope>system</scope>
<systemPath>O:/projects/_Libs/gdal-2.4.0.jar</systemPath>
</dependency-->
<!-- https://mvnrepository.com/artifact/edu.ucar/netcdfAll -->
<dependency>
<groupId>edu.ucar</groupId>
@ -85,7 +68,7 @@
</goals>
</execution>
</executions>
</plugin-->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@ -100,5 +83,4 @@
</plugins>
</build>
</project>

View File

@ -17,23 +17,20 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.CacheControl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.ServletContextAware;
import org.w3c.dom.Document;
@ -75,58 +72,7 @@ public class AirTemperature implements ServletContextAware {
public void setServletContext(jakarta.servlet.ServletContext servletContext) {
this.context = servletContext;
}
//---------------------------------------------------------------------------
public String getCountryId(Statement st,double lon,double lat) {
String country_id = "";
try {
String sql = "select c.id from main.countries c where del=false and ST_Contains(c.geom,ST_SetSRID(st_makepoint(" + lon + "," + lat + "),4326)) limit 1";
try (ResultSet rs = st.executeQuery(sql)) {
if (rs.next()) {
country_id = rs.getString(1);
}
}
} catch (SQLException ex) {
logger.error("N9: " + ex.getMessage(), ex);
}
return country_id;
}
//---------------------------------------------------------------------------
public String getPointId(Statement st, String country_id, double lon, double lat) {
String point_id = "";
PreparedStatement pstmt = null;
try {
String sql = "SELECT id FROM main.points WHERE lon = ? AND lat = ? LIMIT 1";
pstmt = st.getConnection().prepareStatement(sql);
pstmt.setDouble(1, lon);
pstmt.setDouble(2, lat);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
point_id = rs.getString(1);
}
}
} catch (SQLException ex) {
logger.error("N9: " + ex.getMessage(), ex);
}
if(point_id.isEmpty()){
try {
String sql = "insert into main.points(country_id,geom,lon,lat)values(?,ST_SetSRID(st_makepoint(?,?),4326),?,?) RETURNING id";
pstmt = st.getConnection().prepareStatement(sql);
pstmt.setLong(1, Long.valueOf(country_id));
pstmt.setDouble(2, lon);
pstmt.setDouble(3, lat);
pstmt.setDouble(4, lon);
pstmt.setDouble(5, lat);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
point_id = rs.getString(1); // Получаем ID, возвращаемый SQL-запросом
}
}
} catch (SQLException ex) {
logger.error("N11: " + ex.getMessage(), ex);
}
}
return point_id;
}
//---------------------------------------------------------------------------
public Connection getConn(String url, String login,String password){
Connection conn = null;
@ -363,27 +309,52 @@ public class AirTemperature implements ServletContextAware {
String country_id="";
boolean db_all=false,db_ru=false;
if(st_all!=null && country_id.isEmpty()) {
country_id = getCountryId(st_all, lon, lat);
country_id = DBTools.getCountryId(st_all, lon, lat);
if(!country_id.isEmpty()) db_all=true;
}
if(st_ru!=null && country_id.isEmpty()) {
country_id = getCountryId(st_ru, lon, lat);
country_id = DBTools.getCountryId(st_ru, lon, lat);
if(!country_id.isEmpty()) db_ru=true;
}
if(country_id!=null && !country_id.equals("") && !country_id.equals("null"))
{
try {
String sql= """
insert into main.air_temperature(
val,
country_id,
point_id,
air_temperature_date_id
)values(
?,
?,
?,
main.get_air_temperature_date(cast(to_timestamp(?, 'YYYYMMDD HH24') as timestamp without time zone),?)
);
""";
String point_id="";
if(db_all) {
point_id = getPointId(st_all, country_id, lon, lat);
String sql="insert into main.air_temperature(val,point_id,country_id,air_temperature_date_id)values("+dataArrayTmp.getFloat(pos)+","+point_id+","+country_id+",main.get_air_temperature_date(cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone),"+forecast+"));";
st_all.executeUpdate(sql);
point_id = DBTools.getPointId(st_all, country_id, lon, lat);
try (PreparedStatement pstmt = conn_all.prepareStatement(sql)) {
pstmt.setFloat(1, dataArrayTmp.getFloat(pos));
pstmt.setLong(2, Long.valueOf(country_id));
pstmt.setLong(3, Long.valueOf(point_id));
pstmt.setString(4, date+" "+time);
pstmt.setInt(5, Integer.valueOf(forecast));
pstmt.executeUpdate();
}
}
if(db_ru) {
point_id = getPointId(st_ru, country_id, lon, lat);
String sql="insert into main.air_temperature(val,point_id,country_id,air_temperature_date_id)values("+dataArrayTmp.getFloat(pos)+","+point_id+","+country_id+",main.get_air_temperature_date(cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone),"+forecast+"));";
st_ru.executeUpdate(sql);
point_id = DBTools.getPointId(st_ru, country_id, lon, lat);
try (PreparedStatement pstmt = conn_ru.prepareStatement(sql)) {
pstmt.setFloat(1, dataArrayTmp.getFloat(pos));
pstmt.setLong(2, Long.valueOf(country_id));
pstmt.setLong(3, Long.valueOf(point_id));
pstmt.setString(4, date+" "+time);
pstmt.setInt(5, Integer.valueOf(forecast));
pstmt.executeUpdate();
}
}
} catch (SQLException ex) {
logger.error("N10:"+ex.getMessage(),ex);
@ -398,14 +369,13 @@ public class AirTemperature implements ServletContextAware {
//Cut data piece from big country of Russia
try {
String sql="update main.points w set country_id=null where country_id=7 and not ST_Contains(ST_SetSRID(ST_GeomFromText('POLYGON((10.00 66.00,10.00 40.00,179.00 40.00,179.00 66.00,10.00 66.00))'),4326),w.geom);";
String sql="delete from main.points where country_id=7 and not ST_Contains(ST_SetSRID(ST_GeomFromText('POLYGON((10.00 66.00,10.00 40.00,179.00 40.00,179.00 66.00,10.00 66.00))'),4326),w.geom);";
if(st_all!=null) st_all.executeUpdate(sql);
if(st_ru!=null) st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N11:"+ex.getMessage(),ex);
}
//Delete values where country_id is null
try {
String sql="delete from main.points where country_id is null;";
if(st_all!=null) st_all.executeUpdate(sql);
@ -451,21 +421,34 @@ public class AirTemperature implements ServletContextAware {
return result;
}
//---------------------------------------------------------------------------
//@Autowired
@GetMapping("/geodatalist/AirTemperatureDates0")
@ResponseBody
public String getGeoDataList0(HttpServletResponse response) {
String headerValue = CacheControl.maxAge(60, TimeUnit.SECONDS).getHeaderValue();
response.addHeader("Cache-Control", headerValue);
String html="";
html+="<a href=\"./geodatalist/AirTemperatureDates\">AirTemperatureDates</a><br><form action=\"./AirTemperature\" method=\"get\"><label for=\"fname\">Date:</label><input type=\"text\" name=\"date\" value=\"20240420\"><input type=\"submit\" value=\"Submit\"></form><br>";
html+="<a href=\"./geodatalist/PrecipitationDates\">PrecipitationDates</a><br><form action=\"./Precipitation\" method=\"get\"><label for=\"fname\">Date:</label><input type=\"text\" name=\"date\" value=\"\"20240420\"><input type=\"submit\" value=\"Submit\"></form><br>";
html+="<a href=\"./geodatalist/SoilDates\">SoilDates</a><br><form action=\"./DownloadSoil\" method=\"get\"><label for=\"fname\">Date:</label><input type=\"text\" name=\"date\" value=\"\"20240420\"><label for=\"forecast\">Forecast:</label><input type=\"text\" name=\"forecast\" value=\"000\"><input type=\"submit\" value=\"Submit\"></form><br>";
return html;
}
//---------------------------------------------------------------------------
//List of "Air temperature" dates from database in JSON
//@CacheControl(maxAge = 300)
@CrossOrigin
@RequestMapping(value = "/geodatalist/AirTemperatureDates",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
@RequestMapping(value = "/geodatalist/AirTemperatureDates",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
@ResponseBody
public Object ajaxAirDates(HttpServletResponse response) {
String headerValue = CacheControl.maxAge(60, TimeUnit.SECONDS).getHeaderValue();
response.addHeader("Cache-Control", headerValue);
boolean error=false;
String result="";
Connection conn_all = getConn(db_url_all,db_login_all,db_password_all);
if(!error)
if(conn_all!=null)
{
Statement st_all;
try {

View File

@ -0,0 +1,64 @@
package org.ccalm.weather;
import org.slf4j.LoggerFactory;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBTools {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(DBTools.class);
//---------------------------------------------------------------------------
public static String getCountryId(Statement st,double lon,double lat) {
String country_id = "";
try {
String sql = "select c.id from main.countries c where del=false and ST_Contains(c.geom,ST_SetSRID(st_makepoint(" + lon + "," + lat + "),4326)) limit 1";
try (ResultSet rs = st.executeQuery(sql)) {
if (rs.next()) {
country_id = rs.getString(1);
}
}
} catch (SQLException ex) {
logger.error("N9: " + ex.getMessage(), ex);
}
return country_id;
}
//---------------------------------------------------------------------------
public static String getPointId(Statement st, String country_id, double lon, double lat) {
String point_id = "";
PreparedStatement pstmt = null;
try {
String sql = "SELECT id FROM main.points WHERE lon = ? AND lat = ? LIMIT 1";
pstmt = st.getConnection().prepareStatement(sql);
pstmt.setDouble(1, lon);
pstmt.setDouble(2, lat);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
point_id = rs.getString(1);
}
}
} catch (SQLException ex) {
logger.error("N9: " + ex.getMessage(), ex);
}
if(point_id.isEmpty()){
try {
String sql = "insert into main.points(country_id,geom,lon,lat)values(?,ST_SetSRID(st_makepoint(?,?),4326),?,?) RETURNING id";
pstmt = st.getConnection().prepareStatement(sql);
pstmt.setLong(1, Long.valueOf(country_id));
pstmt.setDouble(2, lon);
pstmt.setDouble(3, lat);
pstmt.setDouble(4, lon);
pstmt.setDouble(5, lat);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
point_id = rs.getString(1); // Получаем ID, возвращаемый SQL-запросом
}
}
} catch (SQLException ex) {
logger.error("N11: " + ex.getMessage(), ex);
}
}
return point_id;
}
}

View File

@ -16,7 +16,7 @@ public class MainController {
@GetMapping("/")
@ResponseBody
public String getIndex(Model model) {
return "The weather list is working! <br><a href=\"./geodatalist/\">/geodatalist/</a>";
return "The weather list is working! <br><a href=\"./geodatalist\">/geodatalist</a>";
}
@CrossOrigin
@ -24,9 +24,9 @@ public class MainController {
@ResponseBody
public String getGeoDataList(Model model) {
String html="";
html+="<a href=\"./AirTemperatureDates\">AirTemperatureDates</a><br><form action=\"./AirTemperature\" method=\"get\"><label for=\"fname\">Date:</label><input type=\"text\" name=\"date\" value=\"20240420\"><input type=\"submit\" value=\"Submit\"></form><br>";
html+="<a href=\"./PrecipitationDates\">PrecipitationDates</a><br><form action=\"./Precipitation\" method=\"get\"><label for=\"fname\">Date:</label><input type=\"text\" name=\"date\" value=\"\"20240420\"><input type=\"submit\" value=\"Submit\"></form><br>";
html+="<a href=\"./SoilDates\">SoilDates</a><br><form action=\"./DownloadSoil\" method=\"get\"><label for=\"fname\">Date:</label><input type=\"text\" name=\"date\" value=\"\"20240420\"><label for=\"forecast\">Forecast:</label><input type=\"text\" name=\"forecast\" value=\"000\"><input type=\"submit\" value=\"Submit\"></form><br>";
html+="<a href=\"/geodatalist/AirTemperatureDates\">AirTemperatureDates</a><br><form action=\"./AirTemperature\" method=\"get\"><label for=\"fname\">Date:</label><input type=\"text\" name=\"date\" value=\"20240420\"><input type=\"submit\" value=\"Submit\"></form><br>";
html+="<a href=\"/geodatalist/PrecipitationDates\">PrecipitationDates</a><br><form action=\"./Precipitation\" method=\"get\"><label for=\"fname\">Date:</label><input type=\"text\" name=\"date\" value=\"\"20240420\"><input type=\"submit\" value=\"Submit\"></form><br>";
html+="<a href=\"/geodatalist/SoilDates\">SoilDates</a><br><form action=\"./DownloadSoil\" method=\"get\"><label for=\"fname\">Date:</label><input type=\"text\" name=\"date\" value=\"\"20240420\"><label for=\"forecast\">Forecast:</label><input type=\"text\" name=\"forecast\" value=\"000\"><input type=\"submit\" value=\"Submit\"></form><br>";
return html;
}

View File

@ -5,11 +5,7 @@ import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
@ -20,7 +16,8 @@ import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@ -98,26 +95,6 @@ public class Precipitation implements ServletContextAware {
return result;
}
//---------------------------------------------------------------------------
public String getCountryId(Statement st,double lon,double lat) {
String country_id = "";
ResultSet rs = null;
try {
rs = st.executeQuery("select c.id from main.countries c where del=false and ST_Contains(c.geom,ST_SetSRID(st_makepoint(" + lon + "," + lat + "),4326)) limit 1");
} catch (SQLException ex) {
logger.error("N9: " + ex.getMessage(), ex);
}
if (rs != null) {
try {
if (rs.next())
country_id = rs.getString(1);
rs.close();
} catch (SQLException ex) {
logger.error("N10: " + ex.getMessage(), ex);
}
}
return country_id;
}
//---------------------------------------------------------------------------
public Connection getConn(String url, String login,String password){
Connection conn = null;
try{
@ -352,23 +329,53 @@ public class Precipitation implements ServletContextAware {
String country_id="";
boolean db_all=false,db_ru=false;
if(country_id.isEmpty()) {
country_id = getCountryId(st_all, lon, lat);
country_id = DBTools.getCountryId(st_all, lon, lat);
if(!country_id.isEmpty()) db_all=true;
}
if(country_id.isEmpty()) {
country_id = getCountryId(st_ru, lon, lat);
country_id = DBTools.getCountryId(st_ru, lon, lat);
if(!country_id.isEmpty()) db_ru=true;
}
if(country_id!=null && !country_id.equals("") && !country_id.equals("null"))
{
//logger.info(lon + "," + lat +","+dataArrayTmp.getFloat(pos));
try {
String sql="insert into main.precipitation(date,hours,val,geom,country_id,precipitation_date_id)values(cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone),"+forecast+","+dataArrayTmp.getFloat(pos)+",ST_SetSRID(st_makepoint("+lon+","+lat+"),4326),"+country_id+",main.get_precipitation_date(cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone),"+forecast+"));";
if(db_all)
st_all.executeUpdate(sql);
if(db_ru)
st_ru.executeUpdate(sql);
String sql= """
insert into main.precipitation(
val,
country_id,
point_id,
air_temperature_date_id
)values(
?,
?,
?,
main.get_precipitation_date(cast(to_timestamp(?, 'YYYYMMDD HH24') as timestamp without time zone),?)
);
""";
String point_id="";
if(db_all) {
point_id = DBTools.getPointId(st_all, country_id, lon, lat);
try (PreparedStatement pstmt = conn_all.prepareStatement(sql)) {
pstmt.setFloat(1, dataArrayTmp.getFloat(pos));
pstmt.setLong(2, Long.valueOf(country_id));
pstmt.setLong(3, Long.valueOf(point_id));
pstmt.setString(4, date+" "+time);
pstmt.setInt(5, Integer.valueOf(forecast));
pstmt.executeUpdate();
}
}
if(db_ru) {
point_id = DBTools.getPointId(st_ru, country_id, lon, lat);
try (PreparedStatement pstmt = conn_ru.prepareStatement(sql)) {
pstmt.setFloat(1, dataArrayTmp.getFloat(pos));
pstmt.setLong(2, Long.valueOf(country_id));
pstmt.setLong(3, Long.valueOf(point_id));
pstmt.setString(4, date+" "+time);
pstmt.setInt(5, Integer.valueOf(forecast));
pstmt.executeUpdate();
}
}
} catch (SQLException ex) {
logger.error("N10:"+ex.getMessage(),ex);
throw new Exception("Failed insert precipitation...");
@ -383,16 +390,15 @@ public class Precipitation implements ServletContextAware {
//Cut data piece from big country of Russia
try {
String sql="update main.precipitation w set country_id=null where country_id=7 and not ST_Contains(ST_SetSRID(ST_GeomFromText('POLYGON((10.00 66.00,10.00 40.00,179.00 40.00,179.00 66.00,10.00 66.00))'),4326),w.geom);";
String sql="delete from main.points where country_id=7 and not ST_Contains(ST_SetSRID(ST_GeomFromText('POLYGON((10.00 66.00,10.00 40.00,179.00 40.00,179.00 66.00,10.00 66.00))'),4326),w.geom);";
st_all.executeUpdate(sql);
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N11:"+ex.getMessage(),ex);
}
//Delete values where country_id is null
try {
String sql="delete from main.precipitation where country_id is null;";
String sql="delete from main.points where country_id is null;";
st_all.executeUpdate(sql);
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
@ -423,19 +429,18 @@ public class Precipitation implements ServletContextAware {
//---------------------------------------------------------------------------
//List of "Air temperature" dates from database in JSON
@CrossOrigin
@RequestMapping(value = "/geodatalist/PrecipitationDates",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
@RequestMapping(value = "/geodatalist/PrecipitationDates",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
@ResponseBody
public Object ajaxAirDates(HttpServletResponse response) {
String headerValue = CacheControl.maxAge(60, TimeUnit.SECONDS).getHeaderValue();
response.addHeader("Cache-Control", headerValue);
boolean error=false;
String result="";
//Load DB configuration from "config.xml"
Connection conn_all = getConn(db_url_all,db_login_all,db_password_all);
if(!error)
if(conn_all!=null)
{
Statement st_all=null;
try {

View File

@ -5,6 +5,7 @@ import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
@ -17,7 +18,8 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@ -89,26 +91,6 @@ public class SoilTmperature implements ServletContextAware {
return conn;
}
//---------------------------------------------------------------------------
public String getCountryId(Statement st,double lon,double lat) {
String country_id = "";
ResultSet rs = null;
try {
rs = st.executeQuery("select c.id from main.countries c where del=false and ST_Contains(c.geom,ST_SetSRID(st_makepoint(" + lon + "," + lat + "),4326)) limit 1");
} catch (SQLException ex) {
logger.error("N9: " + ex.getMessage(), ex);
}
if (rs != null) {
try {
if (rs.next())
country_id = rs.getString(1);
rs.close();
} catch (SQLException ex) {
logger.error("N10: " + ex.getMessage(), ex);
}
}
return country_id;
}
//---------------------------------------------------------------------------
public static String CutBeforeFirst(StringBuffer str,String ch)
{
int pos=str.indexOf(ch);
@ -366,18 +348,17 @@ public class SoilTmperature implements ServletContextAware {
String country_id="";
boolean db_all=false,db_ru=false;
if(country_id.isEmpty()) {
country_id = getCountryId(st_all, lon, lat);
country_id = DBTools.getCountryId(st_all, lon, lat);
if(!country_id.isEmpty()) db_all=true;
}
if(country_id.isEmpty()) {
country_id = getCountryId(st_ru, lon, lat);
country_id = DBTools.getCountryId(st_ru, lon, lat);
if(!country_id.isEmpty()) db_ru=true;
}
if(!country_id.isEmpty() && !country_id.equals("null"))
{
try {
+++++++
String sql="""
insert into main.soil_temperature(
val,
@ -387,29 +368,32 @@ public class SoilTmperature implements ServletContextAware {
)values(
?,
?,
main.get_point_id(?,?,?),
"+country_id+",
main.get_soil_temperature_date(cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone),"+forecast+")
?,
main.get_soil_temperature_date(cast(to_timestamp(?, 'YYYYMMDD HH24') as timestamp without time zone),?)
);
""";
String point_id="";
if(db_all) {
point_id = DBTools.getPointId(st_all, country_id, lon, lat);
try (PreparedStatement pstmt = conn_all.prepareStatement(sql)) {
// Устанавливаем параметры в PreparedStatement
pstmt.setFloat(1, dataArrayTmp.getFloat(pos)); // val
pstmt.setLong(2, country_id); // country_id
pstmt.setString(2, lon); // lon
pstmt.setString(3, lat); // lat
pstmt.setString(5, date); // date
pstmt.setString(6, time); // time
pstmt.setInt(7, forecast); // forecast
pstmt.setFloat(1, dataArrayTmp.getFloat(pos));
pstmt.setLong(2, Long.valueOf(country_id));
pstmt.setLong(3, Long.valueOf(point_id));
pstmt.setString(4, date+" "+time);
pstmt.setInt(5, Integer.valueOf(forecast));
pstmt.executeUpdate();
}
}
if(db_ru) {
st_ru.executeUpdate(sql);
point_id = DBTools.getPointId(st_ru, country_id, lon, lat);
try (PreparedStatement pstmt = conn_ru.prepareStatement(sql)) {
pstmt.setFloat(1, dataArrayTmp.getFloat(pos));
pstmt.setLong(2, Long.valueOf(country_id));
pstmt.setLong(3, Long.valueOf(point_id));
pstmt.setString(4, date+" "+time);
pstmt.setInt(5, Integer.valueOf(forecast));
pstmt.executeUpdate();
}
}
} catch (SQLException ex) {
logger.error("N11: "+ex.getMessage(),ex);
@ -425,16 +409,15 @@ public class SoilTmperature implements ServletContextAware {
//Cut data piece from big country of Russia
try {
String sql="update main.soil_temperature w set country_id=null where country_id=7 and not ST_Contains(ST_SetSRID(ST_GeomFromText('POLYGON((10.00 66.00,10.00 40.00,179.00 40.00,179.00 66.00,10.00 66.00))'),4326),w.geom);";
String sql="delete from main.points where country_id=7 and not ST_Contains(ST_SetSRID(ST_GeomFromText('POLYGON((10.00 66.00,10.00 40.00,179.00 40.00,179.00 66.00,10.00 66.00))'),4326),w.geom);";
st_all.executeUpdate(sql);
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N12: "+ex.getMessage(),ex);
}
//Delete values where country_id is null
try {
String sql="delete from main.soil_temperature where country_id is null;";
String sql="delete from main.points where country_id is null;";
st_all.executeUpdate(sql);
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
@ -465,19 +448,17 @@ public class SoilTmperature implements ServletContextAware {
//---------------------------------------------------------------------------
//List of "Soil temperature" dates from database in JSON
@CrossOrigin
@RequestMapping(value = "/geodatalist/SoilDates",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
@RequestMapping(value = "/geodatalist/SoilDates",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
@ResponseBody
public Object ajaxSoilDates(HttpServletResponse response) {
String headerValue = CacheControl.maxAge(60, TimeUnit.SECONDS).getHeaderValue();
response.addHeader("Cache-Control", headerValue);
boolean error=false;
String result="";
Connection conn_all = getConn(db_url_all,db_login_all,db_password_all);
//Connection conn_ru = getConn(db_url_ru,db_login_ru,db_password_ru);
if(!error)
if(conn_all!=null)
{
Statement st;
try {