management of technical id for domain object cleaned up a bit
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user