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

@@ -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);
}