6
6
import java .util .Collections ;
7
7
import java .util .List ;
8
8
9
+ import jakarta .persistence .*;
9
10
import org .eventplanner .events .entities .Event ;
10
11
import org .eventplanner .events .entities .Registration ;
11
12
import org .eventplanner .events .entities .Slot ;
15
16
16
17
import com .fasterxml .jackson .core .type .TypeReference ;
17
18
import com .fasterxml .jackson .databind .ObjectMapper ;
18
- import jakarta .persistence .Column ;
19
- import jakarta .persistence .Entity ;
20
- import jakarta .persistence .Id ;
21
- import jakarta .persistence .Table ;
22
19
import lombok .EqualsAndHashCode ;
23
20
import lombok .Getter ;
24
21
import lombok .NoArgsConstructor ;
@@ -68,6 +65,15 @@ public class EventJpaEntity {
68
65
@ Column (name = "registrations" )
69
66
private String registrationsRaw ;
70
67
68
+ @ Transient
69
+ private List <Location > locations ;
70
+
71
+ @ Transient
72
+ private List <Slot > slots ;
73
+
74
+ @ Transient
75
+ private List <Registration > registrations ;
76
+
71
77
public static @ NonNull EventJpaEntity fromDomain (@ NonNull Event domain ) {
72
78
var eventJpaEntity = new EventJpaEntity ();
73
79
eventJpaEntity .setKey (domain .getKey ().value ());
@@ -77,59 +83,74 @@ public class EventJpaEntity {
77
83
eventJpaEntity .setNote (domain .getNote ());
78
84
eventJpaEntity .setDescription (domain .getDescription ());
79
85
eventJpaEntity .setStart (domain .getStart ().format (DateTimeFormatter .ISO_DATE_TIME ));
80
- eventJpaEntity .setEnd (domain .getStart ().format (DateTimeFormatter .ISO_DATE_TIME ));
86
+ eventJpaEntity .setEnd (domain .getEnd ().format (DateTimeFormatter .ISO_DATE_TIME ));
81
87
eventJpaEntity .setLocations (domain .getLocations ());
82
88
eventJpaEntity .setSlots (domain .getSlots ());
83
89
eventJpaEntity .setRegistrations (domain .getRegistrations ());
84
90
return eventJpaEntity ;
85
91
}
86
92
87
93
public List <Location > getLocations () {
94
+ if (locations != null ) {
95
+ return locations ;
96
+ }
88
97
try {
89
- return objectMapper .readValue (locationsRaw , new TypeReference <> () {
90
- } );
98
+ var entities = objectMapper .readValue (locationsRaw , new TypeReference <List < LocationJsonEntity >> () {});
99
+ locations = entities . stream (). map ( LocationJsonEntity :: toDomain ). toList ( );
91
100
} catch (IOException e ) {
92
- return Collections .emptyList ();
101
+ locations = Collections .emptyList ();
93
102
}
103
+ return locations ;
94
104
}
95
105
96
106
public void setLocations (List <Location > locations ) {
97
107
try {
98
- this .locationsRaw = objectMapper .writeValueAsString (locations );
108
+ var entities = locations .stream ().map (LocationJsonEntity ::fromDomain ).toList ();
109
+ this .locationsRaw = objectMapper .writeValueAsString (entities );
99
110
} catch (IOException e ) {
100
111
this .locationsRaw = "[]" ;
101
112
}
102
113
}
103
114
104
115
public List <Slot > getSlots () {
116
+ if (slots != null ) {
117
+ return slots ;
118
+ }
105
119
try {
106
- return objectMapper .readValue (slotsRaw , new TypeReference <> () {
107
- } );
120
+ var entities = objectMapper .readValue (slotsRaw , new TypeReference <List < SlotJsonEntity >> () {});
121
+ slots = entities . stream (). map ( SlotJsonEntity :: toDomain ). toList ( );
108
122
} catch (IOException e ) {
109
- return Collections .emptyList ();
123
+ slots = Collections .emptyList ();
110
124
}
125
+ return slots ;
111
126
}
112
127
113
128
public void setSlots (List <Slot > slots ) {
114
129
try {
115
- this .slotsRaw = objectMapper .writeValueAsString (slots );
130
+ var entities = slots .stream ().map (SlotJsonEntity ::fromDomain ).toList ();
131
+ this .slotsRaw = objectMapper .writeValueAsString (entities );
116
132
} catch (IOException e ) {
117
133
this .slotsRaw = "[]" ;
118
134
}
119
135
}
120
136
121
137
public List <Registration > getRegistrations () {
138
+ if (registrations != null ) {
139
+ return registrations ;
140
+ }
122
141
try {
123
- return objectMapper .readValue (registrationsRaw , new TypeReference <> () {
124
- } );
142
+ var entities = objectMapper .readValue (registrationsRaw , new TypeReference <List < RegistrationJsonEntity >> () {});
143
+ registrations = entities . stream (). map ( RegistrationJsonEntity :: toDomain ). toList ( );
125
144
} catch (IOException e ) {
126
- return Collections .emptyList ();
145
+ registrations = Collections .emptyList ();
127
146
}
147
+ return registrations ;
128
148
}
129
149
130
150
public void setRegistrations (List <Registration > registrations ) {
131
151
try {
132
- this .registrationsRaw = objectMapper .writeValueAsString (registrations );
152
+ var entities = registrations .stream ().map (RegistrationJsonEntity ::fromDomain ).toList ();
153
+ this .registrationsRaw = objectMapper .writeValueAsString (entities );
133
154
} catch (IOException e ) {
134
155
this .registrationsRaw = "[]" ;
135
156
}
0 commit comments