diff --git a/src/main/java/org/ccalm/main/AcceptJSON.java b/src/main/java/org/ccalm/main/AcceptJSON.java index eaac883..93d86cf 100644 --- a/src/main/java/org/ccalm/main/AcceptJSON.java +++ b/src/main/java/org/ccalm/main/AcceptJSON.java @@ -132,13 +132,10 @@ 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(); + int count=0; 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"); @@ -147,299 +144,597 @@ public class AcceptJSON implements ServletContextAware { ex.printStackTrace(); } - StringBuilder sb = new StringBuilder(1024); - try - { - String strURL="http://91.185.13.233:98/get_fito"; - URL url = new URL(strURL); - HttpURLConnection hConn = (HttpURLConnection) url.openConnection(); - hConn.setRequestMethod("GET"); - hConn.setRequestProperty("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEyMyIsInVzZXJuYW1lIjoiQ0NBTE0iLCJyb2xlIjoidXNlciIsImlhdCI6MTcwNjc4NjQ0Nn0.bypDbAZYjbQu8hWrb6MnnAZ80AbO4ZifP3inEnpbabI"); - - hConn.setConnectTimeout(60000); // 60 сек - - 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(); + 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")); } - }catch (IOException e) - { - e.printStackTrace(); } + if(lastDate==null) + lastDate = LocalDate.parse("2025-07-01"); + //lastDate = LocalDate.parse("2025-07-16"); - String content=sb.toString(); + //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); + String dateBegin = lastDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); + String dateEnd = dateBegin; - int year=2025; + System.out.println("date = " + dateEnd); + StringBuilder sb = new StringBuilder(1024); + try { + //String strURL = "http://91.185.13.233:98/get_fito?start_date="+dateBegin+"&end_date="+dateEnd; + String strURL = "http://95.56.225.145:98/get_fito?start_date="+dateBegin+"&end_date="+dateEnd; - if(content==null || content.isEmpty()) { - System.out.println("Content is null"); - }else { - System.out.println("Start"); + URL url = new URL(strURL); + HttpURLConnection hConn = (HttpURLConnection) url.openConnection(); + hConn.setRequestMethod("GET"); + hConn.setRequestProperty("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEyMyIsInVzZXJuYW1lIjoiQ0NBTE0iLCJyb2xlIjoidXNlciIsImlhdCI6MTcwNjc4NjQ0Nn0.bypDbAZYjbQu8hWrb6MnnAZ80AbO4ZifP3inEnpbabI"); - 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) { + 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(); + } + } catch (IOException e) { e.printStackTrace(); } - //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 year = 2025; - 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"; + + 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(); + //} + + //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; + + 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++; + + System.out.println( "count = " + count ); + + /* + //I don't miss other dates + ZonedDateTime dateTime = ZonedDateTime.parse(jsonObj.getString("examination_date"), DateTimeFormatter.ISO_ZONED_DATE_TIME); + if (dateTime.getYear() != year) + continue; + + 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"; + } + + 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++;*/ } - - 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++; } - System.out.println("Proc count = "+String.valueOf(cnt)); } + } 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.getInt("id")); //Internal identifier in the Uzbek database + + //result.seq = obj.getLong("seq")*1000; + result.country_id = "5"; //Kazakhstan + result.country_uid = "3c7df90e-9632-4e71-8f36-c184751216df"; //Kazakhstan + +/* + 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 + )"""; +*/ + + + + result.eid = "0"; + result.lat_center = String.valueOf(obj.getDouble("latitude")); + result.lon_center = String.valueOf(obj.getDouble("longitude")); + result.date = obj.getString("examination_date"); //2023-03-20T06:23:20.000Z + 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 = "5"; + //result.region_uid = "8f4f586f-6159-4fbf-a627-2c8d637abb5e"; + break; + case "Жамбылская": + result.region_id = "4"; + //result.region_uid = "2a6adb56-19a7-4f28-9a00-87559db91db9"; + break; + case "Алматинская": + result.region_id = "3"; + //result.region_uid = "2a6adb56-19a7-4f28-9a00-87559db91db9"; + break; + case "Карагандинская": + result.region_id = "2"; + //result.region_uid = "2a6adb56-19a7-4f28-9a00-87559db91db9"; + break; + case "Жетысу": + result.region_id = "478"; + //result.region_uid = "2a6adb56-19a7-4f28-9a00-87559db91db9"; + break; + case "Кызылординская": + result.region_id = "478"; + //result.region_uid = "2a6adb56-19a7-4f28-9a00-87559db91db9"; + 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("region") && !obj.optString("region","").isBlank()) { + switch (obj.optString("region",null).trim()) { + case "Туркестанская": + result.region_id = "5"; + //result.region_uid = ""; + break; + case "Жамбылская": + result.region_id = "4"; + //result.region_uid = ""; + break; + case "Алматинская": + result.region_id = "3"; + //result.region_uid = ""; + break; + case "Карагандинская": + result.region_id = "2"; + //result.region_uid = ""; + break; + case "Жетысу": + result.region_id = "478"; + //result.region_uid = ""; + break; + case "Кызылординская": + result.region_id = "6"; + //result.region_uid = ""; + break; + case "Атырауская": + result.region_id = "13"; + //result.region_uid = ""; + break; + case "Западно-Казахстанская": + result.region_id = "12"; + //result.region_uid = ""; + break; + case "Акмолинская": + result.region_id = "10"; + //result.region_uid = ""; + break; + case "Восточно-Казахстанская": + result.region_id = "1"; + //result.region_uid = ""; + break; + case "Абай": + result.region_id = "477"; + //result.region_uid = ""; + break; + case "Костанайская": + result.region_id = "8"; + //result.region_uid = ""; + break; + case "Павлодарская": + result.region_id = "11"; + //result.region_uid = ""; + break; + case "Северо-Казахстанская": + result.region_id = "9"; + //result.region_uid = ""; + break; + case "Актюбинская": + result.region_id = "7"; + //result.region_uid = ""; + break; + case "Мангистауская": + result.region_id = "14"; + //result.region_uid = ""; + break; + case "Улытау": + result.region_id = "479"; + //result.region_uid = ""; + 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("region") && !obj.optString("region","").isBlank()) { + switch (obj.getString("grasshopper_type")) { + case "Мароккская саранча": + result.locust_type_id = "2"; + //result.locust_type_uid = ""; + break; + case "Итальянский прус": + result.locust_type_id = "1"; + //result.locust_type_uid = ""; + break; + case "Азиатская саранча": + result.locust_type_id = "3"; + //result.locust_type_uid = ""; + 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")) + stmt.setDouble(13, Double.parseDouble(obj.get("examination_area").toString())); + else + stmt.setNull(13, Types.DOUBLE); + + //Площадь заселения + if (obj.has("settlement_area") && !obj.isNull("settlement_area")) + stmt.setDouble(14, Double.parseDouble(obj.get("settlement_area").toString())); + else + stmt.setNull(14, Types.DOUBLE); + + boolean eggs = false; // Яйца + boolean hoppers = false; // Личинки + boolean bands = false; // Кулиги + boolean adults = false; // Имаго + boolean swarms = false; // Стаи + if (obj.has("development_stage") && !obj.isNull("development_stage")) { + if (obj.getString("development_stage").equals("Яйца")) eggs = true; + if (obj.getString("development_stage").equals("Личинки") || obj.getString("development_stage").equals("Заселенность личинками") || obj.getString("development_stage").equals("Личинка")) + hoppers = true; + if (obj.getString("development_stage").equals("Кулиги")) bands = true; + if (obj.getString("development_stage").equals("Имаго")) adults = true; + if (obj.getString("development_stage").equals("Стаи")) swarms = true; + } + if (eggs) + result.eggs = "true"; + //stmt.setBoolean(16, true); + + if (hoppers) + result.hoppers = "true"; + //stmt.setBoolean(17, true); + + if (bands) + result.bands = "true"; + //stmt.setBoolean(18, true); + + if (adults) + result.adults = "true"; + //stmt.setBoolean(19, true); + + if (swarms) + result.swarms = "true"; + //stmt.setBoolean(20, true); + + + + result.description = ""; + result.description += "city: " + obj.getString("city") + "\n"; + 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);" а также номер дня @@ -1516,8 +1811,7 @@ 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 ); } }