38
38
* @author Oliver Drotbohm
39
39
* @author Dmitry Belyaev
40
40
* @author Björn Kieling
41
+ * @author Cora Iberkleid
41
42
*/
42
43
@ Transactional
43
44
class JpaEventPublicationRepository implements EventPublicationRepository {
@@ -53,7 +54,7 @@ class JpaEventPublicationRepository implements EventPublicationRepository {
53
54
54
55
private static String COMPLETE = """
55
56
select p
56
- from JpaEventPublication p
57
+ from %s p
57
58
where
58
59
p.completionDate is not null
59
60
order by
@@ -113,14 +114,14 @@ class JpaEventPublicationRepository implements EventPublicationRepository {
113
114
114
115
private static final String DELETE_COMPLETED = """
115
116
delete
116
- from JpaEventPublication p
117
+ from %s p
117
118
where
118
119
p.completionDate is not null
119
120
""" ;
120
121
121
122
private static final String DELETE_COMPLETED_BEFORE = """
122
123
delete
123
- from JpaEventPublication p
124
+ from %s p
124
125
where
125
126
p.completionDate < ?1
126
127
""" ;
@@ -131,6 +132,8 @@ class JpaEventPublicationRepository implements EventPublicationRepository {
131
132
private final EventSerializer serializer ;
132
133
private final CompletionMode completionMode ;
133
134
135
+ private final String getCompleted , deleteCompleted , deleteCompletedBefore ;
136
+
134
137
/**
135
138
* Creates a new {@link JpaEventPublicationRepository} for the given {@link EntityManager} and
136
139
* {@link EventSerializer}.
@@ -148,7 +151,15 @@ public JpaEventPublicationRepository(EntityManager entityManager, EventSerialize
148
151
this .entityManager = entityManager ;
149
152
this .serializer = serializer ;
150
153
this .completionMode = completionMode ;
151
- }
154
+
155
+ var archiveEntityName = completionMode == CompletionMode .ARCHIVE
156
+ ? ArchivedJpaEventPublication .class .getSimpleName ()
157
+ : JpaEventPublication .class .getSimpleName ();
158
+
159
+ this .getCompleted = COMPLETE .formatted (archiveEntityName );
160
+ this .deleteCompleted = DELETE_COMPLETED .formatted (archiveEntityName );
161
+ this .deleteCompletedBefore = DELETE_COMPLETED_BEFORE .formatted (archiveEntityName );
162
+ }
152
163
153
164
/*
154
165
* (non-Javadoc)
@@ -179,6 +190,18 @@ public void markCompleted(Object event, PublicationTargetIdentifier identifier,
179
190
.setParameter (2 , identifierValue )
180
191
.executeUpdate ();
181
192
193
+ } else if (completionMode == CompletionMode .ARCHIVE ) {
194
+
195
+ var publication = entityManager .createQuery (BY_EVENT_AND_LISTENER_ID , JpaEventPublication .class )
196
+ .setParameter (1 , serializedEvent )
197
+ .setParameter (2 , identifierValue )
198
+ .getSingleResult ();
199
+
200
+ var archived = publication .archive (completionDate );
201
+
202
+ entityManager .remove (publication );
203
+ entityManager .persist (archived );
204
+
182
205
} else {
183
206
184
207
entityManager .createQuery (MARK_COMPLETED_BY_EVENT_AND_LISTENER_ID )
@@ -202,6 +225,15 @@ public void markCompleted(UUID identifier, Instant completionDate) {
202
225
.setParameter (1 , identifier )
203
226
.executeUpdate ();
204
227
228
+ } else if (completionMode == CompletionMode .ARCHIVE ) {
229
+
230
+ var publication = entityManager .find (JpaEventPublication .class , identifier );
231
+
232
+ var archived = publication .archive (completionDate );
233
+
234
+ entityManager .remove (publication );
235
+ entityManager .persist (archived );
236
+
205
237
} else {
206
238
207
239
entityManager .createQuery (MARK_COMPLETED_BY_ID )
@@ -260,7 +292,11 @@ public Optional<TargetEventPublication> findIncompletePublicationsByEventAndTarg
260
292
@ Override
261
293
public List <TargetEventPublication > findCompletedPublications () {
262
294
263
- return entityManager .createQuery (COMPLETE , JpaEventPublication .class )
295
+ var type = completionMode == CompletionMode .ARCHIVE
296
+ ? ArchivedJpaEventPublication .class
297
+ : JpaEventPublication .class ;
298
+
299
+ return entityManager .createQuery (getCompleted , type )
264
300
.getResultList ()
265
301
.stream ()
266
302
.map (this ::entityToDomain )
@@ -285,7 +321,7 @@ public void deletePublications(List<UUID> identifiers) {
285
321
*/
286
322
@ Override
287
323
public void deleteCompletedPublications () {
288
- entityManager .createQuery (DELETE_COMPLETED ).executeUpdate ();
324
+ entityManager .createQuery (deleteCompleted ).executeUpdate ();
289
325
}
290
326
291
327
/*
@@ -297,7 +333,7 @@ public void deleteCompletedPublicationsBefore(Instant instant) {
297
333
298
334
Assert .notNull (instant , "Instant must not be null!" );
299
335
300
- entityManager .createQuery (DELETE_COMPLETED_BEFORE )
336
+ entityManager .createQuery (deleteCompletedBefore )
301
337
.setParameter (1 , instant )
302
338
.executeUpdate ();
303
339
}
@@ -341,6 +377,7 @@ private static class JpaEventPublicationAdapter implements TargetEventPublicatio
341
377
342
378
private final JpaEventPublication publication ;
343
379
private final EventSerializer serializer ;
380
+ private Object deserializedEvent ;
344
381
345
382
/**
346
383
* Creates a new {@link JpaEventPublicationAdapter} for the given {@link JpaEventPublication} and
@@ -373,7 +410,12 @@ public UUID getIdentifier() {
373
410
*/
374
411
@ Override
375
412
public Object getEvent () {
376
- return serializer .deserialize (publication .serializedEvent , publication .eventType );
413
+
414
+ if (deserializedEvent == null ) {
415
+ this .deserializedEvent = serializer .deserialize (publication .serializedEvent , publication .eventType );
416
+ }
417
+
418
+ return deserializedEvent ;
377
419
}
378
420
379
421
/*
0 commit comments