diff --git a/.gitignore b/.gitignore index eef07ec..67b8fa5 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,7 @@ target/ /nbproject/private/ /nbbuild/ /dist/ +/logs/ /nbdist/ /.nb-gradle/ build/ diff --git a/org_ccalm_weather.yml b/org_ccalm_weather.yml new file mode 100644 index 0000000..339559f --- /dev/null +++ b/org_ccalm_weather.yml @@ -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 + diff --git a/pom.xml b/pom.xml index 0dbf35b..fc46a4e 100644 --- a/pom.xml +++ b/pom.xml @@ -5,12 +5,12 @@ org.springframework.boot spring-boot-starter-parent - 2.5.4 + 3.3.3 org.ccalm weather - 0.0.6-SNAPSHOT + 0.0.7-SNAPSHOT weather Demo project for Spring Boot @@ -78,65 +78,26 @@ org.springframework.boot spring-boot-maven-plugin - - - - - diff --git a/src/main/java/org/ccalm/weather/AirTemperature.java b/src/main/java/org/ccalm/weather/AirTemperature.java index 271fc54..9b06245 100644 --- a/src/main/java/org/ccalm/weather/AirTemperature.java +++ b/src/main/java/org/ccalm/weather/AirTemperature.java @@ -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; @@ -53,20 +49,97 @@ 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()+"
",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!
"; - }else - { - logger.info("
Connect is ERROR
"); - result+="Connect is ERROR!
"; - } - }catch(Exception ex) - { - logger.error("N1:"+ex.getMessage()+"
",ex); - result+="Connect Exception:"+ex.getMessage()+"
"; - } + 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()+"
"; //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,41 +398,42 @@ 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); } - + gid.close(); } catch (IOException ex) { 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!
"; return result; } - //--------------------------------------------------------------------------- - @Override - public void setServletContext(ServletContext context) { - this.context=context; - } + //--------------------------------------------------------------------------- public static String CutBeforeFirst(StringBuffer str,String ch) { @@ -393,29 +462,14 @@ 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="
SQLException: "+ex.getMessage()+"
"; - } + + 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="
SQLException:"+ex.getMessage()+"
"; logger.error("N18:"+ex.getMessage(),ex); - } + }finally{ + try { conn_all.close(); } catch (SQLException ex) { logger.error("N19: "+ex.getMessage(),ex); } + } } return result; } - //--------------------------------------------------------------------------- + //--------------------------------------------------------------------------- } diff --git a/src/main/java/org/ccalm/weather/Precipitation.java b/src/main/java/org/ccalm/weather/Precipitation.java index cc41668..e4500fd 100644 --- a/src/main/java/org/ccalm/weather/Precipitation.java +++ b/src/main/java/org/ccalm/weather/Precipitation.java @@ -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; @@ -53,22 +54,88 @@ import ucar.nc2.dataset.NetcdfDataset; @Controller public class Precipitation implements ServletContextAware { + + private static final Logger logger = LogManager.getLogger(Precipitation.class); - @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; + @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()+"
",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!
"; - }else - { - logger.info("
Connect is ERROR
"); - result+="Connect is ERROR!
"; - } - }catch(Exception e) - { - logger.error("N1:"+e.getMessage()+"
",e); - result+="Connect Exception:"+e.getMessage()+"
"; - } + 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!
"; 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="
SQLException: "+ex.getMessage()+"
"; - } - + 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,13 +481,14 @@ public class Precipitation implements ServletContextAware { result="[]"; } } - st.close(); - conn.close(); + st_all.close(); } catch (SQLException ex) { result="
SQLException:"+ex.getMessage()+"
"; logger.error("N17:"+ex.getMessage(),ex); - } - } + }finally{ + try { conn_all.close(); } catch (SQLException ex) { logger.error("N18: "+ex.getMessage(),ex); } + } + } return result; } //--------------------------------------------------------------------------- diff --git a/src/main/java/org/ccalm/weather/SoilTmperature.java b/src/main/java/org/ccalm/weather/SoilTmperature.java index c8dd7c5..c381f85 100644 --- a/src/main/java/org/ccalm/weather/SoilTmperature.java +++ b/src/main/java/org/ccalm/weather/SoilTmperature.java @@ -53,22 +53,89 @@ 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 + private ServletContext context; + + //--------------------------------------------------------------------------- + @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()+"
",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!
"; - }else - { - logger.info("
Connect is ERROR
"); - result+="Connect is ERROR!
"; - } - }catch(Exception ex) - { - logger.error("N1: "+ex.getMessage()+"
",ex); - result+="Connect Exception:"+ex.getMessage()+"
"; - } + 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!
"; 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 @@ -417,28 +456,15 @@ 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="
SQLException: "+ex.getMessage()+"
"; - } + + 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="
SQLException:"+ex.getMessage()+"
"; - } + }finally { + if(conn_all!=null) try{ conn_all.close(); } catch (SQLException ignored){} + } } return result; } - //--------------------------------------------------------------------------- + //--------------------------------------------------------------------------- } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index c7e5127..0000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -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 diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml deleted file mode 100644 index 073c1ad..0000000 --- a/src/main/resources/log4j2.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - - - diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml index 9081a3a..08412b6 100644 --- a/src/main/resources/logback-spring.xml +++ b/src/main/resources/logback-spring.xml @@ -2,13 +2,16 @@ - + + - ${LOGS}/weather.log - + ${LOGS}/${appName}.log + + {"timestamp":"%d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z'}","thread":"[%thread]","level":"%level","logger":"%logger{36}","marker":"%X{marker}","message":"%msg"}%n + - ${LOGS}/weather.%d{yyyy-MM-dd}.%i.log.gz + ${LOGS}/${appName}.%d{yyyy-MM-dd}.%i.log 30 100MB @@ -16,8 +19,16 @@ - + + + %d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z'} | %level | %logger{36} | %X{marker} | %msg%n + + + + + +