From fab0c34ffb3c8973f575c2b94d80f26382201207 Mon Sep 17 00:00:00 2001 From: igor Date: Wed, 23 Jul 2025 07:37:24 +0500 Subject: [PATCH 1/3] AcceptJSON_RUS model --- src/main/java/org/ccalm/main/AcceptJSON.java | 875 +++++++------------ src/main/resources/templates/json.html | 25 - 2 files changed, 302 insertions(+), 598 deletions(-) delete mode 100644 src/main/resources/templates/json.html diff --git a/src/main/java/org/ccalm/main/AcceptJSON.java b/src/main/java/org/ccalm/main/AcceptJSON.java index b5db563..f015083 100644 --- a/src/main/java/org/ccalm/main/AcceptJSON.java +++ b/src/main/java/org/ccalm/main/AcceptJSON.java @@ -5,7 +5,6 @@ package org.ccalm.main; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; -import java.awt.image.RenderedImage; import java.io.*; import java.net.HttpURLConnection; import java.net.URI; @@ -430,36 +429,10 @@ public class AcceptJSON implements ServletContextAware { * @param skip * @return */ - @RequestMapping(value = {"/AcceptJSON_RUS", "/api/locust/v01/AcceptJSON_RUS"}, method = { RequestMethod.GET, RequestMethod.POST }) + @RequestMapping(value = {"/AcceptJSON_RUS", "/api/locust/v01/AcceptJSON_RUS"}, method = { RequestMethod.GET, RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_VALUE) public String acceptCSV(@ModelAttribute User user, Model model,@RequestParam(required=false,name="file") MultipartFile file,@RequestParam(required=false,name="skip",defaultValue = "0") Boolean skip) { + JSONObject json = new JSONObject(); try{ - Connection conn = null; - try { - Class.forName("org.postgresql.Driver"); - conn = DriverManager.getConnection(db_url, db_login, db_password); - } catch (Exception ex) { - throw new CustomException(200, 10000, ex.getMessage(),null,true); - } - - //Set UTC time - Statement stm=null; - try { - stm = conn.createStatement(); - try { - stm.execute("SET timezone TO 'UTC';"); - } catch( SQLException ex ) { - throw new CustomException(200, 10000, ex.getMessage(),null,true); - } - stm.close(); - } catch (SQLException ex) { - throw new CustomException(200, 10000, ex.getMessage(),null,true); - } - - - String sqlData=""; - String table=""; - table+=""; - //I select the maximum date, check that it is less than the current minus 2 days and try to load it LocalDate lastDate = null; String sql= """ @@ -478,36 +451,8 @@ public class AcceptJSON implements ServletContextAware { if(lastDate==null) lastDate = LocalDate.parse("2025-04-01"); - Statement stt = null; - ResultSet rs = null; - - //Выбираю максимальную дату проверяю что она меньше текущей минус 2 дня и пытаюсь загрузить её - /*LocalDate lastDate = null; - Statement stt = null; - ResultSet rs = null; - try { - stt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); - rs = stt.executeQuery("select to_char(max(date), 'YYYY-MM-DD') as date from integration.days_ru"); - if (rs != null) { - try { - if (rs.next()) { - String value = rs.getString(1); - if(value!=null) - lastDate = LocalDate.parse(value); - } - rs.close(); - } catch (SQLException ex) { - throw new CustomException(200, 10000, ex.getMessage(),null,true); - } - } - } catch (SQLException ex) { - throw new CustomException(200, 10000, ex.getMessage(),null,true); - }finally { - if(rs!=null) try{rs.close();}catch(SQLException ex){ ex.printStackTrace(); } - if(stt!=null) try{stt.close();}catch(SQLException ex){ ex.printStackTrace(); } - } - if(lastDate==null) lastDate = LocalDate.parse("2025-04-01");*/ - + //Statement stt = null; + //ResultSet rs = null; //By number of day downloading data and write result of downloading to database, try or false, after repair need repeat download while(lastDate.isBefore(LocalDate.now().minusDays(3))){ @@ -548,16 +493,12 @@ public class AcceptJSON implements ServletContextAware { String content=sb.toString(); if(content!=null && !content.isEmpty()) { - //byte[] data = sb.toByteArray(in); - //String content=new String(data, Charsets.UTF_8); - - //Iterate over the elements of the array. - int cntO = 1; - int cntV = 1; + int count = 0; JSONArray jsonArray = new JSONArray(content); for (Object o : jsonArray) { JSONObject jsonObj = (JSONObject) o; FrmLocustModel locust = new FrmLocustModel(); + locust.company_uid = "2925b0a4-2bc5-4083-b3f1-9fddd909289e"; //ФГБУ "Россельхозцентр" try{ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm"); @@ -567,11 +508,6 @@ public class AcceptJSON implements ServletContextAware { throw new CustomException(200, 10000, "Date error: " + jsonObj.optString("date","null"), null, true); } - String regionName=""; - if (!jsonObj.isNull("regionName")) - regionName = jsonObj.getString("regionName"); - - if (!jsonObj.isNull("townName")) locust.district = jsonObj.getString("townName"); @@ -591,164 +527,134 @@ public class AcceptJSON implements ServletContextAware { if (!jsonObj.isNull("polygonArea")) locust.size = String.valueOf(jsonObj.getDouble("polygonArea")); - //jsonObj.drawPolygon Краснодар + if (!jsonObj.isNull("regionName") || jsonObj.getString("regionName").isEmpty()){ - if (!regionName.isEmpty()) { - //Выбираю ID области - locust.region_id = ""; - //Statement stt = null; - //ResultSet rs = null; - try { - - stt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); - String sql_query = "select id from main.countriesregions where name like '%" + regionName + "%';"; - rs = stt.executeQuery(sql_query); - if (rs != null) { - try { - if (rs.next()) { - locust.region_id = rs.getString(1); - } - rs.close(); - } catch (SQLException ex) { - throw new CustomException(200, 10000, ex.getMessage(),null,true); - } - } - } catch (SQLException ex) { - throw new CustomException(200, 10000, ex.getMessage(),null,true); - } finally { - if (rs != null) try { - rs.close(); - } catch (SQLException ex) { - throw new CustomException(200, 10000, ex.getMessage(),null,true); - } - if (stt != null) try { - stt.close(); - } catch (SQLException ex) { - throw new CustomException(200, 10000, ex.getMessage(),null,true); + sql= "select id from main.countriesregions where name ilike :regionName"; + parameters = new MapSqlParameterSource(); + parameters.addValue("regionName", "%" + jsonObj.getString("regionName") + "%"); + ret = jdbcTemplate.query(sql, parameters, new DBTools.JsonRowMapper()); + for (String s : ret) { + JSONObject rObj = new JSONObject(s); + if(!rObj.isNull("id")) { + locust.region_id = String.valueOf(rObj.getLong("id")); } } + if(!locust.region_id.isEmpty() && !jsonObj.isNull("townName")) { - /*if(locust.region_id.isEmpty()){ - if(regionName.indexOf("Воронежс��ая область")>=0) locust.region_id="110"; - if(regionName.indexOf("Кра��нодарский край")>=0) locust.region_id="164"; - if(regionName.indexOf("Астраха��ская область")>=0) locust.region_id="68"; //dayOfYear: 97 at year: 2022 = 07.04.2022 - if(regionName.indexOf("Астраханская област��")>=0) locust.region_id="68"; // dayOfYear: 101 at year: 2022 = 11.04.2022 - }*/ - - if (locust.region_id.length() > 0) { - //Выбираю id региона (main.countriesdistricts) - locust.district_id = ""; - stt = null; - rs = null; - try { - - if (locust.district.equals("Алапаевский район")) - locust.district = "городской округ Алапаевское"; - if (locust.district.equals("Сухоложский район")) - locust.district = "городской округ Сухой Лог"; - if (locust.district.equals("Талицкий район")) - locust.district = "Талицкий городской округ"; - if (locust.district.equals("Артинский район")) - locust.district = "Талицкий городской округ"; - if (locust.district.equals("Богдановичский район")) - locust.district = "городской округ Богданович"; - if (locust.district.equals("Красноуфимский район")) - locust.district = "Красноуфимский округ"; - if (locust.district.equals("Белоярский район")) locust.region_id = "210"; - if (locust.district.equals("Пышминский район")) - locust.district = "Пышминский городской округ"; - if (locust.district.equals("Ирбитский район")) - locust.district = "городской округ Ирбитское"; - if (locust.district.equals("Фрязино г.о.")) locust.district = "городской округ Фрязино"; - if (locust.district.equals("Коломенский г.о.")) locust.district = "Коломенский район"; - if (locust.district.equals("Протвино г.о.")) - locust.district = "городской округ Протвино"; - if (locust.district.equals("Зарайск г.о.")) locust.district = "Зарайский район"; - if (locust.district.equals("Анапский район")) locust.district = "городской округ Анапа"; - if (locust.district.equals("Луховицы г.о.")) locust.district = "Луховицкий район"; - if (locust.district.equals("Дергачевский район")) - locust.district = "Дергачёвский район"; - if (locust.district.equals("Чистоозерный район")) - locust.district = "Чистоозёрный район"; - if (locust.district.equals("Сунжа город")) locust.district = "Сунженский район"; - if (locust.district.equals("Буденновский район")) - locust.district = "Будённовский район"; - if (locust.district.equals("Новоселовский район")) { + locust.district = jsonObj.getString("townName"); + switch(locust.district) + { + case "Алапаевский район": locust.district = "городской округ Алапаевское"; + break; + case "Сухоложский район": locust.district = "городской округ Сухой Лог"; + break; + case "Талицкий район": locust.district = "Талицкий городской округ"; + break; + case "Артинский район": locust.district = "Талицкий городской округ"; + break; + case "Богдановичский район": locust.district = "городской округ Богданович"; + break; + case "Красноуфимский район": locust.district = "Красноуфимский округ"; + break; + case "Белоярский район": locust.region_id = "210"; + break; + case "Пышминский район": locust.district = "Пышминский городской округ"; + break; + case "Ирбитский район": locust.district = "городской округ Ирбитское"; + break; + case "Фрязино г.о.": locust.district = "городской округ Фрязино"; + break; + case "Коломенский г.о.": locust.district = "Коломенский район"; + break; + case "Протвино г.о.": locust.district = "городской округ Протвино"; + break; + case "Зарайск г.о.": locust.district = "Зарайский район"; + break; + case "Анапский район": locust.district = "городской округ Анапа"; + break; + case "Луховицы г.о.": locust.district = "Луховицкий район"; + break; + case "Дергачевский район": locust.district = "Дергачёвский район"; + break; + case "Чистоозерный район": locust.district = "Чистоозёрный район"; + break; + case "Сунжа город": locust.district = "Сунженский район"; + break; + case "Буденновский район": locust.district = "Будённовский район"; + break; + case "Новоселовский район": { locust.district = "Новоселицкий район"; locust.region_id = "168"; - } - if (locust.district.equals("Тимашевский район")) locust.district = "Тимашёвский район"; - if (locust.district.equals("Славгородский район")) - locust.district = "городской округ Славгород"; - if (locust.district.equals("Режевский район")) - locust.district = "Режевской городской округ"; - if (locust.district.equals("Веселовский район")) locust.district = "Весёловский район"; - if (locust.district.equals("Карабулак город")) - locust.district = "городской округ Карабулак"; - if (locust.district.equals("Пугачевский район")) locust.district = "Пугачёвский район"; - if (locust.district.equals("Кущевский район")) locust.district = "Кущёвский район"; - if (locust.district.equals("Репьевский район")) locust.district = "Репьёвский район"; - if (locust.district.equals("Калачевский район")) locust.district = "Калачёвский район"; - if (locust.district.equals("Гайский район")) - locust.district = "Гайский городской округ"; - if (locust.district.equals("Новохоперский район")) - locust.district = "Новохопёрский район"; - if (locust.district.equals("Федоровский район")) locust.district = "Фёдоровский район"; - if (locust.district.equals("Серебряные Пруды г.о.")) - locust.district = "городской округ Серебряные Пруды"; - if (locust.district.equals("Минераловодский район")) - locust.district = "Минераловодский городской округ"; - if (locust.district.equals("Рузский г.о.")) locust.district = "Рузский район"; - if (locust.district.equals("Воробьевский район")) - locust.district = "Воробьёвский район"; - if (locust.district.equals("Ачитский район")) - locust.district = "Ачитский городской округ"; - if (locust.district.equals("Грачевский район")) locust.district = "Грачёвский район"; - if (locust.district.equals("Уссурийский район")) - locust.district = "Уссурийский городской округ"; - if (locust.district.equals("Сорочинский район")) - locust.district = "Сорочинский городской округ"; - if (locust.district.equals("Ясненский район")) - locust.district = "Ясненский городской округ"; - if (locust.district.equals("Белогорский муниципальный округ")) { + }break; + case "Тимашевский район": locust.district = "Тимашёвский район"; + break; + case "Славгородский район": locust.district = "городской округ Славгород"; + break; + case "Режевский район": locust.district = "Режевской городской округ"; + break; + case "Веселовский район": locust.district = "Весёловский район"; + break; + case "Карабулак город": locust.district = "городской округ Карабулак"; + break; + case "Пугачевский район": locust.district = "Пугачёвский район"; + break; + case "Кущевский район": locust.district = "Кущёвский район"; + break; + case "Репьевский район": locust.district = "Репьёвский район"; + break; + case "Калачевский район": locust.district = "Калачёвский район"; + break; + case "Гайский район": locust.district = "Гайский городской округ"; + break; + case "Новохоперский район": locust.district = "Новохопёрский район"; + break; + case "Федоровский район": locust.district = "Фёдоровский район"; + break; + case "Серебряные Пруды г.о.": locust.district = "городской округ Серебряные Пруды"; + break; + case "Минераловодский район": locust.district = "Минераловодский городской округ"; + break; + case "Рузский г.о.": locust.district = "Рузский район"; + break; + case "Воробьевский район": locust.district = "Воробьёвский район"; + break; + case "Ачитский район": locust.district = "Ачитский городской округ"; + break; + case "Грачевский район": locust.district = "Грачёвский район"; + break; + case "Уссурийский район": locust.district = "Уссурийский городской округ"; + break; + case "Сорочинский район": locust.district = "Сорочинский городской округ"; + break; + case "Ясненский район": locust.district = "Ясненский городской округ"; + break; + case "Белогорский муниципальный округ": { locust.district = "городской округ Белгород"; locust.region_id = "69"; - } + }break; + } - stt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); - String sql_query = "select id from main.countriesdistricts where region_id=" + locust.region_id + " and name like '%" + locust.district + "%';"; - rs = stt.executeQuery(sql_query); - if (rs != null) { - try { - if (rs.next()) { - locust.district_id = rs.getString(1); - } - rs.close(); - } catch (SQLException ex) { - throw new CustomException(200, 10000, ex.getMessage(),null,true); - } - } - } catch (SQLException ex) { - throw new CustomException(200, 10000, ex.getMessage(),null,true); - } finally { - if (rs != null) try { - rs.close(); - } catch (SQLException ex) { - throw new CustomException(200, 10000, ex.getMessage(),null,true); - } - if (stt != null) try { - stt.close(); - } catch (SQLException ex) { - throw new CustomException(200, 10000, ex.getMessage(),null,true); + sql = "select id from main.countriesdistricts where region_id=" + locust.region_id + " and name like '%" + locust.district + "%';"; + parameters = new MapSqlParameterSource(); + ret = jdbcTemplate.query(sql, parameters, new DBTools.JsonRowMapper()); + for (String s : ret) { + JSONObject rObj = new JSONObject(s); + if (!rObj.isNull("id")) { + locust.district_id = String.valueOf(rObj.getLong("id")); } } + //if(locust.district_id==null){ + // throw new CustomException(200, 10000, "Not find district: "+locust.district,null,true); + //} + + }else{ + throw new CustomException(200, 10000, "Not find region: "+jsonObj.getString("regionName"),null,true); } }else{ throw new CustomException(200, 10000, "regionName.isEmpty()",null,true); } - String row = ""; if (!jsonObj.isNull("subjectList")) { JSONArray jsonArraySub = jsonObj.getJSONArray("subjectList"); @@ -757,9 +663,10 @@ public class AcceptJSON implements ServletContextAware { JSONObject jsonObjSub = (JSONObject) o2; locust.eid = String.valueOf(jsonObjSub.getInt("id")); + locust.phytoType = jsonObjSub.getString("phytoType"); //"Вредители" или "Обработка" if (locust.phytoType.equals("Обработка")) { - locust.insecticide_name = jsonObjSub.getString("nameSubject"); //Наименование действующего вещества + /*locust.insecticide_name = jsonObjSub.getString("nameSubject"); //Наименование действующего вещества if (!jsonObjSub.isNull("nameLatSubject")) locust.insecticide_active_substance = jsonObjSub.getString("nameLatSubject"); //Латинское наименование действующего вещества if (!jsonObjSub.isNull("protectionDoze")) @@ -927,12 +834,11 @@ public class AcceptJSON implements ServletContextAware { throw new CustomException(200, 10000, ex.getMessage(),null,true); } - cntO++; + cntO++;*/ } else if (locust.phytoType.equals("Вредители")) { - //Вид саранчи - locust.locust_type_id = ""; + //Вид саранчи if(!jsonObjSub.isNull("nameSubject") && !jsonObjSub.optString("nameSubject","").isBlank()) { locust.locust = jsonObjSub.getString("nameSubject"); switch (jsonObjSub.optString("nameSubject",null).replace("'", "").trim()) { @@ -949,342 +855,198 @@ public class AcceptJSON implements ServletContextAware { locust.locust_type_id = "2"; break; default: - throw new CustomException(200, 10000, "Not find swarms_height: (" + locust.locust+")", null, true); + throw new CustomException(200, 10000, "Not find nameSubject: (" + locust.locust+")", null, true); } }else{ throw new CustomException(200, 10000, "Field nameSubject is null", null, true); } //фаза саранчи - if (!jsonObjSub.isNull("phaseEvolutionName")) + if(!jsonObjSub.isNull("phaseEvolutionName") && !jsonObjSub.optString("phaseEvolutionName","").isBlank()) { locust.phase = jsonObjSub.getString("phaseEvolutionName"); - - locust.locust_have = "3"; - if (locust.phase != null) { - if (locust.phase.equals("Кубышка")) locust.locust_have = "2"; //Кубышка = Яйца - if (locust.phase.equals("Гусеница")) locust.locust_have = "3"; - if (locust.phase.equals("Личинка 2-го возраста")) locust.locust_have = "3"; - if (locust.phase.equals("Личинка 3-го возраста")) locust.locust_have = "3"; - if (locust.phase.equals("Личинка 4-го возраста")) locust.locust_have = "3"; - if (locust.phase.equals("Личинка 5-го возраста")) locust.locust_have = "3"; - if (locust.phase.equals("Имаго")) locust.locust_have = "5"; + switch (jsonObjSub.optString("phaseEvolutionName",null).replace("'", "").trim()) { + case "Кубышка": + locust.locust_have = "2"; //Кубышка = Яйца + break; + case "Гусеница","Личинка","Личинка 2-го возраста","Личинка 3-го возраста","Личинка 4-го возраста","Личинка 5-го возраста": + locust.locust_have = "3"; + break; + case "Кулиги": + locust.locust_have = "4"; + break; + case "Имаго": + locust.locust_have = "5"; + break; + default: + throw new CustomException(200, 10000, "Not find phaseEvolutionName: (" + locust.phase+")", null, true); + } + }else{ + throw new CustomException(200, 10000, "Field phaseEvolutionName is null", null, true); } - - - //Если эта анкета "Имаго" то записываем плотность имаго на m2 - if (locust.locust_have.equals("5")) { - if (!jsonObjSub.isNull("subPestCount")) - locust.imago_density = String.valueOf(jsonObjSub.getFloat("subPestCount")); - } - //Если эта анкета "Кубышка"="Яйца" то записываем плотность кулиг на m2 - if (locust.locust_have.equals("2")) { - if (!jsonObjSub.isNull("subPestCount")) - locust.eggs_capsules_density = String.valueOf(jsonObjSub.getFloat("subPestCount")); - } - //Если эта анкета "Кулиги" то записываем плотность кулиг на m2 - if (locust.locust_have.equals("4")) { - if (!jsonObjSub.isNull("subPestCount")) - locust.kuliguli_density = String.valueOf(jsonObjSub.getFloat("subPestCount")); - } - //Если эта анкета "Личинок" то записываем плотность личинок на m2 - if (locust.locust_have.equals("3")) { - if (!jsonObjSub.isNull("subPestCount")) - locust.larva_density = String.valueOf(jsonObjSub.getFloat("subPestCount")); - } + if(!jsonObjSub.isNull("subPestCount") && !jsonObjSub.optString("subPestCount","").isBlank()) { + switch (locust.locust_have) { + case "5": //Если эта анкета "Имаго" то записываем плотность имаго на m2 + locust.imago_density = String.valueOf(jsonObjSub.getFloat("subPestCount")); + break; + case "2": //Если эта анкета "Кубышка"="Яйца" то записываем плотность кулиг на m2 + locust.eggs_capsules_density = String.valueOf(jsonObjSub.getFloat("subPestCount")); + break; + case "4": //Если эта анкета "Кулиги" то записываем плотность кулиг на m2 + locust.kuliguli_density = String.valueOf(jsonObjSub.getFloat("subPestCount")); + break; + case "3": //Если эта анкета "Личинок" то записываем плотность личинок на m2 + locust.larva_density = String.valueOf(jsonObjSub.getFloat("subPestCount")); + break; + default: + throw new CustomException(200, 10000, "Not set subPestCount: (" + locust.locust_have+")", null, true); + } + }//else{ + // throw new CustomException(200, 10000, "Field subPestCount is null", null, true); + //} //Биотоп - switch (jsonObjSub.getString("faoBiotopeName")) { - case "CIT степь": - locust.bio_biotope_id = "1"; - break; - case "CIT залежь": - locust.bio_biotope_id = "2"; - break; - case "CIT посевы": - locust.bio_biotope_id = "3"; - break; - case "CIT пастбища": - locust.bio_biotope_id = "11"; - break; - case "CIT холмы, горы": - locust.bio_biotope_id = "19"; - break; - case "DMA равнины": - locust.bio_biotope_id = "4"; - break; - case "DMA посевы": - locust.bio_biotope_id = "6"; - break; - case "DMA пастбища": - locust.bio_biotope_id = "13"; - break; - case "DMA холмы, горы": - locust.bio_biotope_id = "14"; - break; - case "LMI низкие сухие тростники": - locust.bio_biotope_id = "7"; - break; - case "LMI высокие влажные тростники": - locust.bio_biotope_id = "8"; - break; - case "LMI другое": - locust.bio_biotope_id = "10"; - break; - case "Другие саранчовые на посевах": - locust.bio_biotope_id = "15"; - break; - case "Другие саранчовые на равнине": - locust.bio_biotope_id = "16"; - break; - case "Другие саранчовые на холмах": - locust.bio_biotope_id = "17"; - break; - case "Другое": - locust.bio_biotope_id = "18"; - break; - default: - locust.bio_biotope_id = null; - break; + if(!jsonObjSub.isNull("faoBiotopeName") && !jsonObjSub.optString("faoBiotopeName","").isBlank()) { + switch (jsonObjSub.getString("faoBiotopeName")) { + case "CIT степь": + locust.bio_biotope_id = "1"; + break; + case "CIT залежь": + locust.bio_biotope_id = "2"; + break; + case "CIT посевы": + locust.bio_biotope_id = "3"; + break; + case "CIT пастбища": + locust.bio_biotope_id = "11"; + break; + case "CIT холмы, горы": + locust.bio_biotope_id = "19"; + break; + case "DMA равнины": + locust.bio_biotope_id = "4"; + break; + case "DMA посевы": + locust.bio_biotope_id = "6"; + break; + case "DMA пастбища": + locust.bio_biotope_id = "13"; + break; + case "DMA холмы, горы": + locust.bio_biotope_id = "14"; + break; + case "LMI низкие сухие тростники": + locust.bio_biotope_id = "7"; + break; + case "LMI высокие влажные тростники": + locust.bio_biotope_id = "8"; + break; + case "LMI другое": + locust.bio_biotope_id = "10"; + break; + case "Другие саранчовые на посевах": + locust.bio_biotope_id = "15"; + break; + case "Другие саранчовые на равнине": + locust.bio_biotope_id = "16"; + break; + case "Другие саранчовые на холмах": + locust.bio_biotope_id = "17"; + break; + case "Другое": + locust.bio_biotope_id = "18"; + break; + default: + throw new CustomException(200, 10000, "Not set faoBiotopeName: (" + jsonObjSub.getString("faoBiotopeName")+")", null, true); + } + }else{ + throw new CustomException(200, 10000, "Field faoBiotopeName is null", null, true); } - //Если те виды что нам нужны - if (regionName == null || regionName.isEmpty() || locust.lat_center == null || Double.parseDouble(locust.lat_center) == 0 || locust.lon_center == null || Double.parseDouble(locust.lon_center) == 0 || locust.locust.equals("Совка ипсилон") || locust.locust.equals("Восточный майский хрущ") || locust.locust.equals("Кукурузный стеблевой мотылек") || locust.locust.equals("Картофельная, или болотная совка, лиловатая яровая совка") || locust.locust.equals("Луговой мотылёк") || locust.locust.equals("Хлопковая совка") || locust.locust.equals("Стеблевой кукурузный мотылек") || locust.locust.equals("Восточная луговая совка") || locust.locust.equals("Коричневая щитовка") || locust.locust.equals("Продолговатая (чайная) подушечница") || locust.locust.equals("Яблоневая запятовидная щитовка") || locust.locust.equals("Виноградная филлоксера") || locust.locust.equals("Бахчевая коровка")) { + //Тех полей что нет в анкете вставляю в комментарии + try { + locust.description = ""; - } else { + if (jsonObjSub.getBoolean("notFound")) + locust.description += "Вредитель не обнаружен: " + jsonObjSub.getString("nameSubject") + "\n"; + else + locust.description += "Вредитель обнаружен: " + jsonObjSub.getString("nameSubject") + "\n"; - if (locust.locust_type_id.isEmpty()) { - System.out.println("locust.locust = "+locust.locust); - model.addAttribute("PreviewTable", "locust.locust_type_id == 0 locust name = " + locust.locust); - throw new CustomException(200, 10000, "Error, not find locust.locust = "+locust.locust,null,true); - } - if (locust.region_id.isEmpty()) { - System.out.println("locust.region = "+regionName); - model.addAttribute("PreviewTable", "locust.region_id == 0 region name = " + regionName); - throw new CustomException(200, 10000, "Error, not find locust.region = "+regionName,null,true); + if (!jsonObjSub.isNull("phaseEvolutionName")) + locust.description += "Фаза: " + jsonObjSub.getString("phaseEvolutionName") + "\n"; //Так как фаза у росиян более подробная + if (!jsonObjSub.isNull("subPestCount")) { + locust.description += "Вредителей: " + jsonObjSub.getFloat("subPestCount"); + if (!jsonObjSub.isNull("unitName")) + locust.description += " " + jsonObjSub.getString("unitName"); + locust.description += "\n"; } - //Тех полей что нет в анкете вствляю в комментарии - try { - locust.description = ""; - - if (jsonObjSub.getBoolean("notFound")) - locust.description += "Вредитель не обнаружен: " + jsonObjSub.getString("nameSubject") + "\n"; - else - locust.description += "Вредитель обнаружен: " + jsonObjSub.getString("nameSubject") + "\n"; - - if (!jsonObjSub.isNull("phaseEvolutionName")) - locust.description += "Фаза: " + jsonObjSub.getString("phaseEvolutionName") + "\n"; //Так как фаза у росиян более подробная - if (!jsonObjSub.isNull("subPestCount")) { - locust.description += "Вредителей: " + jsonObjSub.getFloat("subPestCount"); - if (!jsonObjSub.isNull("unitName")) - locust.description += " " + jsonObjSub.getString("unitName"); - locust.description += "\n"; - } - - if (!jsonObj.isNull("cropCurrentType")) - locust.description += "Тип сева: " + jsonObj.getString("cropCurrentType") + "\n"; - if (!jsonObj.isNull("cropCurrentCulture")) - locust.description += "Наименование культуры: " + jsonObj.getString("cropCurrentCulture") + "\n"; - if (!jsonObj.isNull("cropCurrentCultureSort")) - locust.description += "Наименование сорта культуры: " + jsonObj.getString("cropCurrentCultureSort") + "\n"; - if (!jsonObj.isNull("cropCurrentGrowthPhase")) - locust.description += "Фаза роста культуры: " + jsonObj.getString("cropCurrentGrowthPhase") + "\n"; - if (!jsonObj.isNull("cropFieldArea")) { - locust.description += "Площадь поля: " + jsonObj.getFloat("cropFieldArea") + "\n"; - locust.bio_hectare = String.valueOf(jsonObj.getFloat("cropFieldArea")); - } - - - } catch (Exception ex) { - ex.printStackTrace(); + if (!jsonObjSub.isNull("cropCurrentType")) + locust.description += "Тип сева: " + jsonObjSub.getString("cropCurrentType") + "\n"; + if (!jsonObjSub.isNull("cropCurrentCulture")) + locust.description += "Наименование культуры: " + jsonObjSub.getString("cropCurrentCulture") + "\n"; + if (!jsonObjSub.isNull("cropCurrentCultureSort")) + locust.description += "Наименование сорта культуры: " + jsonObjSub.getString("cropCurrentCultureSort") + "\n"; + if (!jsonObjSub.isNull("cropCurrentGrowthPhase")) + locust.description += "Фаза роста культуры: " + jsonObjSub.getString("cropCurrentGrowthPhase") + "\n"; + if (!jsonObjSub.isNull("cropFieldArea")) { + locust.description += "Площадь поля: " + jsonObjSub.getFloat("cropFieldArea") + "\n"; + locust.bio_hectare = String.valueOf(jsonObjSub.getFloat("cropFieldArea")); } - locust.terrain = "null"; //У росиян нет сельского округа + } catch (Exception ex) { + throw new CustomException(200, 10000, "Error: " +ex.getMessage(), null, true); + } - row = ""; - if (locust.region_id == null || locust.region_id.isEmpty() || locust.district_id == null || locust.district_id.isEmpty() || locust.locust_type_id == null || locust.locust_type_id.isEmpty()) { - row = ""; - } - row += ""; - row += "\n"; + locust.terrain = "null"; //У Россиян нет сельского округа - //Если записи не существует то вставляем если существует то обновляем - boolean exists = false; //Is there a record. - Statement st; - try { - st = conn.createStatement(); - //ResultSet rs=null; - try { - sql = "select 1 from main.frmlocust where eid=" + String.valueOf(locust.eid); - rs = st.executeQuery(sql); - } catch (SQLException ex) { - ex.printStackTrace(); - model.addAttribute("PreviewTable", "Error select"); - return "json"; - } - if (rs != null) { - if (rs.next()) - exists = true; - } - st.close(); - } catch (SQLException ex) { - ex.printStackTrace(); - model.addAttribute("PreviewTable", "Error select 2"); - return "json"; - } + //Если те виды вредителей что нам нужны + if(!( + jsonObj.isNull("regionName") || + jsonObj.getString("regionName").isEmpty() || + locust.lat_center == null || + Double.parseDouble(locust.lat_center) == 0 || + locust.lon_center == null || + Double.parseDouble(locust.lon_center) == 0 || + locust.locust.equals("Совка ипсилон") || + locust.locust.equals("Восточный майский хрущ") || + locust.locust.equals("Кукурузный стеблевой мотылек") || + locust.locust.equals("Картофельная, или болотная совка, лиловатая яровая совка") || + locust.locust.equals("Луговой мотылёк") || + locust.locust.equals("Хлопковая совка") || + locust.locust.equals("Стеблевой кукурузный мотылек") || + locust.locust.equals("Восточная луговая совка") || + locust.locust.equals("Коричневая щитовка") || + locust.locust.equals("Продолговатая (чайная) подушечница") || + locust.locust.equals("Яблоневая запятовидная щитовка") || + locust.locust.equals("Виноградная филлоксера") || + locust.locust.equals("Бахчевая коровка") + )) { - PreparedStatement stmt = null; - sql = ""; - if (exists) { - System.out.println(String.valueOf(cntV)+") update main.frmlocust"); - sql = "update main.frmlocust set\n" - + " eid=?,\n" - + " lat_center=?,\n" - + " lon_center=?,\n" - + " country_id=?,\n" - + " region_id=?,\n" - + " area=?,\n" - + " district=?,\n" - + " date=TO_TIMESTAMP(?, 'DD.MM.YYYY HH24:MI'),\n" - + " description=?,\n" - + " geom=ST_SetSRID(ST_GeomFromGeoJSON(?),4326),\n" - + " locust_type_id=?,\n" - + " imago_density=?,\n" - + " kuliguli_density=?,\n" - + " larva_density=?,\n" - + " bio_hectare=?\n," - + " eggs_capsules_density=?,\n" - + " bio_biotope_id=?\n" - + " where eid='" + locust.eid + "'"; - try { - stmt = conn.prepareStatement(sql); - } catch (SQLException ex) { - ex.printStackTrace(); - model.addAttribute("PreviewTable", "Error prepare 1"); - return "json"; - } - } else { - System.out.println(String.valueOf(cntV)+") insert into main.frmlocust"); - sql = "insert into main.frmlocust(\n" - + " eid,\n" //1 - + " lat_center,\n" //2 - + " lon_center,\n" //3 - + " country_id,\n" //4 - + " region_id,\n" //5 - + " area,\n" //6 - + " district,\n" //7 - + " date,\n" //8 - + " description,\n" //9 - + " geom,\n" //10 - + " locust_type_id,\n" //11 - + " imago_density,\n" //12 - + " kuliguli_density,\n" //13 - + " larva_density,\n" //14 - + " bio_hectare,\n" //15 - + " eggs_capsules_density,\n" //16 - + " bio_biotope_id\n" //17 - + ")values(\n" - + " ?,\n" //1 eid - + " ?,\n" //2 lat1 - + " ?,\n" //3 lon1 - + " ?,\n" //4 country_id - + " ?,\n" //5 region_id - + " ?,\n" //6 district (area) - + " ?,\n" //7 district (area) - + " TO_TIMESTAMP(?, 'DD.MM.YYYY HH24:MI'),\n" //8 date - + " ?,\n" //9 description - + " ST_SetSRID(ST_GeomFromGeoJSON(?),4326),\n" //10 geom - + " ?,\n" //11 locust_type_id - + " ?,\n" //12 imago_density - + " ?,\n" //13 kuliguli_density - + " ?,\n" //14 larva_density - + " ?,\n" //15 - + " ?,\n" //16 - + " ?\n" //17 - + ")"; - try { - stmt = conn.prepareStatement(sql); - } catch (SQLException ex) { - ex.printStackTrace(); - model.addAttribute("PreviewTable", "Error prepare 2"); - return "json"; - } - } + //I check if there is already such a record in the database, if not, I insert it, if there is, I update it + UpdateLocust updateLocust = new UpdateLocust(jdbcTemplate); + if(updateLocust.update(locust)) + count++; + System.out.println("Count = "+count); - try { - stmt.setLong(1, Long.parseLong(locust.eid)); - stmt.setDouble(2, Double.parseDouble(locust.lat_center)); - stmt.setDouble(3, Double.parseDouble(locust.lon_center)); - stmt.setInt(4, 7); - stmt.setInt(5, Integer.parseInt(locust.region_id)); - stmt.setString(6, locust.district); //Пишу в поле "area" - stmt.setString(7, null); - stmt.setTimestamp(8, locust.date); - stmt.setString(9, locust.description); - stmt.setString(10, locust.drawPolygon); - stmt.setInt(11, Integer.parseInt(locust.locust_type_id)); - if (locust.imago_density != null) - stmt.setFloat(12, Float.parseFloat(locust.imago_density)); - else - stmt.setNull(12, Types.FLOAT); - if (locust.kuliguli_density != null) - stmt.setFloat(13, Float.parseFloat(locust.kuliguli_density)); - else - stmt.setNull(13, Types.FLOAT); - if (locust.larva_density != null) - stmt.setFloat(14, Float.parseFloat(locust.larva_density)); - else - stmt.setNull(14, Types.FLOAT); - if (locust.bio_hectare != null) - stmt.setFloat(15, Float.parseFloat(locust.bio_hectare)); - else - stmt.setNull(15, Types.FLOAT); - if (locust.eggs_capsules_density != null) - stmt.setFloat(16, Float.parseFloat(locust.eggs_capsules_density)); - else - stmt.setNull(16, Types.FLOAT); - if (locust.bio_biotope_id != null) - stmt.setFloat(17, Float.parseFloat(locust.bio_biotope_id)); - else - stmt.setNull(17, Types.INTEGER); - - } catch (SQLException ex) { - ex.printStackTrace(); - model.addAttribute("PreviewTable", "Error set data"); - return "json"; - } - //Выпоняю запрос на свтавку либо обновление - try { - stmt.execute(); - } catch (SQLException ex) { - ex.printStackTrace(); - model.addAttribute("PreviewTable", "Error execute 0"); - return "json"; - } - cntV++; - - //Скачиваю фотографию для анкеты + //Downloading photos from locust survey forms sb = new StringBuilder(); try { String strURL = "https://rscagex.ru/pmon/rest/getPhotosByParentId?token=QVOUE97HBSI6LBSGJGQZMP3KPSS1QK&parentId=" + locust.eid; - URL url = new URL(strURL); + URI uri = new URI(strURL); + URL url = uri.toURL(); HttpURLConnection hConn = (HttpURLConnection) url.openConnection(); hConn.setRequestMethod("GET"); - //hConn.setRequestProperty("Range","bytes=" + posStart + "-" + posEnd); hConn.connect(); int responseCode = hConn.getResponseCode(); if (responseCode / 100 == 2) //Code 206 is "Partial Content" { InputStream inputStream = hConn.getInputStream(); - //FileOutputStream outputStream = new FileOutputStream(strFile); int bytesRead; byte[] buffer = new byte[BUFFER_SIZE]; while ((bytesRead = inputStream.read(buffer)) != -1) { - //outputStream.write(buffer, 0, bytesRead); sb.append(new String(buffer, 0, bytesRead)); } - //outputStream.close(); inputStream.close(); } hConn.disconnect(); @@ -1294,12 +1056,12 @@ public class AcceptJSON implements ServletContextAware { String base64 = sb.toString(); if (base64.length() > 10) { - //Так как мне присылаю неправильный jSOM то пытаюсь его поправить,. + //Trying to fix the malformed JSON string received in base64 format... base64 = base64.replace("[", "[\""); base64 = base64.replace("]", "\"]"); base64 = base64.replace(", ", "\",\""); - String strFile = "O:\\temp\\CCALM\\ru_" + locust.eid; + String strFile = data_dir + "data/frmlocust/ru_" + locust.eid; //First pat of file name int pos = 0; JSONArray jsonArrayIMGS = new JSONArray(base64); @@ -1308,14 +1070,6 @@ public class AcceptJSON implements ServletContextAware { pos++; if (pos > 3) break; - //try { - // BufferedWriter writer = new BufferedWriter(new FileWriter(strFile)); - // writer.write(jsonObjIMG); - // writer.close(); - //} catch (IOException e1) { - // e1.printStackTrace(); - //} - byte[] decodedString = null; try { decodedString = Base64.getDecoder().decode(jsonObjIMG); @@ -1353,74 +1107,49 @@ public class AcceptJSON implements ServletContextAware { buffered.getGraphics().drawImage(img, 0, 0, null); } try { - //BufferedImage bIMG = toBufferedImage(img); ImageIO.write(buffered, "jpg", new File(strFile + "_" + pos + ".jpg")); } catch (IOException ex) { ex.printStackTrace(); } } - - if (pos > 3) { - break; - } else { - sql = ""; - if (pos == 1) { - sql = "update main.frmlocust set image_name1='ru_" + locust.eid + "_" + pos + ".jpg" + "' where eid=" + locust.eid; - } - if (pos == 2) { - sql = "update main.frmlocust set image_name2='ru_" + locust.eid + "_" + pos + ".jpg" + "' where eid=" + locust.eid; - } - if (pos == 3) { - sql = "update main.frmlocust set image_name3='ru_" + locust.eid + "_" + pos + ".jpg" + "' where eid=" + locust.eid; - } - if (sql.length() > 0) { - try { - st = conn.createStatement(); - st.execute(sql); - st.close(); - } catch (SQLException ex) { - ex.printStackTrace(); - model.addAttribute("PreviewTable", "Error update img"); - return "json"; - } - } + sql = ""; + if (pos == 1) { + sql = "update main.frmlocust set image_name1=:image_name where eid=:eid"; } + if (pos == 2) { + sql = "update main.frmlocust set image_name2=:image_name where eid=:eid"; + } + if (pos == 3) { + sql = "update main.frmlocust set image_name3=:image_name where eid=:eid"; + } + MapSqlParameterSource insertParams = new MapSqlParameterSource(); + insertParams.addValue("image_name", "ru_" + locust.eid + "_" + pos + ".jpg", Types.VARCHAR); + insertParams.addValue("eid", locust.eid, Types.INTEGER); + jdbcTemplate.update(sql, insertParams); + } } } } //equals("Вредители") - } - } - - table += row; } - System.out.println("Обработка = "+String.valueOf(cntO)); - System.out.println("Вредители = "+String.valueOf(cntV)); + //Save last download date + String insertSql = """ + insert into integration.days_ru(date, count) values (:date, :count) + """; + MapSqlParameterSource insertParams = new MapSqlParameterSource(); + insertParams.addValue("date", lastDate, Types.DATE); + insertParams.addValue("count", count, Types.INTEGER); + jdbcTemplate.update(insertSql, insertParams); - table += "
Ext idLatitude (Широта)Longitude (Долгота)Oblast (область)District (Район)Сельский округХозяйство или местностьВид саранчиФазаЭПВЗаселено ГаДата
" + String.valueOf(cntO+cntV) + "" + locust.eid + "" + locust.lat_center + "" + locust.lon_center + "" + regionName + " = " + locust.region_id + "" + locust.district + " = " + locust.district_id + "" + locust.terrain + "" + locust.village + "" + locust.locust + " = " + locust.locust_type_id + "" + locust.phase + " = " + locust.locust_have + "" + locust.evp + "" + locust.size + " = " + locust.drawPolygon + "" + locust.date + "
"; - - //json=json.substring(0,json.length()-1); //Удаляю последнюю запятую - //json+="\n]}"; - - //Сохраняю lastDate в базу данных - try { - Statement st = conn.createStatement(); - st.execute("insert into integration.days_ru(date,count)values('" + lastDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "'," + String.valueOf(cntV+cntO) + ");"); - st.close(); - } catch (SQLException ex) { - model.addAttribute("PreviewTable", "Error update img"); - throw new CustomException(200, 10000, ex.getMessage(),null,true); - } } } - model.addAttribute("PreviewTable",table); - //model.addAttribute("PreviewGEOJSON",json); - model.addAttribute("PreviewSQL",sqlData); + model.addAttribute("PreviewTable",""); + model.addAttribute("PreviewSQL",""); } catch (CustomException e) { if(e.isSaveToLog()) { @@ -1431,7 +1160,7 @@ public class AcceptJSON implements ServletContextAware { String uuid = UUID.randomUUID().toString(); logger.error(MarkerFactory.getMarker(uuid), ex.getMessage(), ex); } - return "json"; + return json.toString(); } //--------------------------------------------------------------------------- //https://127.0.0.1:8083/AcceptJSON_UZB diff --git a/src/main/resources/templates/json.html b/src/main/resources/templates/json.html deleted file mode 100644 index ec2b237..0000000 --- a/src/main/resources/templates/json.html +++ /dev/null @@ -1,25 +0,0 @@ -<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> - - - - Select the file with CSV to upload and parsing. - - - - -
- Select CSV UTF-8 file to upload and export: -
- -
-

- - ${PreviewTable} - - - - - \ No newline at end of file From e423647a597848ff785327832cf40f4d48b2f8fc Mon Sep 17 00:00:00 2001 From: igor Date: Fri, 8 Aug 2025 07:22:40 +0500 Subject: [PATCH 2/3] Delete Excel import --- .../org/ccalm/main/AcceptASDCController.java | 6 +- src/main/java/org/ccalm/main/AcceptEXCEL.java | 395 ------------------ .../resources/templates/engine/index.html | 2 +- 3 files changed, 5 insertions(+), 398 deletions(-) delete mode 100644 src/main/java/org/ccalm/main/AcceptEXCEL.java diff --git a/src/main/java/org/ccalm/main/AcceptASDCController.java b/src/main/java/org/ccalm/main/AcceptASDCController.java index 11f4479..2ba4128 100644 --- a/src/main/java/org/ccalm/main/AcceptASDCController.java +++ b/src/main/java/org/ccalm/main/AcceptASDCController.java @@ -2563,14 +2563,16 @@ public class AcceptASDCController implements ServletContextAware { String sql; if(pN.equals("frmlocustdel_locations") && days>0 && country_id>0){ + sql = "select fldl.* from main.frmlocustdel_locations fldl join main.frmlocustdel fld on main.strtouuid(fld.uid)=main.strtouuid(fldl.frmlocustdel_uid) where fld.date>now()-interval '"+String.valueOf(days)+" days' and fld.country_id="+String.valueOf(country_id)+" and fld.device_id!='"+device_id+"' order by fldl.seq"; //sql = "select fldl.* from main.frmlocustdel_locations fldl join main.frmlocustdel fld on main.strtouuid(fld.uid)=main.strtouuid(fldl.frmlocustdel_uid) where fld.date>now()-interval '"+String.valueOf(days)+" days' and fld.country_id="+String.valueOf(country_id)+" and fldl.seq>"+pS+" and fld.device_id!='"+device_id+"' order by fldl.seq"; - sql = "select fldl.* from main.frmlocustdel_locations fldl join main.frmlocustdel fld on main.strtouuid(fld.uid)=main.strtouuid(fldl.frmlocustdel_uid) where fld.date>now()-interval '"+String.valueOf(days)+" days' and fld.country_id="+String.valueOf(country_id)+" and fldl.seq>"+pS+" order by fldl.seq"; //TODO удалить + //sql = "select fldl.* from main.frmlocustdel_locations fldl join main.frmlocustdel fld on main.strtouuid(fld.uid)=main.strtouuid(fldl.frmlocustdel_uid) where fld.date>now()-interval '"+String.valueOf(days)+" days' and fld.country_id="+String.valueOf(country_id)+" and fldl.seq>"+pS+" order by fldl.seq"; //TODO удалить }else if(pN.equals("frmlocustdel") && days>0 && country_id>0){ Map fields = TCTableTools.getTableSchema(conn, "main.frmlocustdel"); STools.delFromMapByValue(fields, "geometry"); //Исключаем поля с типом геометрия, SQLite в android их не понимает + sql = "select "+String.join(", ", fields.keySet())+" from main.frmlocustdel where date>now()-interval '"+String.valueOf(days)+" days' and country_id="+String.valueOf(country_id)+" and device_id!='"+device_id+"' order by seq"; //sql = "select "+String.join(", ", fields.keySet())+" from main.frmlocustdel where date>now()-interval '"+String.valueOf(days)+" days' and country_id="+String.valueOf(country_id)+" and seq>"+pS+" and device_id!='"+device_id+"' order by seq"; - sql = "select "+String.join(", ", fields.keySet())+" from main.frmlocustdel where date>now()-interval '"+String.valueOf(days)+" days' and country_id="+String.valueOf(country_id)+" and seq>"+pS+" order by seq"; //TODO удалить + //sql = "select "+String.join(", ", fields.keySet())+" from main.frmlocustdel where date>now()-interval '"+String.valueOf(days)+" days' and country_id="+String.valueOf(country_id)+" and seq>"+pS+" order by seq"; //TODO удалить }else if(pN.equals("_translations")) { diff --git a/src/main/java/org/ccalm/main/AcceptEXCEL.java b/src/main/java/org/ccalm/main/AcceptEXCEL.java deleted file mode 100644 index 3478285..0000000 --- a/src/main/java/org/ccalm/main/AcceptEXCEL.java +++ /dev/null @@ -1,395 +0,0 @@ -package org.ccalm.main; - - - -import java.io.*; -//import java.io.FileInputStream; -//import java.io.OutputStream; -//import java.io.UnsupportedEncodingException; -//import java.nio.file.Paths; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.Properties; -//import java.util.zip.CRC32; -//import java.util.zip.Checksum; - -//import javax.servlet.ServletContext; -//import javax.servlet.http.Part; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; - -import jakarta.servlet.ServletContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.core.io.ClassPathResource; -//import org.apache.commons.io.FileUtils; -//import org.springframework.core.io.FileSystemResource; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.ModelAttribute; -//import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.context.ServletContextAware; -import org.springframework.web.multipart.MultipartFile; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; - -import tools.User; - - -@Controller -public class AcceptEXCEL implements ServletContextAware { - - private static final Logger logger = LoggerFactory.getLogger(AcceptEXCEL.class); - - private ServletContext context; - - @Value("${spring.datasource.url}") - String db_url=""; - @Value("${spring.datasource.username}") - String db_login=""; - @Value("${spring.datasource.password}") - String db_password=""; - @Value("${data.dir}") - String data_dir = ""; - - @Override - public void setServletContext(ServletContext context) { - this.context=context; - } - - @RequestMapping(value = {"/AcceptCSV", "/api/locust/v01/"}, method = { RequestMethod.GET, RequestMethod.POST }) - public String acceptCSV(@ModelAttribute User user, Model model,@RequestParam(required=false,name="file") MultipartFile file,@RequestParam(required=false,name="skip",defaultValue = "0") Boolean skip) { - - Connection conn = null; - try { - Class.forName("org.postgresql.Driver"); - conn = DriverManager.getConnection(db_url, db_login, db_password); - } catch (Exception ex) { - - } - - String sqlData=""; - String json="{\"type\": \"FeatureCollection\",\"features\":["; - - - String table=""; - table+=""; - - //row+=""; - if(error==true || locust.lat==0 || locust.lon==0 || locust.region_id.isEmpty() || locust.district_id.isEmpty() || locust.terrain.isEmpty() || locust.locust_id.isEmpty()) { - row=""; - } - row+=""; - row+="\n"; - - - - table += row; - i++; - - json+=" \n{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":["+locust.lon+","+locust.lat+"]},\"properties\":{\"oblast\":\""+locust.region.replace("\"", "'")+"\",\"district\":\""+locust.district.replace("\"", "'")+"\",\"region\":\""+locust.terrain.replace("\"", "'")+"\",\"village\":\""+locust.village.replace("\"", "'")+"\",\"phase\":\""+locust.phase+"\",\"locust\":\""+locust.locust+"\",\"evp\": \""+locust.evp+"\",\"ga\": \""+locust.size+"\",\"date\": \""+locust.date+"\"}},"; - - sqlData+="insert into main.frmlocust(country_id,region_id,district,terrain,village,lon1,lat1,locust_type_id,locust_have,locust_populated,date)values(5,"+locust.region_id+",'"+locust.district.trim()+"','"+locust.terrain.trim()+"','"+locust.village.trim()+"',"+locust.lon+","+locust.lat+","+locust.locust_id+","+locust.locust_have+","+locust.size+",TO_DATE('"+locust.date+"','DD.MM.YYYY'));\n"; - - if(i>5000) break; - line=reader.readLine(); - } - reader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - - - - }else { - table="CSV file is empty! "+skip; - } - table += "
Latitude (Широта)Longitude (Долгота)Oblast (область)District (Район)Сельский округХозяйство или местностьВид саранчиФазаЭПВЗаселено ГаДата
"+i+""+locust.lat+""+locust.lon+""+locust.region+" = "+locust.region_id+""+locust.district+" = "+locust.district_id+""+locust.terrain+""+locust.village+""; - - if (file!=null && !file.isEmpty()) { - - BufferedReader reader; - try { - int i=1; //for testing - - reader = new BufferedReader(new InputStreamReader(file.getInputStream(), "UTF-8")); - //reader = new BufferedReader(new FileReader(file)); - - String line = reader.readLine(); - if(skip) line = reader.readLine(); - while (line != null) { - - StringBuffer data=new StringBuffer(line); - Boolean error=false; - - Locust locust = new Locust(); - try { - String lat=CutBeforeFirst(data,";"); - if(lat.equals("46.3104.6")) lat="46.31046"; - if(lat.equals("43.21303.")) lat="43.21303"; - if(lat.equals("43.26067.")) lat="43.26067"; - if(lat.equals("43.20181.")) lat="43.20181"; - if(lat.equals("43.20181.")) lat="43.20181"; - if(lat.equals("43.74691.")) lat="43.74691"; - if(lat.equals("43.41954.")) lat="43.41954"; - if(lat.equals("43.78288.")) lat="43.78288"; - if(lat.equals("43.26260.")) lat="43.26260"; - if(lat.equals("43.79702.")) lat="43.79702"; - if(lat.equals("43.64891.")) lat="43.64891"; - if(lat.equals("43.64891.")) lat="43.64891"; - if(lat.equals("43.42271.")) lat="43.42271"; - if(lat.equals("43.64891.")) lat="43.64891"; - if(lat.equals("43.89990.")) lat="43.89990"; - if(lat.equals("43.96273.")) lat="43.96273"; - if(lat.equals("43.26907.")) lat="43.26907"; - if(lat.equals("43.26630.")) lat="43.26630"; - if(lat.equals("43.50605.")) lat="43.50605"; - if(lat.equals("43.74965.")) lat="43.74965"; - if(lat.equals("43.20813.")) lat="43.20813"; - if(lat.equals("43.23298.")) lat="43.23298"; - if(lat.equals("43.74774.")) lat="43.74774"; - if(lat.equals("43.77144.")) lat="43.77144"; - if(lat.equals("43.58847.")) lat="43.58847"; - if(lat.equals("43.58944.")) lat="43.58944"; - if(lat.equals("4342755.")) lat="43.42755"; - if(lat.equals("43.80416.")) lat="43.80416"; - if(lat.equals("43.79536.")) lat="43.79536"; - if(lat.equals("50.75 767")) lat="50.75767"; - if(lat.equals("50.77 542")) lat="50.77542"; - if(lat.equals("50.85 140")) lat="50.85140"; - if(lat.equals("50.79 773")) lat="50.79773"; - if(lat.equals("50.63 469")) lat="50.63469"; - if(lat.equals("51.23 130")) lat="51.23130"; - if(lat.equals("51.03 220")) lat="51.03220"; - if(lat.equals("51.38 922")) lat="51.38922"; - if(lat.equals("51.06.940")) lat="51.06940"; - if(lat.equals("51.08 273")) lat="51.08273"; - if(lat.equals("50.96 705")) lat="50.96705"; - if(lat.equals("51.03 021")) lat="51.03021"; - if(lat.equals("51.01 764")) lat="51.01764"; - if(lat.equals("50.99 388")) lat="50.99388"; - if(lat.equals("50.50 509")) lat="50.50509"; - if(lat.equals("43.109.94")) lat="43.10994"; - if(lat.equals("50.11.926")) lat="50.11926"; - if(lat.equals("50.04.966")) lat="50.04966"; - if(lat.equals("49.26.385")) lat="49.26385"; - if(lat.equals("49.26.251")) lat="49.26251"; - if(lat.equals("49.25.307")) lat="49.25307"; - if(lat.equals("44.4930.")) lat="49.25307"; - - locust.lat=Double.parseDouble(lat); - - String lon=CutBeforeFirst(data,";"); - if(lon.equals("51.25 560")) lon="51.25560"; - if(lon.equals("51.25 099")) lon="51.25099"; - if(lon.equals("51.26 378")) lon="51.26378"; - if(lon.equals("51.25 235")) lon="51.25235"; - if(lon.equals("51.83 107")) lon="51.83107"; - if(lon.equals("51.71 702")) lon="51.71702"; - if(lon.equals("52.21 390")) lon="52.21390"; - if(lon.equals("52.10 873")) lon="52.10873"; - if(lon.equals("51.85 606")) lon="51.85606"; - if(lon.equals("52.41 085")) lon="52.41085"; - if(lon.equals("52.36 125")) lon="52.36125"; - if(lon.equals("51. 56 025")) lon="51.56025"; - if(lon.equals("51.56 786")) lon="51.56786"; - if(lon.equals("51.57 946")) lon="51.57946"; - if(lon.equals("51.16 758")) lon="51.16758"; - if(lon.equals("85.09.142")) lon="85.09142"; - - - locust.lon=Double.parseDouble(lon); - - }catch(Exception ex) - { - error=true; - } - - locust.region=CutBeforeFirst(data,";"); - - //Выбираю ID области - Statement stt = null; - ResultSet rs = null; - try { - - if(locust.region.equals("Алматинский")) locust.region="Алматинская"; - if(locust.region.equals("Туркестанский")) locust.region="Туркестанская"; - - stt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); - String sql_query = "select id from main.countriesregions where name like '%"+locust.region+"%';"; - rs = stt.executeQuery(sql_query); - if (rs != null) { - try { - if (rs.next()) { - locust.region_id=rs.getString(1); - } - rs.close(); - } catch (SQLException ex) { - } - } - } catch (SQLException ex) { - - }finally { - if(rs!=null) try{rs.close();}catch(SQLException ex){} - if(stt!=null) try{stt.close();}catch(SQLException ex){} - } - - //Выбираю id региона (main.countriesdistricts) - locust.district_id=""; - locust.district=CutBeforeFirst(data,";"); - stt = null; - rs = null; - try { - - if(locust.district.equals("Сарканский ")) locust.district="Саркандский"; - if(locust.district.equals("Уйгуский")) locust.district="Уйгурский"; - if(locust.district.equals("г.Капшагай")) locust.district="Капчагайский городской округ"; - if(locust.district.equals("Каратальскиий")) locust.district="Каратальский"; - if(locust.district.equals("г. Талдыкорган")) locust.district="Талдыкорганский"; - if(locust.district.equals("г Атырау ")) locust.district="Атырауский городской округ"; - if(locust.district.equals("г. Атырау")) locust.district="Атырауский городской округ"; - if(locust.district.equals("Кызылкуга")) locust.district="Кзылкогинский район"; - if(locust.district.equals("Курчумский ")) locust.district="Куршимский район"; - if(locust.district.equals("г.Семей")) locust.district="Семипалатинский городской округ"; - if(locust.region_id.equals("4") && locust.district.equals("Жамбылский")) locust.district="Джамбулский район"; - if(locust.district.equals("Т.Рыскуловский")) locust.district="Рыскуловский район"; - if(locust.district.equals("Шуйский")) locust.district="Чуйский район"; - if(locust.district.equals("Сарысуский")) locust.district="Сары-Суйский район"; - if(locust.district.equals("Федоровский")) locust.district="Фёдоровский район"; - if(locust.district.equals("Жангельдинский")) locust.district="Джангельдинский район"; - if(locust.district.equals("Сырдария")) locust.district="Сырдарьинский район"; - if(locust.district.equals("Кызылорда")) locust.district="Кызылординский городской округ"; - if(locust.district.equals("к.Кызылорда")) locust.district="Кызылординский городской округ"; - if(locust.district.equals("Аралский")) locust.district="Аральский район"; - if(locust.district.equals("Шиелі")) locust.district="Шиелийский район"; - if(locust.region_id.equals("11") && locust.district.equals("Аксуский")) locust.district="Аксуйский городской округ"; - if(locust.region_id.equals("11") && locust.district.equals("Аксуский")) locust.district="Аксуйский городской округ"; - if(locust.region_id.equals("11") && locust.district.equals("Ақсуский")) locust.district="Аксуйский городской округ"; - if(locust.district.equals("Аққулы")) locust.district="Аккулинский район"; - if(locust.district.equals("Аккулы")) locust.district="Аккулинский район"; - if(locust.district.equals("Тереңкөл")) locust.district="Теренкольский"; - if(locust.district.equals("г. Павлодар")) locust.district="Павлодарский городской округ"; - if(locust.district.equals("Екибастузский")) locust.district="Экибастузский городской округ"; - if(locust.district.equals("Шербактнский")) locust.district="Щербактинский район"; - if(locust.district.equals("Толебиский ")) locust.district="Толебийский район"; - if(locust.district.equals("г.Шымкент Абайский ")) locust.district="Шымкентский городской округ"; - if(locust.district.equals("г.Шымкент Каратауский ")) locust.district="Шымкентский городской округ"; - if(locust.district.equals("Баянауыл")) locust.district="Баянаул"; - if(locust.district.equals("Екібастұз")) locust.district="Экибастуз"; - - - - - stt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); - String sql_query = "select id from main.countriesdistricts where region_id="+locust.region_id+" and name like '%"+locust.district+"%';"; - rs = stt.executeQuery(sql_query); - if (rs != null) { - try { - if (rs.next()) { - locust.district_id=rs.getString(1); - } - rs.close(); - } catch (SQLException ex) { - } - } - } catch (SQLException ex) { - - }finally { - if(rs!=null) try{rs.close();}catch(SQLException ex){} - if(stt!=null) try{stt.close();}catch(SQLException ex){} - } - - //Сельский округ - locust.terrain=CutBeforeFirst(data,";"); - //Хозяйство или местность - locust.village=CutBeforeFirst(data,";"); - //Вид саранчи - locust.locust=CutBeforeFirst(data,";"); - locust.locust_id=""; - if(locust.locust.equals(" итальянский прус")) locust.locust_id="1"; - if(locust.locust.equals("итальянский прус")) locust.locust_id="1"; - if(locust.locust.equals("Итальянский прус")) locust.locust_id="1"; - if(locust.locust.equals("итальянский прус ")) locust.locust_id="1"; - if(locust.locust.equals("Азиатская саранча")) locust.locust_id="3"; - if(locust.locust.equals("азиатская саранча")) locust.locust_id="3"; - if(locust.locust.equals("нестадные")) locust.locust_id="4"; - if(locust.locust.equals("нестадные саранчовые")) locust.locust_id="4"; - if(locust.locust.equals("Нестадные саранчевые")) locust.locust_id="4"; - if(locust.locust.equals("Нестадная саранча ")) locust.locust_id="4"; - if(locust.locust.equals("Нестадная саранча")) locust.locust_id="4"; - if(locust.locust.equals("нестадная саранча")) locust.locust_id="4"; - if(locust.locust.equals("Мароккская саранча")) locust.locust_id="2"; - - //фаза саранчи - locust.phase=CutBeforeFirst(data,";"); - locust.locust_have="3"; - if(locust.phase.equals("кубышки")) locust.locust_have="2"; - if(locust.phase.equals("личинки")) locust.locust_have="3"; - if(locust.phase.equals("имаго")) locust.locust_have="5"; - - locust.evp=CutBeforeFirst(data,";"); //ЭФП - locust.size=CutBeforeFirst(data,";"); //Заселённая площадь - locust.size=locust.size.replace(",","."); - locust.date=CutBeforeFirst(data,";"); //Дата - - String row="
"+i+""+locust.lat+""+locust.lon+""+locust.region+" = "+locust.region_id+""+locust.district+" = "+locust.district_id+""+locust.terrain+""+locust.village+""+locust.locust+" = "+locust.locust_id+""+locust.phase+" = "+locust.locust_have+""+locust.evp+""+locust.size+""+locust.date+"
"; - - json=json.substring(0,json.length()-1); //Удаляю последнюю запятую - json+="\n]}"; - - model.addAttribute("PreviewTable",table); - model.addAttribute("PreviewGEOJSON",json); - model.addAttribute("PreviewSQL",sqlData); - - return "excel"; - - } - //--------------------------------------------------------------------------- - 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; - } - //--------------------------------------------------------------------------- - class Locust{ - double lon; - double lat; - String region; //Область - String region_id; - - String district; //Район - String district_id; - - String terrain; //Название месности - - String village; //Хозяйство или местность - - String locust; //Вид саранчи - String locust_id; //Вид саранчи - - String phase; //Фаза саранчи - String locust_have; //id Фазы саранчи - String evp; //ЭФП - String size; //Заселённая площадь - String date; //Дата - - } -} diff --git a/src/main/resources/templates/engine/index.html b/src/main/resources/templates/engine/index.html index f1d7b67..26d7552 100644 --- a/src/main/resources/templates/engine/index.html +++ b/src/main/resources/templates/engine/index.html @@ -1915,7 +1915,7 @@ map.on('singleclick', function(evt){ //map.on('click', function(evt){ '' + trt('Creator') + ': ' + feature.userName + '
' + '' + trt('Radius') + ': ' + feature.userRadius + 'm.
' + '' + trt('Want_air_temperature') + ': ' + feature.userTemperature_air + '°C
' + - '' + trt('Want_soil_temperature') + ': ' + feature.userTemperature_air + '°C'; + '' + trt('Want_soil_temperature') + ': ' + feature.userTemperature_soil + '°C'; content.innerHTML += '
'; overlay.setPosition(coordinates); }else{ From 9f06a550fc4cc3485c15a5461926f5ac8cde4046 Mon Sep 17 00:00:00 2001 From: igor Date: Mon, 11 Aug 2025 07:20:50 +0500 Subject: [PATCH 3/3] Repair send threshold warnings for soil and air temperature --- org-ccalm-main.yml | 4 ++-- src/main/java/org/ccalm/main/SendWarning.java | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/org-ccalm-main.yml b/org-ccalm-main.yml index 8b9683f..f73da5a 100644 --- a/org-ccalm-main.yml +++ b/org-ccalm-main.yml @@ -10,9 +10,9 @@ spring: application: name: org-ccalm-main datasource: - #url: jdbc:postgresql://almaty.ccalm.org:5432/CCALM?ApplicationName=org_ccalm_main&ssl=true&sslmode=require&connectTimeout=10000&socketTimeout=10000 + url: jdbc:postgresql://almaty.ccalm.org:5432/CCALM?ApplicationName=org_ccalm_main&ssl=true&sslmode=require&connectTimeout=10000&socketTimeout=10000 #url: jdbc:postgresql://ccalm.org:5432/CCALM?ApplicationName=org_ccalm_main&ssl=true&sslmode=require&connectTimeout=10000&socketTimeout=10000 - url: jdbc:postgresql://127.0.0.1:5432/CCALM?ApplicationName=org_ccalm_main&ssl=true&sslmode=require&connectTimeout=10000&socketTimeout=10000 + #url: jdbc:postgresql://127.0.0.1:5432/CCALM?ApplicationName=org_ccalm_main&ssl=true&sslmode=require&connectTimeout=10000&socketTimeout=10000 username: postgres password: 309A86FF65A78FB428F4E38DFE35F730 driver-class-name: org.postgresql.Driver diff --git a/src/main/java/org/ccalm/main/SendWarning.java b/src/main/java/org/ccalm/main/SendWarning.java index 855f750..b8a76fb 100644 --- a/src/main/java/org/ccalm/main/SendWarning.java +++ b/src/main/java/org/ccalm/main/SendWarning.java @@ -147,7 +147,7 @@ public class SendWarning { JSONObject tmp_air = getAirTemperature(obj.getDouble("lat"),obj.getDouble("lon")); JSONObject tmp_soil = getSoilTemperature(obj.getDouble("lat"),obj.getDouble("lon")); - if(tmp_air!=null && obj.getBoolean("warn_air")) { + if(tmp_air!=null && obj.getBoolean("warn_air") && !obj.isNull("temperature_air")) { Translation trt=new Translation(obj.getString("short_name"),jdbcTemplate); double value = obj.getDouble("temperature_air"); if (value < tmp_air.getDouble("value")) { @@ -169,7 +169,7 @@ public class SendWarning { .append("") .append("
") .append("

").append(trt.trt(true,"Warning")).append("

") - .append("

").append(trt.trt(true,"The_soil_temperature_has_exceeded_the_set_value")).append(": ").append(value).append("

") + .append("

").append(trt.trt(true,"The_air_temperature_has_exceeded_the_set_value")).append(": ").append(value).append("

") .append("") .append("") .append("") @@ -192,7 +192,9 @@ public class SendWarning { main.frmcheckpoints set warn_air=false - where uid=CAST(:uid AS uuid) + where + uid=CAST(:uid AS uuid) + and (warn_hold!=true or warn_hold is null) """; MapSqlParameterSource param = new MapSqlParameterSource(); param.addValue("uid", obj.getString("uid"), Types.VARCHAR); @@ -202,7 +204,7 @@ public class SendWarning { } } - if(tmp_soil!=null && obj.getBoolean("warn_soil")) { + if(tmp_soil!=null && obj.getBoolean("warn_soil") && !obj.isNull("temperature_soil")) { Translation trt=new Translation(obj.getString("short_name"),jdbcTemplate); double value = obj.getDouble("temperature_soil"); if (value < tmp_soil.getDouble("value")) { @@ -247,7 +249,9 @@ public class SendWarning { main.frmcheckpoints set warn_soil=false - where uid=CAST(:uid AS uuid) + where + uid=CAST(:uid AS uuid) + and (warn_hold!=true or warn_hold is null) """; MapSqlParameterSource param = new MapSqlParameterSource(); param.addValue("uid", obj.getString("uid"), Types.VARCHAR); @@ -260,6 +264,7 @@ public class SendWarning { } catch (Exception ex) { String uuid = UUID.randomUUID().toString(); logger.error(MarkerFactory.getMarker(uuid),ex.getMessage(), ex); + result.put("error_code", 10000); } return new ResponseEntity<>(result, HttpStatus.OK); }
").append(trt.trt(true,"Date")).append("").append(tmp_air.getString("date")).append("
").append(trt.trt(true,"Value")).append("").append(tmp_air.getDouble("value")).append(" °C