diff --git a/src/main/java/org/ccalm/weather/AirTemperature.java b/src/main/java/org/ccalm/weather/AirTemperature.java index 9b06245..ba8dab8 100644 --- a/src/main/java/org/ccalm/weather/AirTemperature.java +++ b/src/main/java/org/ccalm/weather/AirTemperature.java @@ -306,14 +306,6 @@ public class AirTemperature implements ServletContextAware { result+="Size="+dataArrayLat.getSize()+"
"; //Delete old data - /*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; - 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; @@ -324,14 +316,22 @@ public class AirTemperature implements ServletContextAware { } logger.info("Delete old data 3"); try { - //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); } - + if(Integer.parseInt(forecast)!=0) { + logger.info("Delete all old forecasts"); + try { + String sql="delete from main.air_temperature_dates where hours="+forecast; + st_all.executeUpdate(sql); + st_ru.executeUpdate(sql); + } catch (SQLException ex) { + logger.error("N8: "+ex.getMessage(),ex); + } + } try { if(st_all!=null) st_all.executeUpdate("END TRANSACTION;"); if(st_ru!=null) st_ru.executeUpdate("END TRANSACTION;"); diff --git a/src/main/java/org/ccalm/weather/Precipitation.java b/src/main/java/org/ccalm/weather/Precipitation.java index 2d69d56..49355ef 100644 --- a/src/main/java/org/ccalm/weather/Precipitation.java +++ b/src/main/java/org/ccalm/weather/Precipitation.java @@ -294,47 +294,33 @@ public class Precipitation implements ServletContextAware { result+="Size="+dataArrayLat.getSize()+"
"; - //Delete old data + //If repeat download, delete old data 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; + 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_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_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_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_dates where date=cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone) and hours="+forecast; - st.executeUpdate(sql); - } catch (SQLException ex) { - logger.error("N6:"+ex.getMessage(),ex); - } - logger.info("Delete old data 3"); - try { - String sql="delete from main.precipitation where date<=CURRENT_DATE-'730 days'::INTERVAL"; + String sql="delete from main.precipitation_dates where date<=CURRENT_DATE-'730 days'::INTERVAL"; st_all.executeUpdate(sql); st_ru.executeUpdate(sql); } catch (SQLException ex) { logger.error("N7:"+ex.getMessage(),ex); } - + if(Integer.parseInt(forecast)!=0) { + logger.info("Delete all old forecasts"); + try { + String sql="delete from main.precipitation_dates where hours="+forecast; + st_all.executeUpdate(sql); + st_ru.executeUpdate(sql); + } catch (SQLException ex) { + logger.error("N8: "+ex.getMessage(),ex); + } + } try { st_all.executeUpdate("END TRANSACTION;"); st_ru.executeUpdate("END TRANSACTION;"); diff --git a/src/main/java/org/ccalm/weather/SoilTmperature.java b/src/main/java/org/ccalm/weather/SoilTmperature.java index c381f85..241d53d 100644 --- a/src/main/java/org/ccalm/weather/SoilTmperature.java +++ b/src/main/java/org/ccalm/weather/SoilTmperature.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; @@ -311,10 +307,10 @@ public class SoilTmperature implements ServletContextAware { result+="Size="+dataArrayLat.getSize()+"
"; - //Delete old data + //If repeat download, delete old data 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; + 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_all.executeUpdate(sql); st_ru.executeUpdate(sql); } catch (SQLException ex) { @@ -322,24 +318,16 @@ public class SoilTmperature implements ServletContextAware { } 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_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"; + String sql="delete from main.soil_temperature_dates where date<=CURRENT_DATE-'730 days'::INTERVAL"; st_all.executeUpdate(sql); st_ru.executeUpdate(sql); } catch (SQLException ex) { logger.error("N7: "+ex.getMessage(),ex); } if(Integer.parseInt(forecast)!=0) { - logger.info("Delete old data 4"); + logger.info("Delete all old forecasts"); try { - String sql="delete from main.soil_temperature where hours="+forecast; + String sql="delete from main.soil_temperature_dates where hours="+forecast; st_all.executeUpdate(sql); st_ru.executeUpdate(sql); } catch (SQLException ex) { @@ -389,11 +377,40 @@ public class SoilTmperature implements ServletContextAware { if(!country_id.isEmpty() && !country_id.equals("null")) { 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+"));"; - if(db_all) - st_all.executeUpdate(sql); - if(db_ru) ++++++++ + String sql=""" + insert into main.soil_temperature( + val, + country_id, + point_id, + soil_temperature_date_id + )values( + ?, + ?, + main.get_point_id(?,?,?), + "+country_id+", + main.get_soil_temperature_date(cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone),"+forecast+") + ); + """; + if(db_all) { + try (PreparedStatement pstmt = conn_all.prepareStatement(sql)) { + // Устанавливаем параметры в PreparedStatement + pstmt.setFloat(1, dataArrayTmp.getFloat(pos)); // val + pstmt.setLong(2, country_id); // country_id + + pstmt.setString(2, lon); // lon + pstmt.setString(3, lat); // lat + + pstmt.setString(5, date); // date + pstmt.setString(6, time); // time + pstmt.setInt(7, forecast); // forecast + pstmt.executeUpdate(); + } + + } + if(db_ru) { st_ru.executeUpdate(sql); + } } catch (SQLException ex) { logger.error("N11: "+ex.getMessage(),ex); throw new Exception("Failed insert soil temperature..."); @@ -465,7 +482,6 @@ public class SoilTmperature implements ServletContextAware { Statement st; try { 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 to_char(date, 'YYYY-MM-DD') as date,