Импорт анкет из узбекистана
This commit is contained in:
@ -10,11 +10,13 @@ spring:
|
|||||||
application:
|
application:
|
||||||
name: org.ccalm.main
|
name: org.ccalm.main
|
||||||
datasource:
|
datasource:
|
||||||
url: jdbc:postgresql://91.201.214.156: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
|
||||||
username: postgres
|
username: postgres
|
||||||
password: 309A86FF65A78FB428F4E38DFE35F730
|
password: 309A86FF65A78FB428F4E38DFE35F730
|
||||||
driver-class-name: org.postgresql.Driver
|
driver-class-name: org.postgresql.Driver
|
||||||
hikari:
|
hikari:
|
||||||
|
connection-init-sql: "SET timezone TO 'UTC';"
|
||||||
maximum-pool-size: 10
|
maximum-pool-size: 10
|
||||||
minimum-idle: 5
|
minimum-idle: 5
|
||||||
max-lifetime: 1740000
|
max-lifetime: 1740000
|
||||||
|
|||||||
24
pom.xml
24
pom.xml
@ -69,12 +69,18 @@
|
|||||||
<artifactId>mail</artifactId>
|
<artifactId>mail</artifactId>
|
||||||
<version>1.4</version>
|
<version>1.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<!--dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>commons-io</artifactId>
|
<artifactId>commons-io</artifactId>
|
||||||
<version>1.3.2</version>
|
<version>1.3.2</version>
|
||||||
|
</dependency-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-io</groupId>
|
||||||
|
<artifactId>commons-io</artifactId>
|
||||||
|
<version>2.15.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.logstash.logback</groupId>
|
<groupId>net.logstash.logback</groupId>
|
||||||
<artifactId>logstash-logback-encoder</artifactId>
|
<artifactId>logstash-logback-encoder</artifactId>
|
||||||
@ -121,6 +127,22 @@
|
|||||||
<artifactId>spring-jdbc</artifactId>
|
<artifactId>spring-jdbc</artifactId>
|
||||||
<version>6.1.12</version>
|
<version>6.1.12</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springdoc</groupId>
|
||||||
|
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
||||||
|
<version>2.8.4</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.annotation</groupId>
|
||||||
|
<artifactId>javax.annotation-api</artifactId>
|
||||||
|
<version>1.3.2</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.36</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
413
src/main/java/org/ccalm/main/UpdateLocust.java
Normal file
413
src/main/java/org/ccalm/main/UpdateLocust.java
Normal file
@ -0,0 +1,413 @@
|
|||||||
|
package org.ccalm.main;
|
||||||
|
|
||||||
|
import org.ccalm.main.models.FrmLocustModel;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
||||||
|
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||||
|
import tools.DBTools;
|
||||||
|
|
||||||
|
import java.sql.Types;
|
||||||
|
import java.text.DateFormat;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class UpdateLocust {
|
||||||
|
|
||||||
|
private final NamedParameterJdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
UpdateLocust(NamedParameterJdbcTemplate jdbcTemplate){
|
||||||
|
this.jdbcTemplate = jdbcTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean update(FrmLocustModel locust){
|
||||||
|
|
||||||
|
String sql;
|
||||||
|
boolean exists=false; //Is there a record.
|
||||||
|
|
||||||
|
//Если данные пришли из внешнего источника то проверяем по ID компании и eid
|
||||||
|
if(locust.eid!=null && locust.uid==null) {
|
||||||
|
sql = """
|
||||||
|
select uid from main.frmlocust where eid=:eid and company_uid=CAST(:company_uid AS uuid)
|
||||||
|
""";
|
||||||
|
MapSqlParameterSource parameters = new MapSqlParameterSource();
|
||||||
|
parameters.addValue("eid", locust.eid, Types.BIGINT);
|
||||||
|
parameters.addValue("company_uid", locust.company_uid, Types.VARCHAR);
|
||||||
|
List<String> ret = jdbcTemplate.query(sql, parameters, new DBTools.JsonRowMapper());
|
||||||
|
for (String s : ret) {
|
||||||
|
JSONObject obj = new JSONObject(s);
|
||||||
|
if(!obj.isNull("uid")) {
|
||||||
|
locust.uid = obj.getString("uid");
|
||||||
|
exists=true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Check exist record by uid
|
||||||
|
sql= """
|
||||||
|
select uid from main.frmlocust where uid=CAST(:uid AS uuid);
|
||||||
|
""";
|
||||||
|
MapSqlParameterSource parameters = new MapSqlParameterSource();
|
||||||
|
parameters.addValue("uid", locust.uid, Types.VARCHAR);
|
||||||
|
List<String> ret = jdbcTemplate.query(sql, parameters, new DBTools.JsonRowMapper());
|
||||||
|
for (String s : ret) {
|
||||||
|
exists=true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(exists)
|
||||||
|
{
|
||||||
|
sql= """
|
||||||
|
update main.frmlocust set
|
||||||
|
changed = false,
|
||||||
|
user_id=:user_id,
|
||||||
|
device_id=:device_id,
|
||||||
|
image_name1=:image_name1,
|
||||||
|
image_name2=:image_name2,
|
||||||
|
image_name3=:image_name3,
|
||||||
|
country_id=:country_id,
|
||||||
|
region_id=:region_id,
|
||||||
|
area=:area,
|
||||||
|
district=:district,
|
||||||
|
village=:village,
|
||||||
|
terrain=:terrain,
|
||||||
|
observer=:observer,
|
||||||
|
date=:date,
|
||||||
|
timezone=CAST(:timezone AS interval),
|
||||||
|
lat_center=:lat_center,
|
||||||
|
lon_center=:lon_center,
|
||||||
|
bio_hectare=:bio_hectare,
|
||||||
|
bio_biotope_id=:bio_biotope_id,
|
||||||
|
bio_greenery_id=:bio_greenery_id,
|
||||||
|
bio_greenery_cover_id=:bio_greenery_cover_id,
|
||||||
|
bio_temperature=:bio_temperature,
|
||||||
|
bio_wind=:bio_wind,
|
||||||
|
locust_have=:locust_have,
|
||||||
|
locust_type_id=:locust_type_id,
|
||||||
|
locust_populated=:locust_populated,
|
||||||
|
eggs_capsules_area=:eggs_capsules_area,
|
||||||
|
eggs_capsules_density=:eggs_capsules_density,
|
||||||
|
eggs_capsules_density_to=:eggs_capsules_density_to,
|
||||||
|
eggs_capsules=:eggs_capsules,
|
||||||
|
eggs_live=:eggs_live,
|
||||||
|
eggs_enemies_id=:eggs_enemies_id,
|
||||||
|
larva_born_id=:larva_born_id,
|
||||||
|
larva_age_id=:larva_age_id,
|
||||||
|
larva_painting_id=:larva_painting_id,
|
||||||
|
larva_behavior_id=:larva_behavior_id,
|
||||||
|
larva_density=:larva_density,
|
||||||
|
larva_density_to=:larva_density_to,
|
||||||
|
kuliguli_age_id=:kuliguli_age_id,
|
||||||
|
kuliguli_density=:kuliguli_density,
|
||||||
|
kuliguli_density_to=:kuliguli_density_to,
|
||||||
|
kuliguli_size=:kuliguli_size,
|
||||||
|
kuliguli_count=:kuliguli_count,
|
||||||
|
kuliguli_action_id=:kuliguli_action_id,
|
||||||
|
imago_wing_id=:imago_wing_id,
|
||||||
|
imago_maturity=:imago_maturity,
|
||||||
|
imago_phase_id=:imago_phase_id,
|
||||||
|
imago_action_id=:imago_action_id,
|
||||||
|
imago_density=:imago_density,
|
||||||
|
imago_density_ga=:imago_density_ga,
|
||||||
|
imago_feeding=:imago_feeding,
|
||||||
|
imago_copulation=:imago_copulation,
|
||||||
|
imago_laying=:imago_laying,
|
||||||
|
imago_flying=:imago_flying,
|
||||||
|
swarm_maturity=:swarm_maturity,
|
||||||
|
swarm_density_id=:swarm_density_id,
|
||||||
|
swarm_size=:swarm_size,
|
||||||
|
swarm_count=:swarm_count,
|
||||||
|
swarm_copulation=:swarm_copulation,
|
||||||
|
swarm_laying=:swarm_laying,
|
||||||
|
swarm_flying_direction_id=:swarm_flying_direction_id,
|
||||||
|
swarm_height_id=:swarm_height_id,
|
||||||
|
description=:description,
|
||||||
|
geom=ST_SetSRID(ST_GeomFromGeoJSON(:geom),4326),
|
||||||
|
test=:test
|
||||||
|
where uid=CAST(:uid AS uuid)
|
||||||
|
""";
|
||||||
|
}else{
|
||||||
|
sql="""
|
||||||
|
insert into main.frmlocust(
|
||||||
|
changed,
|
||||||
|
user_id,
|
||||||
|
device_id,
|
||||||
|
image_name1,
|
||||||
|
image_name2,
|
||||||
|
image_name3,
|
||||||
|
country_id,
|
||||||
|
region_id,
|
||||||
|
area,
|
||||||
|
district,
|
||||||
|
village,
|
||||||
|
terrain,
|
||||||
|
observer,
|
||||||
|
date,
|
||||||
|
timezone,
|
||||||
|
lat_center,
|
||||||
|
lon_center,
|
||||||
|
bio_hectare,
|
||||||
|
bio_biotope_id,
|
||||||
|
bio_greenery_id,
|
||||||
|
bio_greenery_cover_id,
|
||||||
|
bio_temperature,
|
||||||
|
bio_wind,
|
||||||
|
locust_have,
|
||||||
|
locust_type_id,
|
||||||
|
locust_populated,
|
||||||
|
eggs_capsules_area,
|
||||||
|
eggs_capsules_density,
|
||||||
|
eggs_capsules_density_to,
|
||||||
|
eggs_capsules,
|
||||||
|
eggs_live,
|
||||||
|
eggs_enemies_id,
|
||||||
|
larva_born_id,
|
||||||
|
larva_age_id,
|
||||||
|
larva_painting_id,
|
||||||
|
larva_behavior_id,
|
||||||
|
larva_density,
|
||||||
|
larva_density_to,
|
||||||
|
kuliguli_age_id,
|
||||||
|
kuliguli_density,
|
||||||
|
kuliguli_density_to,
|
||||||
|
kuliguli_size,
|
||||||
|
kuliguli_count,
|
||||||
|
kuliguli_action_id,
|
||||||
|
imago_wing_id,
|
||||||
|
imago_maturity,
|
||||||
|
imago_phase_id,
|
||||||
|
imago_action_id,
|
||||||
|
imago_density,
|
||||||
|
imago_density_ga,
|
||||||
|
imago_feeding,
|
||||||
|
imago_copulation,
|
||||||
|
imago_laying,
|
||||||
|
imago_flying,
|
||||||
|
swarm_maturity,
|
||||||
|
swarm_density_id,
|
||||||
|
swarm_size,
|
||||||
|
swarm_count,
|
||||||
|
swarm_copulation,
|
||||||
|
swarm_laying,
|
||||||
|
swarm_flying_direction_id,
|
||||||
|
swarm_height_id,
|
||||||
|
description,
|
||||||
|
geom,
|
||||||
|
test,
|
||||||
|
uid
|
||||||
|
)values(
|
||||||
|
false,
|
||||||
|
:user_id,
|
||||||
|
:device_id,
|
||||||
|
:image_name1,
|
||||||
|
:image_name2,
|
||||||
|
:image_name3,
|
||||||
|
:country_id,
|
||||||
|
:region_id,
|
||||||
|
:area,
|
||||||
|
:district,
|
||||||
|
:village,
|
||||||
|
:terrain,
|
||||||
|
:observer,
|
||||||
|
:date,
|
||||||
|
CAST(:timezone AS interval),
|
||||||
|
:lat_center,
|
||||||
|
:lon_center,
|
||||||
|
:bio_hectare,
|
||||||
|
:bio_biotope_id,
|
||||||
|
:bio_greenery_id,
|
||||||
|
:bio_greenery_cover_id,
|
||||||
|
:bio_temperature,
|
||||||
|
:bio_wind,
|
||||||
|
:locust_have,
|
||||||
|
:locust_type_id,
|
||||||
|
:locust_populated,
|
||||||
|
:eggs_capsules_area,
|
||||||
|
:eggs_capsules_density,
|
||||||
|
:eggs_capsules_density_to,
|
||||||
|
:eggs_capsules,
|
||||||
|
:eggs_live,
|
||||||
|
:eggs_enemies_id,
|
||||||
|
:larva_born_id,
|
||||||
|
:larva_age_id,
|
||||||
|
:larva_painting_id,
|
||||||
|
:larva_behavior_id,
|
||||||
|
:larva_density,
|
||||||
|
:larva_density_to,
|
||||||
|
:kuliguli_age_id,
|
||||||
|
:kuliguli_density,
|
||||||
|
:kuliguli_density_to,
|
||||||
|
:kuliguli_size,
|
||||||
|
:kuliguli_count,
|
||||||
|
:kuliguli_action_id,
|
||||||
|
:imago_wing_id,
|
||||||
|
:imago_maturity,
|
||||||
|
:imago_phase_id,
|
||||||
|
:imago_action_id,
|
||||||
|
:imago_density,
|
||||||
|
:imago_density_ga,
|
||||||
|
:imago_feeding,
|
||||||
|
:imago_copulation,
|
||||||
|
:imago_laying,
|
||||||
|
:imago_flying,
|
||||||
|
:swarm_maturity,
|
||||||
|
:swarm_density_id,
|
||||||
|
:swarm_size,
|
||||||
|
:swarm_count,
|
||||||
|
:swarm_copulation,
|
||||||
|
:swarm_laying,
|
||||||
|
:swarm_flying_direction_id,
|
||||||
|
:swarm_height_id,
|
||||||
|
:description,
|
||||||
|
ST_SetSRID(ST_GeomFromGeoJSON(:geom),4326),
|
||||||
|
:test,
|
||||||
|
:uid
|
||||||
|
)
|
||||||
|
""";
|
||||||
|
}
|
||||||
|
MapSqlParameterSource parameters = new MapSqlParameterSource();
|
||||||
|
parameters.addValue("uid", locust.uid, Types.VARCHAR);
|
||||||
|
|
||||||
|
if(locust.user_id == null) { parameters.addValue("user_id", null, Types.BIGINT); } else { parameters.addValue("user_id", Long.parseLong(locust.user_id), Types.BIGINT); }
|
||||||
|
|
||||||
|
parameters.addValue("device_id", locust.device_id, Types.VARCHAR);
|
||||||
|
|
||||||
|
parameters.addValue("image_name1", locust.image_name1, Types.VARCHAR);
|
||||||
|
parameters.addValue("image_name2", locust.image_name2, Types.VARCHAR);
|
||||||
|
parameters.addValue("image_name3", locust.image_name3, Types.VARCHAR);
|
||||||
|
|
||||||
|
if(locust.country_id == null) { parameters.addValue("country_id", null, Types.BIGINT); } else { parameters.addValue("country_id", Long.parseLong(locust.country_id), Types.BIGINT); }
|
||||||
|
|
||||||
|
|
||||||
|
if(locust.region_id == null) { parameters.addValue("region_id", null, Types.BIGINT); } else { parameters.addValue("region_id", Long.parseLong(locust.region_id), Types.BIGINT); }
|
||||||
|
|
||||||
|
parameters.addValue("area", locust.area, Types.VARCHAR);
|
||||||
|
parameters.addValue("district", locust.district, Types.VARCHAR);
|
||||||
|
|
||||||
|
parameters.addValue("village", locust.village, Types.VARCHAR);
|
||||||
|
parameters.addValue("terrain", locust.terrain, Types.VARCHAR);
|
||||||
|
|
||||||
|
parameters.addValue("observer", locust.observer, Types.VARCHAR);
|
||||||
|
if(locust.date==null)
|
||||||
|
{
|
||||||
|
parameters.addValue("date", null, Types.DATE);
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
if(date.indexOf("-")==-1) //If old date format in UNIX time
|
||||||
|
{
|
||||||
|
int uDate=Integer.parseInt(date)+1;
|
||||||
|
java.sql.Timestamp tm=new java.sql.Timestamp((long)uDate*1000);
|
||||||
|
stmt.setTimestamp(14, tm);
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
java.sql.Timestamp tm=null;
|
||||||
|
DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //2016-05-29 18:00:01 in "GMT"
|
||||||
|
try{
|
||||||
|
tm = new java.sql.Timestamp(dfm.parse(date).getTime());
|
||||||
|
} catch (Exception ex) {
|
||||||
|
logger.error("Error",ex.getMessage());
|
||||||
|
}
|
||||||
|
stmt.setTimestamp(14, tm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parameters.addValue("timezone", locust.timezone, Types.VARCHAR);
|
||||||
|
|
||||||
|
if(locust.lat_center==null) parameters.addValue("lat_center", null, Types.DOUBLE); else parameters.addValue("lat_center", Double.parseDouble(locust.lat_center), Types.DOUBLE);
|
||||||
|
if(locust.lon_center==null) parameters.addValue("lon_center", null, Types.DOUBLE); else parameters.addValue("lon_center", Double.parseDouble(locust.lon_center), Types.DOUBLE);
|
||||||
|
|
||||||
|
if(locust.bio_hectare==null) parameters.addValue("bio_hectare", null, Types.DOUBLE); else parameters.addValue("bio_hectare",Double.parseDouble(locust.bio_hectare), Types.DOUBLE);
|
||||||
|
if(locust.bio_biotope_id==null) parameters.addValue("bio_biotope_id", null, Types.BIGINT); else parameters.addValue("bio_biotope_id",Long.parseLong(locust.bio_biotope_id), Types.BIGINT);
|
||||||
|
if(locust.bio_greenery_id==null) parameters.addValue("bio_greenery_id", null, Types.BIGINT); else parameters.addValue("bio_greenery_id", Long.parseLong(locust.bio_greenery_id), Types.BIGINT);
|
||||||
|
if(locust.bio_greenery_cover_id==null) parameters.addValue("bio_greenery_cover_id", null, Types.BIGINT); else parameters.addValue("bio_greenery_cover_id",Long.parseLong(locust.bio_greenery_cover_id), Types.BIGINT);
|
||||||
|
if(locust.bio_temperature==null) parameters.addValue("bio_temperature", null, Types.DOUBLE); else parameters.addValue("bio_temperature",Double.parseDouble(locust.bio_temperature), Types.DOUBLE);
|
||||||
|
if(locust.bio_wind==null) parameters.addValue("bio_wind", null, Types.DOUBLE); else parameters.addValue("bio_wind",Double.parseDouble(locust.bio_wind), Types.DOUBLE);
|
||||||
|
if(locust.locust_have==null) parameters.addValue("locust_have", null, Types.BIGINT); else parameters.addValue("locust_have",Long.parseLong(locust.locust_have), Types.BIGINT);
|
||||||
|
if(locust.locust_type_id==null) parameters.addValue("locust_type_id", null, Types.BIGINT); else parameters.addValue("locust_type_id",Long.parseLong(locust.locust_type_id), Types.BIGINT);
|
||||||
|
if(locust.locust_populated==null) parameters.addValue("locust_populated", null, Types.DOUBLE); else parameters.addValue("locust_populated",Double.parseDouble(locust.locust_populated), Types.DOUBLE);
|
||||||
|
|
||||||
|
if(locust.eggs_capsules_area==null) parameters.addValue("eggs_capsules_area", null, Types.DOUBLE); else parameters.addValue("eggs_capsules_area",Double.parseDouble(locust.eggs_capsules_area), Types.DOUBLE);
|
||||||
|
if(locust.eggs_capsules_density==null) parameters.addValue("eggs_capsules_density", null, Types.DOUBLE); else parameters.addValue("eggs_capsules_density",Double.parseDouble(locust.eggs_capsules_density), Types.DOUBLE);
|
||||||
|
if(locust.eggs_capsules_density_to==null) parameters.addValue("eggs_capsules_density_to", null, Types.DOUBLE); else parameters.addValue("eggs_capsules_density_to",Double.parseDouble(locust.eggs_capsules_density_to), Types.DOUBLE);
|
||||||
|
if(locust.eggs_capsules==null) parameters.addValue("eggs_capsules", null, Types.DOUBLE); else parameters.addValue("eggs_capsules",Double.parseDouble(locust.eggs_capsules), Types.DOUBLE);
|
||||||
|
if(locust.eggs_live==null) parameters.addValue("eggs_live", null, Types.DOUBLE); else parameters.addValue("eggs_live",Double.parseDouble(locust.eggs_live), Types.DOUBLE);
|
||||||
|
if(locust.eggs_enemies_id==null) parameters.addValue("eggs_enemies_id", null, Types.BIGINT); else parameters.addValue("eggs_enemies_id",Long.parseLong(locust.eggs_enemies_id), Types.BIGINT);
|
||||||
|
|
||||||
|
if(locust.larva_born_id==null) parameters.addValue("larva_born_id", null, Types.BIGINT); else parameters.addValue("larva_born_id",Long.parseLong(locust.larva_born_id), Types.BIGINT);
|
||||||
|
if(locust.larva_age_id==null) parameters.addValue("larva_age_id", null, Types.BIGINT); else parameters.addValue("larva_age_id",Long.parseLong(locust.larva_age_id), Types.BIGINT);
|
||||||
|
if(locust.larva_painting_id==null) parameters.addValue("larva_painting_id", null, Types.BIGINT); else parameters.addValue("larva_painting_id",Long.parseLong(locust.larva_painting_id), Types.BIGINT);
|
||||||
|
if(locust.larva_behavior_id==null) parameters.addValue("larva_behavior_id", null, Types.BIGINT); else parameters.addValue("larva_behavior_id",Long.parseLong(locust.larva_behavior_id), Types.BIGINT);
|
||||||
|
|
||||||
|
if(locust.larva_density==null) parameters.addValue("larva_density", null, Types.DOUBLE); else parameters.addValue("larva_density",Double.parseDouble(locust.larva_density), Types.DOUBLE);
|
||||||
|
if(locust.larva_density_to==null) parameters.addValue("larva_density_to", null, Types.DOUBLE); else parameters.addValue("larva_density_to",Double.parseDouble(locust.larva_density_to), Types.DOUBLE);
|
||||||
|
if(locust.kuliguli_age_id==null) parameters.addValue("kuliguli_age_id", null, Types.BIGINT); else parameters.addValue("kuliguli_age_id",Long.parseLong(locust.kuliguli_age_id), Types.BIGINT);
|
||||||
|
|
||||||
|
if(locust.kuliguli_density==null) parameters.addValue("kuliguli_density", null, Types.DOUBLE); else parameters.addValue("kuliguli_density",Double.parseDouble(locust.kuliguli_density), Types.DOUBLE);
|
||||||
|
if(locust.kuliguli_density_to==null) parameters.addValue("kuliguli_density_to", null, Types.DOUBLE); else parameters.addValue("kuliguli_density_to",Double.parseDouble(locust.kuliguli_density_to), Types.DOUBLE);
|
||||||
|
if(locust.kuliguli_size==null) parameters.addValue("kuliguli_size", null, Types.DOUBLE); else parameters.addValue("kuliguli_size",Double.parseDouble(locust.kuliguli_size), Types.DOUBLE);
|
||||||
|
if(locust.kuliguli_count==null) parameters.addValue("kuliguli_count", null, Types.DOUBLE); else parameters.addValue("kuliguli_count",Double.parseDouble(locust.kuliguli_count), Types.DOUBLE);
|
||||||
|
if(locust.kuliguli_action_id==null) parameters.addValue("kuliguli_action_id", null, Types.BIGINT); else parameters.addValue("kuliguli_action_id",Long.parseLong(locust.kuliguli_action_id), Types.BIGINT);
|
||||||
|
if(locust.imago_wing_id==null) parameters.addValue("imago_wing_id", null, Types.BIGINT); else parameters.addValue("imago_wing_id",Long.parseLong(locust.imago_wing_id), Types.BIGINT);
|
||||||
|
if(locust.imago_maturity==null) parameters.addValue("imago_maturity", null, Types.BOOLEAN);
|
||||||
|
else {
|
||||||
|
if(locust.imago_maturity.equals("1")) parameters.addValue("imago_maturity",true,Types.BOOLEAN);
|
||||||
|
else parameters.addValue("imago_maturity",false,Types.BOOLEAN);
|
||||||
|
}
|
||||||
|
if(locust.imago_phase_id==null) parameters.addValue("imago_phase_id", null, Types.BIGINT); else parameters.addValue("imago_phase_id",Long.parseLong(locust.imago_phase_id), Types.BIGINT);
|
||||||
|
if(locust.imago_action_id==null) parameters.addValue("imago_action_id", null, Types.BIGINT); else parameters.addValue("imago_action_id",Long.parseLong(locust.imago_action_id), Types.BIGINT);
|
||||||
|
|
||||||
|
if(locust.imago_density==null) parameters.addValue("imago_density", null, Types.DOUBLE); else parameters.addValue("imago_density",Double.parseDouble(locust.imago_density), Types.DOUBLE);
|
||||||
|
if(locust.imago_density_ga==null) parameters.addValue("imago_density_ga", null, Types.DOUBLE); else parameters.addValue("imago_density_ga",Double.parseDouble(locust.imago_density_ga), Types.DOUBLE);
|
||||||
|
if(locust.imago_feeding==null) parameters.addValue("imago_feeding", null, Types.BOOLEAN);
|
||||||
|
else {
|
||||||
|
if(locust.imago_feeding.equals("1")) parameters.addValue("imago_feeding",true,Types.BOOLEAN);
|
||||||
|
else parameters.addValue("imago_feeding",false,Types.BOOLEAN);
|
||||||
|
}
|
||||||
|
if(locust.imago_copulation==null) parameters.addValue("imago_copulation", null, Types.BOOLEAN);
|
||||||
|
else {
|
||||||
|
if(locust.imago_copulation.equals("1")) parameters.addValue("imago_copulation",true,Types.BOOLEAN);
|
||||||
|
else parameters.addValue("imago_copulation",false,Types.BOOLEAN);
|
||||||
|
}
|
||||||
|
if(locust.imago_laying==null) parameters.addValue("imago_laying", null, Types.BOOLEAN);
|
||||||
|
else {
|
||||||
|
if(locust.imago_laying.equals("1")) parameters.addValue("imago_laying",true,Types.BOOLEAN);
|
||||||
|
else parameters.addValue("imago_laying",false,Types.BOOLEAN);
|
||||||
|
}
|
||||||
|
if(locust.imago_flying==null) parameters.addValue("imago_flying", null, Types.BOOLEAN);
|
||||||
|
else {
|
||||||
|
if(locust.imago_flying.equals("1")) parameters.addValue("imago_flying",true,Types.BOOLEAN);
|
||||||
|
else parameters.addValue("imago_flying",false,Types.BOOLEAN);
|
||||||
|
}
|
||||||
|
if(locust.swarm_maturity==null) parameters.addValue("swarm_maturity", null, Types.BOOLEAN);
|
||||||
|
else {
|
||||||
|
if(locust.swarm_maturity.equals("1")) parameters.addValue("swarm_maturity",true,Types.BOOLEAN);
|
||||||
|
else parameters.addValue("swarm_maturity",false,Types.BOOLEAN);
|
||||||
|
}
|
||||||
|
if(locust.swarm_density_id==null) parameters.addValue("swarm_density_id", null, Types.BIGINT); else parameters.addValue("swarm_density_id",Long.parseLong(locust.swarm_density_id), Types.BIGINT);
|
||||||
|
if(locust.swarm_size==null) parameters.addValue("swarm_size", null, Types.DOUBLE); else parameters.addValue("swarm_size",Double.parseDouble(locust.swarm_size), Types.DOUBLE);
|
||||||
|
if(locust.swarm_count==null) parameters.addValue("swarm_count", null, Types.DOUBLE); else parameters.addValue("swarm_count",Double.parseDouble(locust.swarm_count), Types.DOUBLE);
|
||||||
|
if(locust.swarm_copulation==null) parameters.addValue("swarm_copulation", null, Types.BOOLEAN);
|
||||||
|
else {
|
||||||
|
if(locust.swarm_copulation.equals("1")) parameters.addValue("swarm_copulation",true,Types.DOUBLE);
|
||||||
|
else parameters.addValue("swarm_copulation",false,Types.DOUBLE);
|
||||||
|
}
|
||||||
|
if(locust.swarm_laying==null) parameters.addValue("swarm_laying", null, Types.BOOLEAN);
|
||||||
|
else {
|
||||||
|
if(locust.swarm_laying.equals("1")) parameters.addValue("swarm_laying",true,Types.DOUBLE);
|
||||||
|
else parameters.addValue("swarm_laying",false,Types.DOUBLE);
|
||||||
|
}
|
||||||
|
if(locust.swarm_flying_direction_id==null) parameters.addValue("swarm_flying_direction_id", null, Types.BIGINT); else parameters.addValue("swarm_flying_direction_id",Long.parseLong(locust.swarm_flying_direction_id), Types.BIGINT);
|
||||||
|
if(locust.swarm_height_id==null) parameters.addValue("swarm_height_id", null, Types.BIGINT); else parameters.addValue("swarm_height_id",Long.parseLong(locust.swarm_height_id), Types.BIGINT);
|
||||||
|
|
||||||
|
parameters.addValue("description", Types.VARCHAR);
|
||||||
|
parameters.addValue("geom", Types.VARCHAR);
|
||||||
|
if(locust.test==null) parameters.addValue("test", null, Types.BOOLEAN);
|
||||||
|
else {
|
||||||
|
if(locust.test.equals("1")) parameters.addValue("test",true, Types.BOOLEAN);
|
||||||
|
else parameters.addValue("test",false,Types.BOOLEAN);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
96
src/main/java/org/ccalm/main/models/ErrorResponseModel.java
Normal file
96
src/main/java/org/ccalm/main/models/ErrorResponseModel.java
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
package org.ccalm.main.models;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Schema(
|
||||||
|
description = "Error API response",
|
||||||
|
example = "{ \"error_code\": 10000, \"error_message\": [\"Internal_Server_Error\",\"Please_log_in\"], \"error_setting\": [\"99;day\",\"1;2\"], \"error_marker\": \"2a449883-c7c6-468e-b3ae-5f73fc96627d\" }"
|
||||||
|
)
|
||||||
|
|
||||||
|
public class ErrorResponseModel {
|
||||||
|
|
||||||
|
@Schema(description = "Error code", example = "10000")
|
||||||
|
@JsonProperty("error_code")
|
||||||
|
private int errorCode;
|
||||||
|
|
||||||
|
@Schema(description = "List of error descriptions", example = "[\"Internal_Server_Error\",\"Please_log_in\"]")
|
||||||
|
@JsonProperty("error_message")
|
||||||
|
private List<String> errorMessage;
|
||||||
|
|
||||||
|
@Schema(description = "Options for translated text", example = "[\"99;day\",\"1;2\"]")
|
||||||
|
@JsonProperty("error_setting")
|
||||||
|
private List<String> errorSetting;
|
||||||
|
|
||||||
|
@Schema(description = "Unique identifier for searching in the database", example = "4260aad8-f7ee-4be4-b52c-15d56ec83232")
|
||||||
|
@JsonProperty("error_marker")
|
||||||
|
private String errorMarker;
|
||||||
|
|
||||||
|
public ErrorResponseModel(int errorCode) {
|
||||||
|
this.errorCode = errorCode;
|
||||||
|
this.errorMessage = null;
|
||||||
|
this.errorSetting = null;
|
||||||
|
this.errorMarker = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ErrorResponseModel(int errorCode, List<String> errorMessage, String errorMarker) {
|
||||||
|
this.errorCode = errorCode;
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
this.errorMarker = errorMarker;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ErrorResponseModel(int errorCode, String errorMessage, String errorMarker) {
|
||||||
|
this.errorCode = errorCode;
|
||||||
|
this.errorMessage = Collections.singletonList(errorMessage);
|
||||||
|
this.errorMarker = errorMarker;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ErrorResponseModel(int errorCode, String errorMessage, String errorSetting, String errorMarker) {
|
||||||
|
this.errorCode = errorCode;
|
||||||
|
this.errorMessage = Collections.singletonList(errorMessage);
|
||||||
|
this.errorSetting = Collections.singletonList(errorSetting);
|
||||||
|
this.errorMarker = errorMarker;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ErrorResponseModel(int errorCode, List<String> errorMessage, List<String> errorSetting, String errorMarker) {
|
||||||
|
this.errorCode = errorCode;
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
this.errorSetting = errorSetting;
|
||||||
|
this.errorMarker = errorMarker;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getError_code() {
|
||||||
|
return errorCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setError_code(int errorCode) {
|
||||||
|
this.errorCode = errorCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getError_message() {
|
||||||
|
return errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setError_message(List<String> errorMessage) {
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setError_setting(List<String> errorSetting) {
|
||||||
|
this.errorSetting = errorSetting;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getError_setting() {
|
||||||
|
return errorSetting;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setError_marker(String errorMarker) {
|
||||||
|
this.errorMarker = errorMarker;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getError_marker() {
|
||||||
|
return errorMarker;
|
||||||
|
}
|
||||||
|
}
|
||||||
151
src/main/java/org/ccalm/main/models/FrmLocustModel.java
Normal file
151
src/main/java/org/ccalm/main/models/FrmLocustModel.java
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
package org.ccalm.main.models;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.ccalm.main.utils.CustomException;
|
||||||
|
|
||||||
|
import java.sql.Types;
|
||||||
|
|
||||||
|
public class FrmLocustModel {
|
||||||
|
public String uid;
|
||||||
|
public String eid; //Внешний идентификатор
|
||||||
|
public long seq;
|
||||||
|
public long country_id;
|
||||||
|
public String country_uid;
|
||||||
|
|
||||||
|
public String phytoType; //Вредители либо обработка
|
||||||
|
public String observer;
|
||||||
|
|
||||||
|
public String device_id = null;
|
||||||
|
|
||||||
|
public String image_name1 = null;
|
||||||
|
public String image_name2 = null;
|
||||||
|
public String image_name3 = null;
|
||||||
|
|
||||||
|
public String lon_center;
|
||||||
|
public String lat_center;
|
||||||
|
//Область
|
||||||
|
public String region_id; //Область
|
||||||
|
public String region_uid; //Область
|
||||||
|
|
||||||
|
public String district; //Район
|
||||||
|
public String district_id;
|
||||||
|
|
||||||
|
public String terrain=""; //Название месности
|
||||||
|
|
||||||
|
public String village; //Хозяйство или местность
|
||||||
|
|
||||||
|
public String locust; //Вид саранчи
|
||||||
|
//public String locust_id; //Вид саранчи
|
||||||
|
public String locust_type_id; //Locust species (Вид саранчи)
|
||||||
|
public String locust_populated; //Area infested (ha)
|
||||||
|
|
||||||
|
public String bio_temperature; //air temperature
|
||||||
|
public String bio_wind; //Ветер (м/с)
|
||||||
|
public String bio_greenery_id; //растительность
|
||||||
|
public String bio_greenery_uid; //растительность
|
||||||
|
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 description="";
|
||||||
|
|
||||||
|
//Hopper
|
||||||
|
public String larva_born = null; //Hatching character varying(255) COLLATE pg_catalog."default" DEFAULT NULL::character varying,
|
||||||
|
public String larva_born_id = null; // integer,
|
||||||
|
public String larva_born_uid = null;
|
||||||
|
public String larva_age_id = null; //Hopper stages integer,
|
||||||
|
public String larva_age_uid = null; //Hopper stages integer,
|
||||||
|
public String larva_painting_id = null; //Appearance integer,
|
||||||
|
public String larva_behavior = null; //Behaviour character varying(50) COLLATE pg_catalog."default" DEFAULT NULL::character varying,
|
||||||
|
public String larva_behavior_id = null; // integer,
|
||||||
|
public String larva_behavior_uid = null; // integer,
|
||||||
|
public String larva_density = null; //Hopper density numeric,
|
||||||
|
public String larva_density_to = null; //Hopper density to numeric,
|
||||||
|
|
||||||
|
//Eggs
|
||||||
|
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_density=null; //Плотность кулиг на м²
|
||||||
|
public String kuliguli_density_to = null; //плотность максимальная в кулиге
|
||||||
|
public String kuliguli_size = null; // Размер кулиг
|
||||||
|
public String kuliguli_count = null; // Количество кулиг
|
||||||
|
public String kuliguli_action_id = null; // Поведение
|
||||||
|
public String kuliguli_action_uid = null; // Поведение
|
||||||
|
public String kuliguli_age_id = null; // Возраст личинок
|
||||||
|
public String kuliguli_age_uid = null; // Возраст личинок
|
||||||
|
|
||||||
|
//Adults (Имаго)
|
||||||
|
public String imago_wing_id = null;
|
||||||
|
public String imago_wing_uid = null;
|
||||||
|
public String imago_maturity = null;
|
||||||
|
//public String imago_phase = null; not using
|
||||||
|
public String imago_phase_id = null;
|
||||||
|
public String imago_phase_uid = null;
|
||||||
|
public String imago_action = null;
|
||||||
|
public String imago_action_id = null;
|
||||||
|
public String imago_action_uid = null;
|
||||||
|
public String imago_density = null; //Плотность имаго (/м²)*
|
||||||
|
public String imago_density_ga = null;
|
||||||
|
public String imago_feeding = null;
|
||||||
|
public String imago_copulation = null;
|
||||||
|
public String imago_laying = null;
|
||||||
|
public String imago_flying = null;
|
||||||
|
|
||||||
|
|
||||||
|
public String larva_density=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,
|
||||||
|
public String swarm_density_id = null; //Density of swarm integer,
|
||||||
|
public String swarm_size = null; //Swarm size double precision,
|
||||||
|
public String swarm_count = null; //Number of swarms double precision,
|
||||||
|
public String swarm_copulation = null; //Not used boolean,
|
||||||
|
public String swarm_laying = null; //Not used boolean,
|
||||||
|
public String swarm_flying_direction_id = null; //Flying direction integer,
|
||||||
|
public String swarm_height_id = null; //Flying height integer,
|
||||||
|
public String swarm_height_uid = null; //Flying height integer,
|
||||||
|
|
||||||
|
public String company_uid=null;
|
||||||
|
|
||||||
|
//Ниже то что относиться только к обработке
|
||||||
|
public String insecticide_name=null;
|
||||||
|
public String insecticide_active_substance=null;
|
||||||
|
public String insecticide_dose=null;
|
||||||
|
public String spray_platform=null;
|
||||||
|
|
||||||
|
public String test=null;
|
||||||
|
|
||||||
|
public void update(){
|
||||||
|
if(
|
||||||
|
swarm_maturity!=null ||
|
||||||
|
swarm_density_id!=null ||
|
||||||
|
swarm_size!=null ||
|
||||||
|
swarm_count!=null ||
|
||||||
|
swarm_copulation!=null ||
|
||||||
|
swarm_laying!=null ||
|
||||||
|
swarm_flying_direction_id!=null ||
|
||||||
|
swarm_height_id!=null ||
|
||||||
|
swarm_height_uid!=null
|
||||||
|
) swarms = "true";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
79
src/main/java/org/ccalm/main/utils/CustomException.java
Normal file
79
src/main/java/org/ccalm/main/utils/CustomException.java
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
package org.ccalm.main.utils;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import org.ccalm.main.models.ErrorResponseModel;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
|
||||||
|
public class CustomException extends Exception {
|
||||||
|
private static final Logger logger = LogManager.getLogger(CustomException.class);
|
||||||
|
|
||||||
|
private ErrorResponseModel error;
|
||||||
|
@Getter
|
||||||
|
private boolean saveToLog = false;
|
||||||
|
|
||||||
|
public CustomException(int errorCode, String errorMessage, String marker, boolean saveToLog) {
|
||||||
|
super(errorMessage);
|
||||||
|
error = new ErrorResponseModel(errorCode, errorMessage, marker);
|
||||||
|
this.saveToLog = saveToLog;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CustomException(int errorCode, String errorMessage, String errorSetting, String marker, boolean saveToLog) {
|
||||||
|
super(errorMessage);
|
||||||
|
error = new ErrorResponseModel(errorCode, errorMessage, errorSetting, marker);
|
||||||
|
this.saveToLog = saveToLog;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CustomException(int errorCode, List<String> errorMessages, String marker, boolean saveToLog) {
|
||||||
|
super(String.join(" ", errorMessages));
|
||||||
|
error = new ErrorResponseModel(errorCode, errorMessages, marker);
|
||||||
|
this.saveToLog = saveToLog;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CustomException(int errorCode, List<String> errorMessages, List<String> errorSettings, String marker, boolean saveToLog) {
|
||||||
|
super(String.join(" ", errorMessages));
|
||||||
|
error = new ErrorResponseModel(errorCode, errorMessages, errorSettings, marker);
|
||||||
|
this.saveToLog = saveToLog;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getErrorCode() {
|
||||||
|
return error.getError_code();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getErrorMarker() {
|
||||||
|
return error.getError_marker();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getErrorMessages() {
|
||||||
|
return error.getError_message();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getErrorSettings() {
|
||||||
|
return error.getError_setting();
|
||||||
|
}
|
||||||
|
|
||||||
|
public JSONObject getJson() {
|
||||||
|
JSONObject json = new JSONObject();
|
||||||
|
try {
|
||||||
|
json.put("error_code", this.getErrorCode());
|
||||||
|
json.put("error_message", this.getErrorMessages());
|
||||||
|
json.put("error_setting", this.getErrorSettings());
|
||||||
|
json.put("error_marker", this.getErrorMarker());
|
||||||
|
} catch (JSONException e) {
|
||||||
|
logger.error("Error", e);
|
||||||
|
}
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ErrorResponseModel getErrorResponseModel() {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
167
src/main/java/org/ccalm/main/utils/Tools.java
Normal file
167
src/main/java/org/ccalm/main/utils/Tools.java
Normal file
@ -0,0 +1,167 @@
|
|||||||
|
package org.ccalm.main.utils;
|
||||||
|
|
||||||
|
import io.jsonwebtoken.SignatureAlgorithm;
|
||||||
|
import io.jsonwebtoken.security.Keys;
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import javax.crypto.*;
|
||||||
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.*;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class Tools {
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
private static final Logger logger = LogManager.getLogger(Tools.class);
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
public static JSONObject createJSONError(int code, String message, String setting, String marker) {
|
||||||
|
JSONObject json = new JSONObject();
|
||||||
|
try {
|
||||||
|
json.put("error_code", code);
|
||||||
|
json.put("error_message", Arrays.asList(message));
|
||||||
|
json.put("error_setting", Arrays.asList(setting));
|
||||||
|
json.put("error_marker", marker);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
logger.error(e);
|
||||||
|
}
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
//Зашифровать
|
||||||
|
public static String encryptText(String pass,String data){
|
||||||
|
String encryptedBase64="";
|
||||||
|
String encryptionIV = "jazz_tyt_net_111"; // Ваш вектор инициализации
|
||||||
|
try {
|
||||||
|
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
|
||||||
|
SecretKeySpec key = new SecretKeySpec(Base64.getDecoder().decode(pass), "AES");
|
||||||
|
IvParameterSpec iv = new IvParameterSpec(encryptionIV.getBytes()); // Создание объекта IvParameterSpec для вектора инициализации
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, key, iv); // Инициализация шифра с ключом и вектором инициализации
|
||||||
|
byte[] encrypted = cipher.doFinal(data.getBytes()); // Шифрование строки
|
||||||
|
encryptedBase64 = Base64.getEncoder().encodeToString(encrypted); // Преобразование зашифрованных данных в base64
|
||||||
|
} catch (InvalidKeyException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (InvalidAlgorithmParameterException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (NoSuchPaddingException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (IllegalBlockSizeException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (BadPaddingException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return encryptedBase64;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String decryptText(String pass,String data){
|
||||||
|
String encryptionIV = "jazz_tyt_net_111"; // Ваш вектор инициализации
|
||||||
|
String decryptedText= "";
|
||||||
|
try {
|
||||||
|
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
|
||||||
|
SecretKeySpec key = new SecretKeySpec(Base64.getDecoder().decode(pass), "AES");
|
||||||
|
IvParameterSpec iv = new IvParameterSpec(encryptionIV.getBytes()); // Создание объекта IvParameterSpec для вектора инициализации
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, key, iv); // Инициализация шифра с ключом и вектором инициализации
|
||||||
|
|
||||||
|
byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(data)); // Расшифровка данных
|
||||||
|
decryptedText = new String(decrypted); // Преобразование расшифрованных данных в строку
|
||||||
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException |
|
||||||
|
InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return decryptedText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String generateSignature(String pass,String input) {
|
||||||
|
try {
|
||||||
|
SecretKey secretKey = new SecretKeySpec(Base64.getDecoder().decode(pass), "HmacSHA256");
|
||||||
|
|
||||||
|
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||||
|
byte[] encodedInput = digest.digest(input.getBytes(StandardCharsets.UTF_8));
|
||||||
|
byte[] encodedKey = secretKey.getEncoded();
|
||||||
|
|
||||||
|
// Создание HMAC-подписи
|
||||||
|
SecretKeySpec keySpec = new SecretKeySpec(encodedKey, "HmacSHA256");
|
||||||
|
Mac mac = Mac.getInstance("HmacSHA256");
|
||||||
|
mac.init(keySpec);
|
||||||
|
byte[] rawHmac = mac.doFinal(encodedInput);
|
||||||
|
|
||||||
|
// Кодирование подписи в base64
|
||||||
|
return Base64.getEncoder().encodeToString(rawHmac);
|
||||||
|
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isValidEmail(String email) {
|
||||||
|
if(email==null || email.isEmpty()) return false;
|
||||||
|
String EMAIL_REGEX = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
|
||||||
|
Pattern pattern = Pattern.compile(EMAIL_REGEX);
|
||||||
|
Matcher matcher = pattern.matcher(email);
|
||||||
|
return matcher.matches();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isInteger(String str) {
|
||||||
|
if (str == null || str.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Integer.parseInt(str);
|
||||||
|
return true;
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String genKey(){
|
||||||
|
SecretKey key = Keys.secretKeyFor(SignatureAlgorithm.HS256);
|
||||||
|
byte[] keyBytes = key.getEncoded();
|
||||||
|
return Base64.getEncoder().encodeToString(keyBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String generatePassword(int length) {
|
||||||
|
String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||||
|
SecureRandom random = new SecureRandom();
|
||||||
|
StringBuilder sb = new StringBuilder(length);
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
int randomIndex = random.nextInt(CHARACTERS.length());
|
||||||
|
sb.append(CHARACTERS.charAt(randomIndex));
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Метод для извлечения подписи из JWT токена
|
||||||
|
public static String extractSignature(String jwtToken) {
|
||||||
|
String[] jwtParts = jwtToken.split("\\.");
|
||||||
|
if (jwtParts.length != 3) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return jwtParts[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
//Для извлечения содержимого токена
|
||||||
|
public static JSONObject extractToken(String jwtToken) {
|
||||||
|
String[] jwtParts = jwtToken.split("\\.");
|
||||||
|
if (jwtParts.length != 3) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String payloadJson = new String(Base64.getUrlDecoder().decode(jwtParts[1]));
|
||||||
|
JSONObject payload=null;
|
||||||
|
try {
|
||||||
|
payload = new JSONObject(payloadJson);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user