-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationService.java
More file actions
63 lines (52 loc) · 2.41 KB
/
NotificationService.java
File metadata and controls
63 lines (52 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package uk.gov.hmcts.cp.subscription.services;
import static java.time.Duration.ofSeconds;
import static org.awaitility.Awaitility.await;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import uk.gov.hmcts.cp.material.openapi.api.MaterialApi;
import uk.gov.hmcts.cp.material.openapi.model.MaterialMetadata;
import uk.gov.hmcts.cp.openapi.model.PcrEventPayload;
import uk.gov.hmcts.cp.subscription.model.EntityEventType;
import java.time.Duration;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
@Service
@Slf4j
public class NotificationService {
private final MaterialApi materialApi;
private final DocumentService documentService;
private final Duration waitTimeout;
private final Duration pollInterval;
@Autowired
public NotificationService(final MaterialApi materialApi,
final DocumentService documentService) {
this(materialApi, documentService, ofSeconds(30), ofSeconds(5));
}
public NotificationService(final MaterialApi materialApi,
final DocumentService documentService,
final Duration waitTimeout,
final Duration pollInterval) {
this.materialApi = materialApi;
this.documentService = documentService;
this.waitTimeout = waitTimeout;
this.pollInterval = pollInterval;
}
public void processInboundEvent(final PcrEventPayload pcrEventPayload) {
final MaterialMetadata materialMetadata = waitForMaterialMetadata(pcrEventPayload.getMaterialId());
final EntityEventType eventType = EntityEventType.valueOf(pcrEventPayload.getEventType().name());
documentService.saveDocumentMapping(materialMetadata.getMaterialId(), eventType);
}
private MaterialMetadata waitForMaterialMetadata(final UUID materialId) {
final AtomicReference<MaterialMetadata> materialResponse = new AtomicReference<>();
await()
.atMost(waitTimeout)
.pollInterval(pollInterval)
.until(() -> {
final MaterialMetadata response = materialApi.getMaterialMetadataByMaterialId(materialId);
materialResponse.set(response);
return response != null;
});
return materialResponse.get();
}
}