Production.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package fr.triplea.demovote.model;
  2. import java.awt.image.BufferedImage;
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.sql.Types;
  6. import java.time.LocalDateTime;
  7. import java.time.format.DateTimeFormatter;
  8. import java.util.Base64;
  9. import java.util.List;
  10. import java.util.Locale;
  11. import javax.imageio.ImageIO;
  12. import net.coobird.thumbnailator.Thumbnails;
  13. import net.coobird.thumbnailator.geometry.Positions;
  14. import org.hibernate.annotations.CreationTimestamp;
  15. import org.hibernate.annotations.JdbcTypeCode;
  16. import org.hibernate.annotations.Type;
  17. import org.hibernate.annotations.UpdateTimestamp;
  18. import org.springframework.util.StringUtils;
  19. import com.fasterxml.jackson.annotation.JsonFormat;
  20. import io.hypersistence.utils.hibernate.type.basic.Inet;
  21. import io.hypersistence.utils.hibernate.type.basic.PostgreSQLInetType;
  22. import jakarta.persistence.CascadeType;
  23. import jakarta.persistence.Column;
  24. import jakarta.persistence.Entity;
  25. import jakarta.persistence.EnumType;
  26. import jakarta.persistence.Enumerated;
  27. import jakarta.persistence.FetchType;
  28. import jakarta.persistence.GeneratedValue;
  29. import jakarta.persistence.GenerationType;
  30. import jakarta.persistence.Id;
  31. import jakarta.persistence.JoinColumn;
  32. import jakarta.persistence.Lob;
  33. import jakarta.persistence.ManyToOne;
  34. import jakarta.persistence.OneToMany;
  35. import jakarta.persistence.Table;
  36. import jakarta.persistence.Temporal;
  37. import jakarta.persistence.TemporalType;
  38. import jakarta.persistence.Transient;
  39. @Entity(name = "vote.productions")
  40. @Table(name = "productions")
  41. public class Production
  42. {
  43. @Temporal(TemporalType.TIMESTAMP)
  44. @CreationTimestamp
  45. @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd/MM/yyyy HH:mm:ss", timezone="Europe/Paris")
  46. private LocalDateTime dateCreation;
  47. @Temporal(TemporalType.TIMESTAMP)
  48. @UpdateTimestamp
  49. @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd/MM/yyyy HH:mm:ss", timezone="Europe/Paris")
  50. private LocalDateTime dateModification;
  51. @Id
  52. @GeneratedValue(strategy = GenerationType.IDENTITY)
  53. @Column(name = "numero_production", nullable = false)
  54. private Integer numeroProduction;
  55. @Column(name = "flag_actif")
  56. private Boolean enabled = true;
  57. @ManyToOne
  58. @JoinColumn(name="numero_participant", referencedColumnName="numero_participant")
  59. private Participant participant;
  60. @Transient
  61. private Integer numeroGestionnaire;
  62. @Transient
  63. private String nomGestionnaire;
  64. @Type(PostgreSQLInetType.class)
  65. @Column(name = "adresse_ip", columnDefinition = "inet")
  66. private Inet adresseIP;
  67. @Enumerated(EnumType.STRING)
  68. private ProductionType type;
  69. @Column(length = 256, nullable = false)
  70. private String titre = "unknown compo entry name";
  71. @Column(length = 256, nullable = false)
  72. private String auteurs = "unknown author(s)";
  73. @Column(length = 256)
  74. private String groupes = "unknown group(s)";
  75. @Column(length = 128)
  76. private String plateforme;
  77. private String commentaire;
  78. private String informationsPrivees;
  79. @Column(length = 256)
  80. private String nomArchive;
  81. @Lob @JdbcTypeCode(Types.BINARY)
  82. @Column(name="archive")
  83. private byte[] archive;
  84. @Lob @JdbcTypeCode(Types.BINARY)
  85. @Column(name="vignette")
  86. private byte[] vignette;
  87. private Integer numeroVersion = 0;
  88. @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "production")
  89. private List<Presentation> presentations;
  90. @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "production01")
  91. private List<Bulletin> bulletins01;
  92. @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "production02")
  93. private List<Bulletin> bulletins02;
  94. @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "production03")
  95. private List<Bulletin> bulletins03;
  96. @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "production04")
  97. private List<Bulletin> bulletins04;
  98. @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "production05")
  99. private List<Bulletin> bulletins05;
  100. @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "production06")
  101. private List<Bulletin> bulletins06;
  102. @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "production07")
  103. private List<Bulletin> bulletins07;
  104. @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "production08")
  105. private List<Bulletin> bulletins08;
  106. @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "production09")
  107. private List<Bulletin> bulletins09;
  108. @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "production10")
  109. private List<Bulletin> bulletins10;
  110. public Production() { super(); }
  111. @Transient
  112. DateTimeFormatter df = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss", Locale.FRANCE);
  113. public void setDateCreation(LocalDateTime d) { this.dateCreation = d; }
  114. public void setDateCreation(String s) { this.dateCreation = LocalDateTime.parse(s, df); }
  115. public LocalDateTime getDateCreation() { return this.dateCreation; }
  116. public void setDateModification(LocalDateTime d) { this.dateModification = d; }
  117. public void setDateModification(String s) { this.dateModification = LocalDateTime.parse(s, df); }
  118. public LocalDateTime getDateModification() { return this.dateModification; }
  119. public void setNumeroProduction(Integer numeroProduction) { this.numeroProduction = numeroProduction; }
  120. public Integer getNumeroProduction() { return this.numeroProduction; }
  121. public void setEnabled(boolean b) { this.enabled = Boolean.valueOf(b); }
  122. public Boolean getEnabled() { return this.enabled; }
  123. @Transient
  124. public boolean isEnabled() { return (getEnabled().booleanValue()); }
  125. public void setParticipant(Participant p) { this.participant = p; }
  126. public Participant getParticipant() { return this.participant; }
  127. @Transient
  128. public void setNumeroGestionnaire(Integer num) { if (num != null) { this.numeroGestionnaire = num; } }
  129. @Transient
  130. public Integer getNumeroGestionnaire() { return this.numeroGestionnaire; }
  131. @Transient
  132. public void setNomGestionnaire(String str) { if (str != null) { this.nomGestionnaire = new String(str); } }
  133. @Transient
  134. public String getNomGestionnaire() { return this.nomGestionnaire; }
  135. public void setAdresseIP(Inet ip) { this.adresseIP = ip; }
  136. public void setAdresseIP(String ip) { this.adresseIP = new Inet(ip); }
  137. public String getAdresseIP() { return this.adresseIP.getAddress(); }
  138. public void setType(ProductionType enu) { this.type = enu; }
  139. public ProductionType getType() { return this.type; }
  140. public void setTitre(String str) { if (str != null) { this.titre = StringUtils.truncate(str, 256); } }
  141. public String getTitre() { return this.titre; }
  142. public void setAuteurs(String str) { if (str != null) { this.auteurs = StringUtils.truncate(str, 256); } }
  143. public String getAuteurs() { return this.auteurs; }
  144. public void setGroupes(String str) { if (str != null) { this.groupes = StringUtils.truncate(str, 256); } }
  145. public String getGroupes() { return this.groupes; }
  146. public void setPlateforme(String str) { if (str != null) { this.plateforme = StringUtils.truncate(str, 128); } }
  147. public String getPlateforme() { return this.plateforme; }
  148. public void setCommentaire(String str) { this.commentaire = new String(str); }
  149. public String getCommentaire() { return this.commentaire; }
  150. public void setInformationsPrivees(String str) { this.informationsPrivees = new String(str); }
  151. public String getInformationsPrivees() { return this.informationsPrivees; }
  152. public void setNomArchive(String str) { if (str != null) { this.nomArchive = StringUtils.truncate(str, 256); } }
  153. public String getNomArchive() { return this.nomArchive; }
  154. public void setArchive(String a)
  155. {
  156. if (a.startsWith("data:") && a.contains(",")) { a = a.split(",")[1]; }
  157. try { this.archive = Base64.getDecoder().decode(a); } catch(Exception e) { this.archive = null; }
  158. }
  159. @Transient
  160. public void setArchive(byte[] a) { this.archive = (a == null) ? null : a.clone(); }
  161. public String getArchive() { if (this.archive == null) { return ""; } return "data:application/zip;base64," + Base64.getEncoder().encodeToString(this.archive); }
  162. @Transient
  163. public byte[] getArchiveAsBinary() { return this.archive; }
  164. public void setVignette(String v)
  165. {
  166. String[] s;
  167. if (v.startsWith("data:") && v.contains(","))
  168. {
  169. s = v.split(",");
  170. v = s[1];
  171. }
  172. try
  173. {
  174. byte[] img = Base64.getDecoder().decode(v);
  175. ByteArrayInputStream bais = new ByteArrayInputStream(img);
  176. BufferedImage originalImage = ImageIO.read(bais);
  177. BufferedImage thumbnail = Thumbnails.of(originalImage).crop(Positions.CENTER).size(300, 300).asBufferedImage();
  178. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  179. ImageIO.write(thumbnail, "png", baos);
  180. this.vignette = baos.toByteArray();
  181. }
  182. catch(Exception e) { this.vignette = null; }
  183. }
  184. public void setVignette(byte[] a) { this.vignette = (a == null) ? null : a.clone(); }
  185. public String getVignette() { if (this.vignette == null) { return ""; } return "data:image/png;base64," + Base64.getEncoder().encodeToString(this.vignette); }
  186. //@Version
  187. public void setNumeroVersion(int n) { this.numeroVersion = Integer.valueOf(n); }
  188. public Integer getNumeroVersion() { return this.numeroVersion; }
  189. @Override
  190. public int hashCode()
  191. {
  192. final int prime = 42;
  193. int result = 1;
  194. result = (prime * result) + ((getNumeroProduction() == null) ? 0 : getNumeroProduction().hashCode());
  195. result = (prime * result) + ((getEnabled() == null) ? 0 : getEnabled().hashCode());
  196. result = (prime * result) + ((getParticipant() == null) ? 0 : getParticipant().hashCode());
  197. result = (prime * result) + ((getAdresseIP() == null) ? 0 : getAdresseIP().hashCode());
  198. result = (prime * result) + ((getType() == null) ? 0 : getType().hashCode());
  199. result = (prime * result) + ((getTitre() == null) ? 0 : getTitre().hashCode());
  200. result = (prime * result) + ((getAuteurs() == null) ? 0 : getAuteurs().hashCode());
  201. result = (prime * result) + ((getGroupes() == null) ? 0 : getGroupes().hashCode());
  202. result = (prime * result) + ((getPlateforme() == null) ? 0 : getPlateforme().hashCode());
  203. result = (prime * result) + ((getCommentaire() == null) ? 0 : getCommentaire().hashCode());
  204. result = (prime * result) + ((getInformationsPrivees() == null) ? 0 : getInformationsPrivees().hashCode());
  205. result = (prime * result) + ((getNomArchive() == null) ? 0 : getNomArchive().hashCode());
  206. result = (prime * result) + ((getNumeroVersion() == null) ? 0 : getNumeroVersion().hashCode());
  207. return result;
  208. }
  209. @Override
  210. public boolean equals(final Object obj)
  211. {
  212. if (this == obj) { return true; }
  213. if (obj == null) { return false; }
  214. if (getClass() != obj.getClass()) { return false; }
  215. final Production p = (Production) obj;
  216. if (getNumeroProduction() == null) { if (p.getNumeroProduction() == null) { return false; } } else if (!getNumeroProduction().equals(p.getNumeroProduction())) { return false; }
  217. return true;
  218. }
  219. @Override
  220. public String toString()
  221. {
  222. final StringBuilder builder = new StringBuilder();
  223. builder.append("Production [id=")
  224. .append(numeroProduction)
  225. .append(", participant=").append(participant)
  226. .append(", IP=").append(adresseIP)
  227. .append(", type=").append(type)
  228. .append(", titre=").append(titre)
  229. .append(", auteurs=").append(auteurs)
  230. .append(", groupes=").append(groupes)
  231. .append(", plateforme=").append(plateforme)
  232. .append(", commentaire=").append(commentaire)
  233. .append(", nomArchive=").append(nomArchive)
  234. .append(", archive=").append("" + archive.length + " bytes")
  235. .append(", vignette=").append("" + vignette.length + " bytes")
  236. .append(", version=").append(numeroVersion)
  237. .append(", créé=").append(dateCreation)
  238. .append(", modifié=").append(dateModification)
  239. .append(enabled ? "" : ", inactif")
  240. .append("]");
  241. return builder.toString();
  242. }
  243. }