-
Notifications
You must be signed in to change notification settings - Fork 274
/
CommandsHandler.java
59 lines (51 loc) · 2.12 KB
/
CommandsHandler.java
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
package io.dddbyexamples.factory.demand.forecasting.command;
import io.dddbyexamples.factory.demand.forecasting.persistence.DocumentEntity;
import lombok.AllArgsConstructor;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.data.rest.core.annotation.HandleAfterCreate;
import org.springframework.data.rest.core.annotation.HandleBeforeCreate;
import org.springframework.data.rest.core.annotation.HandleBeforeSave;
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import io.dddbyexamples.factory.demand.forecasting.DemandService;
import java.time.Clock;
import java.time.LocalDate;
@Component
@Transactional
@AllArgsConstructor
@RepositoryEventHandler
public class CommandsHandler {
private final DemandService service;
private final DemandAdjustmentDao adjustments;
private final RequiredReviewDao reviews;
private final Clock clock;
@HandleBeforeCreate
@HandleBeforeSave
public void adjust(DemandAdjustmentEntity adjustment) {
LocalDate latest = adjustment.getAdjustment()
.latestAdjustment()
.orElse(LocalDate.now(clock));
adjustment.setCleanAfter(latest.plusDays(7));
service.adjust(adjustment.getAdjustment());
}
@HandleAfterCreate
public void process(DocumentEntity document) {
service.process(document.getDocument());
}
@HandleBeforeSave
public void review(RequiredReviewEntity review) {
if (review.decisionTaken()) {
review.setCleanAfter(LocalDate.now(clock).plusDays(7));
service.review(review.getReviewDecision());
}
}
@Scheduled(cron = "0 0 12 * * ?")
@EventListener(ApplicationReadyEvent.class)
public void clean() {
adjustments.deleteByCleanAfterGreaterThanEqual(LocalDate.now(clock));
reviews.deleteByCleanAfterGreaterThanEqual(LocalDate.now(clock));
}
}