под новую базу
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -24,6 +24,7 @@ target/
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/logs/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
|
||||
28
org_ccalm_weather.yml
Normal file
28
org_ccalm_weather.yml
Normal file
@ -0,0 +1,28 @@
|
||||
server:
|
||||
port: 8081
|
||||
servlet:
|
||||
session:
|
||||
timeout: 300s
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: kz_mcp_weather
|
||||
cache:
|
||||
type: none
|
||||
|
||||
#custom.config.db_url=jdbc:postgresql://192.168.0.90:5432/weather
|
||||
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
|
||||
login: postgres
|
||||
password: PasSecrKey10
|
||||
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
|
||||
login: postgres
|
||||
password: PasSecrKey1
|
||||
|
||||
57
pom.xml
57
pom.xml
@ -5,12 +5,12 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.5.4</version>
|
||||
<version>3.3.3</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>org.ccalm</groupId>
|
||||
<artifactId>weather</artifactId>
|
||||
<version>0.0.6-SNAPSHOT</version>
|
||||
<version>0.0.7-SNAPSHOT</version>
|
||||
<name>weather</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
<properties>
|
||||
@ -78,65 +78,26 @@
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
|
||||
<!--plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<version>3.2.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
<goal>build-info</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/lib</outputDirectory>
|
||||
<includeScope>runtime</includeScope>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.2.0</version>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<classpathPrefix>lib/</classpathPrefix>
|
||||
<mainClass>org.ccalm.weather.WeatherApplication</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<systemPropertyVariables>
|
||||
<spring.config.location>file:org_ccalm_weather.yml</spring.config.location>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<!--plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-install-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>install-external</id>
|
||||
<phase>clean</phase>
|
||||
<configuration>
|
||||
<file>O:/projects/Workspace_Java/CCALM_weather/src/main/lib/netcdfAll-5.3.1.jar</file>
|
||||
<repositoryLayout>default</repositoryLayout>
|
||||
<groupId>edu.ucar</groupId>
|
||||
<artifactId>netcdfAll</artifactId>
|
||||
<version>5.3.1</version>
|
||||
<packaging>jar</packaging>
|
||||
<generatePom>true</generatePom>
|
||||
</configuration>
|
||||
<goals>
|
||||
<goal>install-file</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin-->
|
||||
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
@ -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;
|
||||
@ -54,19 +50,96 @@ import ucar.nc2.dataset.NetcdfDataset;
|
||||
@Controller
|
||||
public class AirTemperature implements ServletContextAware {
|
||||
|
||||
@Value("${custom.config.db_url}")
|
||||
private String db_url;
|
||||
@Value("${custom.config.db_login}")
|
||||
private String db_login;
|
||||
@Value("${custom.config.db_password}")
|
||||
private String db_password;
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(AirTemperature.class);
|
||||
|
||||
@Value("${custom.config.db_all.url}")
|
||||
private String db_url_all;
|
||||
@Value("${custom.config.db_all.login}")
|
||||
private String db_login_all;
|
||||
@Value("${custom.config.db_all.password}")
|
||||
private String db_password_all;
|
||||
|
||||
@Value("${custom.config.db_ru.url}")
|
||||
private String db_url_ru;
|
||||
@Value("${custom.config.db_ru.login}")
|
||||
private String db_login_ru;
|
||||
@Value("${custom.config.db_ru.password}")
|
||||
private String db_password_ru;
|
||||
|
||||
@Value("${custom.config.data_dir}")
|
||||
private String data_dir;
|
||||
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SoilTmperature.class);
|
||||
|
||||
private ServletContext context;
|
||||
private jakarta.servlet.ServletContext context;
|
||||
|
||||
@Override
|
||||
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;
|
||||
try{
|
||||
Class.forName("org.postgresql.Driver");
|
||||
conn = DriverManager.getConnection(url,login,password);
|
||||
}catch(Exception ex)
|
||||
{
|
||||
logger.error("N1: "+ex.getMessage()+"<br>",ex);
|
||||
}
|
||||
return conn;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
/**
|
||||
* This function is run every day from CRON, to see the settings call the function: "sudo crontab -e -u tomcat" on PC 127.0.0.1
|
||||
* @param date - If the field is empty, it is filled in automatically with the current day (When running from CRON, this field is empty)
|
||||
@ -86,24 +159,8 @@ public class AirTemperature implements ServletContextAware {
|
||||
if (!dir.exists()) dir.mkdirs();
|
||||
|
||||
//response.getWriter().append("Served at: ").append(request.getContextPath());
|
||||
Connection conn = null;
|
||||
try{
|
||||
Class.forName("org.postgresql.Driver");
|
||||
conn = DriverManager.getConnection(db_url,db_login,db_password);
|
||||
if(conn!=null)
|
||||
{
|
||||
logger.info("Connect is OK!");
|
||||
result+="Connect is OK!<br>";
|
||||
}else
|
||||
{
|
||||
logger.info("<br>Connect is ERROR<br>");
|
||||
result+="Connect is ERROR!<br>";
|
||||
}
|
||||
}catch(Exception ex)
|
||||
{
|
||||
logger.error("N1:"+ex.getMessage()+"<br>",ex);
|
||||
result+="Connect Exception:"+ex.getMessage()+"<br>";
|
||||
}
|
||||
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);
|
||||
|
||||
//Example request: http://ccalm.org/AirTemperature?date=20210531
|
||||
//Example request: http://localhost:8080/AirTemperature?date=20210531
|
||||
@ -229,15 +286,19 @@ public class AirTemperature implements ServletContextAware {
|
||||
}
|
||||
dimIt = null;
|
||||
|
||||
Statement st=null;
|
||||
Statement st_all=null;
|
||||
Statement st_ru=null;
|
||||
try {
|
||||
st = conn.createStatement();
|
||||
if(conn_all!=null) st_all = conn_all.createStatement();
|
||||
if(conn_ru!=null) st_ru = conn_ru.createStatement();
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N3:"+ex.getMessage(),ex);
|
||||
}
|
||||
|
||||
try {
|
||||
st.executeUpdate("BEGIN TRANSACTION;");
|
||||
String sql = "BEGIN TRANSACTION;";
|
||||
if(st_all!=null) st_all.executeUpdate(sql);
|
||||
if(st_ru!=null) st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N4:"+ex.getMessage(),ex);
|
||||
}
|
||||
@ -245,35 +306,41 @@ public class AirTemperature implements ServletContextAware {
|
||||
result+="Size="+dataArrayLat.getSize()+"<br>";
|
||||
|
||||
//Delete old data
|
||||
logger.info("Delete old data 1");
|
||||
/*logger.info("Delete old data 1");
|
||||
try {
|
||||
String sql="delete from main.air_temperature where date=cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone) and hours="+forecast;
|
||||
st.executeUpdate(sql);
|
||||
if(st_all!=null) st_all.executeUpdate(sql);
|
||||
if(st_ru!=null) st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N5:"+ex.getMessage(),ex);
|
||||
}
|
||||
}*/
|
||||
logger.info("Delete old data 2");
|
||||
try {
|
||||
String sql="delete from main.air_temperature_dates where date=cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone) and hours="+forecast;
|
||||
st.executeUpdate(sql);
|
||||
if(st_all!=null) st_all.executeUpdate(sql);
|
||||
if(st_ru!=null) st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N6:"+ex.getMessage(),ex);
|
||||
}
|
||||
logger.info("Delete old data 3");
|
||||
try {
|
||||
String sql="delete from main.air_temperature where date<=CURRENT_DATE-'730 days'::INTERVAL";
|
||||
st.executeUpdate(sql);
|
||||
//String sql="delete from main.air_temperature where date<=CURRENT_DATE-'730 days'::INTERVAL";
|
||||
String sql="delete from main.air_temperature_dates where date<=CURRENT_DATE-'730 days'::INTERVAL";
|
||||
if(st_all!=null) st_all.executeUpdate(sql);
|
||||
if(st_ru!=null) st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N7:"+ex.getMessage(),ex);
|
||||
}
|
||||
|
||||
try {
|
||||
st.executeUpdate("END TRANSACTION;");
|
||||
if(st_all!=null) st_all.executeUpdate("END TRANSACTION;");
|
||||
if(st_ru!=null) st_ru.executeUpdate("END TRANSACTION;");
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N7.1:"+ex.getMessage(),ex);
|
||||
}
|
||||
try {
|
||||
st.executeUpdate("BEGIN TRANSACTION;");
|
||||
if(st_all!=null) st_all.executeUpdate("BEGIN TRANSACTION;");
|
||||
if(st_ru!=null) st_ru.executeUpdate("BEGIN TRANSACTION;");
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N7.2:"+ex.getMessage(),ex);
|
||||
}
|
||||
@ -294,29 +361,30 @@ public class AirTemperature implements ServletContextAware {
|
||||
if(!Float.isNaN(dataArrayTmp.getFloat(pos))) //On the water none temperatyre.
|
||||
{
|
||||
String country_id="";
|
||||
ResultSet rs=null;
|
||||
try {
|
||||
String sql="select c.id from main.countries c where ST_Contains(c.geom,ST_SetSRID(st_makepoint("+lon+","+lat+"),4326)) limit 1";
|
||||
rs = st.executeQuery(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N8:"+ex.getMessage(),ex);
|
||||
boolean db_all=false,db_ru=false;
|
||||
if(st_all!=null && country_id.isEmpty()) {
|
||||
country_id = getCountryId(st_all, lon, lat);
|
||||
if(!country_id.isEmpty()) db_all=true;
|
||||
}
|
||||
if (rs != null) {
|
||||
try {
|
||||
if (rs.next())
|
||||
country_id=rs.getString(1);
|
||||
rs.close();
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N9:"+ex.getMessage(),ex);
|
||||
}
|
||||
if(st_ru!=null && country_id.isEmpty()) {
|
||||
country_id = 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.air_temperature(date,hours,val,geom,country_id,air_temperature_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_air_temperature_date(cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone),"+forecast+"));";
|
||||
st.executeUpdate(sql);
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N10:"+ex.getMessage(),ex);
|
||||
}
|
||||
@ -330,22 +398,26 @@ public class AirTemperature implements ServletContextAware {
|
||||
|
||||
//Cut data piece from big country of Russia
|
||||
try {
|
||||
String sql="update main.air_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);";
|
||||
st.executeUpdate(sql);
|
||||
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);";
|
||||
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.air_temperature where country_id is null;";
|
||||
st.executeUpdate(sql);
|
||||
String sql="delete from main.points where country_id is null;";
|
||||
if(st_all!=null) st_all.executeUpdate(sql);
|
||||
if(st_ru!=null) st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N12:"+ex.getMessage(),ex);
|
||||
}
|
||||
|
||||
try {
|
||||
st.executeUpdate("END TRANSACTION;");
|
||||
String sql = "END TRANSACTION;";
|
||||
if(st_all!=null) st_all.executeUpdate(sql);
|
||||
if(st_ru!=null) st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N13:"+ex.getMessage(),ex);
|
||||
}
|
||||
@ -355,16 +427,13 @@ public class AirTemperature implements ServletContextAware {
|
||||
logger.error("N14:"+ex.getMessage(),ex);
|
||||
}
|
||||
|
||||
try {conn.close();} catch (SQLException ex) {logger.error("N15:"+ex.getMessage(),ex);}
|
||||
try { if(conn_all!=null) conn_all.close(); } catch (SQLException ex) {logger.error("N15:"+ex.getMessage(),ex);}
|
||||
try { if(conn_ru!=null) conn_ru.close();} catch (SQLException ex) {logger.error("N15:"+ex.getMessage(),ex);}
|
||||
|
||||
result+="End!<br>";
|
||||
return result;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
@Override
|
||||
public void setServletContext(ServletContext context) {
|
||||
this.context=context;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
public static String CutBeforeFirst(StringBuffer str,String ch)
|
||||
{
|
||||
@ -394,28 +463,13 @@ public class AirTemperature implements ServletContextAware {
|
||||
boolean error=false;
|
||||
String result="";
|
||||
|
||||
Connection conn = null;
|
||||
try {
|
||||
Class.forName("org.postgresql.Driver");
|
||||
conn = DriverManager.getConnection(db_url, db_login, db_password);
|
||||
if (conn != null) {
|
||||
logger.info("Connect is OK!");
|
||||
} else {
|
||||
error=true;
|
||||
result="An error occurred while connecting to the database!";
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
logger.error("N16:"+ex.getMessage(),ex);
|
||||
error=true;
|
||||
result="<br>SQLException: "+ex.getMessage()+"<br>";
|
||||
}
|
||||
Connection conn_all = getConn(db_url_all,db_login_all,db_password_all);
|
||||
|
||||
if(!error)
|
||||
{
|
||||
Statement st;
|
||||
Statement st_all;
|
||||
try {
|
||||
st = conn.createStatement();
|
||||
//String sql = "SELECT to_char(date, 'YYYY-MM-DD') as date,hours as hour,EXTRACT(DAY FROM CURRENT_DATE-date) as day FROM main.air_temperature group by date,hours order by date,hours;";
|
||||
st_all = conn_all.createStatement();
|
||||
String sql = """
|
||||
SELECT
|
||||
to_char(date, 'YYYY-MM-DD') as date,
|
||||
@ -426,7 +480,7 @@ public class AirTemperature implements ServletContextAware {
|
||||
group by date,hours
|
||||
order by date,hours
|
||||
""";
|
||||
ResultSet rs = st.executeQuery(sql);
|
||||
ResultSet rs = st_all.executeQuery(sql);
|
||||
if(rs!=null)
|
||||
{
|
||||
boolean exists=false;
|
||||
@ -448,15 +502,17 @@ public class AirTemperature implements ServletContextAware {
|
||||
result="[]";
|
||||
}
|
||||
}
|
||||
st.close();
|
||||
conn.close();
|
||||
st_all.close();
|
||||
conn_all.close();
|
||||
} catch (SQLException ex) {
|
||||
result="<br>SQLException:"+ex.getMessage()+"<br>";
|
||||
logger.error("N18:"+ex.getMessage(),ex);
|
||||
}finally{
|
||||
try { conn_all.close(); } catch (SQLException ex) { logger.error("N19: "+ex.getMessage(),ex); }
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
}
|
||||
|
||||
@ -18,13 +18,14 @@ import java.util.List;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
@ -54,21 +55,87 @@ import ucar.nc2.dataset.NetcdfDataset;
|
||||
@Controller
|
||||
public class Precipitation implements ServletContextAware {
|
||||
|
||||
@Value("${custom.config.db_url}")
|
||||
private String db_url;
|
||||
@Value("${custom.config.db_login}")
|
||||
private String db_login;
|
||||
@Value("${custom.config.db_password}")
|
||||
private String db_password;
|
||||
private static final Logger logger = LogManager.getLogger(Precipitation.class);
|
||||
|
||||
@Value("${custom.config.db_all.url}")
|
||||
private String db_url_all;
|
||||
@Value("${custom.config.db_all.login}")
|
||||
private String db_login_all;
|
||||
@Value("${custom.config.db_all.password}")
|
||||
private String db_password_all;
|
||||
|
||||
@Value("${custom.config.db_ru.url}")
|
||||
private String db_url_ru;
|
||||
@Value("${custom.config.db_ru.login}")
|
||||
private String db_login_ru;
|
||||
@Value("${custom.config.db_ru.password}")
|
||||
private String db_password_ru;
|
||||
|
||||
@Value("${custom.config.data_dir}")
|
||||
private String data_dir;
|
||||
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Precipitation.class);
|
||||
|
||||
private ServletContext context;
|
||||
|
||||
//http://127.0.0.1:8080/AirTemperature
|
||||
private jakarta.servlet.ServletContext context;
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
@Override
|
||||
public void setServletContext(jakarta.servlet.ServletContext servletContext) {
|
||||
this.context=servletContext;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
public static String CutBeforeFirst(StringBuffer str,String ch)
|
||||
{
|
||||
int pos=str.indexOf(ch);
|
||||
String result="";
|
||||
if(pos==-1)
|
||||
{
|
||||
result.concat(str.toString());
|
||||
str.delete(0,str.length());
|
||||
}else
|
||||
{
|
||||
result=str.substring(0,pos);
|
||||
str.delete(0,pos+1);
|
||||
}
|
||||
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{
|
||||
Class.forName("org.postgresql.Driver");
|
||||
conn = DriverManager.getConnection(url,login,password);
|
||||
}catch(Exception ex)
|
||||
{
|
||||
logger.error("N1: "+ex.getMessage()+"<br>",ex);
|
||||
}
|
||||
return conn;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
/**
|
||||
* Example http://127.0.0.1:8080/AirTemperature
|
||||
* @param response
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping(value = "/geodatalist/Precipitation",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
|
||||
@ResponseBody
|
||||
public Object ajaxTamer(HttpServletResponse response,@RequestParam(required=false,name="date") String date) {
|
||||
@ -85,24 +152,8 @@ public class Precipitation implements ServletContextAware {
|
||||
if (!dir.exists()) dir.mkdirs();
|
||||
|
||||
//response.getWriter().append("Served at: ").append(request.getContextPath());
|
||||
Connection conn = null;
|
||||
try{
|
||||
Class.forName("org.postgresql.Driver");
|
||||
conn = DriverManager.getConnection(db_url,db_login,db_password);
|
||||
if(conn!=null)
|
||||
{
|
||||
logger.info("Connect is OK!");
|
||||
result+="Connect is OK!<br>";
|
||||
}else
|
||||
{
|
||||
logger.info("<br>Connect is ERROR<br>");
|
||||
result+="Connect is ERROR!<br>";
|
||||
}
|
||||
}catch(Exception e)
|
||||
{
|
||||
logger.error("N1:"+e.getMessage()+"<br>",e);
|
||||
result+="Connect Exception:"+e.getMessage()+"<br>";
|
||||
}
|
||||
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);
|
||||
|
||||
//Example request: http://localhost:8080/Precipitation?date=20210531
|
||||
if(date==null || date.equals(""))
|
||||
@ -225,15 +276,18 @@ public class Precipitation implements ServletContextAware {
|
||||
}
|
||||
dimIt = null;
|
||||
|
||||
Statement st=null;
|
||||
Statement st_all=null;
|
||||
Statement st_ru=null;
|
||||
try {
|
||||
st = conn.createStatement();
|
||||
st_all = conn_all.createStatement();
|
||||
st_ru = conn_ru.createStatement();
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N3:"+ex.getMessage(),ex);
|
||||
}
|
||||
|
||||
try {
|
||||
st.executeUpdate("BEGIN TRANSACTION;");
|
||||
st_all.executeUpdate("BEGIN TRANSACTION;");
|
||||
st_ru.executeUpdate("BEGIN TRANSACTION;");
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N4:"+ex.getMessage(),ex);
|
||||
}
|
||||
@ -244,39 +298,45 @@ public class Precipitation implements ServletContextAware {
|
||||
logger.info("Delete old data 1");
|
||||
try {
|
||||
String sql="delete from main.precipitation where date=cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone) and hours="+forecast;
|
||||
st.executeUpdate(sql);
|
||||
st_all.executeUpdate(sql);
|
||||
st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N5:"+ex.getMessage(),ex);
|
||||
}
|
||||
logger.info("Delete old data 2");
|
||||
try {
|
||||
String sql="delete from main.precipitation_dates where date=cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone) and hours="+forecast;
|
||||
st.executeUpdate(sql);
|
||||
st_all.executeUpdate(sql);
|
||||
st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N6:"+ex.getMessage(),ex);
|
||||
}
|
||||
logger.info("Delete old data 3");
|
||||
try {
|
||||
String sql="delete from main.precipitation_dates where date=cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone) and hours="+forecast;
|
||||
st.executeUpdate(sql);
|
||||
st_all.executeUpdate(sql);
|
||||
st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.info(ex.getMessage());
|
||||
}
|
||||
System.out.println("Delete old data 3");
|
||||
try {
|
||||
String sql="delete from main.precipitation where date<=CURRENT_DATE-'730 days'::INTERVAL";
|
||||
st.executeUpdate(sql);
|
||||
st_all.executeUpdate(sql);
|
||||
st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N7:"+ex.getMessage(),ex);
|
||||
}
|
||||
|
||||
try {
|
||||
st.executeUpdate("END TRANSACTION;");
|
||||
st_all.executeUpdate("END TRANSACTION;");
|
||||
st_ru.executeUpdate("END TRANSACTION;");
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N7.1:"+ex.getMessage(),ex);
|
||||
}
|
||||
try {
|
||||
st.executeUpdate("BEGIN TRANSACTION;");
|
||||
st_all.executeUpdate("BEGIN TRANSACTION;");
|
||||
st_ru.executeUpdate("BEGIN TRANSACTION;");
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N7.2:"+ex.getMessage(),ex);
|
||||
}
|
||||
@ -297,29 +357,28 @@ public class Precipitation implements ServletContextAware {
|
||||
if(!Float.isNaN(dataArrayTmp.getFloat(pos))) //On the water none temperatyre.
|
||||
{
|
||||
String country_id="";
|
||||
ResultSet rs=null;
|
||||
try {
|
||||
rs = st.executeQuery("select c.id from main.countries c where ST_Contains(c.geom,ST_SetSRID(st_makepoint("+lon+","+lat+"),4326)) limit 1");
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N8:"+ex.getMessage(),ex);
|
||||
boolean db_all=false,db_ru=false;
|
||||
if(country_id.isEmpty()) {
|
||||
country_id = getCountryId(st_all, lon, lat);
|
||||
if(!country_id.isEmpty()) db_all=true;
|
||||
}
|
||||
if (rs != null) {
|
||||
try {
|
||||
if (rs.next())
|
||||
country_id=rs.getString(1);
|
||||
rs.close();
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N9:"+ex.getMessage(),ex);
|
||||
}
|
||||
if(country_id.isEmpty()) {
|
||||
country_id = 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+"));";
|
||||
st.executeUpdate(sql);
|
||||
if(db_all)
|
||||
st_all.executeUpdate(sql);
|
||||
if(db_ru)
|
||||
st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N10:"+ex.getMessage(),ex);
|
||||
throw new Exception("Failed insert precipitation...");
|
||||
}
|
||||
}
|
||||
|
||||
@ -332,7 +391,8 @@ 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);";
|
||||
st.executeUpdate(sql);
|
||||
st_all.executeUpdate(sql);
|
||||
st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N11:"+ex.getMessage(),ex);
|
||||
}
|
||||
@ -340,13 +400,16 @@ public class Precipitation implements ServletContextAware {
|
||||
//Delete values where country_id is null
|
||||
try {
|
||||
String sql="delete from main.precipitation where country_id is null;";
|
||||
st.executeUpdate(sql);
|
||||
st_all.executeUpdate(sql);
|
||||
st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N12:"+ex.getMessage(),ex);
|
||||
}
|
||||
|
||||
try {
|
||||
st.executeUpdate("END TRANSACTION;");
|
||||
String sql="END TRANSACTION;";
|
||||
st_all.executeUpdate(sql);
|
||||
st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N13:"+ex.getMessage(),ex);
|
||||
}
|
||||
@ -354,34 +417,16 @@ public class Precipitation implements ServletContextAware {
|
||||
gid.close();
|
||||
} catch (IOException ex) {
|
||||
logger.error("N14:"+ex.getMessage(),ex);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
logger.error("N15: "+ex.getMessage(),ex);
|
||||
}
|
||||
|
||||
try {conn.close();} catch (SQLException ex) {logger.info(ex.getMessage(),ex);}
|
||||
try {conn_all.close();} catch (SQLException ex) {logger.error("N16: "+ex.getMessage(),ex);}
|
||||
try {conn_ru.close();} catch (SQLException ex) {logger.error("N16: "+ex.getMessage(),ex);}
|
||||
|
||||
result+="End!<br>";
|
||||
return result;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
@Override
|
||||
public void setServletContext(ServletContext context) {
|
||||
this.context=context;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
public static String CutBeforeFirst(StringBuffer str,String ch)
|
||||
{
|
||||
int pos=str.indexOf(ch);
|
||||
String result="";
|
||||
if(pos==-1)
|
||||
{
|
||||
result.concat(str.toString());
|
||||
str.delete(0,str.length());
|
||||
}else
|
||||
{
|
||||
result=str.substring(0,pos);
|
||||
str.delete(0,pos+1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
//List of "Air temperature" dates from database in JSON
|
||||
@CrossOrigin
|
||||
@ -395,28 +440,13 @@ public class Precipitation implements ServletContextAware {
|
||||
String result="";
|
||||
|
||||
//Load DB configuration from "config.xml"
|
||||
Connection conn = null;
|
||||
try {
|
||||
Class.forName("org.postgresql.Driver");
|
||||
conn = DriverManager.getConnection(db_url, db_login, db_password);
|
||||
if (conn != null) {
|
||||
logger.info("Connect is OK!");
|
||||
} else {
|
||||
error=true;
|
||||
result="An error occurred while connecting to the database!";
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
logger.error("N15:"+ex.getMessage(),ex);
|
||||
error=true;
|
||||
result="<br>SQLException: "+ex.getMessage()+"<br>";
|
||||
}
|
||||
Connection conn_all = getConn(db_url_all,db_login_all,db_password_all);
|
||||
|
||||
if(!error)
|
||||
{
|
||||
Statement st;
|
||||
Statement st_all=null;
|
||||
try {
|
||||
st = conn.createStatement();
|
||||
//String sql = "SELECT to_char(date, 'YYYY-MM-DD') as date,hours as hour,DATE_PART('doy',date)-1 as day FROM main.precipitation group by date,hours order by date,hours";
|
||||
st_all = conn_all.createStatement();
|
||||
String sql = """
|
||||
SELECT
|
||||
to_char(date, 'YYYY-MM-DD') as date,
|
||||
@ -428,8 +458,8 @@ public class Precipitation implements ServletContextAware {
|
||||
date,
|
||||
hours
|
||||
order by date,hours
|
||||
""";
|
||||
ResultSet rs = st.executeQuery(sql);
|
||||
""";
|
||||
ResultSet rs = st_all.executeQuery(sql);
|
||||
if(rs!=null)
|
||||
{
|
||||
boolean exists=false;
|
||||
@ -451,12 +481,13 @@ public class Precipitation implements ServletContextAware {
|
||||
result="[]";
|
||||
}
|
||||
}
|
||||
st.close();
|
||||
conn.close();
|
||||
st_all.close();
|
||||
} catch (SQLException ex) {
|
||||
result="<br>SQLException:"+ex.getMessage()+"<br>";
|
||||
logger.error("N17:"+ex.getMessage(),ex);
|
||||
}
|
||||
}finally{
|
||||
try { conn_all.close(); } catch (SQLException ex) { logger.error("N18: "+ex.getMessage(),ex); }
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -54,21 +54,88 @@ import ucar.nc2.dataset.NetcdfDataset;
|
||||
@Controller
|
||||
public class SoilTmperature implements ServletContextAware {
|
||||
|
||||
@Value("${custom.config.db_url}")
|
||||
private String db_url;
|
||||
@Value("${custom.config.db_login}")
|
||||
private String db_login;
|
||||
@Value("${custom.config.db_password}")
|
||||
private String db_password;
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SoilTmperature.class);
|
||||
|
||||
@Value("${custom.config.db_all.url}")
|
||||
private String db_url_all;
|
||||
@Value("${custom.config.db_all.login}")
|
||||
private String db_login_all;
|
||||
@Value("${custom.config.db_all.password}")
|
||||
private String db_password_all;
|
||||
|
||||
@Value("${custom.config.db_ru.url}")
|
||||
private String db_url_ru;
|
||||
@Value("${custom.config.db_ru.login}")
|
||||
private String db_login_ru;
|
||||
@Value("${custom.config.db_ru.password}")
|
||||
private String db_password_ru;
|
||||
|
||||
@Value("${custom.config.data_dir}")
|
||||
private String data_dir;
|
||||
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SoilTmperature.class);
|
||||
|
||||
private ServletContext context;
|
||||
|
||||
//Example: http://127.0.0.1:8081/geodatalist/DownloadSoil?forecast=000
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
@Override
|
||||
public void setServletContext(jakarta.servlet.ServletContext servletContext) {
|
||||
this.context=context;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
public Connection getConn(String url, String login,String password){
|
||||
Connection conn = null;
|
||||
try{
|
||||
Class.forName("org.postgresql.Driver");
|
||||
conn = DriverManager.getConnection(url,login,password);
|
||||
}catch(Exception ex)
|
||||
{
|
||||
logger.error("N1: "+ex.getMessage()+"<br>",ex);
|
||||
}
|
||||
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);
|
||||
String result="";
|
||||
if(pos==-1)
|
||||
{
|
||||
result.concat(str.toString());
|
||||
str.delete(0,str.length());
|
||||
}else
|
||||
{
|
||||
result=str.substring(0,pos);
|
||||
str.delete(0,pos+1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
/**
|
||||
* Example: http://127.0.0.1:8081/geodatalist/DownloadSoil?forecast=000
|
||||
* @param response
|
||||
* @param forecast
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping(value = "/geodatalist/DownloadSoil",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
|
||||
@ResponseBody
|
||||
public Object ajaxTamer(HttpServletResponse response,@RequestParam(required=true,name="forecast") String forecast,@RequestParam(required=false,name="date") String date) {
|
||||
@ -88,24 +155,9 @@ public class SoilTmperature implements ServletContextAware {
|
||||
if (!dir.exists()) dir.mkdirs();
|
||||
|
||||
//response.getWriter().append("Served at: ").append(request.getContextPath());
|
||||
Connection conn = null;
|
||||
try{
|
||||
Class.forName("org.postgresql.Driver");
|
||||
conn = DriverManager.getConnection(db_url,db_login,db_password);
|
||||
if(conn!=null)
|
||||
{
|
||||
logger.info("Connect is OK!");
|
||||
result+="Connect is OK!<br>";
|
||||
}else
|
||||
{
|
||||
logger.info("<br>Connect is ERROR<br>");
|
||||
result+="Connect is ERROR!<br>";
|
||||
}
|
||||
}catch(Exception ex)
|
||||
{
|
||||
logger.error("N1: "+ex.getMessage()+"<br>",ex);
|
||||
result+="Connect Exception:"+ex.getMessage()+"<br>";
|
||||
}
|
||||
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);
|
||||
|
||||
|
||||
//Example request: http://ccalm.org/DownloadWeather?forecast=000&date=20210531
|
||||
//Example request: http://localhost:8080/CCALM/DownloadWeather?forecast=000
|
||||
@ -241,15 +293,18 @@ public class SoilTmperature implements ServletContextAware {
|
||||
}
|
||||
dimIt = null;
|
||||
|
||||
Statement st=null;
|
||||
Statement st_all=null;
|
||||
Statement st_ru=null;
|
||||
try {
|
||||
st = conn.createStatement();
|
||||
st_all = conn_all.createStatement();
|
||||
st_ru = conn_all.createStatement();
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N3: "+ex.getMessage(),ex);
|
||||
}
|
||||
|
||||
try {
|
||||
st.executeUpdate("BEGIN TRANSACTION;");
|
||||
st_all.executeUpdate("BEGIN TRANSACTION;");
|
||||
st_ru.executeUpdate("BEGIN TRANSACTION;");
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N4: "+ex.getMessage(),ex);
|
||||
}
|
||||
@ -260,21 +315,24 @@ public class SoilTmperature implements ServletContextAware {
|
||||
logger.info("Delete old data 1");
|
||||
try {
|
||||
String sql="delete from main.soil_temperature where date=cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone) and hours="+forecast;
|
||||
st.executeUpdate(sql);
|
||||
st_all.executeUpdate(sql);
|
||||
st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N5: "+ex.getMessage(),ex);
|
||||
}
|
||||
logger.info("Delete old data 2");
|
||||
try {
|
||||
String sql="delete from main.soil_temperature_dates where date=cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone) and hours="+forecast;
|
||||
st.executeUpdate(sql);
|
||||
st_all.executeUpdate(sql);
|
||||
st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N6: "+ex.getMessage(),ex);
|
||||
}
|
||||
logger.info("Delete old data 3");
|
||||
try {
|
||||
String sql="delete from main.soil_temperature where date<=CURRENT_DATE-'730 days'::INTERVAL";
|
||||
st.executeUpdate(sql);
|
||||
st_all.executeUpdate(sql);
|
||||
st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N7: "+ex.getMessage(),ex);
|
||||
}
|
||||
@ -282,19 +340,22 @@ public class SoilTmperature implements ServletContextAware {
|
||||
logger.info("Delete old data 4");
|
||||
try {
|
||||
String sql="delete from main.soil_temperature where hours="+forecast;
|
||||
st.executeUpdate(sql);
|
||||
st_all.executeUpdate(sql);
|
||||
st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N8: "+ex.getMessage(),ex);
|
||||
}
|
||||
}
|
||||
try {
|
||||
st.executeUpdate("END TRANSACTION;");
|
||||
st_all.executeUpdate("END TRANSACTION;");
|
||||
st_ru.executeUpdate("END TRANSACTION;");
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N8.1:"+ex.getMessage(),ex);
|
||||
}
|
||||
|
||||
try {
|
||||
st.executeUpdate("BEGIN TRANSACTION;");
|
||||
st_all.executeUpdate("BEGIN TRANSACTION;");
|
||||
st_ru.executeUpdate("BEGIN TRANSACTION;");
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N8.2:"+ex.getMessage(),ex);
|
||||
}
|
||||
@ -315,29 +376,24 @@ public class SoilTmperature implements ServletContextAware {
|
||||
if(!Float.isNaN(dataArrayTmp.getFloat(pos))) //On the water none temperatyre.
|
||||
{
|
||||
String country_id="";
|
||||
ResultSet rs=null;
|
||||
try {
|
||||
rs = st.executeQuery("select c.id from main.countries c where ST_Contains(c.geom,ST_SetSRID(st_makepoint("+lon+","+lat+"),4326)) limit 1");
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N9: "+ex.getMessage(),ex);
|
||||
throw new Exception("Failed to select country...");
|
||||
boolean db_all=false,db_ru=false;
|
||||
if(country_id.isEmpty()) {
|
||||
country_id = getCountryId(st_all, lon, lat);
|
||||
if(!country_id.isEmpty()) db_all=true;
|
||||
}
|
||||
if (rs != null) {
|
||||
try {
|
||||
if (rs.next())
|
||||
country_id=rs.getString(1);
|
||||
rs.close();
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N10: "+ex.getMessage(),ex);
|
||||
throw new Exception("Failed to select country...");
|
||||
}
|
||||
if(country_id.isEmpty()) {
|
||||
country_id = getCountryId(st_ru, lon, lat);
|
||||
if(!country_id.isEmpty()) db_ru=true;
|
||||
}
|
||||
if(country_id!=null && !country_id.equals("") && !country_id.equals("null"))
|
||||
|
||||
if(!country_id.isEmpty() && !country_id.equals("null"))
|
||||
{
|
||||
//logger.info(lon + "," + lat +","+dataArrayTmp.getFloat(pos));
|
||||
try {
|
||||
String sql="insert into main.soil_temperature(date,hours,val,geom,country_id,soil_temperature_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_soil_temperature_date(cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone),"+forecast+"));";
|
||||
st.executeUpdate(sql);
|
||||
if(db_all)
|
||||
st_all.executeUpdate(sql);
|
||||
if(db_ru)
|
||||
st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N11: "+ex.getMessage(),ex);
|
||||
throw new Exception("Failed insert soil temperature...");
|
||||
@ -353,7 +409,8 @@ 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);";
|
||||
st.executeUpdate(sql);
|
||||
st_all.executeUpdate(sql);
|
||||
st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N12: "+ex.getMessage(),ex);
|
||||
}
|
||||
@ -361,13 +418,16 @@ public class SoilTmperature implements ServletContextAware {
|
||||
//Delete values where country_id is null
|
||||
try {
|
||||
String sql="delete from main.soil_temperature where country_id is null;";
|
||||
st.executeUpdate(sql);
|
||||
st_all.executeUpdate(sql);
|
||||
st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N13: "+ex.getMessage(),ex);
|
||||
}
|
||||
|
||||
try {
|
||||
st.executeUpdate("END TRANSACTION;");
|
||||
String sql="END TRANSACTION;";
|
||||
st_all.executeUpdate(sql);
|
||||
st_ru.executeUpdate(sql);
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N14: "+ex.getMessage(),ex);
|
||||
}
|
||||
@ -375,37 +435,16 @@ public class SoilTmperature implements ServletContextAware {
|
||||
gid.close();
|
||||
} catch (IOException ex) {
|
||||
logger.error("N15: "+ex.getMessage(),ex);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
} catch (Exception ex) {
|
||||
logger.error("N16: "+ex.getMessage(),ex);
|
||||
}
|
||||
|
||||
try {conn.close();} catch (SQLException ex) {logger.error("N16: "+ex.getMessage(),ex);}
|
||||
try {conn_all.close();} catch (SQLException ex) {logger.error("N16: "+ex.getMessage(),ex);}
|
||||
try {conn_ru.close();} catch (SQLException ex) {logger.error("N16: "+ex.getMessage(),ex);}
|
||||
|
||||
result+="End!<br>";
|
||||
return result;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
@Override
|
||||
public void setServletContext(ServletContext context) {
|
||||
this.context=context;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
public static String CutBeforeFirst(StringBuffer str,String ch)
|
||||
{
|
||||
int pos=str.indexOf(ch);
|
||||
String result="";
|
||||
if(pos==-1)
|
||||
{
|
||||
result.concat(str.toString());
|
||||
str.delete(0,str.length());
|
||||
}else
|
||||
{
|
||||
result=str.substring(0,pos);
|
||||
str.delete(0,pos+1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
//List of "Soil temperature" dates from database in JSON
|
||||
@CrossOrigin
|
||||
@ -418,27 +457,14 @@ public class SoilTmperature implements ServletContextAware {
|
||||
boolean error=false;
|
||||
String result="";
|
||||
|
||||
Connection conn = null;
|
||||
try {
|
||||
Class.forName("org.postgresql.Driver");
|
||||
conn = DriverManager.getConnection(db_url, db_login, db_password);
|
||||
if (conn != null) {
|
||||
logger.info("Connect is OK!");
|
||||
} else {
|
||||
error=true;
|
||||
result="An error occurred while connecting to the database!";
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
logger.error("N17: "+ex.getMessage(),ex);
|
||||
error=true;
|
||||
result="<br>SQLException: "+ex.getMessage()+"<br>";
|
||||
}
|
||||
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)
|
||||
{
|
||||
Statement st;
|
||||
try {
|
||||
st = conn.createStatement();
|
||||
st = conn_all.createStatement();
|
||||
//String sql = "SELECT to_char(date, 'YYYY-MM-DD') as date,hours as hour,EXTRACT(DAY FROM CURRENT_DATE-date) as day FROM main.soil_temperature group by date,hours order by date,hours";
|
||||
String sql = """
|
||||
SELECT
|
||||
@ -475,14 +501,16 @@ public class SoilTmperature implements ServletContextAware {
|
||||
|
||||
}
|
||||
st.close();
|
||||
conn.close();
|
||||
|
||||
} catch (SQLException ex) {
|
||||
logger.error("N19: "+ex.getMessage(),ex);
|
||||
result="<br>SQLException:"+ex.getMessage()+"<br>";
|
||||
}finally {
|
||||
if(conn_all!=null) try{ conn_all.close(); } catch (SQLException ignored){}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
}
|
||||
|
||||
@ -1,10 +0,0 @@
|
||||
spring.cache.type=none
|
||||
#custom.config.db_url=jdbc:postgresql://192.168.0.90:5432/weather
|
||||
custom.config.db_url=jdbc:postgresql://127.0.0.1:5432/weather
|
||||
custom.config.db_login=postgres
|
||||
custom.config.db_password=PasSecrKey1
|
||||
#custom.config.data_dir=O:\\temp\\CCALM\\
|
||||
custom.config.data_dir=/temp/CCALM/
|
||||
|
||||
server.port=8081
|
||||
server.servlet.session.timeout=300s
|
||||
@ -1,19 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="INFO">
|
||||
<Appenders>
|
||||
<!-- Ротирующий файловый аппендер -->
|
||||
<RollingFile name="RollingFile" fileName="logs/application.log"
|
||||
filePattern="logs/application-%d{MM-dd-yyyy}-%i.log.gz">
|
||||
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
|
||||
<Policies>
|
||||
<SizeBasedTriggeringPolicy size="100 MB"/>
|
||||
</Policies>
|
||||
<DefaultRolloverStrategy max="10"/>
|
||||
</RollingFile>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="ALL">
|
||||
<AppenderRef ref="RollingFile"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
@ -2,13 +2,16 @@
|
||||
<configuration>
|
||||
|
||||
<!-- Please check if the user has access to the directory from which the application is being executed -->
|
||||
<property name="LOGS" value="/var/log/weather" />
|
||||
<property name="LOGS" value="logs" />
|
||||
<springProperty scope="context" name="appName" source="spring.application.name"/>
|
||||
|
||||
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${LOGS}/weather.log</file>
|
||||
<encoder class="net.logstash.logback.encoder.LogstashEncoder" />
|
||||
<file>${LOGS}/${appName}.log</file>
|
||||
<encoder>
|
||||
<pattern>{"timestamp":"%d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z'}","thread":"[%thread]","level":"%level","logger":"%logger{36}","marker":"%X{marker}","message":"%msg"}%n</pattern>
|
||||
</encoder>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>${LOGS}/weather.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
|
||||
<fileNamePattern>${LOGS}/${appName}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
|
||||
<maxHistory>30</maxHistory>
|
||||
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
|
||||
<maxFileSize>100MB</maxFileSize>
|
||||
@ -16,8 +19,16 @@
|
||||
</rollingPolicy>
|
||||
</appender>
|
||||
|
||||
<root level="info">
|
||||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z'} | %level | %logger{36} | %X{marker} | %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!--root level="info"-->
|
||||
<root level="warn">
|
||||
<appender-ref ref="FILE" />
|
||||
<appender-ref ref="CONSOLE" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
|
||||
Reference in New Issue
Block a user