demand repository persists documents

This commit is contained in:
Michał Michaluk
2017-12-08 09:04:13 +01:00
parent 2aafb8dba4
commit 0b339dd6dd
3 changed files with 35 additions and 57 deletions

View File

@@ -1,9 +1,8 @@
package pl.com.bottega.factory.demand.forecasting;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;
import pl.com.bottega.factory.delivery.planning.definition.DeliveryPlannerDefinition;
import pl.com.bottega.tools.JsonConverter;
import pl.com.bottega.tools.TechnicalId;
import javax.persistence.*;
@@ -26,61 +25,41 @@ public class DemandEntity {
private LocalDate date;
@Column
private Long level;
@Column
private Demand.Schema schema;
@Column
private Long adjustmentLevel;
@Column
private Demand.Schema adjustmentSchema;
@Column
private boolean adjustmentStrong;
@Convert(converter = DemandAsJson.class)
private DemandValue value;
DemandEntity(ProductDemandEntity product, LocalDate date) {
this.product = product;
this.date = date;
}
Demand getDemand() {
return Demand.ofNullable(level, schema);
}
Adjustment getAdjustment() {
return Optional.ofNullable(adjustmentLevel)
.map(level -> new Adjustment(
Demand.of(level, adjustmentSchema),
adjustmentStrong
))
.orElse(null);
}
void setDemand(Demand demand) {
if (demand == null) {
setLevel(null);
setSchema(null);
} else {
setLevel(demand.getLevel());
setSchema(demand.getSchema());
}
}
void setAdjustment(Adjustment adjustment) {
if (adjustment == null) {
setAdjustmentLevel(null);
setAdjustmentSchema(null);
setAdjustmentStrong(false);
} else {
setAdjustmentLevel(adjustment.getDemand().getLevel());
setAdjustmentSchema(adjustment.getDemand().getSchema());
setAdjustmentStrong(adjustment.isStrong());
}
}
DemandEntityId createId() {
return new DemandEntityId(product.getRefNo(), date, id);
}
void set(Demand demand, Adjustment adjustment) {
value = new DemandValue(demand, adjustment);
}
DemandValue get() {
return Optional.ofNullable(value)
.orElse(DemandValue.NO_VAL);
}
@Value
static class DemandValue {
public static final DemandValue NO_VAL = new DemandValue(null, null);
Demand documented;
Adjustment adjustment;
}
public static class DemandAsJson extends JsonConverter<DemandValue> {
public DemandAsJson() {
super(DemandValue.class);
}
}
@Getter
static class DemandEntityId extends DailyId implements TechnicalId {

View File

@@ -53,8 +53,8 @@ class DemandORMRepository {
.map(entity -> new DailyDemand(
entity.createId(),
demands,
entity.getDemand(),
entity.getAdjustment()))
entity.get().getDocumented(),
entity.get().getAdjustment()))
.orElseGet(() -> new DailyDemand(
new DemandEntityId(refNo, date),
demands,
@@ -75,8 +75,7 @@ class DemandORMRepository {
} else {
entity = new DemandEntity(root, updated.getId().getDate());
}
entity.setDemand(updated.getDocumented());
entity.setAdjustment(updated.getAdjustment());
entity.set(updated.getDocumented(), updated.getAdjustment());
if (!TechnicalId.isPersisted(updated.getId())) {
demandDao.save(entity);

View File

@@ -57,7 +57,7 @@ class DemandORMRepositoryTest extends Specification {
given:
def root = rootDao.save(new ProductDemandEntity("3009000"))
def demand = new DemandEntity(root, today)
demand.setDemand(Demand.of(1000))
demand.set(Demand.of(1000), null)
demandDao.save(demand)
when:
@@ -70,18 +70,18 @@ class DemandORMRepositoryTest extends Specification {
then:
def demands = demandDao.findAll()
demands.size() == 1
demand.every { it.getAdjustmentLevel() == 2000 }
demand.every { it.get().getAdjustment() == Adjustment.strong(Demand.of(2000)) }
}
def "doesn't fetch historical data"() {
given:
def root = rootDao.save(new ProductDemandEntity("3009000"))
def old = new DemandEntity(root, today.minusDays(1))
old.setDemand(Demand.of(10000))
old.set(Demand.of(10000), null)
demandDao.save(old)
def todays = new DemandEntity(root, today)
todays.setDemand(Demand.of(1000))
todays.set(Demand.of(1000), null)
demandDao.save(todays)
when: