Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 KB | None | 0 0
  1. package br.com.clinimuni.vaccinaeweb.domain;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.Objects;
  7. import java.util.Set;
  8.  
  9. import javax.persistence.CascadeType;
  10. import javax.persistence.Column;
  11. import javax.persistence.Entity;
  12. import javax.persistence.FetchType;
  13. import javax.persistence.GeneratedValue;
  14. import javax.persistence.GenerationType;
  15. import javax.persistence.Id;
  16. import javax.persistence.OneToMany;
  17. import javax.persistence.Table;
  18. import javax.validation.constraints.NotNull;
  19.  
  20. /**
  21.  * @author ivocalado
  22.  *
  23.  */
  24. @Entity(name = "CalendarioVacinal")
  25. @Table(name = "calendario_vacinal")
  26. public class CalendarioVacinal {
  27.  
  28.     @Id
  29.     @GeneratedValue(strategy = GenerationType.AUTO)
  30.     private Integer id;
  31.  
  32.     @NotNull
  33.     @Column(unique = true)
  34.     private String nome;
  35.    
  36.     @Column(length = 1500)
  37.     private String descricao;
  38.  
  39.     @OneToMany(mappedBy = "calendario", cascade = CascadeType.ALL, orphanRemoval = true)
  40.     private List<VacinaCalendarioVacinal> vacinas = new ArrayList<VacinaCalendarioVacinal>();
  41.  
  42.     public void addVacina(Vacina vacina, Set<Integer> doses) {
  43.         VacinaCalendarioVacinal entrada = new VacinaCalendarioVacinal(vacina, this);
  44.         entrada.setDosesAplicacao(doses);
  45.         vacina.getCalendariosAssociados().add(entrada);
  46.     }
  47.  
  48.     /**
  49.      * Remove interligação entre vacinas e o calendário. Solução adaptada de @see
  50.      * https://vladmihalcea.com/the-best-way-to-map-a-many-to-many-association-with-extra-columns-when-using-jpa-and-hibernate/
  51.      */
  52.     public void removeVacina(Vacina vacina) {
  53.         for (Iterator<VacinaCalendarioVacinal> iterator = vacinas.iterator(); iterator.hasNext();) {
  54.             VacinaCalendarioVacinal entrada = iterator.next();
  55.             if (entrada.getCalendario().equals(this) && entrada.getVacina().equals(vacina)) {
  56.                 iterator.remove();
  57.                 entrada.getVacina().getCalendariosAssociados().remove(entrada);
  58.                 entrada.setCalendario(null);
  59.                 entrada.setVacina(null);
  60.             }
  61.         }
  62.     }
  63.  
  64.     /*
  65.      * (non-Javadoc)
  66.      *
  67.      * @see java.lang.Object#hashCode()
  68.      */
  69.     @Override
  70.     public int hashCode() {
  71.         return Objects.hash(nome);
  72.     }
  73.  
  74.     /*
  75.      * (non-Javadoc)
  76.      *
  77.      * @see java.lang.Object#equals(java.lang.Object)
  78.      */
  79.     @Override
  80.     public boolean equals(Object obj) {
  81.         if (this == obj) {
  82.             return true;
  83.         }
  84.         if (obj == null) {
  85.             return false;
  86.         }
  87.         if (!(obj instanceof CalendarioVacinal)) {
  88.             return false;
  89.         }
  90.         CalendarioVacinal other = (CalendarioVacinal) obj;
  91.         return Objects.equals(nome, other.nome);
  92.     }
  93.  
  94.     /**
  95.      * @return the id
  96.      */
  97.     public Integer getId() {
  98.         return id;
  99.     }
  100.  
  101.     /**
  102.      * @param id the id to set
  103.      */
  104.     public void setId(Integer id) {
  105.         this.id = id;
  106.     }
  107.  
  108.     /**
  109.      * @return the nome
  110.      */
  111.     public String getNome() {
  112.         return nome;
  113.     }
  114.  
  115.     /**
  116.      * @param nome the nome to set
  117.      */
  118.     public void setNome(String nome) {
  119.         this.nome = nome;
  120.     }
  121.  
  122.     /**
  123.      * @return the vacinas
  124.      */
  125.     public List<VacinaCalendarioVacinal> getVacinas() {
  126.         return vacinas;
  127.     }
  128.  
  129.     /**
  130.      * @param vacinas the vacinas to set
  131.      */
  132.     public void setVacinas(List<VacinaCalendarioVacinal> vacinas) {
  133.         this.vacinas = vacinas;
  134.     }
  135.  
  136.     /**
  137.      * @return the descricao
  138.      */
  139.     public String getDescricao() {
  140.         return descricao;
  141.     }
  142.  
  143.     /**
  144.      * @param descricao the descricao to set
  145.      */
  146.     public void setDescricao(String descricao) {
  147.         this.descricao = descricao;
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement