Message.java 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.pharmacopoeia.entity;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.core.type.TypeReference;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import jakarta.persistence.*;
  6. import lombok.*;
  7. import org.hibernate.annotations.JdbcTypeCode;
  8. import org.hibernate.type.SqlTypes;
  9. import java.time.Instant;
  10. import java.util.List;
  11. import java.util.Map;
  12. @Entity
  13. @Table(name = "messages")
  14. @Data
  15. @NoArgsConstructor
  16. @AllArgsConstructor
  17. @Builder
  18. public class Message {
  19. @Id
  20. @GeneratedValue(strategy = GenerationType.IDENTITY)
  21. private Long id;
  22. @Column(name = "conversation_id", nullable = false, length = 64)
  23. private String conversationId;
  24. @Column(name = "user_key", length = 128)
  25. private String userKey;
  26. @Column(nullable = false, length = 32)
  27. private String role;
  28. @Column(columnDefinition = "text", nullable = false)
  29. private String content;
  30. @Column(length = 32)
  31. private String intent;
  32. @JdbcTypeCode(SqlTypes.JSON)
  33. @Column(name = "sources", columnDefinition = "jsonb")
  34. private String sources;
  35. @JdbcTypeCode(SqlTypes.JSON)
  36. @Column(name = "brand_recommendations", columnDefinition = "jsonb")
  37. private String brandRecommendations;
  38. @Column(length = 32)
  39. private String feedback;
  40. @Column(name = "created_at")
  41. private Instant createdAt;
  42. @PrePersist
  43. void prePersist() {
  44. createdAt = Instant.now();
  45. }
  46. @Transient
  47. private static final ObjectMapper mapper = new ObjectMapper();
  48. public List<Map<String, Object>> getSourcesList() {
  49. if (sources == null || sources.isBlank()) return List.of();
  50. try {
  51. return mapper.readValue(sources, new TypeReference<List<Map<String, Object>>>() {});
  52. } catch (JsonProcessingException e) {
  53. return List.of();
  54. }
  55. }
  56. public void setSourcesList(List<Map<String, Object>> sourcesList) {
  57. try {
  58. this.sources = sourcesList != null ? mapper.writeValueAsString(sourcesList) : null;
  59. } catch (JsonProcessingException e) {
  60. this.sources = null;
  61. }
  62. }
  63. public List<Map<String, Object>> getBrandRecommendationsList() {
  64. if (brandRecommendations == null || brandRecommendations.isBlank()) return List.of();
  65. try {
  66. return mapper.readValue(brandRecommendations, new TypeReference<List<Map<String, Object>>>() {});
  67. } catch (JsonProcessingException e) {
  68. return List.of();
  69. }
  70. }
  71. public void setBrandRecommendationsList(List<Map<String, Object>> recs) {
  72. try {
  73. this.brandRecommendations = recs != null ? mapper.writeValueAsString(recs) : null;
  74. } catch (JsonProcessingException e) {
  75. this.brandRecommendations = null;
  76. }
  77. }
  78. }