Series: publishing (STATE) CHANGE
This commit is contained in:
parent
a0d15b1aa4
commit
7f39ee6c41
@ -1,5 +1,5 @@
|
||||
package de.ph87.data;
|
||||
|
||||
public enum Action {
|
||||
CREATED, CHANGED, DELETED
|
||||
CREATED, CHANGED, STATE, DELETED
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ package de.ph87.data.series;
|
||||
import de.ph87.data.*;
|
||||
import lombok.*;
|
||||
import lombok.extern.slf4j.*;
|
||||
import org.springframework.context.*;
|
||||
import org.springframework.stereotype.*;
|
||||
import org.springframework.transaction.annotation.*;
|
||||
|
||||
@ -16,6 +17,8 @@ public class SeriesService {
|
||||
|
||||
private final SeriesRepository seriesRepository;
|
||||
|
||||
private final ApplicationEventPublisher applicationEventPublisher;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public SeriesDto modify(final long id, @NonNull final Consumer<Series> modifier) {
|
||||
final Series series = getById(id);
|
||||
@ -43,7 +46,12 @@ public class SeriesService {
|
||||
@NonNull
|
||||
private SeriesDto publish(@NonNull final Series series, @NonNull final Action action) {
|
||||
final SeriesDto dto = toDto(series);
|
||||
log.info("Series {}: {}", action, series);
|
||||
if (action == Action.STATE) {
|
||||
log.debug("Series {}: {}", action, series);
|
||||
} else {
|
||||
log.info("Series {}: {}", action, series);
|
||||
}
|
||||
applicationEventPublisher.publishEvent(dto);
|
||||
return dto;
|
||||
}
|
||||
|
||||
@ -53,7 +61,7 @@ public class SeriesService {
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public Series getOrCreate(@NonNull final SeriesInbound inbound, @NonNull final SeriesType type) {
|
||||
public <R> R onInbound(@NonNull final SeriesInbound inbound, @NonNull final SeriesType type, @NonNull final Function<Series, R> mapper) {
|
||||
final Series series = seriesRepository
|
||||
.findByName(inbound.getName())
|
||||
.orElseGet(() -> {
|
||||
@ -65,7 +73,9 @@ public class SeriesService {
|
||||
log.warn("Existing Series type does not match requested type: requested={}, series={}", type, series);
|
||||
}
|
||||
series.update(inbound);
|
||||
return series;
|
||||
final R result = mapper.apply(series);
|
||||
publish(series, Action.STATE);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -15,6 +15,7 @@ import org.springframework.stereotype.*;
|
||||
import org.springframework.transaction.annotation.*;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.*;
|
||||
import java.util.stream.*;
|
||||
|
||||
@Slf4j
|
||||
@ -40,31 +41,36 @@ public class MeterService {
|
||||
private final MeterYearRepository meterYearRepository;
|
||||
|
||||
@EventListener(MeterInbound.class)
|
||||
public void onEvent(@NonNull final MeterInbound inbound) {
|
||||
final Meter meter = getOrCreate(inbound);
|
||||
public void onInbound(@NonNull final MeterInbound inbound) {
|
||||
onInbound(inbound, meter -> {
|
||||
final MeterValue.Id five = new MeterValue.Id(meter, inbound.date, Alignment.FIVE);
|
||||
meterFiveRepository.findById(five).ifPresentOrElse(existing -> existing.update(inbound.value), () -> meterFiveRepository.save(new MeterFive(five, inbound.value)));
|
||||
|
||||
final MeterValue.Id five = new MeterValue.Id(meter, inbound.date, Alignment.FIVE);
|
||||
meterFiveRepository.findById(five).ifPresentOrElse(existing -> existing.update(inbound.value), () -> meterFiveRepository.save(new MeterFive(five, inbound.value)));
|
||||
final MeterValue.Id hour = new MeterValue.Id(meter, inbound.date, Alignment.HOUR);
|
||||
meterHourRepository.findById(hour).ifPresentOrElse(existing -> existing.update(inbound.value), () -> meterHourRepository.save(new MeterHour(hour, inbound.value)));
|
||||
|
||||
final MeterValue.Id hour = new MeterValue.Id(meter, inbound.date, Alignment.HOUR);
|
||||
meterHourRepository.findById(hour).ifPresentOrElse(existing -> existing.update(inbound.value), () -> meterHourRepository.save(new MeterHour(hour, inbound.value)));
|
||||
final MeterValue.Id day = new MeterValue.Id(meter, inbound.date, Alignment.DAY);
|
||||
meterDayRepository.findById(day).ifPresentOrElse(existing -> existing.update(inbound.value), () -> meterDayRepository.save(new MeterDay(day, inbound.value)));
|
||||
|
||||
final MeterValue.Id day = new MeterValue.Id(meter, inbound.date, Alignment.DAY);
|
||||
meterDayRepository.findById(day).ifPresentOrElse(existing -> existing.update(inbound.value), () -> meterDayRepository.save(new MeterDay(day, inbound.value)));
|
||||
final MeterValue.Id week = new MeterValue.Id(meter, inbound.date, Alignment.WEEK);
|
||||
meterWeekRepository.findById(week).ifPresentOrElse(existing -> existing.update(inbound.value), () -> meterWeekRepository.save(new MeterWeek(week, inbound.value)));
|
||||
|
||||
final MeterValue.Id week = new MeterValue.Id(meter, inbound.date, Alignment.WEEK);
|
||||
meterWeekRepository.findById(week).ifPresentOrElse(existing -> existing.update(inbound.value), () -> meterWeekRepository.save(new MeterWeek(week, inbound.value)));
|
||||
final MeterValue.Id month = new MeterValue.Id(meter, inbound.date, Alignment.MONTH);
|
||||
meterMonthRepository.findById(month).ifPresentOrElse(existing -> existing.update(inbound.value), () -> meterMonthRepository.save(new MeterMonth(month, inbound.value)));
|
||||
|
||||
final MeterValue.Id month = new MeterValue.Id(meter, inbound.date, Alignment.MONTH);
|
||||
meterMonthRepository.findById(month).ifPresentOrElse(existing -> existing.update(inbound.value), () -> meterMonthRepository.save(new MeterMonth(month, inbound.value)));
|
||||
|
||||
final MeterValue.Id year = new MeterValue.Id(meter, inbound.date, Alignment.YEAR);
|
||||
meterYearRepository.findById(year).ifPresentOrElse(existing -> existing.update(inbound.value), () -> meterYearRepository.save(new MeterYear(year, inbound.value)));
|
||||
final MeterValue.Id year = new MeterValue.Id(meter, inbound.date, Alignment.YEAR);
|
||||
meterYearRepository.findById(year).ifPresentOrElse(existing -> existing.update(inbound.value), () -> meterYearRepository.save(new MeterYear(year, inbound.value)));
|
||||
});
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Meter getOrCreate(@NonNull final MeterInbound inbound) {
|
||||
final Series series = seriesService.getOrCreate(inbound, SeriesType.METER);
|
||||
private void onInbound(@NonNull final MeterInbound inbound, @NonNull final Consumer<Meter> modifier) {
|
||||
final Meter meter = seriesService.onInbound(inbound, SeriesType.METER, series -> onInbound(series, inbound));
|
||||
modifier.accept(meter);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Meter onInbound(@NonNull final Series series, @NonNull final MeterInbound inbound) {
|
||||
return meterRepository.findFirstBySeriesOrderByDateDesc(series)
|
||||
.filter(m -> m.getNumber().equals(inbound.meterNumber))
|
||||
.stream()
|
||||
|
||||
@ -37,9 +37,12 @@ public class VaryingService {
|
||||
private final VaryingYearRepository varyingYearRepository;
|
||||
|
||||
@EventListener(VaryingInbound.class)
|
||||
public void onEvent(@NonNull final VaryingInbound inbound) {
|
||||
final Series series = seriesService.getOrCreate(inbound, SeriesType.VARYING);
|
||||
public void onInbound(@NonNull final VaryingInbound inbound) {
|
||||
seriesService.onInbound(inbound, SeriesType.VARYING, series -> onInbound(series, inbound));
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Series onInbound(@NonNull final Series series, @NonNull final VaryingInbound inbound) {
|
||||
final Varying.Id five = new Varying.Id(series, inbound.date, Alignment.FIVE);
|
||||
varyingFiveRepository.findById(five).ifPresentOrElse(existing -> existing.update(inbound.value), () -> varyingFiveRepository.save(new VaryingFive(five, inbound.value)));
|
||||
|
||||
@ -57,6 +60,8 @@ public class VaryingService {
|
||||
|
||||
final Varying.Id year = new Varying.Id(series, inbound.date, Alignment.YEAR);
|
||||
varyingYearRepository.findById(year).ifPresentOrElse(existing -> existing.update(inbound.value), () -> varyingYearRepository.save(new VaryingYear(year, inbound.value)));
|
||||
|
||||
return series;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
||||
Loading…
Reference in New Issue
Block a user