management of technical id for domain object cleaned up a bit

This commit is contained in:
Michał Michaluk
2018-03-24 17:40:35 +01:00
parent 636215502c
commit 67925711b1
6 changed files with 31 additions and 34 deletions

View File

@@ -1,7 +1,6 @@
package pl.com.dddbyexamples.tools;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.Optional;
public interface TechnicalId {
@@ -11,19 +10,11 @@ public interface TechnicalId {
return getId() != null;
}
static Long get(Object id) {
return isPersisted(id) ? ((TechnicalId) id).getId() : null;
static Optional<Long> get(Object id) {
return isPersisted(id) ? Optional.of(((TechnicalId) id).getId()) : Optional.empty();
}
static boolean isPersisted(Object id) {
return (id instanceof TechnicalId) && ((TechnicalId) id).isPersisted();
}
static <T> T findOrDefault(Object id, Function<Long, T> ifPresent, Supplier<T> orElse) {
if (isPersisted(id)) {
return ifPresent.apply(get(id));
} else {
return orElse.get();
}
}
}

View File

@@ -33,14 +33,15 @@ class ProductDemandORMRepository implements ProductDemandRepository {
@Override
public void initNewProduct(String refNo) {
if (rootDao.findByRefNo(refNo) == null) {
if (!rootDao.findByRefNo(refNo).isPresent()) {
rootDao.save(new ProductDemandEntity(refNo));
}
}
@Override
public ProductDemand get(String refNo) {
ProductDemandEntity root = rootDao.findByRefNo(refNo);
ProductDemandEntity root = rootDao.findByRefNo(refNo)
.orElseThrow(() -> new IllegalArgumentException("ProductDemand not initialized for refNo: " + refNo));
RefNoId id = root.createId();
Map<LocalDate, DemandEntity> data =
@@ -57,21 +58,22 @@ class ProductDemandORMRepository implements ProductDemandRepository {
@Override
public void save(ProductDemand model) {
ProductDemandEntity root = rootDao.getOne(TechnicalId.get(model.id));
ProductDemandEntity root = TechnicalId.get(model.id)
.map(rootDao::getOne)
.orElseThrow(IllegalStateException::new);
if (model.updates.size() > 0) {
em.lock(root, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
}
for (DailyDemand.DemandUpdated updated : model.updates) {
DemandEntity entity;
if (TechnicalId.isPersisted(updated.getId())) {
entity = demandDao.getOne(TechnicalId.get(updated.getId()));
} else {
entity = new DemandEntity(
updated.getId().getRefNo(),
updated.getId().getDate()
);
demandDao.save(entity);
}
DemandEntity entity = TechnicalId.get(updated.getId())
.map(demandDao::getOne)
.orElseGet(() -> demandDao.save(
new DemandEntity(
updated.getId().getRefNo(),
updated.getId().getDate()
))
);
entity.setValue(new DemandValue(
updated.getDocumented().nullIfNone(),
updated.getAdjustment()

View File

@@ -27,7 +27,7 @@ public class DemandAdjustmentEntity implements Serializable {
@Setter
private LocalDate cleanAfter;
DemandAdjustmentEntity(String note, String customerRepresentative, AdjustDemand adjustment) {
public DemandAdjustmentEntity(String note, String customerRepresentative, AdjustDemand adjustment) {
this.note = note;
this.customerRepresentative = customerRepresentative;
this.adjustment = adjustment;

View File

@@ -4,8 +4,10 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
@RestResource(exported = false)
public interface ProductDemandDao extends JpaRepository<ProductDemandEntity, Long> {
ProductDemandEntity findByRefNo(String refNo);
Optional<ProductDemandEntity> findByRefNo(String refNo);
}

View File

@@ -21,7 +21,7 @@ public class ProductDescriptionEntity implements Serializable {
@Convert(converter = DescriptionAsJson.class)
private ProductDescription description;
ProductDescriptionEntity(String refNo, ProductDescription description) {
public ProductDescriptionEntity(String refNo, ProductDescription description) {
this.refNo = refNo;
this.description = description;
}

View File

@@ -10,7 +10,6 @@ import pl.com.dddbyexamples.factory.shortages.prediction.monitoring.persistence.
import pl.com.dddbyexamples.tools.TechnicalId;
import java.util.Optional;
import java.util.function.Function;
@Component
@AllArgsConstructor
@@ -40,16 +39,19 @@ class ShortagePredictionProcessORMRepository implements ShortagePredictionProces
private void save(NewShortage event) {
RefNoId refNo = event.getRefNo();
Function<Long, Optional<ShortagesEntity>> findById = dao::findById;
ShortagesEntity entity = TechnicalId.findOrDefault(
refNo, findById.andThen(Optional::get),
() -> dao.save(new ShortagesEntity(refNo.getRefNo())));
ShortagesEntity entity = TechnicalId.get(refNo)
.flatMap(dao::findById)
.orElseGet(() -> dao.save(new ShortagesEntity(refNo.getRefNo())));
entity.setShortage(event.getShortage());
events.emit(event);
}
private void delete(ShortageSolved event) {
dao.deleteById(TechnicalId.get(event.getRefNo()));
TechnicalId.get(event.getRefNo())
.ifPresent(dao::deleteById);
events.emit(event);
}