1+ package com .countyhospital .healthapi .observation .domain ;
2+
3+ import com .countyhospital .healthapi .patient .domain .Patient ;
4+ import com .countyhospital .healthapi .encounter .domain .Encounter ;
5+
6+ import jakarta .persistence .*;
7+ import jakarta .validation .constraints .*;
8+ import java .time .LocalDateTime ;
9+ import java .util .Objects ;
10+
11+ @ Entity
12+ @ Table (name = "observations" , indexes = {
13+ @ Index (name = "idx_observation_patient_id" , columnList = "patient_id" ),
14+ @ Index (name = "idx_observation_encounter_id" , columnList = "encounter_id" ),
15+ @ Index (name = "idx_observation_effective_date" , columnList = "effectiveDateTime" ),
16+ @ Index (name = "idx_observation_code" , columnList = "code" )
17+ })
18+ public class Observation {
19+
20+ @ Id
21+ @ GeneratedValue (strategy = GenerationType .IDENTITY )
22+ private Long id ;
23+
24+ @ NotNull (message = "Patient is required" )
25+ @ ManyToOne (fetch = FetchType .LAZY )
26+ @ JoinColumn (name = "patient_id" , nullable = false )
27+ private Patient patient ;
28+
29+ @ ManyToOne (fetch = FetchType .LAZY )
30+ @ JoinColumn (name = "encounter_id" )
31+ private Encounter encounter ;
32+
33+ @ NotBlank (message = "Observation code is required" )
34+ @ Size (max = 50 , message = "Code must not exceed 50 characters" )
35+ @ Column (nullable = false , length = 50 )
36+ private String code ;
37+
38+ @ NotBlank (message = "Display name is required" )
39+ @ Size (max = 200 , message = "Display name must not exceed 200 characters" )
40+ @ Column (name = "display_name" , nullable = false , length = 200 )
41+ private String displayName ;
42+
43+ @ NotBlank (message = "Value is required" )
44+ @ Size (max = 500 , message = "Value must not exceed 500 characters" )
45+ @ Column (nullable = false , length = 500 )
46+ private String value ;
47+
48+ @ Size (max = 50 , message = "Unit must not exceed 50 characters" )
49+ @ Column (length = 50 )
50+ private String unit ;
51+
52+ @ NotNull (message = "Effective date time is required" )
53+ @ Column (nullable = false )
54+ private LocalDateTime effectiveDateTime ;
55+
56+ @ Column (name = "created_at" , nullable = false , updatable = false )
57+ private LocalDateTime createdAt ;
58+
59+ // Default constructor for JPA
60+ public Observation () {}
61+
62+ // Constructor for creating new observations
63+ public Observation (Patient patient , Encounter encounter , String code ,
64+ String displayName , String value , String unit , LocalDateTime effectiveDateTime ) {
65+ this .patient = patient ;
66+ this .encounter = encounter ;
67+ this .code = code ;
68+ this .displayName = displayName ;
69+ this .value = value ;
70+ this .unit = unit ;
71+ this .effectiveDateTime = effectiveDateTime ;
72+ this .createdAt = LocalDateTime .now ();
73+ }
74+
75+ @ PrePersist
76+ protected void onCreate () {
77+ createdAt = LocalDateTime .now ();
78+ }
79+
80+ // Getters and setters
81+ public Long getId () { return id ; }
82+ public void setId (Long id ) { this .id = id ; }
83+
84+ public Patient getPatient () { return patient ; }
85+ public void setPatient (Patient patient ) { this .patient = patient ; }
86+
87+ public Encounter getEncounter () { return encounter ; }
88+ public void setEncounter (Encounter encounter ) { this .encounter = encounter ; }
89+
90+ public String getCode () { return code ; }
91+ public void setCode (String code ) { this .code = code ; }
92+
93+ public String getDisplayName () { return displayName ; }
94+ public void setDisplayName (String displayName ) { this .displayName = displayName ; }
95+
96+ public String getValue () { return value ; }
97+ public void setValue (String value ) { this .value = value ; }
98+
99+ public String getUnit () { return unit ; }
100+ public void setUnit (String unit ) { this .unit = unit ; }
101+
102+ public LocalDateTime getEffectiveDateTime () { return effectiveDateTime ; }
103+ public void setEffectiveDateTime (LocalDateTime effectiveDateTime ) { this .effectiveDateTime = effectiveDateTime ; }
104+
105+ public LocalDateTime getCreatedAt () { return createdAt ; }
106+ public void setCreatedAt (LocalDateTime createdAt ) { this .createdAt = createdAt ; }
107+
108+ // Equals and hashCode
109+ @ Override
110+ public boolean equals (Object o ) {
111+ if (this == o ) return true ;
112+ if (o == null || getClass () != o .getClass ()) return false ;
113+ Observation that = (Observation ) o ;
114+ return Objects .equals (id , that .id );
115+ }
116+
117+ @ Override
118+ public int hashCode () {
119+ return Objects .hash (id );
120+ }
121+
122+ @ Override
123+ public String toString () {
124+ return "Observation{" +
125+ "id=" + id +
126+ ", patientId=" + (patient != null ? patient .getId () : null ) +
127+ ", encounterId=" + (encounter != null ? encounter .getId () : null ) +
128+ ", code='" + code + '\'' +
129+ ", value='" + value + '\'' +
130+ ", effectiveDateTime=" + effectiveDateTime +
131+ '}' ;
132+ }
133+ }
0 commit comments