Entities: Simulation:
package Simulations.Entity;
import lombok.*;
import javax.persistence.*;
import java.util.List;
@Builder
@Table
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Simulation {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column
private Long id;
@Column(name = "N")
private String simulation_Name;
@Column(name = "P")
private Double population_Size;
@Column(name = "I")
private Double initial_Infected_Number;
@Column(name = "R")
private Double how_Many_One_Infects;
@Column(name = "M")
private Double mortality_Rate;
@Column(name = "Ti")
private Double number_Of_Days_To_Recovery;
@Column(name = "Tm")
private Double number_Of_Days_To_Death;
@Column(name = "Ts")
private Double simulation_Time;
@OneToMany(mappedBy = "simulation", cascade = CascadeType.REMOVE)
private List<SimulationsValues> simulationsValues;
}
SimulationValues:
package Simulations.Entity;
import lombok.*;
import javax.persistence.*;
@Builder
@Table
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class SimulationsValues {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column
private Long id;
@Column(name = "Day")
private Double day;
@Column(name = "Pi")
private Double number_Of_Infected;
@Column(name = "Pv")
private Double healthy_Prone_To_Infection;
@Column(name = "Pm")
private Double dead;
@Column(name = "Pr")
private Double regained_Health_And_Immunity;
@ManyToOne
@JoinColumn(name = "simulation_id", referencedColumnName = "id")
private Simulation simulation;
}
Logic calss:
package Simulations.Services;
import Simulations.Entity.Simulation;
import Simulations.Entity.SimulationsValues;
import Simulations.Repositories.SimulationRepository;
import Simulations.Repositories.SimulationsValuesRepository;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
@RequiredArgsConstructor
@Component
@Aspect
public class EachRemainingDayService {
private final SimulationRepository simulationRepository;
private final SimulationsValuesRepository simulationsValuesRepository;
@Pointcut("@annotation(Simulations.Annotations.MyAnnotation)")
public void MyAnnotationMethod() {
}
public List<Simulation> getSimulations() {
List<Simulation> listOdIds = new ArrayList<>(simulationRepository.findAll());
listOdIds.sort(Comparator.comparing(Simulation::getId));
return listOdIds;
}
public List<SimulationsValues> getSimulationsValues() {
List<SimulationsValues> listOdIdsOfValues = new ArrayList<>(simulationsValuesRepository.findAll());
listOdIdsOfValues.sort(Comparator.comparing(SimulationsValues::getId));
return listOdIdsOfValues;
}
//3 zapis wynikow do bazy
@After("MyAnnotationMethod()")// zmienilem z Before
public void SimulationParametersForRemainingDays() {
var sim = getSimulations();
var newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());
var simVal = getSimulationsValues();
var newestSimulationsVal = simulationsValuesRepository
.getById(simVal.get(simVal.size() - 1).getId());
//zapisanie do bazy TO LOGIKA!!!
for (double i = 2; i <= newestSimulation.getSimulation_Time(); i ) {
if (newestSimulationsVal.getHealthy_Prone_To_Infection() <= 0) {
break;
}
if (i >= newestSimulation.getNumber_Of_Days_To_Death()
&& i < newestSimulation.getNumber_Of_Days_To_Recovery()) {
sim = getSimulations();
newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());
simVal = getSimulationsValues();
newestSimulationsVal = simulationsValuesRepository
.getById(simVal.get(simVal.size() - 1).getId());
SimulationsValues simulation_values;
Simulation finalNewestSimulation1 = newestSimulation;
double finalI = i;
simulation_values = SimulationsValues.builder()
.day(i)
.healthy_Prone_To_Infection(newestSimulation.getPopulation_Size() -
(((newestSimulation.getHow_Many_One_Infects())
* newestSimulationsVal.getNumber_Of_Infected()
newestSimulationsVal.getNumber_Of_Infected())))
.number_Of_Infected((((newestSimulation.getHow_Many_One_Infects())
* newestSimulationsVal.getNumber_Of_Infected()
newestSimulationsVal.getNumber_Of_Infected())//L24
- (newestSimulation.getMortality_Rate() * (double) ((simulationsValuesRepository
.findAll().stream().filter(e -> e.getDay() == finalI 1 - finalNewestSimulation1
.getNumber_Of_Days_To_Death()))
.collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
.filter(e -> e.getDay() == (finalI 1 - finalNewestSimulation1
.getNumber_Of_Days_To_Death()))).count() - 1).getNumber_Of_Infected()))))//V24
.regained_Health_And_Immunity(0d)// Z24
.dead((newestSimulation.getMortality_Rate() * (double) ((simulationsValuesRepository
.findAll().stream().filter(e -> e.getDay() == finalI 1 - finalNewestSimulation1
.getNumber_Of_Days_To_Death()))
.collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
.filter(e -> e.getDay() == (finalI 1 - finalNewestSimulation1
.getNumber_Of_Days_To_Death()))).count() - 1).getNumber_Of_Infected()))
- newestSimulationsVal.getDead())//V24
.build();
var simId = simulationsValuesRepository.save(simulation_values);
simId.setSimulation(newestSimulation);//dodane
var simVal2 = newestSimulation.getSimulationsValues();
simVal2.add(simId);
newestSimulation.setSimulationsValues(simVal2);
simulationRepository.save(newestSimulation);
} else if (i >= newestSimulation.getNumber_Of_Days_To_Recovery()) {
sim = getSimulations();
newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());
simVal = getSimulationsValues();
newestSimulationsVal = simulationsValuesRepository
.getById(simVal.get(simVal.size() - 1).getId());
Simulation finalNewestSimulation1 = newestSimulation;
double finalI = i;
SimulationsValues simulation_values;
simulation_values = SimulationsValues.builder()
.day(i)
.healthy_Prone_To_Infection(newestSimulation.getPopulation_Size() -
((newestSimulation.getHow_Many_One_Infects())
* newestSimulationsVal.getNumber_Of_Infected()))
.number_Of_Infected(((newestSimulation.getHow_Many_One_Infects())
* newestSimulationsVal.getNumber_Of_Infected()
newestSimulationsVal.getNumber_Of_Infected())//L24
- (newestSimulation.getMortality_Rate() * (double) ((simulationsValuesRepository
.findAll().stream().filter(e -> e.getDay() == finalI 1 - finalNewestSimulation1
.getNumber_Of_Days_To_Death()))
.collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
.filter(e -> e.getDay() == (finalI 1 - finalNewestSimulation1
.getNumber_Of_Days_To_Death()))).count() - 1).getNumber_Of_Infected())//V24
- (((1 - newestSimulation.getMortality_Rate()) * (simulationsValuesRepository
.findAll().stream().filter(e -> e.getDay() == finalI 1 - finalNewestSimulation1
.getNumber_Of_Days_To_Recovery()))
.collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
.filter(e -> e.getDay() == finalI 1 - finalNewestSimulation1
.getNumber_Of_Days_To_Recovery())).count() - 1)
.getNumber_Of_Infected()))//Z24
))
.regained_Health_And_Immunity((1 - newestSimulation.getMortality_Rate()) *
(simulationsValuesRepository.findAll()
.stream().filter(e -> e.getDay() == finalI 1 - finalNewestSimulation1
.getNumber_Of_Days_To_Recovery()))
.collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll()
.stream().filter(e -> e.getDay() == finalI 1 - finalNewestSimulation1
.getNumber_Of_Days_To_Recovery())).count() - 1)
.getNumber_Of_Infected())// Z24
.dead((newestSimulation.getMortality_Rate() * (double) ((simulationsValuesRepository.findAll()
.stream().filter(e -> e.getDay() == finalI 1 - finalNewestSimulation1
.getNumber_Of_Days_To_Death()))
.collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
.filter(e -> e.getDay() == (finalI 1 - finalNewestSimulation1
.getNumber_Of_Days_To_Death()))).count()1).getNumber_Of_Infected()))
- newestSimulationsVal.getDead())//V24
.build();
var simId = simulationsValuesRepository.save(simulation_values);
simId.setSimulation(newestSimulation);//dodane
var simVal2 = newestSimulation.getSimulationsValues();
simVal2.add(simId);
newestSimulation.setSimulationsValues(simVal2);
simulationRepository.save(newestSimulation);
} else if (i == 2) {
sim = getSimulations();
newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());
simVal = getSimulationsValues();
newestSimulationsVal = simulationsValuesRepository
.getById(simVal.get(simVal.size() - 1).getId());
SimulationsValues simulation_values;
simulation_values = SimulationsValues.builder()
.day(i)
.healthy_Prone_To_Infection(newestSimulationsVal.getHealthy_Prone_To_Infection() -
((newestSimulation.getHow_Many_One_Infects())
* newestSimulationsVal.getNumber_Of_Infected()))
.number_Of_Infected((newestSimulation.getHow_Many_One_Infects())
* newestSimulationsVal.getNumber_Of_Infected()
newestSimulationsVal.getNumber_Of_Infected())
.regained_Health_And_Immunity(0d)
.dead(0d)
.build();
var simId2 = simulationsValuesRepository.save(simulation_values);
simId2.setSimulation(newestSimulation);//dodane
var simVal2 = newestSimulation.getSimulationsValues();
simVal2.add(simId2);
newestSimulation.setSimulationsValues(simVal2);
simulationRepository.save(newestSimulation);
} else {
sim = getSimulations();
newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());
simVal = getSimulationsValues();
newestSimulationsVal = simulationsValuesRepository
.getById(simVal.get(simVal.size() - 1).getId());
SimulationsValues simulation_values;
simulation_values = SimulationsValues.builder()
.day(i)
.healthy_Prone_To_Infection(newestSimulationsVal.getHealthy_Prone_To_Infection() -
((newestSimulation.getHow_Many_One_Infects())
* newestSimulationsVal.getNumber_Of_Infected()))
.number_Of_Infected((newestSimulation.getHow_Many_One_Infects())
* newestSimulationsVal.getNumber_Of_Infected()
newestSimulationsVal.getNumber_Of_Infected())
.regained_Health_And_Immunity(0d)
.dead(0d)
.build();
var simId2 = simulationsValuesRepository.save(simulation_values);
simId2.setSimulation(newestSimulation);//dodane
var simVal2 = newestSimulation.getSimulationsValues();
simVal2.add(simId2);
newestSimulation.setSimulationsValues(simVal2);
simulationRepository.save(newestSimulation);
}
}
}
}
ViewModel:
package Simulations.ViewModels;
import Simulations.Annotations.GreaterThan;
import Simulations.Entity.SimulationsValues;
import lombok.*;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@GreaterThan(message = "field number of days to death can not be equal or bigger than field days to recovery")
public class SimulationViewModel {
private Long id;
@NotEmpty(message = "field can not be empty")
private String simulation_Name;
@NotNull(message = "field can not be empty")
@DecimalMax("10000000000.0")
@DecimalMin("0.0")
private Double population_Size;
private Double initial_Infected_Number;
@NotNull(message = "field can not be empty")
private Double how_Many_One_Infects;
@NotNull(message = "field can not be empty")
@DecimalMax("1.0")
@DecimalMin("0.001")
private Double mortality_Rate;
private @NotNull(message = "field can not be empty") @DecimalMin("1.0")
Double number_Of_Days_To_Recovery;
private @NotNull(message = "field can not be empty") @DecimalMin("1.0")
Double number_Of_Days_To_Death;
private @NotNull(message = "field can not be empty")
Double simulation_Time;
List<SimulationsValues> simulationsValues = new ArrayList<>();
}
Hi, I have a roblem. When I create a simulationValues object (using builder) it's fields have double values as intended but when I save this object to database and look at thoese values in database (MySql) thoese values are presented as Integers i.e. without values after comma, for example when I debug my code I see that "dead" field has in 12 iteration value 0.9 but it is saved to database as 1. Why is that? I don;t do any casting and all I use for this calculations are doubles as well as all fields n SimulationValues class and SimulationViewModel are doubles. Why when saving field changes types?Thanks in advance
CodePudding user response:
All columns in MySql were bigint type instead of double as I made them in coresponding entity, changed them to BigDecimal and everything works fine.