diff --git a/pom.xml b/pom.xml index 7c18227..505371a 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ org.ccalm main - 1.0.2 + 1.0.3 main main diff --git a/src/main/java/org/ccalm/main/AcceptJSON.java b/src/main/java/org/ccalm/main/AcceptJSON.java index d2dc50c..b5db563 100644 --- a/src/main/java/org/ccalm/main/AcceptJSON.java +++ b/src/main/java/org/ccalm/main/AcceptJSON.java @@ -23,6 +23,7 @@ import java.sql.Types; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.Year; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; @@ -132,316 +133,295 @@ public class AcceptJSON implements ServletContextAware { @RequestParam(required=false,name="file") MultipartFile file, @RequestParam(required=false,name="skip",defaultValue = "0") Boolean skip ) { - int cnt=0; - + JSONObject json = new JSONObject(); try{ - //TODO add user verification: that he is authorized and that he is an administrator! - //if(user.id==null || user.id.equals("null") || user.id.isBlank()) - // return "Please log in!"; - - Connection conn = null; - try { - Class.forName("org.postgresql.Driver"); - conn = DriverManager.getConnection(db_url, db_login, db_password); - } catch (Exception ex) { - ex.printStackTrace(); + LocalDate lastDate = null; + String sql= """ + select to_char(max(date), 'YYYY-MM-DD') as date from integration.days_kz + """; + MapSqlParameterSource parameters = new MapSqlParameterSource(); + List ret = jdbcTemplate.query(sql, parameters, new DBTools.JsonRowMapper()); + List data = new ArrayList<>(); + for (String s : ret) { + JSONObject obj = new JSONObject(s); + if(!obj.isNull("date")) { + lastDate = LocalDate.parse(obj.getString("date")); + } + } + if(lastDate==null) { + lastDate = LocalDate.parse("2025-07-16"); } - StringBuilder sb = new StringBuilder(1024); - try - { - //String strURL="http://91.185.13.233:98/get_fito"; - String strURL="http://95.56.225.145:98/get_fito"; - URL url = new URL(strURL); - HttpURLConnection hConn = (HttpURLConnection) url.openConnection(); - hConn.setRequestMethod("GET"); - hConn.setRequestProperty("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEyMyIsInVzZXJuYW1lIjoiQ0NBTE0iLCJyb2xlIjoidXNlciIsImlhdCI6MTcwNjc4NjQ0Nn0.bypDbAZYjbQu8hWrb6MnnAZ80AbO4ZifP3inEnpbabI"); + //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))) { + lastDate = lastDate.plusDays(1); - hConn.setConnectTimeout(60000); // 60 сек + String dateBegin = lastDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); + String dateEnd = dateBegin; - hConn.connect(); - int responseCode = hConn.getResponseCode(); - if (responseCode / 100 == 2) //Code 206 is "Partial Content" - { - InputStreamReader inputStream = new InputStreamReader(hConn.getInputStream(), "UTF-8"); - int bytesRead; - char[] buffer = new char[BUFFER_SIZE]; - while ((bytesRead = inputStream.read(buffer)) != -1) { - sb.append(new String(buffer,0,bytesRead)); + System.out.println("date = " + dateEnd); + + StringBuilder sb = new StringBuilder(1024); + try { + + String strURL = "http://95.56.225.145:98/get_fito?start_date="+dateBegin+"&end_date="+dateEnd; + + URI uri = new URI(strURL); + URL url = uri.toURL(); + HttpURLConnection hConn = (HttpURLConnection) url.openConnection(); + hConn.setRequestMethod("GET"); + hConn.setRequestProperty("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEyMyIsInVzZXJuYW1lIjoiQ0NBTE0iLCJyb2xlIjoidXNlciIsImlhdCI6MTcwNjc4NjQ0Nn0.bypDbAZYjbQu8hWrb6MnnAZ80AbO4ZifP3inEnpbabI"); + + hConn.setConnectTimeout(60000); // 60 sec + + hConn.connect(); + int responseCode = hConn.getResponseCode(); + if (responseCode / 100 == 2) //Code 206 is "Partial Content" + { + InputStreamReader inputStream = new InputStreamReader(hConn.getInputStream(), "UTF-8"); + int bytesRead; + char[] buffer = new char[BUFFER_SIZE]; + while ((bytesRead = inputStream.read(buffer)) != -1) { + sb.append(new String(buffer, 0, bytesRead)); + } + inputStream.close(); } - inputStream.close(); - } - }catch (IOException e) - { - e.printStackTrace(); - } - - String content=sb.toString(); - - - int year=2025; - - - if(content==null || content.isEmpty()) { - System.out.println("Content is null"); - }else { - System.out.println("Start"); - - String sql="delete from main.frmlocust where country_id=5 and eid=0 and extract(year from date)="+String.valueOf(year); - try{ - PreparedStatement stmt = conn.prepareStatement(sql); - int cont = stmt.executeUpdate(); - System.out.println("cnt: " + String.valueOf(cont)); - } catch (SQLException e) { - e.printStackTrace(); + } catch (IOException e) { + throw new CustomException(200, 10000, "Error download: "+e.getMessage(), null, true); } - //System.out.println(content); - //Iterate over the elements of the array. - JSONObject jsonObject= new JSONObject(content); - JSONArray jsonArray = jsonObject.getJSONArray("data"); - for (Object o : jsonArray) { - JSONObject jsonObj = (JSONObject) o; + String content = sb.toString(); - //I don't miss other dates - ZonedDateTime dateTime = ZonedDateTime.parse(jsonObj.getString("examination_date"), DateTimeFormatter.ISO_ZONED_DATE_TIME); - if(dateTime.getYear()!=year) - continue; + int count=0; + if (content != null || !content.isEmpty()) { + JSONObject jsonObject = new JSONObject(content); + JSONArray jsonArray = jsonObject.getJSONArray("data"); + for (Object o : jsonArray) { + JSONObject jsonObj = (JSONObject) o; - sql=""" - insert into main.frmlocust( - eid, --1 - lat_center, --2 - lon_center, --3 - lat1, --4 - lon1, --5 - date, --6 - village, --7 - terrain, --8 - district, --9 - country_id, --10 - region_id, --11 - locust_type_id, --12 - bio_hectare, --13 - locust_populated, --14 - description, --15 - eggs, --16 - hoppers, --17 - bands, --18 - adults, --19 - swarms --20 - )values( - ?, --1 eid - ?, --2 lat_center - ?, --3 lon_center - ?, --4 lat1 - ?, --5 lon1 - TO_TIMESTAMP(?, 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"'), --6 date - LEFT(?, 100), --7 village - LEFT(?, 100), --8 terrain - LEFT(?, 100), --9 district - ?, --10 country_id - ?, --11 region_id - ?, --12 locust_type_id - ?, --13 bio_hectare - ?, --14 locust_populated - ?, --15 description - ?, --16 eggs CheckBox - ?, --17 hoppers CheckBox - ?, --18 bands CheckBox - ?, --19 adults CheckBox - ? --20 swarms CheckBox - )"""; - PreparedStatement stmt=null; - try { - stmt = conn.prepareStatement(sql); - } catch (SQLException ex) { - ex.printStackTrace(); - model.addAttribute("PreviewTable","Error prepare 2"); - return "json"; + FrmLocustModel locust = convertKAZJSONToLocustModel(jsonObj); + + //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++; + }else{ + throw new CustomException(200, 10000, "Error insert or update", null, true); + } + System.out.println( "count = " + count ); } - - String description=""; - description += "city: "+jsonObj.getString("city")+"\n"; - description += "message_type: "+jsonObj.getString("message_type")+"\n"; - if(jsonObj.has("development_stage") && !jsonObj.isNull("development_stage")) - description += "stage: "+jsonObj.getString("development_stage")+"\n"; - - try { - stmt.setLong(1, 0); - stmt.setDouble(2,jsonObj.getDouble("latitude")); - stmt.setDouble(3,jsonObj.getDouble("longitude")); - stmt.setDouble(4,jsonObj.getDouble("latitude")); - stmt.setDouble(5,jsonObj.getDouble("longitude")); - stmt.setString(6,jsonObj.getString("examination_date")); //2023-03-20T06:23:20.000Z - if(jsonObj.has("village") && !jsonObj.isNull("village")) - stmt.setString(7,jsonObj.getString("village")); - else - stmt.setNull(7, Types.VARCHAR); - if(jsonObj.has("farm") && !jsonObj.isNull("farm")) - stmt.setString(8,jsonObj.getString("farm")); - else - stmt.setNull(8, Types.VARCHAR); - if(jsonObj.has("district") && !jsonObj.isNull("district")) - stmt.setString(9,jsonObj.getString("district")); - else - stmt.setNull(9, Types.VARCHAR); - - stmt.setInt(10,5); //country_id - - - int region_id=-1; - switch (jsonObj.getString("region")) { - case "Туркестанская": //5 false -215739 1691403679 5 "Түркістан облысы (Туркестанская область)" - region_id=5; - break; - case "Жамбылская": //4 false -215722 1691403679 5 "Жамбыл облысы (Жамбылская область)" - region_id=4; - break; - case "Алматинская": //3 false -215718 1691403679 5 "Алматы облысы (Алматинская область)" - region_id=3; - break; - case "Карагандинская": //2 false -215776 1691403679 5 "Қарағанды облысы (Карагандинская область)" - region_id=2; - break; - case "Жетысу": //478 false -14312169 1691403679 5 "Жетісу облысы" - region_id=478; - break; - case "Кызылординская": //6 false -215727 1691403679 5 "Қызылорда облысы (Кызылординская область)" - region_id=6; - break; - case "Атырауская": //13 false -214834 1691403679 5 "Атырау облысы (Атырауская область)" - region_id=13; - break; - case "Западно-Казахстанская": //12 false -215441 1691403679 5 "Батыс Қазақстан облысы (Западно-Казахстанская область)" - region_id=12; - break; - case "Акмолинская": //10 false -215743 1691403679 5 "Ақмола облысы (Акмолинская область)" - region_id=10; - break; - case "Восточно-Казахстанская": //1 false -215699 1691403679 5 "Шығыс Қазақстан облысы (Восточно-Казахстанская область)" - region_id=1; - break; - case "Абай": //477 false -14243026 1691473439 5 "Абай облысы" - region_id=477; - break; - case "Костанайская": //8 false -1288730 1691403679 5 "Қостанай облысы (Костанайская область)" - region_id=8; - break; - case "Павлодарская": //11 false -215772 1691403679 5 "Павлодар облысы (Павлодарская область)" - region_id=11; - break; - case "Северо-Казахстанская": //9 false -215760 1691403679 5 "Солтүстік Қазақстан облысы (Северо-Казахстанская область)" - region_id=9; - break; - case "Актюбинская": //7 false -215683 1691403679 5 "Ақтөбе облысы (Актюбинская область)" - region_id=7; - break; - case "Мангистауская": //14 false -215686 1691403679 5 "Маңғыстау облысы (Мангистауская область)" - region_id=14; - break; - case "Улытау": //479 false -14312737 1691462768 5 "Ұлытау облысы" - region_id=14; - break; - default: - throw new CustomException(200, 10000, "Not find region: " + jsonObj.getString("region"), null, true); - } - if(region_id==-1) - stmt.setInt(11,region_id); //region_id - else - stmt.setNull(11, Types.INTEGER); - - //Вид саранчи - int locust_id=-1; - switch (jsonObj.getString("grasshopper_type")) { - case "Мароккская саранча": - locust_id=2; - break; - case "Итальянский прус": - locust_id=1; - break; - case "Азиатская саранча": - locust_id=3; - break; - default: - throw new CustomException(200, 10000, "Not grasshopper_type: " + jsonObj.getString("grasshopper_type"), null, true); - } - stmt.setInt(12,locust_id); //locust_id - - //Обследованно - if(jsonObj.has("examination_area") && !jsonObj.isNull("examination_area")) - stmt.setDouble(13,Double.parseDouble(jsonObj.get("examination_area").toString())); - else - stmt.setNull(13, Types.DOUBLE); - - //Площадь заселения - if(jsonObj.has("settlement_area") && !jsonObj.isNull("settlement_area")) - stmt.setDouble(14,Double.parseDouble(jsonObj.get("settlement_area").toString())); - else - stmt.setNull(14, Types.DOUBLE); - - //Прочее описание - stmt.setString(15,description); //description - - boolean eggs=false; // Яйца - boolean hoppers=false; // Личинки - boolean bands=false; // Кулиги - boolean adults=false; // Имаго - boolean swarms=false; // Стаи - if(jsonObj.has("development_stage") && !jsonObj.isNull("development_stage")) { - if (jsonObj.getString("development_stage").equals("Яйца")) eggs = true; - if (jsonObj.getString("development_stage").equals("Личинки") || jsonObj.getString("development_stage").equals("Заселенность личинками") || jsonObj.getString("development_stage").equals("Личинка")) hoppers = true; - if (jsonObj.getString("development_stage").equals("Кулиги")) bands = true; - if (jsonObj.getString("development_stage").equals("Имаго")) adults = true; - if (jsonObj.getString("development_stage").equals("Стаи")) swarms = true; - } - if(eggs) - stmt.setBoolean(16,true); - else - stmt.setNull(16, Types.BOOLEAN); - if(hoppers) - stmt.setBoolean(17,true); - else - stmt.setNull(17, Types.BOOLEAN); - if(bands) - stmt.setBoolean(18,true); - else - stmt.setNull(18, Types.BOOLEAN); - if(adults) - stmt.setBoolean(19,true); - else - stmt.setNull(19, Types.BOOLEAN); - if(swarms) - stmt.setBoolean(20,true); - else - stmt.setNull(20, Types.BOOLEAN); - - if(region_id!=-1) { - stmt.execute(); - }else { - System.out.println("region: "+jsonObj.getString("region")); - } - } catch (SQLException ex) { - ex.printStackTrace(); - model.addAttribute("PreviewTable","Error set data"); - return "json"; - } - cnt++; + }else{ + throw new CustomException(200, 10000, "Data content kz is null", null, true); } - System.out.println("Proc count = "+String.valueOf(cnt)); + //Save last download date + sql = "INSERT INTO integration.days_kz(date, count) VALUES (:date, :count)"; + parameters = new MapSqlParameterSource(); + parameters.addValue("date", java.sql.Date.valueOf(lastDate)); + parameters.addValue("count", count); + jdbcTemplate.update(sql, parameters); } } catch (CustomException e) { if(e.isSaveToLog()) { String uuid = UUID.randomUUID().toString(); logger.error(MarkerFactory.getMarker(uuid), e.getMessage(), e); } + json = e.getJson(); } catch (Exception ex) { String uuid = UUID.randomUUID().toString(); logger.error(MarkerFactory.getMarker(uuid), ex.getMessage(), ex); + json = Tools.createJSONError(10000,"Internal_Server_Error", (String)null, uuid); } - return "Proc count = "+String.valueOf(cnt); + return json.toString(); } + //--------------------------------------------------------------------------- + public FrmLocustModel convertKAZJSONToLocustModel(JSONObject obj) throws CustomException { + FrmLocustModel result = new FrmLocustModel(); + + result.company_uid = "f52a99a6-db5a-4d15-b29c-5a7989d548eb"; //Company: "Kazakhstan Locust" + //result.uid = obj.getString("uuid"); + result.eid = String.valueOf(obj.getLong("OBJECTID")); + + //result.seq = obj.getLong("seq")*1000; + result.country_id = "5"; //Kazakhstan + result.country_uid = "3c7df90e-9632-4e71-8f36-c184751216df"; //Kazakhstan + + result.lat_center = String.valueOf(obj.getDouble("latitude")); + result.lon_center = String.valueOf(obj.getDouble("longitude")); + + try{ + String dateStr = obj.getString("examination_date").replace("Z", ""); + DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; + LocalDateTime dateTime = LocalDateTime.parse(dateStr, formatter); + result.date = java.sql.Timestamp.valueOf(dateTime); + } catch (Exception ex) { + throw new CustomException(200, 10000, "Date error: " + obj.optString("examination_date","null"), null, true); + } + + if (obj.has("village") && !obj.isNull("village")) + result.village = obj.getString("village"); + if (obj.has("farm") && !obj.isNull("farm")) + result.terrain = obj.getString("farm"); + if (obj.has("district") && !obj.isNull("district")) + result.district = obj.getString("district"); + + if(!obj.isNull("region") && !obj.optString("region","").isBlank()) { + switch (obj.optString("region",null).trim()) { + case "Акмолинская": + result.region_id = "10"; + result.region_uid = "3a3deedc-45ef-4fe5-a77a-17092f11b55e"; + case "Туркестанская": + result.region_id = "5"; + result.region_uid = "d7da7b40-dad8-450d-b455-8f39f3c9e824"; + break; + case "Жамбылская": + result.region_id = "4"; + result.region_uid = "1d7c0ab2-62d2-4645-be90-9675c5cc7d3b"; + break; + case "Алматинская": + result.region_id = "3"; + result.region_uid = "ffcb6886-531a-4559-af6c-902a3f3dbcca"; + break; + case "Карагандинская": + result.region_id = "2"; + result.region_uid = "f6cc2454-f78e-48d4-aa91-d36aa6873f07"; + break; + case "Жетысу": + result.region_id = "478"; + result.region_uid = "327f5e9c-91eb-477c-ba02-0a1e1e49ebc6"; + break; + case "Кызылординская": + result.region_id = "6"; + result.region_uid = "54caa739-fa8b-4196-a64c-0df12bfb60bc"; + break; + case "Атырауская": + result.region_id = "13"; + result.region_uid = "51f7f494-a700-42f3-8006-4233655bf6a8"; + break; + case "Западно-Казахстанская": + result.region_id = "12"; + result.region_uid = "b87f6741-e01a-45fc-a9da-2b8f9f0aad97"; + break; + case "Восточно-Казахстанская": + result.region_id = "1"; + result.region_uid = "73d12ffe-55f9-4294-bcb4-0ee78f1875a5"; + break; + case "Абай": + result.region_id = "477"; + result.region_uid = "992ce0b6-85e9-406a-a031-a23b62f167ea"; + break; + case "Костанайская": + result.region_id = "8"; + result.region_uid = "1b9299a2-43a8-4a27-a0f0-ab36b33b8d5c"; + break; + case "Павлодарская": + result.region_id = "11"; + result.region_uid = "c7894755-1ac6-4fbe-8d60-af9b0c3ec3a5"; + break; + case "Северо-Казахстанская": + result.region_id = "9"; + result.region_uid = "5d9e75b4-b7d2-4af8-9aa2-14b5fa38842e"; + break; + case "Актюбинская": + result.region_id = "7"; + result.region_uid = "9e6889d3-aa2f-47a0-a0b9-90973e3971c6"; + break; + case "Мангистауская": + result.region_id = "14"; + result.region_uid = "87a47fe7-3cdf-4d96-924d-5d4888972a82"; + break; + case "Улытау": + result.region_id = "479"; + result.region_uid = "76468ad3-5f73-4c33-bf7d-ce2ff1006fdd"; + break; + case "-----------": + result.region_id = null; + result.region_uid = null; + break; + default: + System.out.println(obj.optString("region",null)); + throw new CustomException(200, 10000, "Not find region by region: " + obj.optString("region",null), null, true); + } + } + + + //Вид саранчи + if(!obj.isNull("grasshopper_type") && !obj.optString("grasshopper_type","").isBlank()) { + switch (obj.getString("grasshopper_type")) { + case "Мароккская саранча": + result.locust_type_id = "2"; + result.locust_type_uid = "8dd9e74f-d7ba-492a-8c75-7aed4f0847c6"; + break; + case "Итальянский прус": + result.locust_type_id = "1"; + result.locust_type_uid = "688e484b-babd-4a48-b318-be85e40682b0"; + break; + case "Азиатская саранча": + result.locust_type_id = "3"; + result.locust_type_uid = "d61a5325-e8c7-497a-9cff-7ee4c4e1e37d"; + break; + default: + System.out.println(obj.optString("grasshopper_type",null)); + throw new CustomException(200, 10000, "Not find grasshopper_type: " + obj.optString("grasshopper_type",null), null, true); + } + } + + //Обследованно + if (obj.has("examination_area") && !obj.isNull("examination_area")) + result.bio_hectare = obj.get("examination_area").toString(); + + //Площадь заселения + if (obj.has("settlement_area") && !obj.isNull("settlement_area")) + result.locust_populated = obj.get("settlement_area").toString(); + + if(!obj.isNull("development_stage") && !obj.optString("development_stage","").isBlank()) { + switch (obj.getString("development_stage")) { + case "Яйца": + result.eggs = "true"; + break; + case "Личинки","Заселенность личинками","Личинка","личинки","личинка","2-5","дернасил","дернәсіл","дернасіл","дернәсілдері": + result.larva = "true"; + break; + case "Кулиги": + result.kuliguli = "true"; + break; + case "Имаго": + result.imago = "true"; + break; + case "спаривание","шағылысу": + result.imago = "true"; + result.imago_copulation="true"; + break; + case "жұмыртқа салу","тұқым": //Кладка яиц + result.imago = "true"; + result.imago_laying="true"; + break; + case "спаривание и яйцекладка", "Спаривания и яйцекладки": + result.imago = "true"; + result.imago_copulation="true"; + result.imago_laying="true"; + break; + case "Стаи","Ұшу шағылысу": + result.swarms = "true"; + break; + //default: закомментировал так как люди понаписали ерунды.... + // System.out.println(obj.optString("development_stage",null)); + // throw new CustomException(200, 10000, "Not find development_stage: " + obj.optString("development_stage",null), null, true); + } + } + + result.description = ""; + result.description += "city: " + obj.getString("city") + "\n"; + if(!obj.isNull("message_type")) { + result.description += "message_type: " + obj.getString("message_type") + "\n"; + } + if (obj.has("development_stage") && !obj.isNull("development_stage")) + result.description += "stage: " + obj.getString("development_stage") + "\n"; + + result.update(); + return result; + } + //--------------------------------------------------------------------------- /** * Пример запроса: http://127.0.0.1:8081/AcceptJSON но в теле нужно поменять строку "Year y = Year.of(2024);" а также номер дня * @param user @@ -579,7 +559,13 @@ public class AcceptJSON implements ServletContextAware { JSONObject jsonObj = (JSONObject) o; FrmLocustModel locust = new FrmLocustModel(); - locust.date = jsonObj.getString("date"); + try{ + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm"); + LocalDateTime dateTime = LocalDateTime.parse(jsonObj.getString("date"), formatter); + locust.date = java.sql.Timestamp.valueOf(dateTime); + } catch (Exception ex) { + throw new CustomException(200, 10000, "Date error: " + jsonObj.optString("date","null"), null, true); + } String regionName=""; if (!jsonObj.isNull("regionName")) @@ -909,7 +895,7 @@ public class AcceptJSON implements ServletContextAware { stmt.setInt(5, Integer.parseInt(locust.region_id)); //region_id stmt.setString(6, locust.district); //Пишу в поле "area" stmt.setString(7, null); - stmt.setString(8, locust.date); //27.04.2020 09:17 + stmt.setTimestamp(8, locust.date); //27.04.2020 09:17 stmt.setString(9, locust.description); stmt.setString(10, locust.drawPolygon); @@ -1234,7 +1220,7 @@ public class AcceptJSON implements ServletContextAware { stmt.setInt(5, Integer.parseInt(locust.region_id)); stmt.setString(6, locust.district); //Пишу в поле "area" stmt.setString(7, null); - stmt.setString(8, locust.date); //27.04.2020 09:17 + stmt.setTimestamp(8, locust.date); stmt.setString(9, locust.description); stmt.setString(10, locust.drawPolygon); stmt.setInt(11, Integer.parseInt(locust.locust_type_id)); @@ -1506,7 +1492,6 @@ public class AcceptJSON implements ServletContextAware { int count=0; if(content!=null && !content.isEmpty()) { - JSONArray jsonArray = new JSONArray(content); for (Object o : jsonArray) { JSONObject jsonObj = (JSONObject) o; @@ -1517,15 +1502,16 @@ public class AcceptJSON implements ServletContextAware { UpdateLocust updateLocust = new UpdateLocust(jdbcTemplate); if(updateLocust.update(locust)) count++; - if(count==66) - System.out.println( "count = " + count ); + System.out.println( "count = " + count ); } + }else{ + throw new CustomException(200, 10000, "Data content uz is null", null, true); } //Save last download date String insertSql = """ - insert into integration.days_uz (date, count) values (:date, :count) + insert into integration.days_uz(date, count) values (:date, :count) """; MapSqlParameterSource insertParams = new MapSqlParameterSource(); insertParams.addValue("date", lastDate, Types.DATE); @@ -1658,7 +1644,14 @@ public class AcceptJSON implements ServletContextAware { } } result.district = obj.optString("town_name",null); - result.date = obj.optString("date",null); + + try{ + DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; + LocalDateTime dateTime = LocalDateTime.parse(obj.getString("date"), formatter); + result.date = java.sql.Timestamp.valueOf(dateTime); + } catch (Exception ex) { + throw new CustomException(200, 10000, "Date error: " + obj.optString("date","null"), null, true); + } result.bio_hectare = obj.optString("surveyed_area",null); if (!obj.isNull("point")) { diff --git a/src/main/java/org/ccalm/main/UpdateLocust.java b/src/main/java/org/ccalm/main/UpdateLocust.java index 0e5f3fc..6e53ddd 100644 --- a/src/main/java/org/ccalm/main/UpdateLocust.java +++ b/src/main/java/org/ccalm/main/UpdateLocust.java @@ -36,14 +36,18 @@ public class UpdateLocust { boolean exists=false; //Is there a record. MapSqlParameterSource parameters; - //Если данные пришли из внешнего источника то проверяем по ID компании и eid + //Если данные пришли из внешнего источника то выбираю uid по eid if(locust.eid!=null && locust.uid==null) { + + int year = locust.date.toLocalDateTime().getYear(); + sql = """ - select uid from main.frmlocust where eid=:eid and company_uid=CAST(:company_uid AS uuid) + select uid from main.frmlocust where eid=:eid and company_uid=CAST(:company_uid AS uuid) and EXTRACT(YEAR FROM date)=:year """; parameters = new MapSqlParameterSource(); parameters.addValue("eid", locust.eid, Types.BIGINT); parameters.addValue("company_uid", locust.company_uid, Types.VARCHAR); + parameters.addValue("year", year, Types.INTEGER); List ret = jdbcTemplate.query(sql, parameters, new DBTools.JsonRowMapper()); for (String s : ret) { JSONObject obj = new JSONObject(s); @@ -68,6 +72,7 @@ public class UpdateLocust { { sql= """ update main.frmlocust set + eid=:eid, changed = false, company_uid=CAST(:company_uid AS uuid), user_id=:user_id, @@ -140,6 +145,7 @@ public class UpdateLocust { }else{ sql=""" insert into main.frmlocust( + eid, changed, company_uid, user_id, @@ -209,6 +215,7 @@ public class UpdateLocust { test, uid )values( + :eid, false, CAST(:company_uid AS uuid), :user_id, @@ -276,12 +283,13 @@ public class UpdateLocust { :description, ST_SetSRID(ST_GeomFromGeoJSON(:geom),4326), :test, - main.strtouuid(:uid) + COALESCE(main.strtouuid(:uid),gen_random_uuid()) ) """; } parameters = new MapSqlParameterSource(); parameters.addValue("uid", locust.uid, Types.VARCHAR); + if(locust.eid==null) parameters.addValue("eid", null, Types.INTEGER); else parameters.addValue("eid", Integer.parseInt(locust.eid), Types.INTEGER); parameters.addValue("company_uid", locust.company_uid, Types.VARCHAR); @@ -310,22 +318,7 @@ public class UpdateLocust { parameters.addValue("date", null, Types.DATE); }else { - if(locust.date.indexOf("-")==-1) //If old date format in UNIX time - { - long uDate=Long.parseLong(locust.date)+1; - java.sql.Timestamp timestamp=new java.sql.Timestamp((long)uDate*1000); - parameters.addValue("date", timestamp, Types.TIMESTAMP); - }else - { - try{ - DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; // ISO 8601 (2025-02-12T14:17:43) - LocalDateTime dateTime = LocalDateTime.parse(locust.date, formatter); - java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf(dateTime); - parameters.addValue("date", timestamp, Types.TIMESTAMP); - } catch (Exception ex) { - logger.error(ex.getMessage(), ex); - } - } + parameters.addValue("date", locust.date, Types.TIMESTAMP); } parameters.addValue("timezone", locust.timezone, Types.VARCHAR); @@ -436,5 +429,4 @@ public class UpdateLocust { return false; } } - } diff --git a/src/main/java/org/ccalm/main/models/FrmLocustModel.java b/src/main/java/org/ccalm/main/models/FrmLocustModel.java index e8a6bdc..df2b4cd 100644 --- a/src/main/java/org/ccalm/main/models/FrmLocustModel.java +++ b/src/main/java/org/ccalm/main/models/FrmLocustModel.java @@ -44,6 +44,10 @@ public class FrmLocustModel { public String locust_type_uid; //Locust species (Вид саранчи) public String locust_populated; //Area infested (ha) + + public String bio_hectare=null; //Обследованная площадь + public String bio_biotope_id=null; //Тип биотопа + public String bio_biotope_uid=null; //Тип биотопа public String bio_temperature; //air temperature public String bio_wind; //Ветер (м/с) public String bio_greenery_id; //растительность @@ -51,18 +55,31 @@ public class FrmLocustModel { public String bio_greenery_cover_id; //Густота растительного покрова public String bio_greenery_cover_uid; //Густота растительного покрова + public String phase=null; //Фаза саранчи public String locust_have; //id Фазы саранчи public String evp; //ЭФП public String drawPolygon="null"; //Область обследования GeoGSON public String size; //Заселённая площадь (polygonArea Площадь контура зоны распространения, в случае если специалист указал его) - public String date; //Дата + //public String date; //Дата + public java.sql.Timestamp date; //Дата public String timezone; public String description=""; public String geom; - //Hopper + //Eggs + public String eggs = null; // For CheckBox + public String eggs_capsules_area = null; //Egg-bed (surface in ha) + public String eggs_capsules_density = null; //Плотность яиц + public String eggs_capsules_density_to = null; //Egg-pods (density) to + public String eggs_capsules = null; //Eggs (average) + public String eggs_live = null; //Eggs (viable) + public String eggs_enemies_id = null; //Natural enemies present + public String eggs_enemies_uid = null; //Natural enemies present + //public String eggs_enemies = null; //not using TODO delete field from database + + //Hopper (личинки разряженные) public String larva = null; public String larva_born = null; //Hatching character varying(255) COLLATE pg_catalog."default" DEFAULT NULL::character varying, public String larva_born_id = null; // integer, @@ -76,19 +93,8 @@ public class FrmLocustModel { public String larva_density = null; //Hopper density numeric, (Плотность личинок) public String larva_density_to = null; //Hopper density to numeric, - //Eggs - public String eggs = null; // For CheckBox - public String eggs_capsules_area = null; //Egg-bed (surface in ha) - public String eggs_capsules_density = null; //Плотность яиц - public String eggs_capsules_density_to = null; //Egg-pods (density) to - public String eggs_capsules = null; //Eggs (average) - public String eggs_live = null; //Eggs (viable) - public String eggs_enemies_id = null; //Natural enemies present - public String eggs_enemies_uid = null; //Natural enemies present - //public String eggs_enemies = null; //not using TODO delete field from database - //Bands (кулиги) - public String kuliguli = null; + public String kuliguli = null; // For CheckBox public String kuliguli_density=null; //Плотность кулиг на м² public String kuliguli_density_to = null; //плотность максимальная в кулиге public String kuliguli_size = null; // Размер кулиг @@ -99,7 +105,7 @@ public class FrmLocustModel { public String kuliguli_age_uid = null; // Возраст личинок //Adults (Имаго) - public String imago = null; + public String imago = null; // For CheckBox public String imago_wing_id = null; public String imago_wing_uid = null; public String imago_maturity = null; @@ -113,13 +119,10 @@ public class FrmLocustModel { public String imago_density_ga = null; public String imago_feeding = null; public String imago_copulation = null; - public String imago_laying = null; + public String imago_laying = null; //Кладка яиц public String imago_flying = null; - public String bio_hectare=null; //Обследованная площадь - public String bio_biotope_id=null; //Тип тиотопа - public String bio_biotope_uid=null; //Тип тиотопа public String swarms = null; // For CheckBox public String swarm_maturity = null; //Maturity boolean,