29
29
import org .springframework .modulith .events .core .PublicationTargetIdentifier ;
30
30
import org .springframework .modulith .events .core .TargetEventPublication ;
31
31
import org .springframework .modulith .events .support .CompletionMode ;
32
+ import org .springframework .stereotype .Repository ;
32
33
import org .springframework .transaction .annotation .Transactional ;
33
34
import org .springframework .util .Assert ;
34
35
41
42
* @author Cora Iberkleid
42
43
*/
43
44
@ Transactional
45
+ @ Repository
44
46
class JpaEventPublicationRepository implements EventPublicationRepository {
45
47
46
48
private static String BY_EVENT_AND_LISTENER_ID = """
47
49
select p
48
- from JpaEventPublication p
50
+ from DefaultJpaEventPublication p
49
51
where
50
52
p.serializedEvent = ?1
51
53
and p.listenerId = ?2
@@ -63,7 +65,7 @@ class JpaEventPublicationRepository implements EventPublicationRepository {
63
65
64
66
private static String INCOMPLETE = """
65
67
select p
66
- from JpaEventPublication p
68
+ from DefaultJpaEventPublication p
67
69
where
68
70
p.completionDate is null
69
71
order by
@@ -72,7 +74,7 @@ class JpaEventPublicationRepository implements EventPublicationRepository {
72
74
73
75
private static String INCOMPLETE_BEFORE = """
74
76
select p
75
- from JpaEventPublication p
77
+ from DefaultJpaEventPublication p
76
78
where
77
79
p.completionDate is null
78
80
and p.publicationDate < ?1
@@ -81,34 +83,34 @@ class JpaEventPublicationRepository implements EventPublicationRepository {
81
83
""" ;
82
84
83
85
private static final String MARK_COMPLETED_BY_EVENT_AND_LISTENER_ID = """
84
- update JpaEventPublication p
86
+ update DefaultJpaEventPublication p
85
87
set p.completionDate = ?3
86
88
where p.serializedEvent = ?1
87
89
and p.listenerId = ?2
88
90
and p.completionDate is null
89
91
""" ;
90
92
91
93
private static final String MARK_COMPLETED_BY_ID = """
92
- update JpaEventPublication p
94
+ update DefaultJpaEventPublication p
93
95
set p.completionDate = ?2
94
96
where p.id = ?1
95
97
""" ;
96
98
97
99
private static final String DELETE = """
98
100
delete
99
- from JpaEventPublication p
101
+ from DefaultJpaEventPublication p
100
102
where p.id in ?1
101
103
""" ;
102
104
103
105
private static final String DELETE_BY_EVENT_AND_LISTENER_ID = """
104
- delete JpaEventPublication p
106
+ delete DefaultJpaEventPublication p
105
107
where p.serializedEvent = ?1
106
108
and p.listenerId = ?2
107
109
""" ;
108
110
109
111
private static final String DELETE_BY_ID = """
110
112
delete
111
- from JpaEventPublication p
113
+ from DefaultJpaEventPublication p
112
114
where p.id = ?1
113
115
""" ;
114
116
@@ -152,14 +154,12 @@ public JpaEventPublicationRepository(EntityManager entityManager, EventSerialize
152
154
this .serializer = serializer ;
153
155
this .completionMode = completionMode ;
154
156
155
- var archiveEntityName = completionMode == CompletionMode .ARCHIVE
156
- ? ArchivedJpaEventPublication .class .getSimpleName ()
157
- : JpaEventPublication .class .getSimpleName ();
157
+ var archiveEntityName = getCompletedEntityType ().getSimpleName ();
158
158
159
159
this .getCompleted = COMPLETE .formatted (archiveEntityName );
160
- this .deleteCompleted = DELETE_COMPLETED .formatted (archiveEntityName );
160
+ this .deleteCompleted = DELETE_COMPLETED .formatted (archiveEntityName );
161
161
this .deleteCompletedBefore = DELETE_COMPLETED_BEFORE .formatted (archiveEntityName );
162
- }
162
+ }
163
163
164
164
/*
165
165
* (non-Javadoc)
@@ -192,15 +192,13 @@ public void markCompleted(Object event, PublicationTargetIdentifier identifier,
192
192
193
193
} else if (completionMode == CompletionMode .ARCHIVE ) {
194
194
195
- var publication = entityManager .createQuery (BY_EVENT_AND_LISTENER_ID , JpaEventPublication .class )
195
+ var publication = entityManager .createQuery (BY_EVENT_AND_LISTENER_ID , JpaEventPublication .getIncompleteType () )
196
196
.setParameter (1 , serializedEvent )
197
197
.setParameter (2 , identifierValue )
198
198
.getSingleResult ();
199
199
200
- var archived = publication .archive (completionDate );
201
-
202
200
entityManager .remove (publication );
203
- entityManager .persist (archived );
201
+ entityManager .persist (publication . archive ( completionDate ) );
204
202
205
203
} else {
206
204
@@ -227,12 +225,10 @@ public void markCompleted(UUID identifier, Instant completionDate) {
227
225
228
226
} else if (completionMode == CompletionMode .ARCHIVE ) {
229
227
230
- var publication = entityManager .find (JpaEventPublication .class , identifier );
231
-
232
- var archived = publication .archive (completionDate );
228
+ var publication = entityManager .find (JpaEventPublication .getIncompleteType (), identifier );
233
229
234
230
entityManager .remove (publication );
235
- entityManager .persist (archived );
231
+ entityManager .persist (publication . archive ( completionDate ) );
236
232
237
233
} else {
238
234
@@ -251,7 +247,7 @@ public void markCompleted(UUID identifier, Instant completionDate) {
251
247
@ Transactional (readOnly = true )
252
248
public List <TargetEventPublication > findIncompletePublications () {
253
249
254
- return entityManager .createQuery (INCOMPLETE , JpaEventPublication .class )
250
+ return entityManager .createQuery (INCOMPLETE , JpaEventPublication .getIncompleteType () )
255
251
.getResultStream ()
256
252
.map (this ::entityToDomain )
257
253
.toList ();
@@ -265,7 +261,7 @@ public List<TargetEventPublication> findIncompletePublications() {
265
261
@ Transactional (readOnly = true )
266
262
public List <TargetEventPublication > findIncompletePublicationsPublishedBefore (Instant instant ) {
267
263
268
- return entityManager .createQuery (INCOMPLETE_BEFORE , JpaEventPublication .class )
264
+ return entityManager .createQuery (INCOMPLETE_BEFORE , JpaEventPublication .getIncompleteType () )
269
265
.setParameter (1 , instant )
270
266
.getResultStream ()
271
267
.map (this ::entityToDomain )
@@ -292,9 +288,7 @@ public Optional<TargetEventPublication> findIncompletePublicationsByEventAndTarg
292
288
@ Override
293
289
public List <TargetEventPublication > findCompletedPublications () {
294
290
295
- var type = completionMode == CompletionMode .ARCHIVE
296
- ? ArchivedJpaEventPublication .class
297
- : JpaEventPublication .class ;
291
+ var type = getCompletedEntityType ();
298
292
299
293
return entityManager .createQuery (getCompleted , type )
300
294
.getResultList ()
@@ -338,12 +332,22 @@ public void deleteCompletedPublicationsBefore(Instant instant) {
338
332
.executeUpdate ();
339
333
}
340
334
341
- private Optional <JpaEventPublication > findEntityBySerializedEventAndListenerIdAndCompletionDateNull ( //
335
+ /**
336
+ * Returns the type representing completed event publications.
337
+ *
338
+ * @return will never be {@literal null}.
339
+ * @since 1.3
340
+ */
341
+ public Class <? extends JpaEventPublication > getCompletedEntityType () {
342
+ return JpaEventPublication .getCompletedType (completionMode );
343
+ }
344
+
345
+ private Optional <? extends JpaEventPublication > findEntityBySerializedEventAndListenerIdAndCompletionDateNull ( //
342
346
Object event , PublicationTargetIdentifier listenerId ) {
343
347
344
348
var serializedEvent = serializeEvent (event );
345
349
346
- var query = entityManager .createQuery (BY_EVENT_AND_LISTENER_ID , JpaEventPublication .class )
350
+ var query = entityManager .createQuery (BY_EVENT_AND_LISTENER_ID , JpaEventPublication .getIncompleteType () )
347
351
.setParameter (1 , serializedEvent )
348
352
.setParameter (2 , listenerId .getValue ());
349
353
@@ -355,9 +359,11 @@ private String serializeEvent(Object event) {
355
359
}
356
360
357
361
private JpaEventPublication domainToEntity (TargetEventPublication domain ) {
358
- return new JpaEventPublication (domain .getIdentifier (), domain .getPublicationDate (),
359
- domain .getTargetIdentifier ().getValue (),
360
- serializeEvent (domain .getEvent ()), domain .getEvent ().getClass ());
362
+
363
+ var event = domain .getEvent ();
364
+
365
+ return JpaEventPublication .of (domain .getIdentifier (), domain .getPublicationDate (),
366
+ domain .getTargetIdentifier ().getValue (), serializeEvent (event ), event .getClass ());
361
367
}
362
368
363
369
private TargetEventPublication entityToDomain (JpaEventPublication entity ) {
0 commit comments